Skip to content

Commit bfc75bc

Browse files
refactor: reduce boiler plate on models when unmarshalling (#12)
1 parent 88a738f commit bfc75bc

37 files changed

+188
-199
lines changed

arazzo/arazzo.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/speakeasy-api/openapi/arazzo/core"
1616
"github.com/speakeasy-api/openapi/extensions"
17+
"github.com/speakeasy-api/openapi/internal/interfaces"
1718
"github.com/speakeasy-api/openapi/marshaller"
1819
"github.com/speakeasy-api/openapi/validation"
1920
"github.com/speakeasy-api/openapi/yml"
@@ -48,15 +49,15 @@ type Arazzo struct {
4849
core core.Arazzo
4950
}
5051

51-
var _ model[core.Arazzo] = (*Arazzo)(nil)
52+
var _ interfaces.Model[core.Arazzo] = (*Arazzo)(nil)
5253

5354
type Option[T any] func(o *T)
5455

5556
type unmarshalOptions struct {
5657
skipValidation bool
5758
}
5859

59-
// WithSkipValidation will skip validation of the Arazzo document during unmarshalling.
60+
// WithSkipValidation will skip validation of the Arazzo document during unmarshaling.
6061
// Useful to quickly load a document that will be mutated and validated later.
6162
func WithSkipValidation() Option[unmarshalOptions] {
6263
return func(o *unmarshalOptions) {

arazzo/components.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/speakeasy-api/openapi/arazzo/core"
99
"github.com/speakeasy-api/openapi/extensions"
10+
"github.com/speakeasy-api/openapi/internal/interfaces"
1011
"github.com/speakeasy-api/openapi/jsonschema/oas31"
1112
"github.com/speakeasy-api/openapi/sequencedmap"
1213
"github.com/speakeasy-api/openapi/validation"
@@ -31,7 +32,7 @@ type Components struct {
3132
core core.Components
3233
}
3334

34-
var _ model[core.Components] = (*Components)(nil)
35+
var _ interfaces.Model[core.Components] = (*Components)(nil)
3536

3637
// GetCore will return the low level representation of the components object.
3738
// Useful for accessing line and column numbers for various nodes in the backing yaml/json document.

arazzo/core/arazzo.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ type Arazzo struct {
2525
Config *yml.Config
2626
}
2727

28-
var _ CoreModel = (*Arazzo)(nil)
29-
3028
func Unmarshal(ctx context.Context, doc io.Reader) (*Arazzo, error) {
3129
data, err := io.ReadAll(doc)
3230
if err != nil {
@@ -52,12 +50,6 @@ func Unmarshal(ctx context.Context, doc io.Reader) (*Arazzo, error) {
5250
return &arazzo, nil
5351
}
5452

55-
func (a *Arazzo) Unmarshal(ctx context.Context, node *yaml.Node) error {
56-
a.RootNode = node
57-
58-
return marshaller.UnmarshalStruct(ctx, node, a)
59-
}
60-
6153
func (a *Arazzo) Marshal(ctx context.Context, w io.Writer) error {
6254
cfg := yml.GetConfigFromContext(ctx)
6355

arazzo/core/components.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package core
22

33
import (
4-
"context"
5-
64
coreExtensions "github.com/speakeasy-api/openapi/extensions/core"
75
"github.com/speakeasy-api/openapi/jsonschema/oas31/core"
86
"github.com/speakeasy-api/openapi/marshaller"
@@ -19,11 +17,3 @@ type Components struct {
1917

2018
RootNode *yaml.Node
2119
}
22-
23-
var _ CoreModel = (*Components)(nil)
24-
25-
func (c *Components) Unmarshal(ctx context.Context, node *yaml.Node) error {
26-
c.RootNode = node
27-
28-
return marshaller.UnmarshalStruct(ctx, node, c)
29-
}

arazzo/core/criterion.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77

88
"github.com/speakeasy-api/openapi/extensions/core"
9+
"github.com/speakeasy-api/openapi/internal/interfaces"
910
"github.com/speakeasy-api/openapi/marshaller"
1011
"gopkg.in/yaml.v3"
1112
)
@@ -17,22 +18,14 @@ type CriterionExpressionType struct {
1718
RootNode *yaml.Node
1819
}
1920

20-
var _ CoreModel = (*CriterionExpressionType)(nil)
21-
22-
func (c *CriterionExpressionType) Unmarshal(ctx context.Context, node *yaml.Node) error {
23-
c.RootNode = node
24-
25-
return marshaller.UnmarshalStruct(ctx, node, c)
26-
}
27-
2821
type CriterionTypeUnion struct {
2922
Type *string
3023
ExpressionType *CriterionExpressionType
3124

3225
RootNode *yaml.Node
3326
}
3427

35-
var _ CoreModel = (*CriterionTypeUnion)(nil)
28+
var _ interfaces.CoreModel = (*CriterionTypeUnion)(nil)
3629

3730
func (c *CriterionTypeUnion) Unmarshal(ctx context.Context, node *yaml.Node) error {
3831
c.RootNode = node
@@ -44,7 +37,7 @@ func (c *CriterionTypeUnion) Unmarshal(ctx context.Context, node *yaml.Node) err
4437
if c.ExpressionType == nil {
4538
c.ExpressionType = &CriterionExpressionType{}
4639
}
47-
return marshaller.UnmarshalStruct(ctx, node, c.ExpressionType)
40+
return marshaller.UnmarshalModel(ctx, node, c.ExpressionType)
4841
default:
4942
return fmt.Errorf("expected scalar or mapping node, got %v", node.Kind)
5043
}
@@ -91,11 +84,3 @@ type Criterion struct {
9184

9285
RootNode *yaml.Node
9386
}
94-
95-
var _ CoreModel = (*Criterion)(nil)
96-
97-
func (c *Criterion) Unmarshal(ctx context.Context, node *yaml.Node) error {
98-
c.RootNode = node
99-
100-
return marshaller.UnmarshalStruct(ctx, node, c)
101-
}

arazzo/core/criterion_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ type:
151151
ValueNode: testutils.CreateStringYamlNode("draft-goessner-dispatch-jsonpath-00", 5, 12),
152152
Present: true,
153153
},
154+
RootNode: testutils.CreateMapYamlNode([]*yaml.Node{
155+
testutils.CreateStringYamlNode("type", 4, 3),
156+
testutils.CreateStringYamlNode("jsonpath", 4, 9),
157+
testutils.CreateStringYamlNode("version", 5, 3),
158+
testutils.CreateStringYamlNode("draft-goessner-dispatch-jsonpath-00", 5, 12),
159+
}, 4, 3),
154160
}, RootNode: testutils.CreateMapYamlNode([]*yaml.Node{
155161
testutils.CreateStringYamlNode("type", 4, 3),
156162
testutils.CreateStringYamlNode("jsonpath", 4, 9),
@@ -189,7 +195,8 @@ type:
189195
require.NoError(t, err)
190196

191197
c := Criterion{}
192-
err = c.Unmarshal(context.Background(), doc.Content[0])
198+
199+
err = marshaller.Unmarshal(context.Background(), doc.Content[0], &c)
193200
require.NoError(t, err)
194201

195202
require.Equal(t, tt.want, c)

arazzo/core/failureaction.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package core
22

33
import (
4-
"context"
5-
64
"github.com/speakeasy-api/openapi/extensions/core"
75
"github.com/speakeasy-api/openapi/marshaller"
86
"gopkg.in/yaml.v3"
@@ -20,11 +18,3 @@ type FailureAction struct {
2018

2119
RootNode *yaml.Node
2220
}
23-
24-
var _ CoreModel = (*FailureAction)(nil)
25-
26-
func (f *FailureAction) Unmarshal(ctx context.Context, node *yaml.Node) error {
27-
f.RootNode = node
28-
29-
return marshaller.UnmarshalStruct(ctx, node, f)
30-
}

arazzo/core/info.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package core
22

33
import (
4-
"context"
5-
64
"github.com/speakeasy-api/openapi/extensions/core"
75
"github.com/speakeasy-api/openapi/marshaller"
86
"gopkg.in/yaml.v3"
@@ -17,11 +15,3 @@ type Info struct {
1715

1816
RootNode *yaml.Node
1917
}
20-
21-
var _ CoreModel = (*Info)(nil)
22-
23-
func (i *Info) Unmarshal(ctx context.Context, node *yaml.Node) error {
24-
i.RootNode = node
25-
26-
return marshaller.UnmarshalStruct(ctx, node, i)
27-
}

arazzo/core/interfaces.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

arazzo/core/parameter.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package core
22

33
import (
4-
"context"
5-
64
"github.com/speakeasy-api/openapi/extensions/core"
75
"github.com/speakeasy-api/openapi/marshaller"
86
"gopkg.in/yaml.v3"
@@ -16,11 +14,3 @@ type Parameter struct {
1614

1715
RootNode *yaml.Node
1816
}
19-
20-
var _ CoreModel = (*Parameter)(nil)
21-
22-
func (p *Parameter) Unmarshal(ctx context.Context, node *yaml.Node) error {
23-
p.RootNode = node
24-
25-
return marshaller.UnmarshalStruct(ctx, node, p)
26-
}

0 commit comments

Comments
 (0)