-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathunmarshaller.go
More file actions
664 lines (544 loc) · 21.1 KB
/
unmarshaller.go
File metadata and controls
664 lines (544 loc) · 21.1 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
package marshaller
import (
"context"
"errors"
"fmt"
"io"
"reflect"
"strings"
"sync"
"github.com/speakeasy-api/openapi/internal/interfaces"
"github.com/speakeasy-api/openapi/validation"
"github.com/speakeasy-api/openapi/yml"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v3"
)
// Pre-computed reflection types for performance (reusing from populator.go where possible)
var (
nodeMutatorType = reflect.TypeOf((*NodeMutator)(nil)).Elem()
unmarshallableType = reflect.TypeOf((*Unmarshallable)(nil)).Elem()
// sequencedMapType and coreModelerType are already defined in populator.go
)
// Unmarshallable is an interface that can be implemented by types that can be unmarshalled from a YAML document.
// These types should handle the node being an alias node and resolve it to the actual value (retaining the original node where needed).
type Unmarshallable interface {
Unmarshal(ctx context.Context, parentName string, node *yaml.Node) ([]error, error)
}
// Unmarshal will unmarshal the provided document into the specified model.
func Unmarshal[T any](ctx context.Context, in io.Reader, out CoreAccessor[T]) ([]error, error) {
if out == nil || reflect.ValueOf(out).IsNil() {
return nil, errors.New("out parameter cannot be nil")
}
data, err := io.ReadAll(in)
if err != nil {
return nil, fmt.Errorf("failed to read document: %w", err)
}
if len(data) == 0 {
return nil, errors.New("empty document")
}
var root yaml.Node
if err := yaml.Unmarshal(data, &root); err != nil {
return nil, fmt.Errorf("failed to unmarshal document: %w", err)
}
core := out.GetCore()
// Check if the core implements CoreModeler interface
if coreModeler, ok := any(core).(CoreModeler); ok {
coreModeler.SetConfig(yml.GetConfigFromDoc(data, &root))
}
return UnmarshalNode(ctx, "", &root, out)
}
// UnmarshalNode will unmarshal the provided node into the provided model.
// This method is useful for unmarshaling partial documents, for a full document use Unmarshal as it will retain the full document structure.
func UnmarshalNode[T any](ctx context.Context, parentName string, node *yaml.Node, out CoreAccessor[T]) ([]error, error) {
if out == nil || reflect.ValueOf(out).IsNil() {
return nil, errors.New("out parameter cannot be nil")
}
core := out.GetCore()
validationErrs, err := UnmarshalCore(ctx, parentName, node, core)
if err != nil {
return nil, err
}
if err := Populate(*core, out); err != nil {
return nil, err
}
return validationErrs, nil
}
func UnmarshalCore(ctx context.Context, parentName string, node *yaml.Node, out any) ([]error, error) {
if node.Kind == yaml.DocumentNode {
if len(node.Content) != 1 {
return nil, fmt.Errorf("expected 1 node, got %d at line %d, column %d", len(node.Content), node.Line, node.Column)
}
return UnmarshalCore(ctx, parentName, node.Content[0], out)
}
v := reflect.ValueOf(out)
if v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
for v.Kind() == reflect.Interface && !v.IsNil() {
v = v.Elem()
}
return unmarshal(ctx, parentName, node, v)
}
func UnmarshalModel(ctx context.Context, node *yaml.Node, structPtr any) ([]error, error) {
return unmarshalModel(ctx, node, structPtr)
}
func UnmarshalKeyValuePair(ctx context.Context, parentName string, keyNode, valueNode *yaml.Node, outValue any) ([]error, error) {
out := reflect.ValueOf(outValue)
if implementsInterface(out, nodeMutatorType) {
return unmarshalNode(ctx, parentName, keyNode, valueNode, "value", out)
} else {
return UnmarshalCore(ctx, parentName, valueNode, outValue)
}
}
// DecodeNode attempts to decode a YAML node into the provided output value.
// It differentiates between type mismatch errors (returned as validation errors)
// and YAML syntax errors (returned as standard errors).
//
// Returns:
// - []error: validation errors for type mismatches
// - error: syntax errors or other decode failures
func DecodeNode(ctx context.Context, parentName string, node *yaml.Node, out any) ([]error, error) {
return decodeNode(ctx, parentName, node, out)
}
func unmarshal(ctx context.Context, parentName string, node *yaml.Node, out reflect.Value) ([]error, error) {
resolvedNode := yml.ResolveAlias(node)
if resolvedNode == nil {
return nil, nil
}
switch {
case out.Type() == reflect.TypeOf((*yaml.Node)(nil)):
out.Set(reflect.ValueOf(node))
return nil, nil
case out.Type() == reflect.TypeOf(yaml.Node{}):
out.Set(reflect.ValueOf(*node))
return nil, nil
}
if implementsInterface(out, nodeMutatorType) {
if out.Kind() != reflect.Ptr {
out = out.Addr()
}
if out.IsNil() {
out.Set(CreateInstance(out.Type().Elem()))
}
nodeMutator, ok := out.Interface().(NodeMutator)
if !ok {
return nil, fmt.Errorf("expected NodeMutator, got %s at line %d, column %d", out.Type(), resolvedNode.Line, resolvedNode.Column)
}
return nodeMutator.Unmarshal(ctx, parentName, nil, node)
}
if isEmbeddedSequencedMap(out) {
return unmarshalMapping(ctx, parentName, node, out)
}
if implementsInterface(out, unmarshallableType) {
if out.Kind() != reflect.Ptr {
out = out.Addr()
}
if out.IsNil() {
out.Set(CreateInstance(out.Type().Elem()))
}
unmarshallable, ok := out.Interface().(Unmarshallable)
if !ok {
return nil, fmt.Errorf("expected Unmarshallable, got %s at line %d, column %d", out.Type(), resolvedNode.Line, resolvedNode.Column)
}
return unmarshallable.Unmarshal(ctx, parentName, node)
}
if implementsInterface(out, sequencedMapType) {
if out.Kind() != reflect.Ptr {
out = out.Addr()
}
if out.IsNil() {
out.Set(CreateInstance(out.Type().Elem()))
}
seqMapInterface, ok := out.Interface().(interfaces.SequencedMapInterface)
if !ok {
return nil, fmt.Errorf("expected sequencedMapInterface, got %s at line %d, column %d", out.Type(), resolvedNode.Line, resolvedNode.Column)
}
return unmarshalSequencedMap(ctx, parentName, node, seqMapInterface)
}
// Type-guided unmarshalling: check target type first, then validate node compatibility
switch {
case isStructType(out):
// Target expects a struct/object
if err := validateNodeKind(resolvedNode, yaml.MappingNode, parentName, "object"); err != nil {
return []error{err}, nil //nolint:nilerr
}
return unmarshalMapping(ctx, parentName, node, out)
case isSliceType(out):
// Target expects a slice/array
if err := validateNodeKind(resolvedNode, yaml.SequenceNode, parentName, "sequence"); err != nil {
return []error{err}, nil //nolint:nilerr
}
return unmarshalSequence(ctx, parentName, node, out)
case isMapType(out):
// Target expects a map
if err := validateNodeKind(resolvedNode, yaml.MappingNode, parentName, "object"); err != nil {
return []error{err}, nil //nolint:nilerr
}
return unmarshalMapping(ctx, parentName, node, out)
default:
// Target expects a scalar value (string, int, bool, etc.)
if err := validateNodeKind(resolvedNode, yaml.ScalarNode, parentName, out.Type().String()); err != nil {
return []error{err}, nil //nolint:nilerr
}
return decodeNode(ctx, parentName, resolvedNode, out.Addr().Interface())
}
}
func unmarshalMapping(ctx context.Context, parentName string, node *yaml.Node, out reflect.Value) ([]error, error) {
if out.Kind() == reflect.Ptr {
out.Set(CreateInstance(out.Type().Elem()))
out = out.Elem()
}
resolvedNode := yml.ResolveAlias(node)
if resolvedNode == nil {
return nil, nil
}
switch {
case out.Kind() == reflect.Struct:
if implementsInterface(out, coreModelerType) {
return unmarshalModel(ctx, node, out.Addr().Interface())
} else {
return unmarshalStruct(ctx, parentName, node, out.Addr().Interface())
}
case out.Kind() == reflect.Map:
return nil, fmt.Errorf("currently unsupported out kind: %v (type: %s) at line %d, column %d", out.Kind(), out.Type(), resolvedNode.Line, resolvedNode.Column)
default:
return nil, fmt.Errorf("expected struct or map, got %s (type: %s) at line %d, column %d", out.Kind(), out.Type(), resolvedNode.Line, resolvedNode.Column)
}
}
func unmarshalModel(ctx context.Context, node *yaml.Node, structPtr any) ([]error, error) {
resolvedNode := yml.ResolveAlias(node)
if resolvedNode == nil {
return nil, nil
}
out := reflect.ValueOf(structPtr)
if out.Kind() == reflect.Ptr {
out = out.Elem()
}
if out.Kind() != reflect.Struct {
return nil, fmt.Errorf("expected a struct, got %s (type: %s) at line %d, column %d", out.Kind(), out.Type(), resolvedNode.Line, resolvedNode.Column)
}
structType := out.Type()
// Get the "model" tag value from the embedded CoreModel field which should be the first field always
if structType.NumField() < 1 {
return nil, fmt.Errorf("expected embedded CoreModel field, got %s at line %d, column %d", out.Type(), resolvedNode.Line, resolvedNode.Column)
}
field := structType.Field(0)
if field.Type != reflect.TypeOf(CoreModel{}) {
return nil, fmt.Errorf("expected embedded CoreModel field to be of type CoreModel, got %s at line %d, column %d", out.Type(), resolvedNode.Line, resolvedNode.Column)
}
modelTag := field.Tag.Get("model")
if modelTag == "" {
return nil, fmt.Errorf("expected embedded CoreModel field to have a 'model' tag, got %s at line %d, column %d", out.Type(), resolvedNode.Line, resolvedNode.Column)
}
if resolvedNode.Kind != yaml.MappingNode {
return []error{
validation.NewValidationError(validation.NewTypeMismatchError("%s expected object, got %s", modelTag, yml.NodeKindToString(resolvedNode.Kind)), resolvedNode),
}, nil
}
var unmarshallable CoreModeler
// Check if struct implements CoreModeler
if implementsInterface(out, coreModelerType) {
var ok bool
unmarshallable, ok = out.Addr().Interface().(CoreModeler)
if !ok {
return nil, fmt.Errorf("expected CoreModeler, got %s at line %d, column %d", out.Type(), resolvedNode.Line, resolvedNode.Column)
}
} else {
return nil, fmt.Errorf("expected struct to implement CoreModeler, got %s at line %d, column %d", out.Type(), resolvedNode.Line, resolvedNode.Column)
}
unmarshallable.SetRootNode(node)
// Get cached field information, build it if not available
fieldMap := getFieldMapCached(structType)
// Handle extensions field using cached index
var extensionsField *reflect.Value
if fieldMap.HasExtensions {
extField := out.Field(fieldMap.ExtensionIndex)
extensionsField = &extField
}
// Handle embedded maps (these need runtime reflection)
var embeddedMap interfaces.SequencedMapInterface
for i := 0; i < out.NumField(); i++ {
field := structType.Field(i)
if field.Anonymous {
fieldVal := out.Field(i)
if seqMap := initializeEmbeddedSequencedMap(fieldVal); seqMap != nil {
embeddedMap = seqMap
}
continue
}
}
// Process YAML nodes and validate required fields in one pass
foundRequiredFields := sync.Map{}
numJobs := len(resolvedNode.Content) / 2
var mapNode yaml.Node
var jobMapContent [][]*yaml.Node
if embeddedMap != nil {
mapNode = *resolvedNode
jobMapContent = make([][]*yaml.Node, numJobs)
}
jobValidationErrs := make([][]error, numJobs)
// Mutex to protect concurrent access to extensionsField
var extensionsMutex sync.Mutex
// TODO allow concurrency to be configurable
g, ctx := errgroup.WithContext(ctx)
for i := 0; i < len(resolvedNode.Content); i += 2 {
g.Go(func() error {
keyNode := resolvedNode.Content[i]
valueNode := resolvedNode.Content[i+1]
key := keyNode.Value
// Direct field index lookup (eliminates map[string]Field allocation)
fieldIndex, ok := fieldMap.FieldIndexes[key]
if !ok {
if strings.HasPrefix(key, "x-") && extensionsField != nil {
// Lock access to extensionsField to prevent concurrent modification
extensionsMutex.Lock()
defer extensionsMutex.Unlock()
err := UnmarshalExtension(keyNode, valueNode, *extensionsField)
if err != nil {
return err
}
} else if embeddedMap != nil {
// Skip alias definitions - these are nodes where:
// 1. The value node has an anchor (e.g., &keyAlias)
// 2. The key is not an alias reference (doesn't start with *)
if valueNode.Anchor != "" && !strings.HasPrefix(key, "*") {
// This is an alias definition, skip it from embedded map processing
// but it should still be preserved at the document level
return nil
}
jobMapContent[i/2] = append(jobMapContent[i/2], keyNode, valueNode)
}
} else {
// Get field info from cache and field value directly
cachedField := fieldMap.Fields[key]
fieldVal := out.Field(fieldIndex)
if implementsInterface(fieldVal, nodeMutatorType) {
fieldValidationErrs, err := unmarshalNode(ctx, modelTag+"."+key, keyNode, valueNode, cachedField.Name, fieldVal)
if err != nil {
return err
}
jobValidationErrs[i/2] = append(jobValidationErrs[i/2], fieldValidationErrs...)
// Mark required field as found
if fieldMap.RequiredFields[key] {
foundRequiredFields.Store(key, true)
}
} else {
return fmt.Errorf("expected field '%s' to be marshaller.Node, got %s at line %d, column %d (key: %s)", cachedField.Name, fieldVal.Type(), keyNode.Line, keyNode.Column, key)
}
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
var validationErrs []error
for _, jobValidationErrs := range jobValidationErrs {
validationErrs = append(validationErrs, jobValidationErrs...)
}
var mapContent []*yaml.Node
for _, jobMapContent := range jobMapContent {
mapContent = append(mapContent, jobMapContent...)
}
// Check for missing required fields using cached required field info
for tag := range fieldMap.RequiredFields {
if _, ok := foundRequiredFields.Load(tag); !ok {
validationErrs = append(validationErrs, validation.NewValidationError(validation.NewMissingFieldError("%s field %s is missing", modelTag, tag), resolvedNode))
}
}
if embeddedMap != nil {
mapNode.Content = mapContent
embeddedMapValidationErrs, err := unmarshalSequencedMap(ctx, modelTag, &mapNode, embeddedMap)
if err != nil {
return nil, err
}
validationErrs = append(validationErrs, embeddedMapValidationErrs...)
}
// Use the errors to determine the validity of the model
unmarshallable.DetermineValidity(validationErrs)
return validationErrs, nil
}
func unmarshalStruct(ctx context.Context, parentName string, node *yaml.Node, structPtr any) ([]error, error) {
return decodeNode(ctx, parentName, node, structPtr)
}
func decodeNode(_ context.Context, parentName string, node *yaml.Node, out any) ([]error, error) {
resolvedNode := yml.ResolveAlias(node)
if resolvedNode == nil {
return nil, errors.New("node is nil")
}
// Attempt to decode the node
err := resolvedNode.Decode(out)
if err == nil {
return nil, nil // Success
}
// Check if this is a type mismatch error
if isTypeMismatchError(err) {
// Convert type mismatch to validation error
validationErr := validation.NewValidationError(validation.NewTypeMismatchError(fmt.Sprintf("%s%s", getOptionalParentName(parentName), err.Error())), resolvedNode)
return []error{validationErr}, nil
}
// For all other errors (syntax, etc.), return as standard error
return nil, err
}
func unmarshalSequence(ctx context.Context, parentName string, node *yaml.Node, out reflect.Value) ([]error, error) {
resolvedNode := yml.ResolveAlias(node)
if resolvedNode == nil {
return nil, nil
}
if out.Kind() != reflect.Slice {
return nil, fmt.Errorf("expected slice, got %s (type: %s) at line %d, column %d", out.Kind(), out.Type(), resolvedNode.Line, resolvedNode.Column)
}
out.Set(reflect.MakeSlice(out.Type(), len(resolvedNode.Content), len(resolvedNode.Content)))
g, ctx := errgroup.WithContext(ctx)
numJobs := len(resolvedNode.Content)
jobValidationErrs := make([][]error, numJobs)
for i := 0; i < numJobs; i++ {
g.Go(func() error {
valueNode := resolvedNode.Content[i]
elementValidationErrs, err := unmarshal(ctx, parentName, valueNode, out.Index(i))
if err != nil {
return err
}
jobValidationErrs[i] = append(jobValidationErrs[i], elementValidationErrs...)
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
var validationErrs []error
for _, jobValidationErrs := range jobValidationErrs {
validationErrs = append(validationErrs, jobValidationErrs...)
}
return validationErrs, nil
}
func unmarshalNode(ctx context.Context, parentName string, keyNode, valueNode *yaml.Node, fieldName string, out reflect.Value) ([]error, error) {
ref := out
resolvedKeyNode := yml.ResolveAlias(keyNode)
if resolvedKeyNode == nil {
return nil, nil
}
if out.Kind() != reflect.Ptr {
if out.CanSet() {
ref = out.Addr()
} else {
// For non-settable values (like local variables), we need to work with what we have
// This typically happens when out is already a pointer to the value we want to modify
ref = out
}
} else if out.IsNil() {
if out.CanSet() {
out.Set(reflect.New(out.Type().Elem()))
ref = out.Elem().Addr()
} else {
return nil, fmt.Errorf("field %s is a nil pointer and cannot be set at line %d, column %d", fieldName, resolvedKeyNode.Line, resolvedKeyNode.Column)
}
}
unmarshallable, ok := ref.Interface().(NodeMutator)
if !ok {
return nil, fmt.Errorf("expected field '%s' to be marshaller.Node, got %s at line %d, column %d", fieldName, ref.Type(), resolvedKeyNode.Line, resolvedKeyNode.Column)
}
validationErrs, err := unmarshallable.Unmarshal(ctx, parentName, keyNode, valueNode)
if err != nil {
return nil, err
}
// Fix: Only set the value if the original field can be set
if out.CanSet() {
if out.Kind() == reflect.Ptr {
out.Set(reflect.ValueOf(unmarshallable))
} else {
// Get the value from the unmarshallable and set it directly
unmarshallableValue := reflect.ValueOf(unmarshallable)
if unmarshallableValue.Kind() == reflect.Ptr {
unmarshallableValue = unmarshallableValue.Elem()
}
out.Set(unmarshallableValue)
}
}
return validationErrs, nil
}
func implementsInterface(out reflect.Value, interfaceType reflect.Type) bool {
// Store original value to check directly
original := out
// Unwrap interface if needed
for out.Kind() == reflect.Interface && !out.IsNil() {
out = out.Elem()
}
// Get addressable value if needed
if out.Kind() != reflect.Ptr {
if !out.CanAddr() {
// Try checking the original value directly
return original.Type().Implements(interfaceType)
}
out = out.Addr()
}
return out.Type().Implements(interfaceType)
}
func isEmbeddedSequencedMap(out reflect.Value) bool {
return implementsInterface(out, sequencedMapType) && implementsInterface(out, coreModelerType)
}
// isStructType checks if the reflect.Value represents a struct type (direct or pointer to struct)
func isStructType(out reflect.Value) bool {
return out.Kind() == reflect.Struct || (out.Kind() == reflect.Ptr && out.Type().Elem().Kind() == reflect.Struct)
}
// isSliceType checks if the reflect.Value represents a slice type (direct or pointer to slice)
func isSliceType(out reflect.Value) bool {
return out.Kind() == reflect.Slice || (out.Kind() == reflect.Ptr && out.Type().Elem().Kind() == reflect.Slice)
}
// isMapType checks if the reflect.Value represents a map type (direct or pointer to map)
func isMapType(out reflect.Value) bool {
return out.Kind() == reflect.Map || (out.Kind() == reflect.Ptr && out.Type().Elem().Kind() == reflect.Map)
}
// validateNodeKind checks if the node kind matches the expected kind and returns appropriate error
func validateNodeKind(resolvedNode *yaml.Node, expectedKind yaml.Kind, parentName, expectedType string) error {
if resolvedNode == nil {
return validation.NewValidationError(validation.NewTypeMismatchError("%sexpected %s, got nil", getOptionalParentName(parentName), yml.NodeKindToString(expectedKind)), nil)
}
if resolvedNode.Kind != expectedKind {
actualKindStr := yml.NodeKindToString(resolvedNode.Kind)
return validation.NewValidationError(validation.NewTypeMismatchError("%sexpected %s, got %s", getOptionalParentName(parentName), expectedType, actualKindStr), resolvedNode)
}
return nil
}
// isTypeMismatchError checks if the error is a YAML type mismatch error
// using proper type checking instead of string matching
func isTypeMismatchError(err error) bool {
if err == nil {
return false
}
// Check using errors.As for wrapped errors
var yamlTypeErr *yaml.TypeError
return errors.As(err, &yamlTypeErr)
}
// initializeEmbeddedSequencedMap handles initialization of embedded sequenced maps
func initializeEmbeddedSequencedMap(fieldVal reflect.Value) interfaces.SequencedMapInterface {
// Check if the field is a embedded sequenced map
if !implementsInterface(fieldVal, sequencedMapType) {
return nil
}
// Handle both pointer and value embeds
if fieldVal.Kind() == reflect.Ptr {
// Pointer embed - check if nil and initialize if needed
if fieldVal.IsNil() {
fieldVal.Set(CreateInstance(fieldVal.Type().Elem()))
}
return fieldVal.Interface().(interfaces.SequencedMapInterface)
} else {
// Value embed - check if initialized and initialize if needed
if seqMapInterface, ok := fieldVal.Addr().Interface().(interfaces.SequencedMapInterface); ok {
if !seqMapInterface.IsInitialized() {
// Initialize the value embed by creating a new instance and copying it
newInstance := CreateInstance(fieldVal.Type())
fieldVal.Set(newInstance.Elem())
}
return seqMapInterface
}
}
return nil
}
func getOptionalParentName(parentName string) string {
if parentName != "" {
parentName += " "
}
return parentName
}