@@ -565,81 +565,6 @@ type FieldInfo struct {
565
565
Schema * devmodel.JsonSchema
566
566
}
567
567
568
- //func ConvertCodeToValue(code string, schema *devmodel.JsonSchema, inputType reflect.Type) (reflect.Value, error) {
569
- // fullCode := `package main
570
- //
571
- // func ToPtr[T any](v T) *T {
572
- // return &v
573
- // }
574
- // ` + code
575
- //
576
- // //node, err := parser.ParseFile(token.NewFileSet(), "", "package main\n"+code, parser.ParseComments)
577
- // node, err := parser.ParseFile(token.NewFileSet(), "", fullCode, parser.ParseComments)
578
- // if err != nil {
579
- // return reflect.Value{}, err
580
- // }
581
- //
582
- // var result interface{}
583
- // ast.Inspect(node, func(n ast.Node) bool {
584
- // vs, ok := n.(*ast.ValueSpec)
585
- // if !ok {
586
- // return true
587
- // }
588
- //
589
- // for _, value := range vs.Values {
590
- // var cl *ast.CompositeLit
591
- // switch v := value.(type) {
592
- // case *ast.UnaryExpr:
593
- // cl, ok = v.X.(*ast.CompositeLit)
594
- // if !ok {
595
- // continue
596
- // }
597
- // result = parseCompositeLit(cl, schema, "")
598
- // case *ast.CompositeLit:
599
- // if schema.Type == devmodel.JsonTypeOfArray {
600
- // result = parseArrayLit(v, schema)
601
- // } else if schema.Type == devmodel.JsonTypeOfObject && schema.AdditionalProperties != nil {
602
- // result = parseMapLit(v, schema)
603
- // } else {
604
- // result = parseCompositeLit(v, schema, "schema.Message")
605
- // }
606
- // case *ast.BasicLit:
607
- // result = parseBasicLit(v)
608
- // case *ast.Ident:
609
- // switch v.Name {
610
- // case "true":
611
- // result = true
612
- // case "false":
613
- // result = false
614
- // case "nil":
615
- // result = nil
616
- // default:
617
- // result = v.Name
618
- // }
619
- // case *ast.CallExpr:
620
- // result = parseCallExpr(v)
621
- // default:
622
- // continue
623
- // }
624
- // }
625
- // return false
626
- // })
627
- //
628
- // val := reflect.New(inputType)
629
- // if schema.Type == "interface" {
630
- // implType, ok := GetRegisteredType("schema.Message")
631
- // if ok {
632
- // val = reflect.New(implType.Type)
633
- // }
634
- // }
635
- //
636
- // if err := convertToValue(result, val.Elem()); err != nil {
637
- // return reflect.Value{}, err
638
- // }
639
- //
640
- // return val.Elem(), nil
641
- //}
642
-
643
568
func ConvertCodeToValue (code string , schema * devmodel.JsonSchema , inputType reflect.Type ) (reflect.Value , error ) {
644
569
fullCode := `package main
645
570
@@ -655,6 +580,7 @@ func ConvertCodeToValue(code string, schema *devmodel.JsonSchema, inputType refl
655
580
656
581
var result interface {}
657
582
var structTypeName string
583
+ var ptrNum int
658
584
ast .Inspect (node , func (n ast.Node ) bool {
659
585
vs , ok := n .(* ast.ValueSpec )
660
586
if ! ok {
@@ -663,7 +589,7 @@ func ConvertCodeToValue(code string, schema *devmodel.JsonSchema, inputType refl
663
589
664
590
// Try to extract struct type name from value spec
665
591
if len (vs .Values ) > 0 {
666
- structTypeName = extractStructTypeName (vs .Values [0 ])
592
+ structTypeName , ptrNum = extractStructTypeName (vs .Values [0 ])
667
593
}
668
594
669
595
for _ , value := range vs .Values {
@@ -709,7 +635,12 @@ func ConvertCodeToValue(code string, schema *devmodel.JsonSchema, inputType refl
709
635
if schema .Type == "interface" && structTypeName != "" {
710
636
implType , ok := GetRegisteredType (structTypeName )
711
637
if ok {
712
- val = reflect .New (implType .Type )
638
+ // create a type with the correct pointer depth.
639
+ valType := implType .Type
640
+ for i := 0 ; i < ptrNum ; i ++ {
641
+ valType = reflect .PointerTo (valType )
642
+ }
643
+ val = reflect .New (valType )
713
644
}
714
645
}
715
646
@@ -720,38 +651,47 @@ func ConvertCodeToValue(code string, schema *devmodel.JsonSchema, inputType refl
720
651
return val .Elem (), nil
721
652
}
722
653
723
- // extractStructTypeName extracts struct type name from an expression
724
- func extractStructTypeName (expr ast.Expr ) string {
654
+ // extractStructTypeName extracts struct type name and pointer depth from an expression
655
+ // Returns the struct type name and number of pointer levels
656
+ func extractStructTypeName (expr ast.Expr ) (string , int ) {
725
657
switch v := expr .(type ) {
726
658
case * ast.CompositeLit :
727
659
// Handle direct struct initialization: schema.Message{...}
728
660
switch t := v .Type .(type ) {
729
661
case * ast.SelectorExpr :
730
662
if x , ok := t .X .(* ast.Ident ); ok {
731
- return x .Name + "." + t .Sel .Name
663
+ return x .Name + "." + t .Sel .Name , 0
732
664
}
733
665
case * ast.Ident :
734
- return t .Name
666
+ return t .Name , 0
735
667
}
736
668
case * ast.UnaryExpr :
737
669
// Handle pointer expressions: &schema.Message{...}
738
- if cl , ok := v .X .(* ast.CompositeLit ); ok {
739
- switch t := cl .Type .(type ) {
740
- case * ast.SelectorExpr :
741
- if x , ok := t .X .(* ast.Ident ); ok {
742
- return x .Name + "." + t .Sel .Name
743
- }
744
- case * ast.Ident :
745
- return t .Name
746
- }
670
+ if v .Op == token .AND {
671
+ typeName , ptrCount := extractStructTypeName (v .X )
672
+ return typeName , ptrCount + 1
747
673
}
748
674
case * ast.CallExpr :
749
- // Handle ToPtr(schema.Message{...}) calls
750
- if len (v .Args ) > 0 {
751
- return extractStructTypeName (v .Args [0 ])
675
+ // Check if this is a ToPtr call
676
+ if fun , ok := v .Fun .(* ast.Ident ); ok && fun .Name == "ToPtr" && len (v .Args ) > 0 {
677
+ // Handle ToPtr(schema.Message{...}) calls
678
+ typeName , ptrCount := extractStructTypeName (v .Args [0 ])
679
+ return typeName , ptrCount + 1
680
+ } else if _ , ok := v .Fun .(* ast.SelectorExpr ); ok {
681
+ // Handle other function calls that might return a struct
682
+ return "" , 0
683
+ } else {
684
+ // Handle nested ToPtr calls: ToPtr(ToPtr(...))
685
+ typeName , ptrCount := extractStructTypeName (v .Fun )
686
+ if typeName != "" && len (v .Args ) > 0 {
687
+ innerTypeName , innerPtrCount := extractStructTypeName (v .Args [0 ])
688
+ if innerTypeName != "" {
689
+ return innerTypeName , ptrCount + innerPtrCount
690
+ }
691
+ }
752
692
}
753
693
}
754
- return ""
694
+ return "" , 0
755
695
}
756
696
757
697
func parseCallExpr (call * ast.CallExpr ) interface {} {
@@ -818,9 +758,13 @@ func parseExpr(expr ast.Expr, schema *devmodel.JsonSchema) interface{} {
818
758
if schema .AdditionalProperties != nil {
819
759
return parseMapLit (v , schema )
820
760
}
821
- return parseCompositeLit (v , schema , "" )
761
+ structName , _ := extractStructTypeName (v )
762
+ return parseCompositeLit (v , schema , structName )
822
763
case devmodel .JsonTypeOfArray :
823
764
return parseArrayLit (v , schema )
765
+ case devmodel .JsonTypeOfInterface :
766
+ structName , _ := extractStructTypeName (v )
767
+ return parseCompositeLit (v , schema , structName )
824
768
default :
825
769
return nil
826
770
}
0 commit comments