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)
| 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. |
| 1181 | func 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 |
no test coverage detected