Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init commit to create PR #4

Open
wants to merge 86 commits into
base: typeset_base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
5475cb6
init commit to create PR
instaclustr-wenbodu Nov 30, 2023
dcf3b6c
add set into terraform schema, be able to convert yaml array+ignore o…
instaclustr-wenbodu Nov 30, 2023
0d0b612
update openapi spec to include typeSet
instaclustr-wenbodu Nov 30, 2023
936da83
update with typeset
instaclustr-wenbodu Dec 4, 2023
958bc47
update with typeset
instaclustr-wenbodu Dec 4, 2023
f4c0816
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
d274cb9
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
6693154
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
5e2e091
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
0fa67b2
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
db5456a
update with typeset add some logs
instaclustr-wenbodu Dec 5, 2023
90f7105
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
15d1268
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
07104e4
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
c0bde65
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
161dfdf
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
8dd8429
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
37ada4d
update to have a hash function
instaclustr-wenbodu Dec 6, 2023
c563fb3
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
f29f955
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
2e44bd5
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
06a264b
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
c859a20
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
71a03cc
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
28bbc16
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
2a9f4b7
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
ec26911
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
5090a3c
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
4b4e99e
introduce TypeSet
instaclustr-wenbodu Dec 12, 2023
8155cf4
introduce TypeSet
instaclustr-wenbodu Dec 13, 2023
22f5601
introduce TypeSet
instaclustr-wenbodu Dec 13, 2023
4b77bbd
update
instaclustr-wenbodu Dec 13, 2023
59391b1
update
instaclustr-wenbodu Dec 14, 2023
4c741f3
update
instaclustr-wenbodu Dec 14, 2023
1041f17
update
instaclustr-wenbodu Dec 14, 2023
b641ee0
update
instaclustr-wenbodu Dec 14, 2023
c50c876
update
instaclustr-wenbodu Dec 14, 2023
d120c80
update
instaclustr-wenbodu Dec 14, 2023
e5bcdb3
update
instaclustr-wenbodu Dec 14, 2023
ce1422f
update
instaclustr-wenbodu Dec 14, 2023
b190124
update
instaclustr-wenbodu Dec 14, 2023
fa4c8c0
update
instaclustr-wenbodu Dec 14, 2023
5ef2c2a
update
instaclustr-wenbodu Dec 14, 2023
305ed98
update
instaclustr-wenbodu Dec 14, 2023
9d75dcb
update
instaclustr-wenbodu Dec 14, 2023
361f32e
update
instaclustr-wenbodu Dec 14, 2023
83ad582
update
instaclustr-wenbodu Dec 15, 2023
4b91d8f
update
instaclustr-wenbodu Dec 15, 2023
bb28070
update
instaclustr-wenbodu Dec 15, 2023
bf3a4de
update
instaclustr-wenbodu Dec 17, 2023
9da8981
update
instaclustr-wenbodu Dec 18, 2023
4a1b30a
update
instaclustr-wenbodu Dec 18, 2023
2c70c6e
update
instaclustr-wenbodu Dec 18, 2023
73e944e
update
instaclustr-wenbodu Dec 18, 2023
b1947a2
update a bit
instaclustr-wenbodu Dec 18, 2023
6c0185d
update a bit
instaclustr-wenbodu Dec 18, 2023
7139f93
update a bit
instaclustr-wenbodu Dec 18, 2023
c696efc
update a bit
instaclustr-wenbodu Dec 18, 2023
48768ef
update a bit
instaclustr-wenbodu Dec 18, 2023
0ca9082
update a bit
instaclustr-wenbodu Dec 18, 2023
8538266
update a bit
instaclustr-wenbodu Dec 18, 2023
9c7aa90
update a bit
instaclustr-wenbodu Dec 19, 2023
3127bd3
update a bit
instaclustr-wenbodu Dec 19, 2023
ebea793
update a bit
instaclustr-wenbodu Dec 19, 2023
295c205
update a bit
instaclustr-wenbodu Dec 19, 2023
c6359dc
update a bit
instaclustr-wenbodu Dec 19, 2023
e24548b
update a bit
instaclustr-wenbodu Dec 19, 2023
8ac7ab6
update a bit
instaclustr-wenbodu Dec 19, 2023
2fcd28d
update a bit
instaclustr-wenbodu Dec 19, 2023
e89a50e
update a bit
instaclustr-wenbodu Dec 19, 2023
d06338c
update a bit
instaclustr-wenbodu Dec 19, 2023
8e523f7
update a bit
instaclustr-wenbodu Dec 19, 2023
03d2e12
update a bit
instaclustr-wenbodu Dec 19, 2023
cc5c929
update a bit
instaclustr-wenbodu Dec 19, 2023
a8beb19
update a bit
instaclustr-wenbodu Dec 19, 2023
84814ba
update a bit
instaclustr-wenbodu Dec 19, 2023
4012274
update a bit
instaclustr-wenbodu Dec 19, 2023
c6c4d92
update a bit
instaclustr-wenbodu Dec 19, 2023
acdfe3e
update a bit
instaclustr-wenbodu Dec 19, 2023
b52d8b8
update a bit
instaclustr-wenbodu Dec 19, 2023
fc0938d
update a bit
instaclustr-wenbodu Dec 19, 2023
f706f0c
update a bit
instaclustr-wenbodu Dec 19, 2023
d1a0f28
update a bit
instaclustr-wenbodu Dec 22, 2023
78c259b
introduce typeSet
instaclustr-wenbodu Jan 7, 2024
681091d
introduce typeSet
instaclustr-wenbodu Jan 7, 2024
a0cc970
introduce typeSet
instaclustr-wenbodu Jan 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update
  • Loading branch information
