diff --git a/config/config_test.go b/config/config_test.go index 4b6faf2..f0c3607 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -3,6 +3,9 @@ package config_test import ( "errors" "fmt" + "os/exec" + "runtime" + "strings" "testing" "github.com/ory/viper" @@ -36,6 +39,14 @@ func newConfig( return config } +func isRepoDirty() bool { + if out, err := exec.Command("git", "status", + "--porcelain").Output(); err == nil { + return strings.TrimSpace(string(out)) != "" + } + return false +} + type ConfigParams struct { setup mock.SetupFunc reader func(test.Test) *config.Reader[config.Config] @@ -122,7 +133,13 @@ var configTestCases = map[string]ConfigParams{ }{}, false) }) }, - expect: newConfig("prod", "info", nil), + expect: newConfig("prod", "info", func(info *info.Info) { + info.Path = "github.com/tkrop/go-config" + info.Go = runtime.Version()[2:] + info.Platform = runtime.GOOS + "/" + runtime.GOARCH + info.Compiler = runtime.Compiler + info.Dirty = isRepoDirty() + }), }, "panic on default config with invalid tag": { diff --git a/info/info.go b/info/info.go index cebb2b7..77f9120 100644 --- a/info/info.go +++ b/info/info.go @@ -105,7 +105,7 @@ func New( Dirty: DirtyParse(dirty), Go: runtime.Version()[2:], Compiler: runtime.Compiler, - Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + Platform: runtime.GOOS + "/" + runtime.GOARCH, }).UseDebug(debug.ReadBuildInfo()).AdjustVersion() } diff --git a/internal/reflect/walker.go b/internal/reflect/walker.go index 3cd8a77..f0a3ab3 100644 --- a/internal/reflect/walker.go +++ b/internal/reflect/walker.go @@ -129,8 +129,9 @@ func (w *TagWalker) walkField( case reflect.Slice, reflect.Array, reflect.Map: if value.Len() != 0 { w.walk(path, value) + } else if field.Tag.Get(w.dtag) != "" { + w.callField(path, field) } - w.callField(path, field) default: if value.IsValid() && !value.IsZero() { w.call(path, value.Interface()) @@ -145,20 +146,18 @@ func (w *TagWalker) walkField( // For complex numbers, which YAML doesn't support natively, we first parse the // value as string/[]string, then convert to the actual complex type. func (w *TagWalker) callField(path string, field reflect.StructField) { - if value := field.Tag.Get(w.dtag); value != "" { - fieldType := field.Type - parseType := parseType(fieldType) - ptr := reflect.New(parseType) - - if err := yaml.Unmarshal([]byte(value), ptr.Interface()); err != nil { - w.errors = append(w.errors, - NewErrTagWalker("yaml parsing", path, value, err)) - w.call(path, value) - } else if parseType != fieldType { - w.callComplex(path, ptr.Elem().Interface(), fieldType) - } else { - w.call(path, ptr.Elem().Interface()) - } + fieldType := field.Type + parseType := parseType(fieldType) + ptr := reflect.New(parseType) + value := field.Tag.Get(w.dtag) + if err := yaml.Unmarshal([]byte(value), ptr.Interface()); err != nil { + w.errors = append(w.errors, + NewErrTagWalker("yaml parsing", path, value, err)) + w.call(path, value) + } else if parseType != fieldType { + w.callComplex(path, ptr.Elem().Interface(), fieldType) + } else { + w.call(path, ptr.Elem().Interface()) } } diff --git a/internal/reflect/walker_test.go b/internal/reflect/walker_test.go index b1fe571..8217e1b 100644 --- a/internal/reflect/walker_test.go +++ b/internal/reflect/walker_test.go @@ -621,6 +621,30 @@ var tagWalkerTestCases = map[string]TagWalkerParams{ }, // Test with special tags. + "tag-yaml-no-tags": { + value: &struct { + S string + }{}, + }, + "tag-yaml-zero-no-tags": { + value: &struct { + S string + }{}, + zero: true, + expect: Call("s", ""), + }, + "tag-yaml-empty": { + value: &struct { + S string `tag:""` + }{}, + }, + "tag-yaml-zero-empty": { + value: &struct { + S string `tag:""` + }{}, + zero: true, + expect: Call("s", ""), + }, "tag-map-squash": { value: struct { S struct { @@ -721,10 +745,9 @@ var tagWalkerTestCases = map[string]TagWalkerParams{ }, } -// TestTagWalker tests TagWalker.Walk. func TestTagWalker(t *testing.T) { test.Map(t, tagWalkerTestCases). - // Filter(test.Pattern[TagWalkerParams]("struct-string-tags")). + // Filter(test.Pattern[TagWalkerParams]("^tag-yaml-zero")). Run(func(t test.Test, param TagWalkerParams) { // Given mocks := mock.NewMocks(t).Expect(param.expect)