diff --git a/cli/parser/field.go b/cli/parser/field.go index 7c1ef8f..f917357 100644 --- a/cli/parser/field.go +++ b/cli/parser/field.go @@ -58,6 +58,7 @@ func parseField(typ types.Type) *models.Field { } field.Definition = models.CollectionPointer + collectedDefinition(elemfield) + field.VariableName = "." + alphastring(elemfield.Definition) case *types.Array: field.Definition = "[" + fmt.Sprint(x.Len()) + "]" + collectedDefinition(parseField(x.Elem())) @@ -200,5 +201,16 @@ func collectedDefinition(collected *models.Field) string { return collected.Definition } + // when a setup file imports the package it will output to, + // do NOT reference the fields defined in the output package, by package. + if outputPkgPath != "" && collected.Import == outputPkgPath { + return collected.Definition + } + + // when a field's import uses an alias, reassign the package reference. + if aliasPkg, ok := aliasImportMap[collected.Import]; ok { + return aliasPkg + "." + collected.Definition + } + return collected.FullDefinition() } diff --git a/cli/parser/parse.go b/cli/parser/parse.go index 840dede..8460372 100644 --- a/cli/parser/parse.go +++ b/cli/parser/parse.go @@ -51,12 +51,32 @@ var ( // setupPkgPath represents the current path of the setup file's package. // - // setupPkgPath is used to remove package references from collected types in collections - // that will be used in the generated file's package (equal to the setup file's package). + // setupPkgPath is used to remove package references from types that will be + // used in the generated file's package (equal to the setup file's package). + // + // setupPkgPath is referenced while parsing collected type definitions for collection fields, + // and while setting package references for non-collection fields after parsing. // // i.e `Collections` parsed as `copygen.Collections` in the setup file's package copygen, // output as `Collections` in the generated file's package copygen. setupPkgPath string + + // outputPkgPath represents the generated file's package path. + // + // outputPkgPath is used to remove package references from types that are imported + // (in the setup file) from the generated file's package. + // + // outputPkgPath is referenced while parsing collected type definitions for collection fields, + // and while setting package references for non-collection fields after parsing. + outputPkgPath string + + // aliasImportMap represents a map of import paths to package names. + // + // aliasImportMap is used to assign the correct package reference to an aliased field. + // + // aliasImportMap is referenced while parsing collected type definitions for collection fields, + // and while setting package references for non-collection fields after parsing. + aliasImportMap map[string]string ) // SetupCache sets up the parser's global cache. @@ -98,6 +118,20 @@ func Parse(gen *models.Generator) error { } setupPkgPath = p.Pkgs[0].PkgPath + // determine the output file package path. + outputPkgs, _ := packages.Load(&packages.Config{Mode: packages.NeedName}, "file="+gen.Outpath) + if len(outputPkgs) > 0 { + outputPkgPath = outputPkgs[0].PkgPath + } + + // set the aliasImportMap. + aliasImportMap = make(map[string]string, len(p.Config.SetupFile.Imports)) + for _, imp := range p.Config.SetupFile.Imports { + if imp.Name != nil { + aliasImportMap[imp.Path.Value[1:len(imp.Path.Value)-1]] = imp.Name.Name + } + } + // find a new instance of a copygen AST since the old one has its comments removed. var newCopygen *ast.InterfaceType for _, decl := range p.Pkgs[0].Syntax[0].Decls { @@ -120,8 +154,8 @@ func Parse(gen *models.Generator) error { return fmt.Errorf("%w", err) } - // rename fields' packages using imports. - p.setPackages(gen) + // rename non-collection fields' packages using imports. + setPackages(gen) // Write the Keep. buf := new(bytes.Buffer) @@ -133,29 +167,14 @@ func Parse(gen *models.Generator) error { // reset global variables. setupPkgPath = "" + outputPkgPath = "" + aliasImportMap = nil return nil } -// setFieldPackage sets the packages for all fields in a generator using names from the setup file. -func (p *Parser) setPackages(gen *models.Generator) { - - // determine the output file package path. - var outputPkgPath string - pkgs, _ := packages.Load(&packages.Config{Mode: packages.NeedName}, "file="+gen.Outpath) - if len(pkgs) > 0 { - outputPkgPath = pkgs[0].PkgPath - } - - // adjust the packages of each field where necessary. - imports := p.Config.SetupFile.Imports - aliasImportMap := make(map[string]string, len(imports)) - for _, imp := range imports { - if imp.Name != nil { - aliasImportMap[imp.Path.Value[1:len(imp.Path.Value)-1]] = imp.Name.Name - } - } - +// setPackages sets the packages for all fields in a generator using names from the setup file. +func setPackages(gen *models.Generator) { for _, function := range gen.Functions { functionTypes := [][]models.Type{ function.From, diff --git a/cli/parser/type.go b/cli/parser/type.go index a4e2a8c..846cb7b 100644 --- a/cli/parser/type.go +++ b/cli/parser/type.go @@ -37,7 +37,9 @@ func parseTypeField(vars *types.Tuple) []models.Type { for i := 0; i < vars.Len(); i++ { field := parseField(vars.At(i).Type()).Deepcopy(nil) field.Name = vars.At(i).Name() - field.VariableName = "." + alphastring(field.Definition) + if !field.IsPointer() { + field.VariableName = "." + alphastring(field.Definition) + } types[i] = models.Type{Field: field} } diff --git a/examples/_tests/multi/copygen.go b/examples/_tests/multi/copygen.go index aad834a..8cff232 100644 --- a/examples/_tests/multi/copygen.go +++ b/examples/_tests/multi/copygen.go @@ -41,9 +41,9 @@ func Array(tb [16]byte, fb [16]byte) { } // ArrayComplex copies a [16]map[byte]string to a *complex.Collection. -func ArrayComplex(tc *complex.Collection, fm [16]map[byte]string) { +func ArrayComplex(tC *complex.Collection, fm [16]map[byte]string) { // *complex.Collection fields - tc.Arr = fm + tC.Arr = fm } // ArrayExternal copies a [16]external.Placeholder to a [16]external.Placeholder. @@ -53,9 +53,9 @@ func ArrayExternal(te [16]external.Placeholder, fe [16]external.Placeholder) { } // ArrayExternalComplex copies a [16]map[*external.Collection]string to a *complex.ComplexCollection. -func ArrayExternalComplex(tc *complex.ComplexCollection, fm [16]map[*external.Collection]string) { +func ArrayExternalComplex(tC *complex.ComplexCollection, fm [16]map[*external.Collection]string) { // *complex.ComplexCollection fields - tc.Arr = fm + tC.Arr = fm } // ArraySimple copies a [16]byte to a *Collection. @@ -77,18 +77,18 @@ func BasicDoublePointer(tb **bool, fb *bool) { } // BasicExternal copies a *external.Placeholder to a external.Placeholder. -func BasicExternal(tP external.Placeholder, fe *external.Placeholder) { +func BasicExternal(tP external.Placeholder, fP *external.Placeholder) { // external.Placeholder fields - tP = *fe + tP = *fP } // BasicExternalMulti copies a *external.Placeholder to a external.Placeholder, *external.Placeholder. -func BasicExternalMulti(tP external.Placeholder, te *external.Placeholder, fe *external.Placeholder) { +func BasicExternalMulti(tP external.Placeholder, tP1 *external.Placeholder, fP *external.Placeholder) { // external.Placeholder fields - tP = *fe + tP = *fP // *external.Placeholder fields - te = fe + tP1 = fP } // BasicPointer copies a bool to a *bool. @@ -126,9 +126,9 @@ func Chan(tc chan int, fc chan int) { } // ChanComplex copies a chan *[]int to a *complex.Collection. -func ChanComplex(tc *complex.Collection, fc chan *[]int) { +func ChanComplex(tC *complex.Collection, fc chan *[]int) { // *complex.Collection fields - tc.C = fc + tC.C = fc } // ChanExternal copies a chan external.Placeholder to a chan external.Placeholder. @@ -161,9 +161,9 @@ func Func(tf func() int, ff func() int) { } // FuncComplex copies a func([]string, uint64) *byte to a *complex.Collection. -func FuncComplex(tc *complex.Collection, ff func([]string, uint64) *byte) { +func FuncComplex(tC *complex.Collection, ff func([]string, uint64) *byte) { // *complex.Collection fields - tc.F = ff + tC.F = ff } // FuncExternal copies a func(external.Placeholder) int to a func(external.Placeholder) int. @@ -173,9 +173,9 @@ func FuncExternal(tf func(external.Placeholder) int, ff func(external.Placeholde } // FuncExternalComplex copies a func(external.Collection) []string to a *complex.ComplexCollection. -func FuncExternalComplex(tc *complex.ComplexCollection, ff func(external.Collection) []string) { +func FuncExternalComplex(tC *complex.ComplexCollection, ff func(external.Collection) []string) { // *complex.ComplexCollection fields - tc.F = ff + tC.F = ff } // FuncSimple copies a func() int to a *Collection. @@ -191,15 +191,15 @@ func Interface(ti interface{}, fi interface{}) { } // InterfaceComplex copies a interface{func(rune) *int; } to a *complex.Collection. -func InterfaceComplex(tc *complex.Collection, fi interface{ func(rune) *int }) { +func InterfaceComplex(tC *complex.Collection, fi interface{ func(rune) *int }) { // *complex.Collection fields - tc.I = fi + tC.I = fi } // InterfaceExternal copies a error to a *external.Collection. -func InterfaceExternal(te *external.Collection, fe error) { +func InterfaceExternal(tC *external.Collection, fe error) { // *external.Collection fields - te.I = fe + tC.I = fe } // InterfaceExternalComplex copies a interface{func(string) map[*external.Collection]bool; func() (int, byte); } to a complex.ComplexCollection. @@ -224,9 +224,9 @@ func Map(tm map[string]bool, fm map[string]bool) { } // MapComplex copies a map[string]interface{func() string; } to a *complex.Collection. -func MapComplex(tc *complex.Collection, fm map[string]interface{ func() string }) { +func MapComplex(tC *complex.Collection, fm map[string]interface{ func() string }) { // *complex.Collection fields - tc.M = fm + tC.M = fm } // MapExternal copies a map[string]external.Placeholder to a map[string]external.Placeholder. @@ -236,9 +236,9 @@ func MapExternal(tm map[string]external.Placeholder, fm map[string]external.Plac } // MapExternalComplex copies a map[*external.Collection]external.Placeholder to a *complex.ComplexCollection. -func MapExternalComplex(tc *complex.ComplexCollection, fm map[*external.Collection]external.Placeholder) { +func MapExternalComplex(tC *complex.ComplexCollection, fm map[*external.Collection]external.Placeholder) { // *complex.ComplexCollection fields - tc.M = fm + tC.M = fm } // MapSimple copies a map[string]bool to a *Collection. @@ -258,7 +258,7 @@ func NoMatchBasic(tP Placeholder, fP Placeholder) { } // NoMatchBasicExternal copies a *Placeholder to a external.Placeholder, *external.Placeholder, bool. -func NoMatchBasicExternal(tP external.Placeholder, te *external.Placeholder, tb bool, fP *Placeholder) { +func NoMatchBasicExternal(tP external.Placeholder, tP1 *external.Placeholder, tb bool, fP *Placeholder) { // external.Placeholder fields // *external.Placeholder fields @@ -303,9 +303,9 @@ func Slice(ts []string, fs []string) { } // SliceComplex copies a []map[string][16]int to a *complex.Collection. -func SliceComplex(tc *complex.Collection, fm []map[string][16]int) { +func SliceComplex(tC *complex.Collection, fm []map[string][16]int) { // *complex.Collection fields - tc.S = fm + tC.S = fm } // SliceExternal copies a []external.Placeholder to a []external.Placeholder. @@ -315,9 +315,9 @@ func SliceExternal(te []external.Placeholder, fe []external.Placeholder) { } // SliceExternalComplex copies a []map[string]func(*external.Collection) string to a *complex.ComplexCollection. -func SliceExternalComplex(tc *complex.ComplexCollection, fm []map[string]func(*external.Collection) string) { +func SliceExternalComplex(tC *complex.ComplexCollection, fm []map[string]func(*external.Collection) string) { // *complex.ComplexCollection fields - tc.S = fm + tC.S = fm } // SlicePointer copies a []*int to a []*int.