Skip to content

Commit

Permalink
fix Go struct tag parsing
Browse files Browse the repository at this point in the history
304ba5d didn't correctly handle struct tags whose
value contained a space. (My fault.)

Use a proper struct tag library with a dedicated parser
that handles all the edge cases. Less code, and better.
  • Loading branch information
josharian committed Aug 11, 2023
1 parent 2b88ce8 commit ae83ce7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (

require (
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
Expand Down
18 changes: 8 additions & 10 deletions internal/config/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"go/types"
"regexp"
"strings"

"github.com/fatih/structtag"
)

type GoType struct {
Expand Down Expand Up @@ -171,16 +173,12 @@ type GoStructTag string
// `a:"b" x:"y,z"` {"a": "b", "x": "y,z"}
func (s GoStructTag) Parse() (map[string]string, error) {
m := make(map[string]string)
fields := strings.Fields(string(s))
for _, f := range fields {
k, v, ok := strings.Cut(f, ":")
if !ok {
return nil, fmt.Errorf("Failed to parse Go struct tag: no colon in field %q", f)
}
if len(v) < 2 || v[0] != '"' || v[len(v)-1] != '"' {
return nil, fmt.Errorf("Failed to parse Go struct tag: missing quotes around value in field %q", f)
}
m[k] = v[1 : len(v)-1] // trim quotes off of v
tags, err := structtag.Parse(string(s))
if err != nil {
return nil, err
}
for _, tag := range tags.Tags() {
m[tag.Key] = tag.Value()
}
return m, nil
}

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

Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
CREATE TABLE foo (
other text NOT NULL,
tagged text NOT NULL
tagged text NOT NULL,
tag3 text NOT NULL
);

CREATE TABLE bar (
other text NOT NULL,
also_tagged text NOT NULL
also_tagged text NOT NULL,
tag3 text NOT NULL
);

CREATE TABLE baz (
other text NOT NULL,
also_tagged text NOT NULL
also_tagged text NOT NULL,
tag3 text NOT NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
{
"go_struct_tag": "also:\"tagged\"",
"column": "*.also_tagged"
},
{
"go_struct_tag": "tag_with_space:\" it's legal!\"",
"column": "*.tag3"
}
]
}
Expand Down

0 comments on commit ae83ce7

Please sign in to comment.