Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/example/datamodel/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@ type TestValMarkers struct {

type SomeStruct struct{}

type AliasedField gns.MyStr
type AliasedFieldMap map[string]gns.MyStr
type AliasedFieldList []gns.MyStr

type StructWithEmbeddedField struct {
SomeStruct
gns.MyStr
ExplicitField gns.MyStr
AliasedField AliasedField
AliasedFieldMap AliasedFieldMap
AliasedFieldList AliasedFieldList
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions compiler/pkg/generator/resolver_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func processNonNexusFields(aliasNameMap map[string]string, node *ast.TypeSpec,
fieldProp FieldProperty
err error
)
typeString := ConstructType(aliasNameMap, f)
typeString := ConstructTypeWrapper(aliasNameMap, f)
// populate each field properties
if len(f.Names) > 0 {
fieldProp.FieldName, err = parser.GetNodeFieldName(f)
Expand Down Expand Up @@ -424,7 +424,7 @@ func processNexusFields(pkg parser.Package, aliasNameMap map[string]string, node
}

// nexus link field
typeString := ConstructType(aliasNameMap, nf)
typeString := ConstructTypeWrapper(aliasNameMap, nf)
if parser.IsOnlyLinkField(nf) {
schemaTypeName, resolverTypeName := ValidateImportPkg(nodeProp.PkgName, typeString, importMap, pkgs)
// `type:string` annotation used to consider the type as string `nexus-graphql:"type:string"`
Expand Down
2 changes: 1 addition & 1 deletion compiler/pkg/generator/template_renderers.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func RenderTypesTemplate(crdModulePath string, pkg parser.Package) (*bytes.Buffe
vars.Imports = parsePackageImports(pkg, aliasNameMap)
vars.CRDTypes = parsePackageCRDs(pkg, aliasNameMap)
vars.Structs = parsePackageStructs(pkg, aliasNameMap)
vars.Types = parsePackageTypes(pkg)
vars.Types = parsePackageTypes(pkg, aliasNameMap)
vars.Consts = parsePackageConsts(pkg)
vars.CommonImport = util.GetInternalImport(crdModulePath, "common")

Expand Down
30 changes: 23 additions & 7 deletions compiler/pkg/generator/types_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ type {{.Name}}Spec struct {
}
}
specDef.Fields += "\t" + name + " "
typeString := ConstructType(aliasNameMap, field)
typeString := ConstructTypeWrapper(aliasNameMap, field)
specDef.Fields += typeString
specDef.Fields += " " + getTag(field, name, false) + "\n"
}
Expand Down Expand Up @@ -272,7 +272,7 @@ type {{.Name}} struct {
}
}
specDef.Fields += "\t" + name + " "
typeString := ConstructType(aliasNameMap, field)
typeString := ConstructTypeWrapper(aliasNameMap, field)

currentTags := parser.GetFieldTags(field)
currentTags = parser.FillEmptyTag(currentTags, name, "json")
Expand All @@ -297,13 +297,23 @@ type {{.Name}} struct {
return b.String()
}

func parsePackageTypes(pkg parser.Package) string {
func parsePackageTypes(pkg parser.Package, aliasNameMap map[string]string) string {
var output string
for _, node := range pkg.GetTypes() {
t, err := pkg.GenDeclToString(&node)
if err != nil {
log.Fatalf("failed to translate type gen decl to string: %v", err)
}

isMap := parser.IsDeclMapField(&node)
isArray := parser.IsDeclArrayField(&node)

lastSpace := strings.LastIndex(t, " ")
name := t[:lastSpace]
typeT := t[lastSpace+1:]

t = fmt.Sprintf("%s %s", name, ConstructType(aliasNameMap, typeT, isMap, isArray))

output += t + "\n"
}

Expand Down Expand Up @@ -407,22 +417,28 @@ func constructImports(inputAlias, inputImportPath string) (string, string) {
return aliasName, importPath
}

func ConstructTypeWrapper(aliasNameMap map[string]string, field *ast.Field) string {
typeString := types.ExprString(field.Type)
isArray := parser.IsArrayField(field)
isMap := parser.IsMapField(field)
return ConstructType(aliasNameMap, typeString, isMap, isArray)
}

// TODO: https://jira.eng.vmware.com/browse/NPT-296
// Support cross-package imports for the following additional types:
// 1. map[gns.MyStr][]gns.MyStr
// 2. map[string]map[string]gns.MyStr
// 3. []map[string]gns.MyStr
// 4. **gns.MyStr
func ConstructType(aliasNameMap map[string]string, field *ast.Field) string {
typeString := types.ExprString(field.Type)
func ConstructType(aliasNameMap map[string]string, typeString string, isMap, isArray bool) string {

// Check if the field is imported from a different package.
if !strings.Contains(typeString, ".") {
return typeString
}

switch {
case parser.IsMapField(field):
case isMap:
// TODO: Check if the function GetFieldType(field) can be reused for cases other than:
// map[string]gns.MyStr
// https://jira.eng.vmware.com/browse/NPT-296
Expand All @@ -438,7 +454,7 @@ func ConstructType(aliasNameMap map[string]string, field *ast.Field) string {
types = append(types, val)
}
typeString = fmt.Sprintf("map[%s]%s", types[0], types[1])
case parser.IsArrayField(field):
case isArray:
arr := regexp.MustCompile(`^(\[])`).ReplaceAllString(typeString, "")
parts := strings.Split(arr, ".")
if len(parts) > 1 {
Expand Down
28 changes: 28 additions & 0 deletions compiler/pkg/parser/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,34 @@ func IsArrayField(f *ast.Field) bool {
return false
}

func IsDeclArrayField(f *ast.GenDecl) bool {
if f == nil {
return false
}
ts, ok := f.Specs[0].(*ast.TypeSpec)
if !ok {
return false
}
if _, ok = ts.Type.(*ast.ArrayType); ok {
return true
}
return false
}

func IsDeclMapField(f *ast.GenDecl) bool {
if f == nil {
return false
}
ts, ok := f.Specs[0].(*ast.TypeSpec)
if !ok {
return false
}
if _, ok = ts.Type.(*ast.MapType); ok {
return true
}
return false
}

func IsAggregateKind(f *ast.Field) bool {
return IsArrayField(f) || IsMapField(f)
}
Expand Down