Skip to content

Commit

Permalink
Merge pull request #85 from alexejk/array-mixed-with-structs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexejk authored Mar 25, 2024
2 parents 96e03e7 + 1f615e0 commit dd87aa3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.5.2

Bugfixes:
* Decoding a `<struct>` that is nested in `<array>` with variable types (e.g mix of ints, bool, string and structs) (#84).
Structs will be decoded into a `map[string]any` type, as it's not possible to decode.

## 0.5.1

Bugfixes:
Expand Down
15 changes: 13 additions & 2 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,24 @@ func (d *StdDecoder) decodeValue(value *ResponseValue, field reflect.Value) erro
// Struct decoding
case len(value.Struct) != 0:
fieldKind := field.Kind()
fieldType := field.Type()

if fieldKind == reflect.Interface {
// Create a new map[string]any and assign it to field
fieldType = reflect.TypeOf(map[string]any{})
fieldKind = reflect.Map

field.Set(reflect.MakeMap(fieldType))
field = field.Elem() // As the field is an interface, need to unwrap it
}

if fieldKind != reflect.Struct && fieldKind != reflect.Map {
return fmt.Errorf(errFormatInvalidFieldTypeOrType, reflect.Struct.String(), reflect.Map.String(), fieldKind.String())
}

// If we are targeting a map, it should be initialized
if fieldKind == reflect.Map {
if kt := field.Type().Key().Kind(); kt != reflect.String {
if kt := fieldType.Key().Kind(); kt != reflect.String {
return fmt.Errorf(errFormatInvalidMapKeyTypeForStruct, kt.String())
}

Expand All @@ -160,7 +171,7 @@ func (d *StdDecoder) decodeValue(value *ResponseValue, field reflect.Value) erro
for _, m := range value.Struct {
if fieldKind == reflect.Map {
mapKey := reflect.ValueOf(m.Name)
f := reflect.New(field.Type().Elem()).Elem()
f := reflect.New(fieldType.Elem()).Elem()

if err := d.decodeValue(&m.Value, f); err != nil {
return fmt.Errorf("failed decoding struct member '%s': %w", m.Name, err)
Expand Down
14 changes: 14 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,20 @@ func Test_structMemberToFieldName(t *testing.T) {
}
}

func Test_github(t *testing.T) {
dec := &StdDecoder{}
decodeTarget := struct {
Array []any
}{}

err := dec.DecodeRaw(loadTestFile(t, "response_array_mixed_with_struct.xml"), &decodeTarget)
require.NoError(t, err)
require.Equal(t, 3, len(decodeTarget.Array))
require.Equal(t, 200, decodeTarget.Array[0])
require.Equal(t, "OK", decodeTarget.Array[1])
require.Equal(t, "OK", decodeTarget.Array[2].(map[string]any)["status"])
}

func loadTestFile(t *testing.T, name string) []byte {
path := filepath.Join("testdata", name) // relative path

Expand Down
29 changes: 29 additions & 0 deletions testdata/response_array_mixed_with_struct.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<array>
<data>
<value>
<i4>200</i4>
</value>
<value>OK</value>
<value>
<struct>
<member>
<name>status</name>
<value>OK</value>
</member>
<member>
<name>contact</name>
<value>&lt;sip:[email protected]:5060&gt;;expires=60</value>
</member>
</struct>
</value>
</data>
</array>
</value>
</param>
</params>
</methodResponse>

0 comments on commit dd87aa3

Please sign in to comment.