...

Source file src/sigs.k8s.io/structured-merge-diff/v4/value/mapunstructured.go

Documentation: sigs.k8s.io/structured-merge-diff/v4/value

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     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 value
    18  
    19  type mapUnstructuredInterface map[interface{}]interface{}
    20  
    21  func (m mapUnstructuredInterface) Set(key string, val Value) {
    22  	m[key] = val.Unstructured()
    23  }
    24  
    25  func (m mapUnstructuredInterface) Get(key string) (Value, bool) {
    26  	return m.GetUsing(HeapAllocator, key)
    27  }
    28  
    29  func (m mapUnstructuredInterface) GetUsing(a Allocator, key string) (Value, bool) {
    30  	if v, ok := m[key]; !ok {
    31  		return nil, false
    32  	} else {
    33  		return a.allocValueUnstructured().reuse(v), true
    34  	}
    35  }
    36  
    37  func (m mapUnstructuredInterface) Has(key string) bool {
    38  	_, ok := m[key]
    39  	return ok
    40  }
    41  
    42  func (m mapUnstructuredInterface) Delete(key string) {
    43  	delete(m, key)
    44  }
    45  
    46  func (m mapUnstructuredInterface) Iterate(fn func(key string, value Value) bool) bool {
    47  	return m.IterateUsing(HeapAllocator, fn)
    48  }
    49  
    50  func (m mapUnstructuredInterface) IterateUsing(a Allocator, fn func(key string, value Value) bool) bool {
    51  	if len(m) == 0 {
    52  		return true
    53  	}
    54  	vv := a.allocValueUnstructured()
    55  	defer a.Free(vv)
    56  	for k, v := range m {
    57  		if ks, ok := k.(string); !ok {
    58  			continue
    59  		} else {
    60  			if !fn(ks, vv.reuse(v)) {
    61  				return false
    62  			}
    63  		}
    64  	}
    65  	return true
    66  }
    67  
    68  func (m mapUnstructuredInterface) Length() int {
    69  	return len(m)
    70  }
    71  
    72  func (m mapUnstructuredInterface) Empty() bool {
    73  	return len(m) == 0
    74  }
    75  
    76  func (m mapUnstructuredInterface) Equals(other Map) bool {
    77  	return m.EqualsUsing(HeapAllocator, other)
    78  }
    79  
    80  func (m mapUnstructuredInterface) EqualsUsing(a Allocator, other Map) bool {
    81  	lhsLength := m.Length()
    82  	rhsLength := other.Length()
    83  	if lhsLength != rhsLength {
    84  		return false
    85  	}
    86  	if lhsLength == 0 {
    87  		return true
    88  	}
    89  	vv := a.allocValueUnstructured()
    90  	defer a.Free(vv)
    91  	return other.IterateUsing(a, func(key string, value Value) bool {
    92  		lhsVal, ok := m[key]
    93  		if !ok {
    94  			return false
    95  		}
    96  		return EqualsUsing(a, vv.reuse(lhsVal), value)
    97  	})
    98  }
    99  
   100  func (m mapUnstructuredInterface) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
   101  	return m.ZipUsing(HeapAllocator, other, order, fn)
   102  }
   103  
   104  func (m mapUnstructuredInterface) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
   105  	return defaultMapZip(a, m, other, order, fn)
   106  }
   107  
   108  type mapUnstructuredString map[string]interface{}
   109  
   110  func (m mapUnstructuredString) Set(key string, val Value) {
   111  	m[key] = val.Unstructured()
   112  }
   113  
   114  func (m mapUnstructuredString) Get(key string) (Value, bool) {
   115  	return m.GetUsing(HeapAllocator, key)
   116  }
   117  func (m mapUnstructuredString) GetUsing(a Allocator, key string) (Value, bool) {
   118  	if v, ok := m[key]; !ok {
   119  		return nil, false
   120  	} else {
   121  		return a.allocValueUnstructured().reuse(v), true
   122  	}
   123  }
   124  
   125  func (m mapUnstructuredString) Has(key string) bool {
   126  	_, ok := m[key]
   127  	return ok
   128  }
   129  
   130  func (m mapUnstructuredString) Delete(key string) {
   131  	delete(m, key)
   132  }
   133  
   134  func (m mapUnstructuredString) Iterate(fn func(key string, value Value) bool) bool {
   135  	return m.IterateUsing(HeapAllocator, fn)
   136  }
   137  
   138  func (m mapUnstructuredString) IterateUsing(a Allocator, fn func(key string, value Value) bool) bool {
   139  	if len(m) == 0 {
   140  		return true
   141  	}
   142  	vv := a.allocValueUnstructured()
   143  	defer a.Free(vv)
   144  	for k, v := range m {
   145  		if !fn(k, vv.reuse(v)) {
   146  			return false
   147  		}
   148  	}
   149  	return true
   150  }
   151  
   152  func (m mapUnstructuredString) Length() int {
   153  	return len(m)
   154  }
   155  
   156  func (m mapUnstructuredString) Equals(other Map) bool {
   157  	return m.EqualsUsing(HeapAllocator, other)
   158  }
   159  
   160  func (m mapUnstructuredString) EqualsUsing(a Allocator, other Map) bool {
   161  	lhsLength := m.Length()
   162  	rhsLength := other.Length()
   163  	if lhsLength != rhsLength {
   164  		return false
   165  	}
   166  	if lhsLength == 0 {
   167  		return true
   168  	}
   169  	vv := a.allocValueUnstructured()
   170  	defer a.Free(vv)
   171  	return other.IterateUsing(a, func(key string, value Value) bool {
   172  		lhsVal, ok := m[key]
   173  		if !ok {
   174  			return false
   175  		}
   176  		return EqualsUsing(a, vv.reuse(lhsVal), value)
   177  	})
   178  }
   179  
   180  func (m mapUnstructuredString) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
   181  	return m.ZipUsing(HeapAllocator, other, order, fn)
   182  }
   183  
   184  func (m mapUnstructuredString) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
   185  	return defaultMapZip(a, m, other, order, fn)
   186  }
   187  
   188  func (m mapUnstructuredString) Empty() bool {
   189  	return len(m) == 0
   190  }
   191  

View as plain text