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

Function goTypeDef

http/codegen/typedef.go:26–97  ·  view source on GitHub ↗

goTypeDef returns the Go code that defines the struct corresponding to ma. It differs from the function defined in the codegen package in the following ways: - It defines marshaler tags on each fields using the HTTP element names. - It produced fields with pointers even if the corresponding attrib

(scope *codegen.NameScope, att *expr.AttributeExpr, ptr, useDefault bool)

Source from the content-addressed store, hash-verified

24// default value so cannot be nil) otherwise the fields are values only when
25// required.
26func goTypeDef(scope *codegen.NameScope, att *expr.AttributeExpr, ptr, useDefault bool) string {
27 switch actual := att.Type.(type) {
28 case expr.Primitive:
29 if t, _ := codegen.GetMetaType(att); t != "" {
30 return t
31 }
32 return codegen.GoNativeTypeName(actual)
33 case *expr.Array:
34 d := goTypeDef(scope, actual.ElemType, ptr, useDefault)
35 if expr.IsObject(actual.ElemType.Type) {
36 d = "*" + d
37 }
38 return "[]" + d
39 case *expr.Map:
40 keyDef := goTypeDef(scope, actual.KeyType, ptr, useDefault)
41 if expr.IsObject(actual.KeyType.Type) {
42 keyDef = "*" + keyDef
43 }
44 elemDef := goTypeDef(scope, actual.ElemType, ptr, useDefault)
45 if expr.IsObject(actual.ElemType.Type) {
46 elemDef = "*" + elemDef
47 }
48 return fmt.Sprintf("map[%s]%s", keyDef, elemDef)
49 case *expr.Object:
50 var ss []string
51 ss = append(ss, "struct {")
52 ma := expr.NewMappedAttributeExpr(att)
53 mat := ma.Attribute()
54 codegen.WalkMappedAttr(ma, func(name, elem string, _ bool, at *expr.AttributeExpr) error { // nolint: errcheck
55 var (
56 fn string
57 tdef string
58 desc string
59 tags string
60 )
61 {
62 fn = codegen.GoifyAtt(at, name, true)
63 tdef = goTypeDef(scope, at, ptr, useDefault)
64 if expr.IsPrimitive(at.Type) {
65 if (ptr || mat.IsPrimitivePointer(name, useDefault)) && at.Type != expr.Bytes && at.Type != expr.Any {
66 tdef = "*" + tdef
67 }
68 } else if expr.IsObject(at.Type) {
69 tdef = "*" + tdef
70 }
71 if at.Description != "" {
72 desc = codegen.Comment(at.Description) + "\n\t"
73 }
74 var optional bool
75 {
76 switch {
77 case ptr:
78 optional = true
79 case useDefault:
80 optional = !ma.IsRequired(name) && !ma.HasDefaultValue(name)
81 default:
82 optional = !ma.IsRequired(name)
83 }

Callers 4

TestGoTypeDefFunction · 0.85
buildRequestBodyTypeMethod · 0.85
buildResponseBodyTypeMethod · 0.85
attributeTypeDataMethod · 0.85

Calls 14

AttributeMethod · 0.95
GetMetaTypeFunction · 0.92
GoNativeTypeNameFunction · 0.92
IsObjectFunction · 0.92
NewMappedAttributeExprFunction · 0.92
WalkMappedAttrFunction · 0.92
GoifyAttFunction · 0.92
IsPrimitiveFunction · 0.92
CommentFunction · 0.92
attributeTagsFunction · 0.85
IsRequiredMethod · 0.80
HasDefaultValueMethod · 0.80

Tested by 1

TestGoTypeDefFunction · 0.68