Browse Source

feat: Adding conversion helper for JSON objects

Matthias Ladkau 3 years ago
parent
commit
1050423c45
2 changed files with 37 additions and 0 deletions
  1. 29 0
      scope/helper.go
  2. 8 0
      scope/helper_test.go

+ 29 - 0
scope/helper.go

@@ -92,3 +92,32 @@ func ConvertJSONToECALObject(v interface{}) interface{} {
 
 	return res
 }
+
+/*
+ConvertECALToJSONbject converts an ECAL container structure into an object which
+can be marshalled into a JSON string.
+*/
+func ConvertECALToJSONObject(v interface{}) interface{} {
+	res := v
+
+	if mapContainer, ok := v.(map[interface{}]interface{}); ok {
+		newRes := make(map[string]interface{})
+
+		for mk, mv := range mapContainer {
+			newRes[fmt.Sprint(mk)] = ConvertECALToJSONObject(mv)
+		}
+
+		res = newRes
+
+	} else if mapList, ok := v.([]interface{}); ok {
+		newRes := make([]interface{}, len(mapList))
+
+		for i, lv := range mapList {
+			newRes[i] = ConvertECALToJSONObject(lv)
+		}
+
+		res = newRes
+	}
+
+	return res
+}

+ 8 - 0
scope/helper_test.go

@@ -59,4 +59,12 @@ func TestConvertJSONToECALObject(t *testing.T) {
 		return
 	}
 
+	res = ConvertECALToJSONObject(res)
+
+	if typeString := fmt.Sprintf("%#v", res); typeString !=
+		`map[string]interface {}{"foo":[]interface {}{map[string]interface {}{"bar":"123"}}}` {
+		t.Error("Unexpected result:", typeString)
+		return
+	}
+
 }