1
- package marshaller
1
+ package marshaller_test
2
2
3
3
import (
4
4
"context"
5
5
"fmt"
6
6
"reflect"
7
7
"testing"
8
8
9
+ "github.com/speakeasy-api/openapi/extensions"
10
+ "github.com/speakeasy-api/openapi/extensions/core"
9
11
"github.com/speakeasy-api/openapi/internal/testutils"
12
+ "github.com/speakeasy-api/openapi/marshaller"
10
13
"github.com/speakeasy-api/openapi/pointer"
11
14
"github.com/stretchr/testify/assert"
12
15
"github.com/stretchr/testify/require"
@@ -15,23 +18,23 @@ import (
15
18
16
19
func TestSyncValue_String (t * testing.T ) {
17
20
target := ""
18
- outNode , err := SyncValue (context .Background (), "some-value" , & target , nil , false )
21
+ outNode , err := marshaller . SyncValue (context .Background (), "some-value" , & target , nil , false )
19
22
require .NoError (t , err )
20
23
assert .Equal (t , testutils .CreateStringYamlNode ("some-value" , 0 , 0 ), outNode )
21
24
assert .Equal (t , "some-value" , target )
22
25
}
23
26
24
27
func TestSyncValue_StringPtrSet (t * testing.T ) {
25
28
target := pointer .From ("" )
26
- outNode , err := SyncValue (context .Background (), pointer .From ("some-value" ), & target , nil , false )
29
+ outNode , err := marshaller . SyncValue (context .Background (), pointer .From ("some-value" ), & target , nil , false )
27
30
require .NoError (t , err )
28
31
assert .Equal (t , testutils .CreateStringYamlNode ("some-value" , 0 , 0 ), outNode )
29
32
assert .Equal (t , "some-value" , * target )
30
33
}
31
34
32
35
func TestSyncValue_StringPtrNil (t * testing.T ) {
33
36
var target * string
34
- outNode , err := SyncValue (context .Background (), pointer .From ("some-value" ), & target , nil , false )
37
+ outNode , err := marshaller . SyncValue (context .Background (), pointer .From ("some-value" ), & target , nil , false )
35
38
require .NoError (t , err )
36
39
assert .Equal (t , testutils .CreateStringYamlNode ("some-value" , 0 , 0 ), outNode )
37
40
assert .Equal (t , "some-value" , * target )
@@ -57,7 +60,7 @@ func (t *TestStructSyncerCore[T]) SyncChanges(ctx context.Context, model any, va
57
60
}
58
61
59
62
var err error
60
- t .RootNode , err = SyncValue (ctx , mv .FieldByName ("Val" ).Interface (), & t .Val , valueNode , false )
63
+ t .RootNode , err = marshaller . SyncValue (ctx , mv .FieldByName ("Val" ).Interface (), & t .Val , valueNode , false )
61
64
return t .RootNode , err
62
65
}
63
66
@@ -66,7 +69,7 @@ func TestSyncValue_StructPtr_CustomSyncer(t *testing.T) {
66
69
67
70
source := & TestStructSyncer [int ]{Val : pointer .From (1 )}
68
71
69
- outNode , err := SyncValue (context .Background (), source , & target , nil , false )
72
+ outNode , err := marshaller . SyncValue (context .Background (), source , & target , nil , false )
70
73
require .NoError (t , err )
71
74
node := testutils .CreateIntYamlNode (1 , 0 , 0 )
72
75
assert .Equal (t , node , outNode )
@@ -79,7 +82,7 @@ func TestSyncValue_Struct_CustomSyncer(t *testing.T) {
79
82
80
83
source := TestStructSyncer [int ]{Val : pointer .From (1 )}
81
84
82
- outNode , err := SyncValue (context .Background (), source , & target , nil , false )
85
+ outNode , err := marshaller . SyncValue (context .Background (), source , & target , nil , false )
83
86
require .NoError (t , err )
84
87
node := testutils .CreateIntYamlNode (1 , 0 , 0 )
85
88
assert .Equal (t , node , outNode )
@@ -96,10 +99,10 @@ type TestStruct struct {
96
99
}
97
100
98
101
type TestStructCore struct {
99
- Int Node [int ] `key:"int"`
100
- Str Node [string ] `key:"str"`
101
- StrPtr Node [* string ] `key:"strPtr"`
102
- BoolPtr Node [* bool ] `key:"boolPtr"`
102
+ Int marshaller. Node [int ] `key:"int"`
103
+ Str marshaller. Node [string ] `key:"str"`
104
+ StrPtr marshaller. Node [* string ] `key:"strPtr"`
105
+ BoolPtr marshaller. Node [* bool ] `key:"boolPtr"`
103
106
104
107
RootNode * yaml.Node
105
108
}
@@ -112,7 +115,7 @@ func TestSyncChanges_Struct(t *testing.T) {
112
115
BoolPtr : pointer .From (true ),
113
116
}
114
117
115
- outNode , err := SyncValue (context .Background (), & source , & source .core , nil , false )
118
+ outNode , err := marshaller . SyncValue (context .Background (), & source , & source .core , nil , false )
116
119
require .NoError (t , err )
117
120
118
121
node := testutils .CreateMapYamlNode ([]* yaml.Node {
@@ -140,7 +143,7 @@ func TestSyncChanges_StructWithOptionalsUnset(t *testing.T) {
140
143
Str : "some-string" ,
141
144
}
142
145
143
- outNode , err := SyncValue (context .Background (), & source , & source .core , nil , false )
146
+ outNode , err := marshaller . SyncValue (context .Background (), & source , & source .core , nil , false )
144
147
require .NoError (t , err )
145
148
146
149
node := testutils .CreateMapYamlNode ([]* yaml.Node {
@@ -166,7 +169,7 @@ func TestSyncChanges_StructPtr(t *testing.T) {
166
169
BoolPtr : pointer .From (true ),
167
170
}
168
171
169
- outNode , err := SyncValue (context .Background (), & source , & source .core , nil , false )
172
+ outNode , err := marshaller . SyncValue (context .Background (), & source , & source .core , nil , false )
170
173
require .NoError (t , err )
171
174
172
175
node := testutils .CreateMapYamlNode ([]* yaml.Node {
@@ -195,7 +198,7 @@ type TestStructNested struct {
195
198
}
196
199
197
200
type TestStructNestedCore struct {
198
- TestStruct Node [TestStructCore ] `key:"testStruct"`
201
+ TestStruct marshaller. Node [TestStructCore ] `key:"testStruct"`
199
202
200
203
RootNode * yaml.Node
201
204
}
@@ -210,7 +213,7 @@ func TestSyncChanges_NestedStruct(t *testing.T) {
210
213
},
211
214
}
212
215
213
- outNode , err := SyncValue (context .Background (), & source , & source .core , nil , false )
216
+ outNode , err := marshaller . SyncValue (context .Background (), & source , & source .core , nil , false )
214
217
require .NoError (t , err )
215
218
216
219
nestedNode := testutils .CreateMapYamlNode ([]* yaml.Node {
@@ -242,8 +245,47 @@ type TestInt int
242
245
243
246
func TestSyncValue_TypeDefinition (t * testing.T ) {
244
247
var target TestInt
245
- outNode , err := SyncValue (context .Background (), 1 , & target , nil , false )
248
+ outNode , err := marshaller . SyncValue (context .Background (), 1 , & target , nil , false )
246
249
require .NoError (t , err )
247
250
assert .Equal (t , testutils .CreateIntYamlNode (1 , 0 , 0 ), outNode )
248
251
assert .Equal (t , TestInt (1 ), target )
249
252
}
253
+
254
+ type TestStructWithExtensions struct {
255
+ Extensions * extensions.Extensions
256
+
257
+ core TestStructWithExtensionsCore
258
+ }
259
+
260
+ type TestStructWithExtensionsCore struct {
261
+ Extensions core.Extensions `key:"extensions"`
262
+
263
+ RootNode * yaml.Node
264
+ }
265
+
266
+ func TestSyncValue_TypeWithExtensions (t * testing.T ) {
267
+ var source TestStructWithExtensions
268
+
269
+ extensionNode := testutils .CreateMapYamlNode (
270
+ []* yaml.Node {
271
+ testutils .CreateStringYamlNode ("name" , 0 , 0 ),
272
+ testutils .CreateStringYamlNode ("test" , 0 , 0 ),
273
+ testutils .CreateStringYamlNode ("value" , 0 , 0 ),
274
+ testutils .CreateIntYamlNode (1 , 0 , 0 ),
275
+ }, 0 , 0 )
276
+
277
+ source .Extensions = extensions .New (extensions .NewElem ("x-speakeasy-test" , extensionNode ))
278
+
279
+ outNode , err := marshaller .SyncValue (context .Background (), & source , & source .core , nil , false )
280
+ require .NoError (t , err )
281
+
282
+ node := testutils .CreateMapYamlNode (
283
+ []* yaml.Node {
284
+ testutils .CreateStringYamlNode ("x-speakeasy-test" , 0 , 0 ),
285
+ extensionNode ,
286
+ }, 0 , 0 )
287
+
288
+ assert .Equal (t , node , outNode )
289
+ assert .Equal (t , node , source .core .RootNode )
290
+ assert .True (t , source .Extensions .GetCore ().Has ("x-speakeasy-test" ))
291
+ }
0 commit comments