Skip to content

Commit

Permalink
feat: Allow tag style "original" for additional tags
Browse files Browse the repository at this point in the history
Some edge cases require using the tag style from avro as-is
for other formats.
The tag style `original` explicitly does not do any processing
on the tag before code generation.

This is mostly already supported in the code generation,
but the CLI options would reject
any configuration value which would trigger this behaviour.
  • Loading branch information
founderio committed Oct 2, 2023
1 parent 00fb9ac commit be1e1a1
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

Check failure on line 30 in gen/gen.go

View workflow job for this annotation

GitHub Actions / test (1.20)

Comment should end in a period (godot)
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 be1e1a1

Please sign in to comment.