Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix result generation for map, slice, pointer when targeting other package #66

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 33 additions & 8 deletions maker/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ func GetReceiverType(fd *ast.FuncDecl) (ast.Expr, error) {
return fd.Recv.List[0].Type, nil
}

// reMatchTypename matches any of the following to extract the <type>:
//
// *<type>
// []<type>
// []*<type>
// map[<keyType>]<type>
// map[<keyType>]*<type>
var reMatchTypename = regexp.MustCompile(`^(\[\]|\*|\[\]\*|map\[\w+\]|map\[\w+\]\*)(\w+)$`)

// FormatFieldList takes in the source code
// as a []byte and a FuncDecl parameters or
// return values as a FieldList.
Expand All @@ -135,12 +144,28 @@ func FormatFieldList(src []byte, fl *ast.FieldList, pkgName string, declaredType
names[i] = n.Name
}
t := string(src[l.Type.Pos()-1 : l.Type.End()-1])
t2 := t
// Try to match <modifier><type>. If matched variable `match` will look like this for t=="[]Category":
// match[0][0] = "[]Category"
// match[0][1] = "[]"
// match[0][2] = "Category"
match := reMatchTypename.FindAllStringSubmatch(t, -1)
if match != nil {
// Set `t` so it will compare correctly with `dt.Name` below
t2 = match[0][2]
}

for _, dt := range declaredTypes {
if t == dt.Name && pkgName != dt.Package {
if t2 == dt.Name && pkgName != dt.Package {
// The type of this field is the same as one declared in the source package,
// and the source package is not the same as the destination package.
t = dt.Fullname()
if match != nil {
// Add back `*`, `[]`, `[]*`, `map[<type>]` or `map[<type>]*` if there was a
// match.
t = match[0][1] + dt.Fullname()
} else {
t = dt.Fullname()
}
}
}

Expand Down Expand Up @@ -341,19 +366,19 @@ func Make(options MakeOptions) ([]byte, error) {
return []byte{}, err
}
types := ParseDeclaredTypes(b)
// validate structs from file against input struct Type
if !validateStructType(types, options.StructType) {
return []byte{},
fmt.Errorf("%q structtype not found in input files",
options.StructType)
}
for _, t := range types {
if _, ok := tset[t.Fullname()]; !ok {
allDeclaredTypes = append(allDeclaredTypes, t)
tset[t.Fullname()] = struct{}{}
}
}
}
// validate structs from file against input struct Type
if !validateStructType(allDeclaredTypes, options.StructType) {
return []byte{},
fmt.Errorf("%q structtype not found in input files",
options.StructType)
}

excludedMethods := make(map[string]struct{}, len(options.ExcludeMethods))
for _, mName := range options.ExcludeMethods {
Expand Down