Skip to content

Commit

Permalink
vcsim: override response namespace from struct tag
Browse files Browse the repository at this point in the history
Example: endpoint Namespace is "pbm", struct tag is "internalpbm"
  • Loading branch information
dougm committed Oct 3, 2024
1 parent 40d679d commit 1f0838e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,29 @@ type response struct {
}

func (r *response) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
val := reflect.ValueOf(r.Body).Elem().FieldByName("Res")
body := reflect.ValueOf(r.Body).Elem()
val := body.FieldByName("Res")
if !val.IsValid() {
return fmt.Errorf("%T: invalid response type (missing 'Res' field)", r.Body)
}
if val.IsNil() {
return fmt.Errorf("%T: invalid response (nil 'Res' field)", r.Body)
}

// Default response namespace
ns := "urn:" + r.Namespace
// Override namespace from struct tag if defined
field, _ := body.Type().FieldByName("Res")
if tag := field.Tag.Get("xml"); tag != "" {
tags := strings.Split(tag, " ")
if len(tags) > 0 && strings.HasPrefix(tags[0], "urn") {
ns = tags[0]
}
}

res := xml.StartElement{
Name: xml.Name{
Space: "urn:" + r.Namespace,
Space: ns,
Local: val.Elem().Type().Name(),
},
}
Expand Down

0 comments on commit 1f0838e

Please sign in to comment.