MCPcopy Create free account
hub / github.com/goadesign/goa / goTypeDefWithPkgOverride

Method goTypeDefWithPkgOverride

codegen/scope.go:148–237  ·  view source on GitHub ↗

goTypeDefWithPkgOverride generates the Go type definition string for the attribute. When targetPkg is not empty, user types referenced inside inline structs are qualified against targetPkg unless they are defined in a different package, in which case their own package is used. When targetPkg is empt

(att *expr.AttributeExpr, ptr, useDefault bool, pkg, targetPkg string)

Source from the content-addressed store, hash-verified

146// in which case their own package is used. When targetPkg is empty, the
147// package qualification falls back to pkg and the user type location.
148func (s *NameScope) goTypeDefWithPkgOverride(att *expr.AttributeExpr, ptr, useDefault bool, pkg, targetPkg string) string {
149 switch actual := att.Type.(type) {
150 case expr.Primitive:
151 if t, _ := GetMetaType(att); t != "" {
152 return t
153 }
154 return GoNativeTypeName(actual)
155 case *expr.Array:
156 d := s.goTypeDefWithPkgOverride(actual.ElemType, ptr, useDefault, pkg, targetPkg)
157 if expr.IsObject(actual.ElemType.Type) {
158 d = "*" + d
159 }
160 return "[]" + d
161 case *expr.Map:
162 keyDef := s.goTypeDefWithPkgOverride(actual.KeyType, ptr, useDefault, pkg, targetPkg)
163 if expr.IsObject(actual.KeyType.Type) {
164 keyDef = "*" + keyDef
165 }
166 elemDef := s.goTypeDefWithPkgOverride(actual.ElemType, ptr, useDefault, pkg, targetPkg)
167 if expr.IsObject(actual.ElemType.Type) {
168 elemDef = "*" + elemDef
169 }
170 return fmt.Sprintf("map[%s]%s", keyDef, elemDef)
171 case *expr.Union:
172 // Unions are generated as named sum-type structs. Refer to the named type
173 // here; the concrete definition is emitted separately by the service
174 // code generator.
175 if targetPkg != "" {
176 return s.GoFullTypeName(att, targetPkg)
177 }
178 return s.GoFullTypeName(att, "")
179 case *expr.Object:
180 ss := []string{"struct {"}
181 for _, nat := range *actual {
182 var (
183 fn string
184 tdef string
185 desc string
186 tags string
187
188 name = nat.Name
189 at = nat.Attribute
190 )
191 {
192 fn = GoifyAtt(at, name, true)
193 tdef = s.goTypeDefWithPkgOverride(at, ptr, useDefault, pkg, targetPkg)
194 if expr.IsObject(at.Type) ||
195 att.IsPrimitivePointer(name, useDefault) ||
196 (ptr && expr.IsPrimitive(at.Type) && at.Type.Kind() != expr.AnyKind && at.Type.Kind() != expr.BytesKind) {
197 tdef = "*" + tdef
198 }
199 if at.Description != "" {
200 desc = Comment(at.Description) + "\n\t"
201 }
202 tags = AttributeTagsWithName(att, name, at)
203 }
204 ss = append(ss, fmt.Sprintf("\t%s%s %s%s", desc, fn, tdef, tags))
205 }

Callers 2

goTypeDefMethod · 0.95

Calls 15

GoFullTypeNameMethod · 0.95
GoTypeNameMethod · 0.95
IsObjectFunction · 0.92
IsPrimitiveFunction · 0.92
GetMetaTypeFunction · 0.85
GoNativeTypeNameFunction · 0.85
GoifyAttFunction · 0.85
CommentFunction · 0.85
AttributeTagsWithNameFunction · 0.85
UserTypeLocationFunction · 0.85
GoifyFunction · 0.85
PackageNameMethod · 0.80

Tested by

no test coverage detected