Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Write OptionAnswer value to non interface{} map (#293)
Browse files Browse the repository at this point in the history
* write OptionAnswer to non interface map

* Fix stupid mistake

* add tests
  • Loading branch information
infalmo authored Aug 10, 2020
1 parent 863da4d commit dbdb3e5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
20 changes: 19 additions & 1 deletion core/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,25 @@ func WriteAnswer(t interface{}, name string, v interface{}) (err error) {
return copy(field, value)
case reflect.Map:
mapType := reflect.TypeOf(t).Elem()
if mapType.Key().Kind() != reflect.String || mapType.Elem().Kind() != reflect.Interface {
if mapType.Key().Kind() != reflect.String {
return errors.New("answer maps key must be of type string")
}

// copy only string value/index value to map if,
// map is not of type interface and is 'OptionAnswer'
if value.Type().Name() == "OptionAnswer" {
if kval := mapType.Elem().Kind(); kval == reflect.String {
mt := *t.(*map[string]string)
mt[name] = value.FieldByName("Value").String()
return nil
} else if kval == reflect.Int {
mt := *t.(*map[string]int)
mt[name] = int(value.FieldByName("Index").Int())
return nil
}
}

if mapType.Elem().Kind() != reflect.Interface {
return errors.New("answer maps must be of type map[string]interface")
}
mt := *t.(*map[string]interface{})
Expand Down
33 changes: 27 additions & 6 deletions core/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ func TestWrite_canWriteSlice(t *testing.T) {
assert.Equal(t, []string{"hello", "world"}, ptr)
}

func TestWrite_canWriteMapString(t *testing.T) {
// a map to hold the values
ptr := map[string]string{}

// copy in a value
WriteAnswer(&ptr, "test", OptionAnswer{Value: "hello", Index: 5})

// make sure only string value is written
assert.Equal(t, map[string]string{"test": "hello"}, ptr)
}

func TestWrite_canWriteMapInt(t *testing.T) {
// a map to hold the values
ptr := map[string]int{}

// copy in a value
WriteAnswer(&ptr, "test", OptionAnswer{Value: "hello", Index: 5})

// make sure only string value is written
assert.Equal(t, map[string]int{"test": 5}, ptr)
}

func TestWrite_recoversInvalidReflection(t *testing.T) {
// a variable to mutate
ptr := false
Expand Down Expand Up @@ -108,7 +130,7 @@ func TestWriteAnswer_canMutateStruct(t *testing.T) {
}

func TestWriteAnswer_optionAnswer(t *testing.T) {
t.Run("writes index for ints", func (t *testing.T) {
t.Run("writes index for ints", func(t *testing.T) {
val := 0

// write a value to an existing field
Expand All @@ -128,7 +150,7 @@ func TestWriteAnswer_optionAnswer(t *testing.T) {
}
})

t.Run("writes OptionAnswer for OptionAnswer", func (t *testing.T) {
t.Run("writes OptionAnswer for OptionAnswer", func(t *testing.T) {
val := OptionAnswer{}

// write a value to an existing field
Expand All @@ -148,7 +170,7 @@ func TestWriteAnswer_optionAnswer(t *testing.T) {
}
})

t.Run("writes value for strings", func (t *testing.T) {
t.Run("writes value for strings", func(t *testing.T) {
val := ""

// write a value to an existing field
Expand All @@ -168,7 +190,7 @@ func TestWriteAnswer_optionAnswer(t *testing.T) {
}
})

t.Run("writes slice of indices for slice of ints", func (t *testing.T) {
t.Run("writes slice of indices for slice of ints", func(t *testing.T) {
val := []int{}

// write a value to an existing field
Expand All @@ -190,7 +212,7 @@ func TestWriteAnswer_optionAnswer(t *testing.T) {
}
})

t.Run("writes slice of values for slice of strings", func (t *testing.T) {
t.Run("writes slice of values for slice of strings", func(t *testing.T) {
val := []string{}

// write a value to an existing field
Expand All @@ -206,7 +228,6 @@ func TestWriteAnswer_optionAnswer(t *testing.T) {
return
}


if len(val) != 1 || val[0] != "string value" {
t.Errorf("Wrong value written. Wanted [string value], got %v", val)
return
Expand Down

0 comments on commit dbdb3e5

Please sign in to comment.