Skip to content

Commit

Permalink
Fix omitempty in tags (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Sep 6, 2021
1 parent de23397 commit 0d8016d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
18 changes: 18 additions & 0 deletions aconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ func TestJSON(t *testing.T) {
}
}

func TestJSONWithOmitempty(t *testing.T) {
type TestConfig struct {
APIKey string `json:"b,omitempty"`
}

var cfg TestConfig
loader := LoaderFor(&cfg, Config{
SkipDefaults: true,
SkipEnv: true,
SkipFlags: true,
AllowUnknownFields: true,
Files: []string{createTestFile(t)},
})
if err := loader.Load(); err != nil {
t.Fatal(err)
}
}

func TestCustomFile(t *testing.T) {
filepath := createTestFile(t, "custom.config")

Expand Down
3 changes: 3 additions & 0 deletions reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func (l *Loader) fullTag(prefix string, f *fieldData, tag string) string {
if before, _, ok := cut(res, ",exact"); ok {
return before
}
if before, _, ok := cut(res, ",omitempty"); ok {
return before
}
for p := f.parent; p != nil; p = p.parent {
if p.Tag(tag) != "-" {
res = p.Tag(tag) + sep + res
Expand Down

0 comments on commit 0d8016d

Please sign in to comment.