MCPcopy Create free account
hub / github.com/apache/fory / formatGoType

Function formatGoType

go/fory/codegen/guard.go:95–156  ·  view source on GitHub ↗

formatGoType converts a Go type to its string representation

(t types.Type)

Source from the content-addressed store, hash-verified

93
94// formatGoType converts a Go type to its string representation
95func formatGoType(t types.Type) string {
96 switch type_ := t.(type) {
97 case *types.Alias:
98 // Handle alias types like 'any' (alias for interface{})
99 // Check if it's the 'any' alias specifically
100 if type_.Obj().Name() == "any" {
101 return "any"
102 }
103 // For other aliases, use the alias name if it's from universe scope,
104 // otherwise format the underlying type
105 if type_.Obj().Pkg() == nil {
106 return type_.Obj().Name()
107 }
108 return formatGoType(types.Unalias(t))
109 case *types.Basic:
110 return type_.Name()
111 case *types.Pointer:
112 return "*" + formatGoType(type_.Elem())
113 case *types.Array:
114 return fmt.Sprintf("[%d]%s", type_.Len(), formatGoType(type_.Elem()))
115 case *types.Slice:
116 return "[]" + formatGoType(type_.Elem())
117 case *types.Map:
118 return fmt.Sprintf("map[%s]%s", formatGoType(type_.Key()), formatGoType(type_.Elem()))
119 case *types.Chan:
120 dir := ""
121 switch type_.Dir() {
122 case types.SendOnly:
123 dir = "chan<- "
124 case types.RecvOnly:
125 dir = "<-chan "
126 default:
127 dir = "chan "
128 }
129 return dir + formatGoType(type_.Elem())
130 case *types.Named:
131 // Handle named types like custom structs, interfaces, etc.
132 obj := type_.Obj()
133 if obj.Pkg() != nil && obj.Pkg().Name() != "" {
134 return obj.Pkg().Name() + "." + obj.Name()
135 }
136 return obj.Name()
137 case *types.Interface:
138 if type_.Empty() {
139 return "any"
140 }
141 // For non-empty interfaces, we need to format method signatures
142 var methods []string
143 for i := 0; i < type_.NumMethods(); i++ {
144 method := type_.Method(i)
145 sig := method.Type().(*types.Signature)
146 methods = append(methods, formatMethodSignature(method.Name(), sig))
147 }
148 return fmt.Sprintf("interface { %s }", strings.Join(methods, "; "))
149 case *types.Struct:
150 // This shouldn't happen in field types typically, but handle it
151 return "struct{...}"
152 default:

Callers 2

formatFieldTypeFunction · 0.85
formatMethodSignatureFunction · 0.85

Calls 5

formatMethodSignatureFunction · 0.85
LenMethod · 0.80
EmptyMethod · 0.45
TypeMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected