Skip to content

Commit

Permalink
fix: prefix
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin committed Aug 3, 2022
1 parent 21625fb commit ca829f3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
25 changes: 25 additions & 0 deletions dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@ T.B: foo bar
assert.Equal(t, expected, out.String())
}

func TestDumpStructWithPrefix(t *testing.T) {
type T struct {
A int
B string
C []string
}

a := T{23, "foo bar", []string{"c1", "c2"}}

out := &bytes.Buffer{}
e := dump.NewEncoder(out)
e.Formatters = []dump.KeyFormatterFunc{dump.WithDefaultFormatter()}
e.Prefix = "test"

err := e.Fdump(a)
assert.NoError(t, err)

expected := `test.T.A: 23
test.T.B: foo bar
test.T.C.C0: c1
test.T.C.C1: c2
`
assert.Equal(t, expected, out.String())
}

func TestDumpStructWithOutPrefix(t *testing.T) {
type T struct {
A int
Expand Down
30 changes: 21 additions & 9 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ func (e *Encoder) fdumpInterface(w map[string]interface{}, i interface{}, roots
return nil
}
k := strings.Join(sliceFormat(roots, e.Formatters), e.Separator)
w[k] = ""
var prefix string
if e.Prefix != "" {
prefix = e.Prefix + e.Separator
}
w[prefix+k] = ""
return nil
}
switch f.Kind() {
Expand Down Expand Up @@ -137,7 +141,11 @@ func (e *Encoder) fdumpInterface(w map[string]interface{}, i interface{}, roots
return err
}
} else {
w[k] = f.Interface()
var prefix string
if e.Prefix != "" {
prefix = e.Prefix + e.Separator
}
w[prefix+k] = f.Interface()
}

}
Expand All @@ -161,7 +169,11 @@ func (e *Encoder) fDumpJSON(w map[string]interface{}, i string, roots []string,
}

if value == i {
w[k] = i
var prefix string
if e.Prefix != "" {
prefix = e.Prefix + e.Separator
}
w[prefix+k] = i
return nil
}
if err := e.fdumpInterface(w, value, roots); err != nil {
Expand Down Expand Up @@ -225,7 +237,11 @@ func (e *Encoder) fDumpArray(w map[string]interface{}, i interface{}, roots []st
stringer, ok := f.Interface().(fmt.Stringer)
if ok {
k := strings.Join(sliceFormat(croots, e.Formatters), e.Separator)
w[k] = stringer.String()
var prefix string
if e.Prefix != "" {
prefix = e.Prefix
}
w[prefix+k] = stringer.String()
}

if err := e.fdumpInterface(w, f.Interface(), croots); err != nil {
Expand Down Expand Up @@ -347,12 +363,8 @@ func (e *Encoder) ToStringMap(i interface{}) (res map[string]string, err error)
return
}
res = map[string]string{}
var prefix string
if e.Prefix != "" {
prefix = e.Prefix + e.Separator
}
for k, v := range ires {
res[prefix+k] = printValue(v)
res[k] = printValue(v)
}
return
}
Expand Down
22 changes: 21 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,24 @@ require (
github.com/stretchr/testify v1.6.1 // tests
)

go 1.14
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.3 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect
golang.org/x/text v0.3.2 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v2 v2.2.4 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

go 1.18
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk=
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
Expand Down Expand Up @@ -232,16 +230,15 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 h1:HyfiK1WMnHj5FXFXatD+Qs1A/xC2Run6RzeW1SyHxpc=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/sys v0.0.0-20220731174439-a90be440212d h1:Sv5ogFZatcgIMMtBSTTAgMYsicp25MXBubjXNDKwm80=
golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
Expand Down

0 comments on commit ca829f3

Please sign in to comment.