From 9a66ff19a439c20a617e83b46cc043a3c235c414 Mon Sep 17 00:00:00 2001 From: Ali Riza Keles Date: Fri, 7 Jan 2022 09:47:49 +0000 Subject: [PATCH 1/3] Fix struct encoding --- encode.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/encode.go b/encode.go index 8487ea0..9e29b98 100644 --- a/encode.go +++ b/encode.go @@ -108,7 +108,18 @@ func (tokens *tokenData) recursiveEncode(hm interface{}) { content := xml.CharData(v.String()) tokens.data = append(tokens.data, content) case reflect.Struct: - tokens.data = append(tokens.data, v.Interface()) + for i := 0; i < v.NumField(); i++ { + field := v.Field(i) + t := xml.StartElement{ + Name: xml.Name{ + Space: "", + Local: v.Type().Field(i).Tag.Get("xml"), + }, + } + tokens.data = append(tokens.data, t) + tokens.recursiveEncode(field.Interface()) + tokens.data = append(tokens.data, xml.EndElement{Name: t.Name}) + } } } From cc77e1cdff637138b1ffb3abe2edbf6252427141 Mon Sep 17 00:00:00 2001 From: Ali Riza Keles Date: Wed, 12 Jan 2022 07:06:29 +0000 Subject: [PATCH 2/3] Fix call by struct test --- soap_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/soap_test.go b/soap_test.go index 90481b2..7114f49 100644 --- a/soap_test.go +++ b/soap_test.go @@ -63,15 +63,12 @@ func TestSoapClienWithClient(t *testing.T) { } type CheckVatRequest struct { - CountryCode string - VatNumber string + CountryCode string `xml:"countryCode"` + VatNumber string `xml:"vatNumber"` } func (r CheckVatRequest) SoapBuildRequest() *Request { - return NewRequest("checkVat", Params{ - "countryCode": r.CountryCode, - "vatNumber": r.VatNumber, - }) + return NewRequest("checkVat", r) } type CheckVatResponse struct { From dc88fa9f2a9272be270c99870952e706474affce Mon Sep 17 00:00:00 2001 From: Ali Riza Keles Date: Wed, 12 Jan 2022 07:40:32 +0000 Subject: [PATCH 3/3] Use either tag or fieldname for element name while encoding structs --- encode.go | 9 ++++++++- soap_test.go | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/encode.go b/encode.go index 9e29b98..fa2c551 100644 --- a/encode.go +++ b/encode.go @@ -4,6 +4,7 @@ import ( "encoding/xml" "fmt" "reflect" + "strings" ) var ( @@ -110,10 +111,16 @@ func (tokens *tokenData) recursiveEncode(hm interface{}) { case reflect.Struct: for i := 0; i < v.NumField(); i++ { field := v.Field(i) + var name string + name = v.Type().Field(i).Tag.Get("xml") + if name == "" { + name = v.Type().Field(i).Name + name = strings.ToLower(name[0:1]) + name[1:] + } t := xml.StartElement{ Name: xml.Name{ Space: "", - Local: v.Type().Field(i).Tag.Get("xml"), + Local: name, }, } tokens.data = append(tokens.data, t) diff --git a/soap_test.go b/soap_test.go index 7114f49..7c9c570 100644 --- a/soap_test.go +++ b/soap_test.go @@ -63,8 +63,8 @@ func TestSoapClienWithClient(t *testing.T) { } type CheckVatRequest struct { - CountryCode string `xml:"countryCode"` - VatNumber string `xml:"vatNumber"` + CountryCode string + VatNumber string } func (r CheckVatRequest) SoapBuildRequest() *Request {