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