Skip to content

Commit ae47704

Browse files
Merge pull request #30291 from ming1013/add-yaml-util
OCPERT-151: Migrate new func for Compare structured data instead of string from openshift-test-private repo
2 parents 6c0816b + d3caeff commit ae47704

File tree

1 file changed

+33
-0
lines changed
  • test/extended/util/compat_otp

1 file changed

+33
-0
lines changed

test/extended/util/compat_otp/yaml.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package compat_otp
33
import (
44
"encoding/json"
55
"io/ioutil"
6+
"reflect"
67
"strconv"
78
"strings"
89

@@ -158,3 +159,35 @@ func setYamlValue(root *yaml.Node, path []string, value yaml.Node) {
158159
}
159160
}
160161
}
162+
163+
// ConvertStrToJson converts a JSON format string to data structure of map[string]interface{}
164+
func ConvertStrToJson(str string) (map[string]interface{}, error) {
165+
var strMap map[string]interface{}
166+
if err := json.Unmarshal([]byte(str), &strMap); err != nil {
167+
return nil, err
168+
}
169+
return strMap, nil
170+
}
171+
172+
// ContainsAll checks if all keys and values in small are also in the big map.
173+
func ContainsAll(big, small map[string]interface{}) bool {
174+
for k, vSmall := range small {
175+
vBig, ok := big[k]
176+
if !ok {
177+
return false // key missing
178+
}
179+
180+
switch vSmallTyped := vSmall.(type) {
181+
case map[string]interface{}:
182+
vBigTyped, ok := vBig.(map[string]interface{})
183+
if !ok || !ContainsAll(vBigTyped, vSmallTyped) {
184+
return false
185+
}
186+
default:
187+
if !reflect.DeepEqual(vBig, vSmall) {
188+
return false
189+
}
190+
}
191+
}
192+
return true
193+
}

0 commit comments

Comments
 (0)