Skip to content

Commit

Permalink
Merge pull request #76 from alexejk/omitempty
Browse files Browse the repository at this point in the history
  • Loading branch information
alexejk authored Dec 6, 2023
2 parents 317a94e + 69e9c7f commit 2830010
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 4 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## 0.x.y
## 0.4.1

Bugfixes:

* Adding missing handling of undeclared value types to default to `string` as per XML-RPC spec (previously `nil` would be returned)

Library is now built against Go 1.21

## 0.4.0

Expand Down
3 changes: 2 additions & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ func (d *StdDecoder) decodeValue(value *ResponseValue, field reflect.Value) erro
}

default:
// NADA
// Default to inner string (raw XML)
val, err = value.RawXML, nil
}

if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions decode_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type ResponseValue struct {
Boolean string `xml:"boolean"`
DateTime string `xml:"dateTime.iso8601"`
Base64 string `xml:"base64"`

RawXML string `xml:",innerxml"`
}

// ResponseStructMember contains name-value pair of the struct
Expand Down
40 changes: 40 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,46 @@ func TestStdDecoder_DecodeRaw_Fault(t *testing.T) {
}, fT)
}

func TestStdDecoder_DecodeRaw_Arrays(t *testing.T) {
type TestStruct struct {
Array []any
}

tests := map[string]struct {
testFile string
expect *TestStruct
err error
}{
"Basic mixed array": {
testFile: "response_array_mixed.xml",
expect: &TestStruct{
Array: []any{10, "s11", true},
},
},
"Basic mixed array - missing type declarations": {
testFile: "response_array_mixed_missing_types.xml",
expect: &TestStruct{
Array: []any{0, "4099", "O3D217AC", "<c><b>123</b></c>"},
},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
dec := &StdDecoder{}
decodeTarget := &TestStruct{}
err := dec.DecodeRaw(loadTestFile(t, tt.testFile), decodeTarget)
if tt.err == nil {
require.NoError(t, err)
require.EqualValues(t, tt.expect, decodeTarget)
} else {
require.Error(t, err)
require.Equal(t, tt.err, err)
}
})
}
}

func Test_fieldsMustEqual(t *testing.T) {
tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module alexejk.io/go-xmlrpc

go 1.19
go 1.21

require github.com/stretchr/testify v1.8.4

Expand Down
2 changes: 1 addition & 1 deletion hack/linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GREEN="\033[32m"
YELLOW="\033[33m"
NORMAL="\033[39m"

LINTER_VERSION=1.53.3
LINTER_VERSION=1.55.2

LINTER_BINDIR=$(go env GOPATH)/bin
LINTER_NAME=golangci-lint
Expand Down
17 changes: 17 additions & 0 deletions testdata/response_array_mixed_missing_types.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<array>
<data>
<value><i4>0</i4></value>
<value>4099</value>
<value>O3D217AC</value>
<value><c><b>123</b></c></value>
</data>
</array>
</value>
</param>
</params>
</methodResponse>

0 comments on commit 2830010

Please sign in to comment.