Skip to content

Commit

Permalink
fix:fix issue leebenson#35
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnickey committed Nov 25, 2022
1 parent 7fbbd1b commit f643591
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
8 changes: 5 additions & 3 deletions conform.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ func getSliceElemType(t reflect.Type) reflect.Type {
elType = t.Elem()
}

if elType.Kind() == reflect.Ptr {
elType = elType.Elem()
}

return elType
}

Expand Down Expand Up @@ -239,9 +243,7 @@ func Strings(iface interface{}) error {
elType := getSliceElemType(v.Type)

// allow strings and string pointers
str := ""
if (elType.ConvertibleTo(reflect.TypeOf(str)) && reflect.TypeOf(str).ConvertibleTo(elType)) ||
(elType.ConvertibleTo(reflect.TypeOf(&str)) && reflect.TypeOf(&str).ConvertibleTo(elType) ) {
if elType.Kind() == reflect.String {
tags := v.Tag.Get("conform")
for i := 0; i < el.Len(); i++ {
el.Index(i).Set(transformValue(tags, el.Index(i)))
Expand Down
24 changes: 18 additions & 6 deletions conform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,9 @@ func (t *testSuite) TestNilArrayPointerType() {
assert := assert.New(t.T())

type Post struct {
HashTags *[]string `conform:"trim"`
}
p := Post{
HashTags *[]string `conform:"trim"`
}
p := Post{}

Strings(&p)
assert.Nil(p.HashTags, 0)
Expand All @@ -640,7 +639,7 @@ func (t *testSuite) TestStringPointerArrayType() {
assert := assert.New(t.T())

type Post struct {
HashTags []*string `conform:"trim"`
HashTags []*string `conform:"trim"`
}
h := " hashtag "
p := Post{
Expand Down Expand Up @@ -691,7 +690,7 @@ func (t *testSuite) TestCustomStringPointerArrayType() {

type String string
type Post struct {
HashTags []*String `conform:"trim"`
HashTags []*String `conform:"trim"`
}
h := String(" hashtag ")
p := Post{
Expand Down Expand Up @@ -772,10 +771,23 @@ func (t *testSuite) TestEmbeddedArrayOfStructsWithIntSlice() {

f := Foo{
Bars: &[]*Bar{
{Baz: " baz ", Bak: []int64{1, 2, 3},},
{Baz: " baz ", Bak: []int64{1, 2, 3}},
},
}

Strings(&f)
assert.Equal("baz", (*f.Bars)[0].Baz)
}

func (t *testSuite) TestStructsWithIntSlice() {

type Bar struct {
Bak []int64
}

f := Bar{
[]int64{1, 2, 3},
}

Strings(&f)
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down

0 comments on commit f643591

Please sign in to comment.