github.com/vmware/govmomi@v0.37.2/vim25/xml/extras.go (about)

     1  /*
     2  Copyright (c) 2014 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package xml
    18  
    19  import (
    20  	"reflect"
    21  	"time"
    22  )
    23  
    24  var xmlSchemaInstance = Name{Space: "http://www.w3.org/2001/XMLSchema-instance", Local: "type"}
    25  
    26  var xsiType = Name{Space: "xsi", Local: "type"}
    27  
    28  var stringToTypeMap = map[string]reflect.Type{
    29  	"xsd:boolean":       reflect.TypeOf((*bool)(nil)).Elem(),
    30  	"xsd:byte":          reflect.TypeOf((*int8)(nil)).Elem(),
    31  	"xsd:short":         reflect.TypeOf((*int16)(nil)).Elem(),
    32  	"xsd:int":           reflect.TypeOf((*int32)(nil)).Elem(),
    33  	"xsd:long":          reflect.TypeOf((*int64)(nil)).Elem(),
    34  	"xsd:unsignedByte":  reflect.TypeOf((*uint8)(nil)).Elem(),
    35  	"xsd:unsignedShort": reflect.TypeOf((*uint16)(nil)).Elem(),
    36  	"xsd:unsignedInt":   reflect.TypeOf((*uint32)(nil)).Elem(),
    37  	"xsd:unsignedLong":  reflect.TypeOf((*uint64)(nil)).Elem(),
    38  	"xsd:float":         reflect.TypeOf((*float32)(nil)).Elem(),
    39  	"xsd:double":        reflect.TypeOf((*float64)(nil)).Elem(),
    40  	"xsd:string":        reflect.TypeOf((*string)(nil)).Elem(),
    41  	"xsd:dateTime":      reflect.TypeOf((*time.Time)(nil)).Elem(),
    42  	"xsd:base64Binary":  reflect.TypeOf((*[]byte)(nil)).Elem(),
    43  }
    44  
    45  // Return a reflect.Type for the specified type. Nil if unknown.
    46  func stringToType(s string) reflect.Type {
    47  	return stringToTypeMap[s]
    48  }
    49  
    50  // Return a string for the specified reflect.Type. Panic if unknown.
    51  func typeToString(typ reflect.Type) string {
    52  	switch typ.Kind() {
    53  	case reflect.Bool:
    54  		return "xsd:boolean"
    55  	case reflect.Int8:
    56  		return "xsd:byte"
    57  	case reflect.Int16:
    58  		return "xsd:short"
    59  	case reflect.Int32:
    60  		return "xsd:int"
    61  	case reflect.Int, reflect.Int64:
    62  		return "xsd:long"
    63  	case reflect.Uint8:
    64  		return "xsd:unsignedByte"
    65  	case reflect.Uint16:
    66  		return "xsd:unsignedShort"
    67  	case reflect.Uint32:
    68  		return "xsd:unsignedInt"
    69  	case reflect.Uint, reflect.Uint64:
    70  		return "xsd:unsignedLong"
    71  	case reflect.Float32:
    72  		return "xsd:float"
    73  	case reflect.Float64:
    74  		return "xsd:double"
    75  	case reflect.String:
    76  		name := typ.Name()
    77  		if name == "string" {
    78  			return "xsd:string"
    79  		}
    80  		return name
    81  	case reflect.Struct:
    82  		if typ == stringToTypeMap["xsd:dateTime"] {
    83  			return "xsd:dateTime"
    84  		}
    85  
    86  		// Expect any other struct to be handled...
    87  		return typ.Name()
    88  	case reflect.Slice:
    89  		if typ.Elem().Kind() == reflect.Uint8 {
    90  			return "xsd:base64Binary"
    91  		}
    92  	case reflect.Array:
    93  		if typ.Elem().Kind() == reflect.Uint8 {
    94  			return "xsd:base64Binary"
    95  		}
    96  	}
    97  
    98  	panic("don't know what to do for type: " + typ.String())
    99  }