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 Go struct tag parsing #2606

Merged
merged 1 commit into from
Aug 28, 2023
Merged
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
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
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error parsing sqlc.json: Failed to parse Go struct tag: no colon in field "abc"
error parsing sqlc.json: bad syntax for struct tag pair

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
Loading