instaclustr-wenbodu committed Dec 14, 2023
commit 4c741f395a7e5193b74c688618b1dd6f3d4d6414
107 changes: 87 additions & 20 deletions openapi/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,35 +241,102 @@ func hashComplexObject(v interface{}) int {
return hashcode.String(buffer.String())
}

func deepConvertArrayToSet(v interface{}) (interface{}, error) {
//func deepConvertArrayToSet(property *SpecSchemaDefinitionProperty, v interface{}) (interface{}, error) {
// switch v := v.(type) {
// case []interface{}:
// // For slices, create a new set and add each element to the set
// if property.IgnoreItemsOrder {
// set := schema.NewSet(hashComplexObject, []interface{}{})
// for k, elem := range v {
// convertedElem, err := deepConvertArrayToSet(property.SpecSchemaDefinition.Properties, elem)
// if err != nil {
// return nil, err
// }
// set.Add(convertedElem)
// }
// return set, nil
// }
// case map[string]interface{}:
// // For maps, create a new map and convert each value in the map
// newMap := make(map[string]interface{})
// for key, value := range v {
// convertedValue, err := deepConvertArrayToSet(property.SpecSchemaDefinition.Properties[key], value)
// if err != nil {
// return nil, err
// }
// newMap[key] = convertedValue
// }
// return newMap, nil
// default:
// // For other types, return the value as is
// return v, nil
// }
//}

func deepConvertArrayToSet(property *SpecSchemaDefinitionProperty, v interface{}) (interface{}, error) {
switch v := v.(type) {
case []interface{}:
// For slices, create a new set and add each element to the set
set := schema.NewSet(hashComplexObject, []interface{}{})
for _, elem := range v {
convertedElem, err := deepConvertArrayToSet(elem)
if err != nil {
return nil, err
}
set.Add(convertedElem)
}
return set, nil
case map[string]interface{}:
// For maps, create a new map and convert each value in the map
newMap := make(map[string]interface{})
for key, value := range v {
convertedValue, err := deepConvertArrayToSet(value)
if err != nil {
return nil, err
if property.isSetProperty() {
set := schema.NewSet(hashComplexObject, []interface{}{})
for _, elem := range v {
if property.isSetOfObjectsProperty() {
convertedElem, err := deepConvertArrayToSetMap(property.SpecSchemaDefinition.Properties, elem)
if err != nil {
return nil, err
}
set.Add(convertedElem)
} else {
set.Add(elem)
}
}
newMap[key] = convertedValue
return set, nil
}
return newMap, nil
return v, nil
default:
// For other types, return the value as is
return v, nil
}
}

func deepConvertArrayToSetMap(properties []*SpecSchemaDefinitionProperty, object interface{}) (interface{}, error) {
outerMap, ok := object.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("object is not a map")
}

// Since outerMap has only one element, we can get that element directly
for outerKey, innerObject := range outerMap {
switch innerMap := innerObject.(type) {
case map[string]interface{}:
// For maps, create a new map and convert each value in the map
newMap := make(map[string]interface{})
for key, value := range innerMap {
//log.Printf("[INFO] key,value %s %s", key, value)
for _, property := range properties {
if key == property.Name {
//log.Printf("[INFO] key,value %s %s", key, value)
if property.isSetOfObjectsProperty() {
convertedValue, err := deepConvertArrayToSet(property, value)
if err != nil {
return nil, err
}
newMap[key] = convertedValue
} else {
newMap[key] = value
}
}
}
}
outerMap[outerKey] = newMap
default:
// For other types, return the value as is
outerMap[outerKey] = innerObject
}
}

return outerMap, nil
}
func convertPayloadToLocalStateDataValue(property *SpecSchemaDefinitionProperty, propertyValue interface{}, propertyLocalStateValue interface{}) (interface{}, error) {
if property.WriteOnly {
return propertyLocalStateValue, nil
Expand Down Expand Up @@ -326,7 +393,7 @@ func convertPayloadToLocalStateDataValue(property *SpecSchemaDefinitionProperty,
if propertyValue != nil {
arrayValue = propertyValue.([]interface{})
}
setValue, err := deepConvertArrayToSet(arrayValue)
setValue, err := deepConvertArrayToSet(property, arrayValue)
setLocalValue := propertyLocalStateValue.(*schema.Set)
if err != nil {
return err, nil
Expand Down
178 changes: 146 additions & 32 deletions openapi/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1140,28 +1140,70 @@ func TestDeepConvertArrayToSet(t *testing.T) {
Convey("Given a resource data (state) loaded with couple properties", t, func() {
Convey("When DeepConvertArrayToSet is called with a array of primitive element", func() {
inputArray := []interface{}{"value2", "value1", "value4"}
resultSet, err := deepConvertArrayToSet(inputArray)
property := newSetSchemaDefinitionPropertyWithDefaults("set_object_property", "", true, false, false, nil, TypeString, nil)
resultSet, err := deepConvertArrayToSet(property, inputArray)
Convey("Then the error should be nil and the result value should be the expected value with the right type array", func() {
So(err, ShouldBeNil)
So(resultSet, ShouldHaveSameTypeAs, &schema.Set{})
So(resultSet.(*schema.Set).Len(), ShouldEqual, 3)
So(resultSet.(*schema.Set).Contains("value2"), ShouldBeTrue)
So(resultSet.(*schema.Set).Contains("value1"), ShouldBeTrue)
So(resultSet.(*schema.Set).Contains("value4"), ShouldBeTrue)
So(resultSet.(*schema.Set).Contains("va"), ShouldBeFalse)
})
})
Convey("When DeepConvertArrayToSet is called with a array of complex object", func() {
nestedSetItemSchemaDefinition := &SpecSchemaDefinition{
Properties: SpecSchemaDefinitionProperties{
newIntSchemaDefinitionPropertyWithDefaults("nested_set_property", "", true, false, nil),
setSchemaDefinitionPropertyWriteOnly(newIntSchemaDefinitionPropertyWithDefaults("nested_set_property_password", "", true, false, nil)),
},
}
nestedArrayItemSchemaDefinition := &SpecSchemaDefinition{
Properties: SpecSchemaDefinitionProperties{
newIntSchemaDefinitionPropertyWithDefaults("nested_array_property", "", true, false, nil),
setSchemaDefinitionPropertyWriteOnly(newIntSchemaDefinitionPropertyWithDefaults("nested_array_property_password", "", true, false, nil)),
},
}
nestedObjectSchemaDefinition := &SpecSchemaDefinition{
Properties: SpecSchemaDefinitionProperties{
setSchemaDefinitionPropertyWriteOnly(newIntSchemaDefinitionPropertyWithDefaults("origin_port", "", true, false, 80)),
newStringSchemaDefinitionPropertyWithDefaults("protocol", "", true, false, "http"),
setSchemaDefinitionPropertyWriteOnly(newStringSchemaDefinitionPropertyWithDefaults("password", "", true, false, nil)),
setSchemaDefinitionPropertyWriteOnly(newListSchemaDefinitionPropertyWithDefaults("password_array", "", true, false, false, nil, TypeString, nil)),
newSetSchemaDefinitionPropertyWithDefaults("nested_set", "", true, false, false, nil, TypeObject, nestedSetItemSchemaDefinition),
newListSchemaDefinitionPropertyWithDefaults("nested_list", "", true, false, false, nil, TypeObject, nestedArrayItemSchemaDefinition),
},
}
//nestedObject := newObjectSchemaDefinitionPropertyWithDefaults("nested_object", "", true, false, false, nil, nestedObjectSchemaDefinition)
//propertyWithNestedObjectSchemaDefinition := &SpecSchemaDefinition{
// Properties: SpecSchemaDefinitionProperties{
// idProperty,
// nestedObject,
// },
//}
object1 := map[string]interface{}{
"nested_object": map[string]interface{}{
"origin_port": 12345,
"protocol": "tcp",
"nested_set": []interface{}{
map[string]interface{}{
"nested_set_property": 123,
"nested_set_property_password": "setpassword123",
},
map[string]interface{}{
"nested_set_property": 456,
"nested_set_property_password": "setpassword456",
},
},
"nested_list": []interface{}{
map[string]interface{}{
"nested_list_property": 123,
"nested_list_property_password": "password123",
"nested_list_property_password": "listpassword123",
},
map[string]interface{}{
"nested_list_property": 456,
"nested_list_property_password": "password456",
"nested_list_property_password": "listpassword456",
},
},
},
Expand All @@ -1170,14 +1212,24 @@ func TestDeepConvertArrayToSet(t *testing.T) {
"nested_object": map[string]interface{}{
"origin_port": 54321,
"protocol": "tcp",
"nested_set": []interface{}{
map[string]interface{}{
"nested_set_property": 789,
"nested_set_property_password": "setpassword789",
},
map[string]interface{}{
"nested_set_property": 987,
"nested_set_property_password": "setpassword987",
},
},
"nested_list": []interface{}{
map[string]interface{}{
"nested_list_property": 789,
"nested_list_property_password": "password789",
"nested_list_property_password": "listpassword789",
},
map[string]interface{}{
"nested_list_property": 987,
"nested_list_property_password": "password987",
"nested_list_property_password": "listpassword987",
},
},
},
Expand All @@ -1186,36 +1238,66 @@ func TestDeepConvertArrayToSet(t *testing.T) {
"nested_object": map[string]interface{}{
"origin_port": 11111,
"protocol": "udp",
"nested_list": []interface{}{
"nested_set": []interface{}{
map[string]interface{}{
"nested_list_property": 987,
"nested_list_property_password": "password987",
"nested_set_property": 789,
"nested_set_property_password": "setpassword789",
},
map[string]interface{}{
"nested_set_property": 987,
"nested_set_property_password": "setpassword987",
},
},
"nested_list": []interface{}{
map[string]interface{}{
"nested_list_property": 789,
"nested_list_property_password": "password789",
"nested_list_property_password": "listpassword789",
},
map[string]interface{}{
"nested_list_property": 987,
"nested_list_property_password": "listpassword987",
},
},
},
}
object1NestedArrayElement1 := map[string]interface{}{
"nested_list_property": 123,
"nested_list_property_password": "password123",
object1NestedSetElement1 := map[string]interface{}{
"nested_set_property": 123,
"nested_set_property_password": "setpassword123",
}
object1NestedSetElement2 := map[string]interface{}{
"nested_set_property": 456,
"nested_set_property_password": "setpassword456",
}
object2NestedSetElement1 := map[string]interface{}{
"nested_set_property": 789,
"nested_set_property_password": "setpassword789",
}
object1NestedArrayElement2 := map[string]interface{}{
"nested_list_property": 456,
"nested_list_property_password": "password456",
object2NestedSetElement2 := map[string]interface{}{
"nested_set_property": 987,
"nested_set_property_password": "setpassword987",
}
object2NestedArrayElement1 := map[string]interface{}{
"nested_list_property": 789,
"nested_list_property_password": "password789",
object1NestedListElement1 := map[string]interface{}{
"nested_set_property": 123,
"nested_set_property_password": "listpassword123",
}
object2NestedArrayElement2 := map[string]interface{}{
"nested_list_property": 987,
"nested_list_property_password": "password987",
object1NestedListElement2 := map[string]interface{}{
"nested_set_property": 456,
"nested_set_property_password": "listpassword456",
}
object2NestedListElement1 := map[string]interface{}{
"nested_set_property": 789,
"nested_set_property_password": "listpassword789",
}
object2NestedListElement2 := map[string]interface{}{
"nested_set_property": 987,
"nested_set_property_password": "listpassword987",
}
inputArray := []interface{}{object1, object2, object3}
resultSet, err := deepConvertArrayToSet(inputArray)
//expectedPropertyWithNestedObjectName := "property_with_nested_object"
//propertyWithNestedObject := newObjectSchemaDefinitionPropertyWithDefaults(expectedPropertyWithNestedObjectName, "", true, false, false, nil, propertyWithNestedObjectSchemaDefinition)
expectedPropertyWithSetWithNestedObjectName := "property_with_set_with_nested_object"
setPropertyWithNestedObject := newSetSchemaDefinitionPropertyWithDefaults(expectedPropertyWithSetWithNestedObjectName, "", true, false, false, nil, TypeObject, nestedObjectSchemaDefinition)
resultSet, err := deepConvertArrayToSet(setPropertyWithNestedObject, inputArray)
//resultSet = resultSet.(*schema.Set)
Convey("Then the error should be nil and the result value should be the expected value with the right type array", func() {
So(err, ShouldBeNil)
Expand All @@ -1227,24 +1309,56 @@ func TestDeepConvertArrayToSet(t *testing.T) {
nestedObject, ok := obj["nested_object"].(map[string]interface{})
So(ok, ShouldBeTrue)

nestedList, ok := nestedObject["nested_list"].(*schema.Set)
nestedList, ok := nestedObject["nested_list"]
So(ok, ShouldBeTrue)
So(nestedList.Len(), ShouldEqual, 2)
So(nestedList, ShouldHaveSameTypeAs, []interface{}{})

nestedSet, ok := nestedObject["nested_set"].(*schema.Set)
So(ok, ShouldBeTrue)
So(nestedSet, ShouldHaveSameTypeAs, &schema.Set{})
So(nestedSet.Len(), ShouldEqual, 2)

protocol, ok := nestedObject["origin_port"].(int)
So(ok, ShouldBeTrue)
So(protocol == 12345 || protocol == 54321 || protocol == 11111, ShouldBeTrue)
if nestedObject["origin_port"] == 12345 {
So(nestedList.Contains(object1NestedArrayElement1), ShouldBeTrue)
So(nestedList.Contains(object1NestedArrayElement2), ShouldBeTrue)
So(nestedList.Contains(object2NestedArrayElement2), ShouldBeFalse)
So(nestedList.Contains(object2NestedArrayElement2), ShouldBeFalse)
expected := []interface{}{
map[string]interface{}{
"nested_list_property": 123,
"nested_list_property_password": "listpassword123",
},
map[string]interface{}{
"nested_list_property": 456,
"nested_list_property_password": "listpassword456",
},
}
So(nestedList, ShouldResemble, expected)

So(nestedSet.Contains(object1NestedSetElement1), ShouldBeTrue)
So(nestedSet.Contains(object1NestedSetElement2), ShouldBeTrue)
So(nestedSet.Contains(object2NestedSetElement1), ShouldBeFalse)
So(nestedSet.Contains(object2NestedSetElement2), ShouldBeFalse)
So(nestedSet.Contains(object1NestedListElement1), ShouldBeFalse)
So(nestedSet.Contains(object1NestedListElement2), ShouldBeFalse)
}
if nestedObject["origin_port"] == 54321 || nestedObject["origin_port"] == 11111 {
So(nestedList.Contains(object2NestedArrayElement1), ShouldBeTrue)
So(nestedList.Contains(object2NestedArrayElement2), ShouldBeTrue)
So(nestedList.Contains(object1NestedArrayElement2), ShouldBeFalse)
So(nestedList.Contains(object1NestedArrayElement2), ShouldBeFalse)
expected := []interface{}{
map[string]interface{}{
"nested_list_property": 789,
"nested_list_property_password": "listpassword789",
},
map[string]interface{}{
"nested_list_property": 987,
"nested_list_property_password": "listpassword987",
},
}
So(nestedList, ShouldResemble, expected)
So(nestedSet.Contains(object2NestedSetElement1), ShouldBeTrue)
So(nestedSet.Contains(object2NestedSetElement2), ShouldBeTrue)
So(nestedSet.Contains(object1NestedSetElement1), ShouldBeFalse)
So(nestedSet.Contains(object1NestedSetElement2), ShouldBeFalse)
So(nestedSet.Contains(object2NestedListElement1), ShouldBeFalse)
So(nestedSet.Contains(object2NestedListElement2), ShouldBeFalse)
}
}
})
Expand Down