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

Function buildUnionTypeData

codegen/service/service_data.go:1101–1141  ·  view source on GitHub ↗

buildUnionTypeData creates the data needed to generate a sum-type union struct, its discriminator kind, and branch metadata.

(u *expr.Union, scope *codegen.NameScope, loc *codegen.Location)

Source from the content-addressed store, hash-verified

1099// buildUnionTypeData creates the data needed to generate a sum-type union
1100// struct, its discriminator kind, and branch metadata.
1101func buildUnionTypeData(u *expr.Union, scope *codegen.NameScope, loc *codegen.Location) *UnionTypeData {
1102 att := &expr.AttributeExpr{Type: u}
1103 name := scope.GoTypeName(att)
1104 kindName := scope.Unique(name + "Kind")
1105 unionPkg := loc.PackageName()
1106
1107 fields := make([]*UnionFieldData, len(u.Values))
1108 for i, nat := range u.Values {
1109 fieldName := codegen.Goify(nat.Name, true)
1110 var pkg string
1111 if tloc := codegen.UserTypeLocation(nat.Attribute.Type); tloc != nil {
1112 pkg = tloc.PackageName()
1113 if pkg == unionPkg {
1114 pkg = ""
1115 }
1116 }
1117 fieldType := scope.GoFullTypeRef(nat.Attribute, pkg)
1118 primitiveAliasType, hasPrimitiveAlias := primitiveAliasGoType(nat.Attribute.Type)
1119 _, isUserType := nat.Attribute.Type.(expr.UserType)
1120 emitPrimitiveAlias := hasPrimitiveAlias && !isUserType && pkg == ""
1121 kindConst := kindName + codegen.Goify(nat.Name, true)
1122 fields[i] = &UnionFieldData{
1123 Name: nat.Name,
1124 KindConst: kindConst,
1125 FieldName: fieldName,
1126 FieldType: fieldType,
1127 EmitPrimitiveAlias: emitPrimitiveAlias,
1128 PrimitiveAliasType: primitiveAliasType,
1129 TypeTag: nat.Name,
1130 }
1131 }
1132
1133 return &UnionTypeData{
1134 Name: name,
1135 KindName: kindName,
1136 Fields: fields,
1137 Loc: loc,
1138 TypeKey: u.GetTypeKey(),
1139 ValueKey: u.GetValueKey(),
1140 }
1141}
1142
1143// collectViewUnionTypes traverses the attribute to gather all union sum-type
1144// definitions referenced by view-projected types. It always uses the provided

Callers 1

collectUnionTypesFunction · 0.85

Calls 9

GoifyFunction · 0.92
UserTypeLocationFunction · 0.92
primitiveAliasGoTypeFunction · 0.85
GoTypeNameMethod · 0.80
UniqueMethod · 0.80
PackageNameMethod · 0.80
GoFullTypeRefMethod · 0.80
GetTypeKeyMethod · 0.80
GetValueKeyMethod · 0.80

Tested by

no test coverage detected