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

Function collectViewUnionTypes

codegen/service/service_data.go:1147–1176  ·  view source on GitHub ↗

collectViewUnionTypes traverses the attribute to gather all union sum-type definitions referenced by view-projected types. It always uses the provided location for all nested user types so that unions are generated in the views package and refer to view-local types (preventing import cycles).

(att *expr.AttributeExpr, scope *codegen.NameScope, loc *codegen.Location, unions map[string]*UnionTypeData, seen map[string]struct{})

Source from the content-addressed store, hash-verified

1145// location for all nested user types so that unions are generated in the views
1146// package and refer to view-local types (preventing import cycles).
1147func collectViewUnionTypes(att *expr.AttributeExpr, scope *codegen.NameScope, loc *codegen.Location, unions map[string]*UnionTypeData, seen map[string]struct{}) {
1148 if att == nil || att.Type == expr.Empty {
1149 return
1150 }
1151 switch dt := att.Type.(type) {
1152 case expr.UserType:
1153 if _, ok := seen[dt.ID()]; ok {
1154 return
1155 }
1156 seen[dt.ID()] = struct{}{}
1157 collectViewUnionTypes(dt.Attribute(), scope, loc, unions, seen)
1158 case *expr.Object:
1159 for _, nat := range sortedNamedAttributes(*dt) {
1160 collectViewUnionTypes(nat.Attribute, scope, loc, unions, seen)
1161 }
1162 case *expr.Array:
1163 collectViewUnionTypes(dt.ElemType, scope, loc, unions, seen)
1164 case *expr.Map:
1165 collectViewUnionTypes(dt.KeyType, scope, loc, unions, seen)
1166 collectViewUnionTypes(dt.ElemType, scope, loc, unions, seen)
1167 case *expr.Union:
1168 hash := dt.Hash()
1169 if _, ok := unions[hash]; !ok {
1170 unions[hash] = buildViewUnionTypeData(dt, scope, loc)
1171 }
1172 for _, nat := range dt.Values {
1173 collectViewUnionTypes(nat.Attribute, scope, loc, unions, seen)
1174 }
1175 }
1176}
1177
1178// buildViewUnionTypeData creates the data needed to generate a sum-type union
1179// in the views package. Field types are computed using the view scope and are

Callers 1

ViewsFileFunction · 0.85

Calls 5

buildViewUnionTypeDataFunction · 0.85
sortedNamedAttributesFunction · 0.70
IDMethod · 0.65
AttributeMethod · 0.65
HashMethod · 0.65

Tested by

no test coverage detected