From c291febdd5c7ecb6bc6cd5ddba0cd3c32814b520 Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sun, 17 Dec 2023 01:45:54 -0500 Subject: [PATCH 1/4] Fix struct validation for multiple files --- maker/maker.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/maker/maker.go b/maker/maker.go index bc31ffb..d2fb9e5 100644 --- a/maker/maker.go +++ b/maker/maker.go @@ -341,12 +341,6 @@ 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) @@ -354,6 +348,12 @@ func Make(options MakeOptions) ([]byte, error) { } } } + // 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 { From 9fa297ca694dbd2a4ed2611751cd58cff656eff0 Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sun, 17 Dec 2023 02:46:56 -0500 Subject: [PATCH 2/4] Fix package reference in return for map, slice, pointer --- maker/maker.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/maker/maker.go b/maker/maker.go index d2fb9e5..d81d37e 100644 --- a/maker/maker.go +++ b/maker/maker.go @@ -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 : +// +// * +// [] +// []* +// map[] +// map[]* +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. @@ -136,11 +145,26 @@ func FormatFieldList(src []byte, fl *ast.FieldList, pkgName string, declaredType } t := string(src[l.Type.Pos()-1 : l.Type.End()-1]) + // Try to match . 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 + t = match[0][2] + } + for _, dt := range declaredTypes { if t == 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 `[]` or `*` if there was such a match + t = match[0][1] + dt.Fullname() + } else { + t = dt.Fullname() + } } } From 3c51e71934344315ca562cf3df1c3da1f763e15f Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sun, 17 Dec 2023 02:49:19 -0500 Subject: [PATCH 3/4] Improve comment --- maker/maker.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maker/maker.go b/maker/maker.go index d81d37e..256404c 100644 --- a/maker/maker.go +++ b/maker/maker.go @@ -160,7 +160,8 @@ func FormatFieldList(src []byte, fl *ast.FieldList, pkgName string, declaredType // 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. if match != nil { - // Add back `[]` or `*` if there was such a match + // Add back `*`, `[]`, `[]*`, `map[]` or `map[]*` if there was a + // match. t = match[0][1] + dt.Fullname() } else { t = dt.Fullname() From ea6d692e558287041f35b7b4ef57714685d1f5ee Mon Sep 17 00:00:00 2001 From: Mike Schinkel Date: Sun, 17 Dec 2023 03:44:50 -0500 Subject: [PATCH 4/4] Fix regression for when type is same package --- maker/maker.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maker/maker.go b/maker/maker.go index 256404c..c383703 100644 --- a/maker/maker.go +++ b/maker/maker.go @@ -144,7 +144,7 @@ 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 . If matched variable `match` will look like this for t=="[]Category": // match[0][0] = "[]Category" // match[0][1] = "[]" @@ -152,11 +152,11 @@ func FormatFieldList(src []byte, fl *ast.FieldList, pkgName string, declaredType match := reMatchTypename.FindAllStringSubmatch(t, -1) if match != nil { // Set `t` so it will compare correctly with `dt.Name` below - t = match[0][2] + 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. if match != nil {