Skip to content

Commit d1eb5e2

Browse files
committed
chore: vim25/xml: sync with Go 1.24 encoding/xml
Last sync was with Go 1.15, see commit 81207ea Move some of the added code from read{_test}.go to extras{_test}.go to help avoid future merge conflicts. Patched via: % cd ~/go/src/github.com/golang/go % git diff go1.15..go1.24.1 src/encoding/xml > encoding-xml.patch % cd ~/go/src/github.com/vmware/govmomi/vim25/xml % patch -p4 < ~/go/src/github.com/golang/go/encoding-xml.patch Signed-off-by: Doug MacEachern <[email protected]>
1 parent 5833c6c commit d1eb5e2

File tree

9 files changed

+1231
-377
lines changed

9 files changed

+1231
-377
lines changed

vim25/xml/extras.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,34 @@ func typeToString(typ reflect.Type) string {
8585

8686
panic("don't know what to do for type: " + typ.String())
8787
}
88+
89+
// Find reflect.Type for an element's type attribute.
90+
func (p *Decoder) typeForElement(val reflect.Value, start *StartElement) reflect.Type {
91+
t := ""
92+
for _, a := range start.Attr {
93+
if a.Name == xmlSchemaInstance || a.Name == xsiType {
94+
t = a.Value
95+
break
96+
}
97+
}
98+
99+
if t == "" {
100+
// No type attribute; fall back to looking up type by interface name.
101+
t = val.Type().Name()
102+
}
103+
104+
// Maybe the type is a basic xsd:* type.
105+
typ := stringToType(t)
106+
if typ != nil {
107+
return typ
108+
}
109+
110+
// Maybe the type is a custom type.
111+
if p.TypeFunc != nil {
112+
if typ, ok := p.TypeFunc(t); ok {
113+
return typ
114+
}
115+
}
116+
117+
return nil
118+
}

vim25/xml/extras_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,51 @@ func TestUnmarshalInterfaceWithoutTypeAttr(t *testing.T) {
208208
}
209209
}
210210
}
211+
212+
// https://github.com/vmware/govmomi/issues/246
213+
func TestNegativeValuesUnsignedFields(t *testing.T) {
214+
type T struct {
215+
I string
216+
O any
217+
U8 uint8 `xml:"u8"`
218+
U16 uint16 `xml:"u16"`
219+
U32 uint32 `xml:"u32"`
220+
U64 uint64 `xml:"u64"`
221+
}
222+
223+
var tests = []T{
224+
{I: "<T><u8>-128</u8></T>", O: uint8(0x80)},
225+
{I: "<T><u8>-1</u8></T>", O: uint8(0xff)},
226+
{I: "<T><u16>-32768</u16></T>", O: uint16(0x8000)},
227+
{I: "<T><u16>-1</u16></T>", O: uint16(0xffff)},
228+
{I: "<T><u32>-2147483648</u32></T>", O: uint32(0x80000000)},
229+
{I: "<T><u32>-1</u32></T>", O: uint32(0xffffffff)},
230+
{I: "<T><u64>-9223372036854775808</u64></T>", O: uint64(0x8000000000000000)},
231+
{I: "<T><u64>-1</u64></T>", O: uint64(0xffffffffffffffff)},
232+
}
233+
234+
for _, test := range tests {
235+
err := Unmarshal([]byte(test.I), &test)
236+
if err != nil {
237+
t.Errorf("Unmarshal error: %v", err)
238+
continue
239+
}
240+
241+
var expected = test.O
242+
var actual any
243+
switch reflect.ValueOf(test.O).Type().Kind() {
244+
case reflect.Uint8:
245+
actual = test.U8
246+
case reflect.Uint16:
247+
actual = test.U16
248+
case reflect.Uint32:
249+
actual = test.U32
250+
case reflect.Uint64:
251+
actual = test.U64
252+
}
253+
254+
if !reflect.DeepEqual(actual, expected) {
255+
t.Errorf("Actual: %v, expected: %v", actual, expected)
256+
}
257+
}
258+
}

0 commit comments

Comments
 (0)