istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/config/map.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package config
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"istio.io/istio/pkg/test/scopes"
    21  )
    22  
    23  type Map map[string]any
    24  
    25  func (m Map) Map(key string) Map {
    26  	nested, ok := m[key]
    27  	if !ok {
    28  		return nil
    29  	}
    30  	out, ok := nested.(Map)
    31  	if !ok {
    32  		return nil
    33  	}
    34  	return out
    35  }
    36  
    37  func (m Map) String(key string) string {
    38  	v, ok := m[key]
    39  	if !ok || v == nil {
    40  		return ""
    41  	}
    42  	str, ok := v.(string)
    43  	if !ok {
    44  		return fmt.Sprint(m[key])
    45  	}
    46  	return str
    47  }
    48  
    49  func (m Map) Slice(key string) []Map {
    50  	v, ok := m[key].([]any)
    51  	if !ok {
    52  		return nil
    53  	}
    54  	var out []Map
    55  	for i, imeta := range v {
    56  		meta, ok := toMap(imeta)
    57  		if !ok {
    58  			scopes.Framework.Warnf("failed to parse item %d of %s, defaulting to empty: %v", i, key, imeta)
    59  			return nil
    60  		}
    61  		out = append(out, meta)
    62  	}
    63  	return out
    64  }
    65  
    66  func toMap(orig any) (Map, bool) {
    67  	// keys are strings, easily cast
    68  	if cfgMeta, ok := orig.(Map); ok {
    69  		return cfgMeta, true
    70  	}
    71  	// keys are interface{}, manually change to string keys
    72  	mapInterface, ok := orig.(map[any]any)
    73  	if !ok {
    74  		// not a map at all
    75  		return nil, false
    76  	}
    77  	mapString := make(map[string]any)
    78  	for key, value := range mapInterface {
    79  		mapString[fmt.Sprintf("%v", key)] = value
    80  	}
    81  	return mapString, true
    82  }
    83  
    84  func (m Map) Bool(key string) *bool {
    85  	if m[key] == nil {
    86  		return nil
    87  	}
    88  	v, ok := m[key].(bool)
    89  	if !ok {
    90  		scopes.Framework.Warnf("failed to parse key of type %T value %q as bool, defaulting to false", m[key], key)
    91  		return nil
    92  	}
    93  	return &v
    94  }
    95  
    96  func (m Map) Int(key string) int {
    97  	if m[key] == nil {
    98  		return 0
    99  	}
   100  	v, ok := m[key].(int)
   101  	if !ok {
   102  		scopes.Framework.Warnf("failed to parse key of type %T value %q as int, defaulting to 0", m[key], key)
   103  		return 0
   104  	}
   105  	return v
   106  }