Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modification to allow attributes in SOAP requests #37

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
}

params := gosoap.Params{
"sIp": "8.8.8.8",
"sIp[type=s:string,anotherAttribute=anotherValue]": "8.8.8.8",
}

res, err := soap.Call("GetIpLocation", params)
Expand Down
42 changes: 33 additions & 9 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/xml"
"fmt"
"reflect"
"regexp"
)

// MarshalXML envelope the body and encode to xml
Expand Down Expand Up @@ -52,19 +53,42 @@ type tokenData struct {
func (tokens *tokenData) recursiveEncode(hm interface{}) {
v := reflect.ValueOf(hm)

regexpAttrBox := regexp.MustCompile(`([^\[]+)\[(.*)\]`)
regexpAttrs := regexp.MustCompile(`,?([^=]+)=([^,]+)`)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the line 56 and 57 to the case case reflect.Map.


switch v.Kind() {
case reflect.Map:
for _, key := range v.MapKeys() {
t := xml.StartElement{
Name: xml.Name{
Space: "",
Local: key.String(),
},
if regexpAttrBox.MatchString(key.String()) {
rs := regexpAttrBox.FindStringSubmatch(key.String())

var attrs []xml.Attr
matches := regexpAttrs.FindAllStringSubmatch(rs[2], -1)
for _, match := range matches {
attrs = append(attrs, xml.Attr{Name: xml.Name{Local: match[1]}, Value: match[2]})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you know the size of matches, please create the variable attrs using that information.

}

t := xml.StartElement{
Name: xml.Name{
Space: "",
Local: rs[1],
},
Attr: attrs,
}
tokens.data = append(tokens.data, t)
tokens.recursiveEncode(v.MapIndex(key).Interface())
tokens.data = append(tokens.data, xml.EndElement{Name: t.Name})
} else {
t := xml.StartElement{
Name: xml.Name{
Space: "",
Local: key.String(),
},
}
tokens.data = append(tokens.data, t)
tokens.recursiveEncode(v.MapIndex(key).Interface())
tokens.data = append(tokens.data, xml.EndElement{Name: t.Name})
}

tokens.data = append(tokens.data, t)
tokens.recursiveEncode(v.MapIndex(key).Interface())
tokens.data = append(tokens.data, xml.EndElement{Name: t.Name})
}
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
Expand Down