Skip to content

Commit

Permalink
Support slices of pointers (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoRuDar authored Jun 28, 2022
1 parent 22234e6 commit 28f6100
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func TestConfigurator(t *testing.T) {
// setting env variable
t.Setenv("AGE_ENV", "45")

expectedURLs := []string{
"http://localhost:3000",
"1.2.3.4:8080",
}

// defining a struct
cfg := struct {
Name string `flag:"name"`
Expand All @@ -34,6 +39,7 @@ func TestConfigurator(t *testing.T) {
IntSlice []int64 `default:"3; 4"`
unexported string `xml:"ignored"`
}
URLs []*string `default:"http://localhost:3000;1.2.3.4:8080"`
}{}

configurator := New(
Expand Down Expand Up @@ -66,6 +72,10 @@ func TestConfigurator(t *testing.T) {
assert(t, []string{"one", "two"}, cfg.Obj.StrSlice)
assert(t, []int64{3, 4}, cfg.Obj.IntSlice)
assert(t, time.Millisecond*100, cfg.ObjPtr.HundredMS)

for i := range expectedURLs {
assert(t, expectedURLs[i], *cfg.URLs[i])
}
}

func TestConfigurator_Errors(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions fieldSetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ func setSlice(t reflect.Type, v reflect.Value, val string) error {
slice.Index(i).SetBool(val)
}

case reflect.Pointer:
slice = reflect.MakeSlice(t, size, size)
for i := 0; i < size; i++ {
err := setPtrValue(slice.Index(i).Type(), slice.Index(i), items[i])
if err != nil {
return fmt.Errorf("setSlice: cannot set type [%s] at index [%d]", slice.Index(i).Type(), i)
}
}

default:
return fmt.Errorf("setSlice: unsupported type of slice item: %v", t.Elem().Kind().String())
}
Expand Down
34 changes: 34 additions & 0 deletions fieldStetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,37 @@ func TestSetValue_Unsupported(t *testing.T) {
err = setSlice(fieldType, fieldVal, testValue)
assert(t, "setSlice: unsupported type of slice item: struct", err.Error())
}

func TestSetValue_IntPtrSlice(t *testing.T) {
var testStr []*int
fieldType := reflect.TypeOf(&testStr).Elem()
fieldVal := reflect.ValueOf(&testStr).Elem()
testValue := "1;2;3"
ints := []int{1, 2, 3}
expected := []*int{&ints[0], &ints[1], &ints[2]}

err := setValue(fieldType, fieldVal, testValue)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(expected, fieldVal.Interface()) {
t.Fatalf("\nexpected result: %+v \nbut got: %+v", expected, fieldVal.Interface())
}
}

func TestSetValue_IntPtrSlice_Err(t *testing.T) {
var testStr []*struct{}
fieldType := reflect.TypeOf(&testStr).Elem()
fieldVal := reflect.ValueOf(&testStr).Elem()
testValue := "1;2;4"

err := setValue(fieldType, fieldVal, testValue)
if err == nil {
t.Fatal("expected err but got nil")
}

if err.Error() != "setSlice: cannot set type [*struct {}] at index [0]" {
t.Fatalf("wrong error: %v", err)
}
}

0 comments on commit 28f6100

Please sign in to comment.