github.com/vmware/govmomi@v0.51.0/vim25/xml/extras.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package xml 6 7 import ( 8 "reflect" 9 "time" 10 ) 11 12 var xmlSchemaInstance = Name{Space: "http://www.w3.org/2001/XMLSchema-instance", Local: "type"} 13 14 var xsiType = Name{Space: "xsi", Local: "type"} 15 16 var stringToTypeMap = map[string]reflect.Type{ 17 "xsd:boolean": reflect.TypeOf((*bool)(nil)).Elem(), 18 "xsd:byte": reflect.TypeOf((*int8)(nil)).Elem(), 19 "xsd:short": reflect.TypeOf((*int16)(nil)).Elem(), 20 "xsd:int": reflect.TypeOf((*int32)(nil)).Elem(), 21 "xsd:long": reflect.TypeOf((*int64)(nil)).Elem(), 22 "xsd:unsignedByte": reflect.TypeOf((*uint8)(nil)).Elem(), 23 "xsd:unsignedShort": reflect.TypeOf((*uint16)(nil)).Elem(), 24 "xsd:unsignedInt": reflect.TypeOf((*uint32)(nil)).Elem(), 25 "xsd:unsignedLong": reflect.TypeOf((*uint64)(nil)).Elem(), 26 "xsd:float": reflect.TypeOf((*float32)(nil)).Elem(), 27 "xsd:double": reflect.TypeOf((*float64)(nil)).Elem(), 28 "xsd:string": reflect.TypeOf((*string)(nil)).Elem(), 29 "xsd:dateTime": reflect.TypeOf((*time.Time)(nil)).Elem(), 30 "xsd:base64Binary": reflect.TypeOf((*[]byte)(nil)).Elem(), 31 } 32 33 // Return a reflect.Type for the specified type. Nil if unknown. 34 func stringToType(s string) reflect.Type { 35 return stringToTypeMap[s] 36 } 37 38 // Return a string for the specified reflect.Type. Panic if unknown. 39 func typeToString(typ reflect.Type) string { 40 switch typ.Kind() { 41 case reflect.Bool: 42 return "xsd:boolean" 43 case reflect.Int8: 44 return "xsd:byte" 45 case reflect.Int16: 46 return "xsd:short" 47 case reflect.Int32: 48 return "xsd:int" 49 case reflect.Int, reflect.Int64: 50 return "xsd:long" 51 case reflect.Uint8: 52 return "xsd:unsignedByte" 53 case reflect.Uint16: 54 return "xsd:unsignedShort" 55 case reflect.Uint32: 56 return "xsd:unsignedInt" 57 case reflect.Uint, reflect.Uint64: 58 return "xsd:unsignedLong" 59 case reflect.Float32: 60 return "xsd:float" 61 case reflect.Float64: 62 return "xsd:double" 63 case reflect.String: 64 name := typ.Name() 65 if name == "string" { 66 return "xsd:string" 67 } 68 return name 69 case reflect.Struct: 70 if typ == stringToTypeMap["xsd:dateTime"] { 71 return "xsd:dateTime" 72 } 73 74 // Expect any other struct to be handled... 75 return typ.Name() 76 case reflect.Slice: 77 if typ.Elem().Kind() == reflect.Uint8 { 78 return "xsd:base64Binary" 79 } 80 case reflect.Array: 81 if typ.Elem().Kind() == reflect.Uint8 { 82 return "xsd:base64Binary" 83 } 84 } 85 86 panic("don't know what to do for type: " + typ.String()) 87 } 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 }