Skip to content

Commit 18f3d48

Browse files
committed
fix Go struct tag parsing
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.
1 parent 2b88ce8 commit 18f3d48

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ require (
3333

3434
require (
3535
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
36+
github.com/fatih/structtag v1.2.0 // indirect
3637
github.com/golang/protobuf v1.5.3 // indirect
3738
github.com/inconshreveable/mousetrap v1.1.0 // indirect
3839
github.com/jackc/chunkreader/v2 v2.0.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs
2020
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2121
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2222
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
23+
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
24+
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
2325
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
2426
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
2527
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=

internal/config/go_type.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"go/types"
77
"regexp"
88
"strings"
9+
10+
"github.com/fatih/structtag"
911
)
1012

1113
type GoType struct {
@@ -171,16 +173,12 @@ type GoStructTag string
171173
// `a:"b" x:"y,z"` {"a": "b", "x": "y,z"}
172174
func (s GoStructTag) Parse() (map[string]string, error) {
173175
m := make(map[string]string)
174-
fields := strings.Fields(string(s))
175-
for _, f := range fields {
176-
k, v, ok := strings.Cut(f, ":")
177-
if !ok {
178-
return nil, fmt.Errorf("Failed to parse Go struct tag: no colon in field %q", f)
179-
}
180-
if len(v) < 2 || v[0] != '"' || v[len(v)-1] != '"' {
181-
return nil, fmt.Errorf("Failed to parse Go struct tag: missing quotes around value in field %q", f)
182-
}
183-
m[k] = v[1 : len(v)-1] // trim quotes off of v
176+
tags, err := structtag.Parse(string(s))
177+
if err != nil {
178+
return nil, err
179+
}
180+
for _, tag := range tags.Tags() {
181+
m[tag.Key] = tag.Value()
184182
}
185183
return m, nil
186184
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
error parsing sqlc.json: Failed to parse Go struct tag: no colon in field "abc"
1+
error parsing sqlc.json: bad syntax for struct tag pair

internal/endtoend/testdata/overrides_go_struct_tags/sqlite/go/models.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
CREATE TABLE foo (
22
other text NOT NULL,
3-
tagged text NOT NULL
3+
tagged text NOT NULL,
4+
tag3 text NOT NULL
45
);
56

67
CREATE TABLE bar (
78
other text NOT NULL,
8-
also_tagged text NOT NULL
9+
also_tagged text NOT NULL,
10+
tag3 text NOT NULL
911
);
1012

1113
CREATE TABLE baz (
1214
other text NOT NULL,
13-
also_tagged text NOT NULL
15+
also_tagged text NOT NULL,
16+
tag3 text NOT NULL
1417
);

internal/endtoend/testdata/overrides_go_struct_tags/sqlite/sqlc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
{
1616
"go_struct_tag": "also:\"tagged\"",
1717
"column": "*.also_tagged"
18+
},
19+
{
20+
"go_struct_tag": "tag_with_space:\" it's legal!\"",
21+
"column": "*.tag3"
1822
}
1923
]
2024
}

0 commit comments

Comments
 (0)