-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmodels.go
More file actions
200 lines (162 loc) · 9.54 KB
/
models.go
File metadata and controls
200 lines (162 loc) · 9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package core
import (
"context"
"github.com/speakeasy-api/openapi/extensions/core"
"github.com/speakeasy-api/openapi/internal/interfaces"
"github.com/speakeasy-api/openapi/marshaller"
"github.com/speakeasy-api/openapi/sequencedmap"
valuescore "github.com/speakeasy-api/openapi/values/core"
"gopkg.in/yaml.v3"
)
// TestPrimitiveModel covers all primitive marshaller.Node field types
type TestPrimitiveModel struct {
marshaller.CoreModel `model:"testPrimitiveModel"`
StringField marshaller.Node[string] `key:"stringField"`
StringPtrField marshaller.Node[*string] `key:"stringPtrField"`
BoolField marshaller.Node[bool] `key:"boolField"`
BoolPtrField marshaller.Node[*bool] `key:"boolPtrField"`
IntField marshaller.Node[int] `key:"intField"`
IntPtrField marshaller.Node[*int] `key:"intPtrField"`
Float64Field marshaller.Node[float64] `key:"float64Field"`
Float64PtrField marshaller.Node[*float64] `key:"float64PtrField"`
Extensions core.Extensions `key:"extensions"`
}
// TestRequiredPointerModel specifically tests required pointer field behavior
type TestRequiredPointerModel struct {
marshaller.CoreModel `model:"testRequiredPointerModel"`
RequiredPtr marshaller.Node[*string] `key:"requiredPtr" required:"true"`
OptionalPtr marshaller.Node[*string] `key:"optionalPtr"`
Extensions core.Extensions `key:"extensions"`
}
// TestComplexModel covers complex marshaller.Node field types
type TestComplexModel struct {
marshaller.CoreModel `model:"testComplexModel"`
NestedModel marshaller.Node[*TestPrimitiveModel] `key:"nestedModel"`
NestedModelValue marshaller.Node[TestPrimitiveModel] `key:"nestedModelValue"`
ArrayField marshaller.Node[[]string] `key:"arrayField"`
NodeArrayField marshaller.Node[[]marshaller.Node[string]] `key:"nodeArrayField"`
StructArrayField marshaller.Node[[]*TestPrimitiveModel] `key:"structArrayField"`
MapPrimitiveField marshaller.Node[*sequencedmap.Map[string, string]] `key:"mapField"`
MapNodeField marshaller.Node[*sequencedmap.Map[string, marshaller.Node[string]]] `key:"mapNodeField"`
MapStructField marshaller.Node[*sequencedmap.Map[string, *TestPrimitiveModel]] `key:"mapStructField"`
EitherField marshaller.Node[*valuescore.EitherValue[string, int]] `key:"eitherField"`
EitherModelOrPrimitive marshaller.Node[*valuescore.EitherValue[TestPrimitiveModel, int]] `key:"eitherModelOrPrimitive" required:"true"`
RawNodeField marshaller.Node[*yaml.Node] `key:"rawNodeField"`
ValueField marshaller.Node[valuescore.Value] `key:"valueField"`
ValuesField marshaller.Node[[]marshaller.Node[valuescore.Value]] `key:"valuesField"`
Extensions core.Extensions `key:"extensions"`
}
// TestEmbeddedMapModel covers embedded sequenced map scenarios with no extra fields
type TestEmbeddedMapModel struct {
marshaller.CoreModel `model:"testEmbeddedMapModel"`
*sequencedmap.Map[string, marshaller.Node[string]]
}
// TestEmbeddedMapWithFieldsModel covers embedded sequenced map with additional fields
type TestEmbeddedMapWithFieldsModel struct {
marshaller.CoreModel `model:"testEmbeddedMapWithFieldsModel"`
*sequencedmap.Map[string, marshaller.Node[*TestPrimitiveModel]]
NameField marshaller.Node[string] `key:"name"`
Extensions core.Extensions `key:"extensions"`
}
// TestEmbeddedMapWithExtensionsModel covers embedded sequenced map with extensions only
type TestEmbeddedMapWithExtensionsModel struct {
marshaller.CoreModel `model:"testEmbeddedMapWithExtensionsModel"`
*sequencedmap.Map[string, marshaller.Node[string]]
Extensions core.Extensions `key:"extensions"`
}
// TestNonCoreModel represents a normal Go struct (not implementing CoreModeler)
type TestNonCoreModel struct {
Name string `json:"name"`
Value int `json:"value"`
Description *string `json:"description,omitempty"`
}
// TestCustomUnmarshalModel implements custom Unmarshal method
type TestCustomUnmarshalModel struct {
marshaller.CoreModel `model:"testCustomUnmarshalModel"`
CustomField marshaller.Node[string] `key:"customField"`
Extensions core.Extensions `key:"extensions"`
// Custom state for testing
UnmarshalCalled bool
}
var _ interfaces.CoreModel = (*TestCustomUnmarshalModel)(nil)
// Unmarshal implements custom unmarshalling logic
func (m *TestCustomUnmarshalModel) Unmarshal(ctx context.Context, parentName string, node *yaml.Node) ([]error, error) {
m.UnmarshalCalled = true
// Use standard unmarshalling for the base
return marshaller.UnmarshalModel(ctx, node, m)
}
// TestEitherValueModel covers EitherValue scenarios
type TestEitherValueModel struct {
marshaller.CoreModel `model:"testEitherValueModel"`
StringOrInt marshaller.Node[*valuescore.EitherValue[string, int]] `key:"stringOrInt"`
ArrayOrString marshaller.Node[*valuescore.EitherValue[[]string, string]] `key:"arrayOrString"`
StructOrString marshaller.Node[*valuescore.EitherValue[TestPrimitiveModel, string]] `key:"structOrString"`
Extensions core.Extensions `key:"extensions"`
}
// TestValidationModel covers field validation scenarios
type TestValidationModel struct {
marshaller.CoreModel `model:"testValidationModel"`
RequiredField marshaller.Node[string] `key:"requiredField" required:"true"`
OptionalField marshaller.Node[*string] `key:"optionalField"`
RequiredArray marshaller.Node[[]string] `key:"requiredArray" required:"true"`
OptionalArray marshaller.Node[[]string] `key:"optionalArray"`
RequiredStruct marshaller.Node[*TestPrimitiveModel] `key:"requiredStruct" required:"true"`
OptionalStruct marshaller.Node[*TestPrimitiveModel] `key:"optionalStruct"`
Extensions core.Extensions `key:"extensions"`
}
// TestEmbeddedMapPointerModel represents core model with pointer embedded sequenced map
// This tests the legacy pointer embed pattern to ensure backward compatibility
type TestEmbeddedMapPointerModel struct {
marshaller.CoreModel `model:"testEmbeddedMapPointerModel"`
*sequencedmap.Map[string, marshaller.Node[string]]
}
// TestEmbeddedMapWithFieldsPointerModel represents core model with pointer embedded sequenced map and additional fields
// This tests the legacy pointer embed pattern with fields to ensure backward compatibility
type TestEmbeddedMapWithFieldsPointerModel struct {
marshaller.CoreModel `model:"testEmbeddedMapWithFieldsPointerModel"`
*sequencedmap.Map[string, marshaller.Node[*TestPrimitiveModel]]
NameField marshaller.Node[string] `key:"name"`
Extensions core.Extensions `key:"extensions"`
}
// TestAliasModel covers alias scenarios
type TestAliasModel struct {
marshaller.CoreModel `model:"testAliasModel"`
AliasField marshaller.Node[string] `key:"aliasField"`
AliasArray marshaller.Node[[]string] `key:"aliasArray"`
AliasStruct marshaller.Node[*TestPrimitiveModel] `key:"aliasStruct"`
Extensions core.Extensions `key:"extensions"`
}
// TestRequiredNilableModel specifically tests required tag with nilable types
type TestRequiredNilableModel struct {
marshaller.CoreModel `model:"testRequiredNilableModel"`
RequiredPtr marshaller.Node[*string] `key:"requiredPtr" required:"true"`
RequiredSlice marshaller.Node[[]string] `key:"requiredSlice" required:"true"`
RequiredMap marshaller.Node[*sequencedmap.Map[string, string]] `key:"requiredMap" required:"true"`
RequiredStruct marshaller.Node[*TestPrimitiveModel] `key:"requiredStruct" required:"true"`
RequiredEither marshaller.Node[*valuescore.EitherValue[string, int]] `key:"requiredEither" required:"true"`
RequiredRawNode marshaller.Node[*yaml.Node] `key:"requiredRawNode" required:"true"`
OptionalPtr marshaller.Node[*string] `key:"optionalPtr"`
OptionalSlice marshaller.Node[[]string] `key:"optionalSlice"`
OptionalMap marshaller.Node[*sequencedmap.Map[string, string]] `key:"optionalMap"`
OptionalStruct marshaller.Node[*TestPrimitiveModel] `key:"optionalStruct"`
Extensions core.Extensions `key:"extensions"`
}
// TestTypeConversionCoreModel represents core model with string keys (like openapi/core/paths.go)
// This simulates the issue where core uses string keys but high-level model expects HTTPMethod keys
type TestTypeConversionCoreModel struct {
marshaller.CoreModel `model:"testTypeConversionCoreModel"`
*sequencedmap.Map[string, marshaller.Node[*TestPrimitiveModel]]
HTTPMethodField marshaller.Node[*string] `key:"httpMethodField"`
Extensions core.Extensions `key:"extensions"`
}
// TestItemModel represents an item with a name and description
type TestItemModel struct {
marshaller.CoreModel `model:"testItemModel"`
Name marshaller.Node[string] `key:"name"`
Description marshaller.Node[string] `key:"description"`
}
// TestArrayOfObjectsModel contains an array of items
type TestArrayOfObjectsModel struct {
marshaller.CoreModel `model:"testArrayOfObjectsModel"`
Items marshaller.Node[[]*TestItemModel] `key:"items"`
}