From cc75a26e1374059d53d23a9068eea2d10bf3674d Mon Sep 17 00:00:00 2001 From: John Beisley Date: Thu, 31 Mar 2022 19:54:33 +0100 Subject: [PATCH] Test and fix edge cases in map args. --- v2alpha/soap/envelope/envelope.go | 5 +++-- v2alpha/soap/envelope/envelope_test.go | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/v2alpha/soap/envelope/envelope.go b/v2alpha/soap/envelope/envelope.go index ca8858f..00dad3e 100644 --- a/v2alpha/soap/envelope/envelope.go +++ b/v2alpha/soap/envelope/envelope.go @@ -60,6 +60,8 @@ func NewSendAction(serviceType, actionName string, args any) *Action { } } +var stringType = reflect.TypeOf("") + var _ xml.Marshaler = &Action{} // MarshalXML implements `xml.Marshaller`. @@ -94,8 +96,7 @@ func (a *Action) MarshalXML(e *xml.Encoder, start xml.StartElement) error { iter := v.MapRange() for iter.Next() { k := iter.Key() - // TODO: does this support string newtypes? convert? - ks := k.Interface().(string) + ks := k.Convert(stringType).Interface().(string) v := iter.Value() ke := xml.StartElement{Name: xml.Name{Local: ks}} if err := e.EncodeElement(v.Interface(), ke); err != nil { diff --git a/v2alpha/soap/envelope/envelope_test.go b/v2alpha/soap/envelope/envelope_test.go index eef6887..5a9b120 100644 --- a/v2alpha/soap/envelope/envelope_test.go +++ b/v2alpha/soap/envelope/envelope_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/huin/goupnp/v2alpha/soap/types" ) type testStructArgs struct { @@ -17,6 +18,8 @@ type testStructArgs struct { Bar string } +type newString string + // TestWriteRead tests the round-trip of writing an envelope and reading it back. func TestWriteRead(t *testing.T) { tests := []struct { @@ -33,13 +36,29 @@ func TestWriteRead(t *testing.T) { &testStructArgs{}, }, { - "map", + "mapString", map[string]string{ "Foo": "foo-1", "Bar": "bar-2", }, map[string]string{}, }, + { + "mapUI2", + map[string]types.UI2{ + "Foo": 1, + "Bar": 2, + }, + map[string]types.UI2{}, + }, + { + "mapNewStringKey", + map[newString]string{ + "Foo": "foo-1", + "Bar": "bar-2", + }, + map[newString]string{}, + }, } for _, test := range tests { @@ -52,7 +71,6 @@ func TestWriteRead(t *testing.T) { if err != nil { t.Fatalf("Write want success, got err=%v", err) } - t.Logf("Encoded envelope:\n%v", buf) actionOut := NewRecvAction(test.argsOut) @@ -67,6 +85,9 @@ func TestWriteRead(t *testing.T) { if diff := cmp.Diff(test.argsIn, test.argsOut); diff != "" { t.Errorf("\nwant argsOut=%+v\ngot %+v\ndiff:\n%s", test.argsIn, test.argsOut, diff) } + if t.Failed() { + t.Logf("Encoded envelope:\n%v", buf) + } }) } }