Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package config_test
import (
"errors"
"fmt"
"os/exec"
"runtime"
"strings"
"testing"

"github.com/ory/viper"
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
29 changes: 14 additions & 15 deletions internal/reflect/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
}
}

Expand Down
27 changes: 25 additions & 2 deletions internal/reflect/walker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Loading