Skip to content

Commit

Permalink
Fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
c4milo committed Oct 22, 2014
1 parent 7a594b4 commit 4267450
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
11 changes: 7 additions & 4 deletions vim/property_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ type PropertyValue struct {
func (pv *PropertyValue) Value() (interface{}, error) {
vimType := pv.Type

if getTypeInstance, ok := registry[vimType]; ok {
v := getTypeInstance()
if getInstance, ok := registry[vimType]; ok {
v := getInstance()

data := []byte("<val>" + pv.Xml + "</val>")
err := xml.Unmarshal(data, v)
// We need to make sure the unmarshalled value has its vSphere type
// when it is not an actual XML document
valueXml := fmt.Sprintf(`<val type="%s">%s</val>`, pv.Type, pv.Xml)
valueBytes := []byte(valueXml)
err := xml.Unmarshal(valueBytes, v)
if err != nil {
return nil, err
}
Expand Down
58 changes: 58 additions & 0 deletions vim/property_value_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vim

import (
"encoding/xml"
"fmt"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -63,3 +64,60 @@ func TestArrayOfString(t *testing.T) {
assert(t, objs[0] == "Datacenter", fmt.Sprintf("%s != %s\n", objs[0], "Datacenter"))
assert(t, objs[1] == "Alarm", fmt.Sprintf("%s != %s\n", objs[1], "Alarm"))
}

func TestUnmarshalPropertyValue(t *testing.T) {
tests := []struct {
value string
expType string
expValue string
}{
{`<returnval>
<obj type="Datacenter">ha-datacenter</obj>
<propSet>
<name>vmFolder</name>
<val type="Folder" xsi:type="ManagedObjectReference">ha-folder-vm</val>
</propSet>
</returnval>`,
"Folder",
"ha-folder-vm",
},
{`<returnval>
<obj type="Folder">ha-folder-vm</obj>
<propSet>
<name>childEntity</name>
<val xsi:type="ArrayOfManagedObjectReference"><ManagedObjectReference type="VirtualMachine" xsi:type="ManagedObjectReference">1</ManagedObjectReference></val>
</propSet>
</returnval>`,
"ArrayOfManagedObjectReference",
`<ManagedObjectReference type="VirtualMachine" xsi:type="ManagedObjectReference">1</ManagedObjectReference>`,
},
}

for _, tt := range tests {
var obj ObjectContent
err := xml.Unmarshal([]byte(tt.value), &obj)
ok(t, err)

motype := obj.PropSet[0].Val.Type
movalue := obj.PropSet[0].Val.Xml
assert(t, motype == tt.expType, "%s != %s", motype, tt.expType)
assert(t, movalue == tt.expValue, "%s != %s", movalue, tt.expValue)
}
}

func TestFixForIssue14(t *testing.T) {
data := `<returnval>
<obj type="Datacenter">ha-datacenter</obj>
<propSet>
<name>vmFolder</name>
<val type="Folder" xsi:type="ManagedObjectReference">ha-folder-vm</val>
</propSet>
</returnval>`
var obj ObjectContent
err := xml.Unmarshal([]byte(data), &obj)
ok(t, err)
value, err := obj.PropSet[0].Val.Value()
ok(t, err)
folder := value.(*Folder)
assert(t, folder.Type == "Folder", "%s != %s", folder.Type, "Folder")
}

0 comments on commit 4267450

Please sign in to comment.