Skip to content

Commit

Permalink
feat: allow tag style "original" for additional tags (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
founderio authored Oct 3, 2023
1 parent 00fb9ac commit 3ac44d5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/avrogen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ func parseTags(raw string) (map[string]gen.TagStyle, error) {
style = gen.Kebab
case string(gen.Snake):
style = gen.Snake
case string(gen.Original):
style = gen.Original
default:
return nil, fmt.Errorf("style %q is invalid in %q", parts[1], tag)
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/avrogen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ func TestParseTags(t *testing.T) {
name: "kebab case",
tags: "json:kebab",
},
{
name: "original case",
tags: "json:original",
},
}

for _, test := range tests {
Expand Down
4 changes: 4 additions & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Config struct {
type TagStyle string

const (
// Original is a style like whAtEVer_IS_InthEInpuT.
Original TagStyle = "original"
// Snake is a style like im_written_in_snake_case.
Snake TagStyle = "snake"
// Camel is a style like imWrittenInCamelCase.
Expand Down Expand Up @@ -355,6 +357,8 @@ func formatTag(tag string, style TagStyle) string {
return strcase.ToCamel(tag)
case Snake:
return strcase.ToSnake(tag)
case Original:
fallthrough
default:
return tag
}
Expand Down
7 changes: 4 additions & 3 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestStruct_ConfigurableFieldTags(t *testing.T) {
"type": "record",
"name": "test",
"fields": [
{ "name": "someString", "type": "string" }
{ "name": "someSTRING", "type": "string" }
]
}`

Expand All @@ -110,7 +110,8 @@ func TestStruct_ConfigurableFieldTags(t *testing.T) {
{tagStyle: gen.Snake, expectedTag: "json:\"some_string\""},
{tagStyle: gen.Kebab, expectedTag: "json:\"some-string\""},
{tagStyle: gen.UpperCamel, expectedTag: "json:\"SomeString\""},
{tagStyle: gen.TagStyle(""), expectedTag: "json:\"someString\""},
{tagStyle: gen.Original, expectedTag: "json:\"someSTRING\""},
{tagStyle: gen.TagStyle(""), expectedTag: "json:\"someSTRING\""},
}

for _, test := range tests {
Expand All @@ -127,7 +128,7 @@ func TestStruct_ConfigurableFieldTags(t *testing.T) {
for _, expected := range []string{
"package something",
"type Test struct {",
"SomeString string `avro:\"someString\" " + test.expectedTag + "`",
"SomeString string `avro:\"someSTRING\" " + test.expectedTag + "`",
"}",
} {
assert.Contains(t, lines, expected)
Expand Down

0 comments on commit 3ac44d5

Please sign in to comment.