github.com/vmware/govmomi@v0.51.0/vim25/types/registry.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 types
     6  
     7  import (
     8  	"reflect"
     9  	"strings"
    10  )
    11  
    12  var (
    13  	t = map[string]reflect.Type{}
    14  
    15  	// minAPIVersionForType is used to lookup the minimum API version for which
    16  	// a type is valid.
    17  	minAPIVersionForType = map[string]string{}
    18  
    19  	// minAPIVersionForEnumValue is used to lookup the minimum API version for
    20  	// which an enum value is valid.
    21  	minAPIVersionForEnumValue = map[string]map[string]string{}
    22  )
    23  
    24  func Add(name string, kind reflect.Type) {
    25  	t[name] = kind
    26  }
    27  
    28  func AddMinAPIVersionForType(name, minAPIVersion string) {
    29  	minAPIVersionForType[name] = minAPIVersion
    30  }
    31  
    32  func AddMinAPIVersionForEnumValue(enumName, enumValue, minAPIVersion string) {
    33  	if v, ok := minAPIVersionForEnumValue[enumName]; ok {
    34  		v[enumValue] = minAPIVersion
    35  	} else {
    36  		minAPIVersionForEnumValue[enumName] = map[string]string{
    37  			enumValue: minAPIVersion,
    38  		}
    39  	}
    40  }
    41  
    42  type Func func(string) (reflect.Type, bool)
    43  
    44  func TypeFunc() Func {
    45  	return func(name string) (reflect.Type, bool) {
    46  		typ, ok := t[name]
    47  		if !ok {
    48  			// The /sdk endpoint does not prefix types with the namespace,
    49  			// but extension endpoints, such as /pbm/sdk do.
    50  			name = strings.TrimPrefix(name, "vim25:")
    51  			typ, ok = t[name]
    52  		}
    53  		return typ, ok
    54  	}
    55  }