diff --git a/casemapper.go b/casemapper.go index b4472fc..4d5a120 100644 --- a/casemapper.go +++ b/casemapper.go @@ -72,10 +72,16 @@ var fmtToFn = map[string]func(string) string{ // every input string, effectively ending the chain 100% of the time. // Generally, this option should be specified prior to any goschtalt.Keymap // options that handle customization. -func ConfigStoredAs(format string) goschtalt.UnmarshalValueOption { - toCase, found := fmtToFn[format] - if found { - return goschtalt.KeymapFn(toCase) +func ConfigStoredAs(format string) goschtalt.Option { + if toCase, found := fmtToFn[format]; found { + return goschtalt.Options( + goschtalt.DefaultUnmarshalOptions( + goschtalt.KeymapFn(toCase), + ), + goschtalt.DefaultValueOptions( + goschtalt.KeymapFn(toCase), + ), + ) } keys := make([]string, 0, len(fmtToFn)) diff --git a/casemapper_test.go b/casemapper_test.go index c7f2c10..5c0f3b7 100644 --- a/casemapper_test.go +++ b/casemapper_test.go @@ -125,15 +125,8 @@ func TestUnknown(t *testing.T) { assert := assert.New(t) require := require.New(t) - gs, err := goschtalt.New(goschtalt.AutoCompile()) - require.NotNil(gs) - require.NoError(err) - - type Config struct { - Foobar string - } - - _, err = goschtalt.Unmarshal[Config](gs, goschtalt.Root, ConfigStoredAs("Invalid")) - assert.NotNil(err) + gs, err := goschtalt.New(goschtalt.AutoCompile(), ConfigStoredAs("Invalid")) + require.Nil(gs) + require.Error(err) assert.True(strings.Contains(err.Error(), expected)) } diff --git a/example_configstoredas_test.go b/example_configstoredas_test.go index 33cebec..0d46880 100644 --- a/example_configstoredas_test.go +++ b/example_configstoredas_test.go @@ -14,18 +14,30 @@ func ExampleConfigStoredAs() { gs, err := goschtalt.New( goschtalt.AutoCompile(), + casemapper.ConfigStoredAs("two_words"), + // Normally you'll be including data from a file or something like that. // Here we want to use the built in options to avoid including additional // dependencies. goschtalt.AddValue("incoming", "", &struct { - Name string `train:"First-Name"` - Header string `train:"HTTP-Header"` + Name string `goschtalt:"FirstName"` // Rename via tags... + Header string + Sally string }{ Name: "Gopher", Header: "Content-Type: text/plain", + Sally: "Likes go", }, - goschtalt.TagName("train"), + + // or rename via mapping, which is useful if you can't change + // the target structure, but want the configuration to be a + // specific key. + goschtalt.Keymap( + map[string]string{ + "header": "HTTPHeader", // Convert Http back to HTTP + }, + ), ), ) if err != nil { @@ -35,13 +47,14 @@ func ExampleConfigStoredAs() { type Config struct { HTTPHeader string FirstName string + Frog string } cfg, err := goschtalt.Unmarshal[Config](gs, "", - casemapper.ConfigStoredAs("Two-Words"), + // You can also remap when you unmarshal if that's simpler goschtalt.Keymap( map[string]string{ - "HTTPHeader": "HTTP-Header", + "frog": "sally", }, ), ) @@ -51,7 +64,9 @@ func ExampleConfigStoredAs() { fmt.Printf("Config.HTTPHeader: '%s'\n", cfg.HTTPHeader) fmt.Printf("Config.FirstName: '%s'\n", cfg.FirstName) + fmt.Printf("Config.Frog: '%s'\n", cfg.Frog) // Output: // Config.HTTPHeader: 'Content-Type: text/plain' // Config.FirstName: 'Gopher' + // Config.Frog: 'Likes go' } diff --git a/go.mod b/go.mod index b91d0b7..13afcfc 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/goschtalt/casemapper go 1.18 require ( - github.com/goschtalt/goschtalt v0.9.0 - github.com/stretchr/testify v1.8.1 + github.com/goschtalt/goschtalt v0.12.0 + github.com/stretchr/testify v1.8.2 resenje.org/casbab v0.1.1 ) diff --git a/go.sum b/go.sum index 391c6df..904f699 100644 --- a/go.sum +++ b/go.sum @@ -3,14 +3,16 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/goschtalt/goschtalt v0.9.0 h1:JJ3WV0ifs5cJvh8iBwOnwFpepSS+y/vVgeWS1ezW97o= -github.com/goschtalt/goschtalt v0.9.0/go.mod h1:l9PsWB+jhCh4hnjAblqLUGTUSnxogQcgERZs8V9rg0g= +github.com/goschtalt/goschtalt v0.11.0 h1:+eFTad9U4RUXEb0/YHFXju8NL14NacN8NgnGwR1zFL8= +github.com/goschtalt/goschtalt v0.11.0/go.mod h1:mfE8q3rKJDM2HTFFbPP1m5m5Nn15A5Hk33JTZxPR2RM= +github.com/goschtalt/goschtalt v0.12.0 h1:IzkdRgmYK3hgsGncvRaxbuMyQJt6C5DKLmyM2lrUtjw= +github.com/goschtalt/goschtalt v0.12.0/go.mod h1:mfE8q3rKJDM2HTFFbPP1m5m5Nn15A5Hk33JTZxPR2RM= github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= -github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mitchellh/hashstructure v1.1.0 h1:P6P1hdjqAAknpY/M1CGipelZgp+4y9ja9kmUZPXP+H0= github.com/mitchellh/hashstructure v1.1.0/go.mod h1:xUDAozZz0Wmdiufv0uyhnHkUTN6/6d8ulp4AwfLKrmA= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= @@ -23,10 +25,10 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= gonum.org/v1/gonum v0.12.0 h1:xKuo6hzt+gMav00meVPUlXwSdoEJP46BR+wdxQEFK2o= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=