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

Function buildViewUnionTypeData

codegen/service/service_data.go:1181–1213  ·  view source on GitHub ↗

buildViewUnionTypeData creates the data needed to generate a sum-type union in the views package. Field types are computed using the view scope and are always emitted unqualified so they refer to the view-local projected types.

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

Source from the content-addressed store, hash-verified

1179// in the views package. Field types are computed using the view scope and are
1180// always emitted unqualified so they refer to the view-local projected types.
1181func buildViewUnionTypeData(u *expr.Union, scope *codegen.NameScope, loc *codegen.Location) *UnionTypeData {
1182 att := &expr.AttributeExpr{Type: u}
1183 name := scope.GoTypeName(att)
1184 kindName := scope.Unique(name + "Kind")
1185
1186 fields := make([]*UnionFieldData, len(u.Values))
1187 for i, nat := range u.Values {
1188 fieldName := codegen.Goify(nat.Name, true)
1189 fieldType := scope.GoTypeRef(nat.Attribute)
1190 primitiveAliasType, hasPrimitiveAlias := primitiveAliasGoType(nat.Attribute.Type)
1191 _, isUserType := nat.Attribute.Type.(expr.UserType)
1192 emitPrimitiveAlias := hasPrimitiveAlias && !isUserType
1193 kindConst := kindName + codegen.Goify(nat.Name, true)
1194 fields[i] = &UnionFieldData{
1195 Name: nat.Name,
1196 KindConst: kindConst,
1197 FieldName: fieldName,
1198 FieldType: fieldType,
1199 EmitPrimitiveAlias: emitPrimitiveAlias,
1200 PrimitiveAliasType: primitiveAliasType,
1201 TypeTag: nat.Name,
1202 }
1203 }
1204
1205 return &UnionTypeData{
1206 Name: name,
1207 KindName: kindName,
1208 Fields: fields,
1209 Loc: loc,
1210 TypeKey: u.GetTypeKey(),
1211 ValueKey: u.GetValueKey(),
1212 }
1213}
1214
1215// sortedNamedAttributes returns object fields sorted by attribute name.
1216// Union naming uses NameScope uniqueness, so callers that discover unions while

Callers 1

collectViewUnionTypesFunction · 0.85

Calls 7

GoifyFunction · 0.92
primitiveAliasGoTypeFunction · 0.85
GoTypeNameMethod · 0.80
UniqueMethod · 0.80
GoTypeRefMethod · 0.80
GetTypeKeyMethod · 0.80
GetValueKeyMethod · 0.80

Tested by

no test coverage detected