-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow overwrites of array fields (#30)
* feat: allow overwrites of array fields Signed-off-by: André Kesser <[email protected]> * fix: fix typos Signed-off-by: André Kesser <[email protected]> * fix: renamed test function Signed-off-by: André Kesser <[email protected]> --------- Signed-off-by: André Kesser <[email protected]> Co-authored-by: André Kesser <[email protected]>
- Loading branch information
Showing
4 changed files
with
255 additions
and
29 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
package/EKS-Cluster/composition-xcluster.eks.aws.example.cloud.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package generator | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
tp "github.com/crossplane-contrib/x-generation/pkg/types" | ||
) | ||
|
||
func Test_generateOverrideFields(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
base map[string]interface{} | ||
overridePaths []tp.OverrideField | ||
want string | ||
}{ | ||
{ | ||
name: "Should render path", | ||
base: map[string]interface{}{}, | ||
overridePaths: []tp.OverrideField{ | ||
{ | ||
Path: "spec.forProvider.certificateAuthorityConfiguration", | ||
Value: "testA", | ||
}, | ||
}, | ||
want: `{"spec": {"forProvider": {"certificateAuthorityConfiguration":"testA"}}}`, | ||
}, | ||
{ | ||
name: "Should not override existing path", | ||
base: map[string]interface{}{ | ||
"spec": map[string]interface{}{ | ||
"forProvider": map[string]interface{}{ | ||
"test": "testValue", | ||
}, | ||
}, | ||
}, | ||
overridePaths: []tp.OverrideField{ | ||
{ | ||
Path: "spec.forProvider.certificateAuthorityConfiguration", | ||
Value: "testA", | ||
}, | ||
}, | ||
want: `{"spec": {"forProvider": {"test":"testValue", "certificateAuthorityConfiguration":"testA"}}}`, | ||
}, | ||
{ | ||
name: "Should create arrays of strings", | ||
base: map[string]interface{}{}, | ||
overridePaths: []tp.OverrideField{ | ||
{ | ||
Path: "spec.forProvider.certificateAuthorityConfiguration[0]", | ||
Value: "testA", | ||
}, | ||
}, | ||
want: `{"spec": {"forProvider": { "certificateAuthorityConfiguration": ["testA"]}}}`, | ||
}, | ||
{ | ||
name: "Should create arrays of arrays", | ||
base: map[string]interface{}{}, | ||
overridePaths: []tp.OverrideField{ | ||
{ | ||
Path: "spec.forProvider.certificateAuthorityConfiguration[0].subproperty[0]", | ||
Value: "testA", | ||
}, | ||
}, | ||
want: `{"spec": {"forProvider": { "certificateAuthorityConfiguration": [{"subproperty":["testA"]}]}}}`, | ||
}, | ||
{ | ||
name: "Should create arrays of arrays with properies", | ||
base: map[string]interface{}{}, | ||
overridePaths: []tp.OverrideField{ | ||
{ | ||
Path: "spec.forProvider.certificateAuthorityConfiguration[0].subproperty[0].arg", | ||
Value: "testA", | ||
}, | ||
}, | ||
want: `{"spec": {"forProvider": { "certificateAuthorityConfiguration": [{"subproperty":[{"arg":"testA"}]}]}}}`, | ||
}, | ||
{ | ||
name: "Should render path", | ||
base: map[string]interface{}{}, | ||
overridePaths: []tp.OverrideField{ | ||
{ | ||
Path: "spec.forProvider[\"certificateAuthorityConfiguration\"]", | ||
Value: "testA", | ||
}, | ||
}, | ||
want: `{"spec": {"forProvider": {"certificateAuthorityConfiguration":"testA"}}}`, | ||
}, | ||
{ | ||
name: "Should render path", | ||
base: map[string]interface{}{}, | ||
overridePaths: []tp.OverrideField{ | ||
{ | ||
Path: "spec.forProvider[\"certificateAuthority.Configuration\"]", | ||
Value: "testA", | ||
}, | ||
}, | ||
want: `{"spec": {"forProvider": {"certificateAuthority.Configuration":"testA"}}}`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := applyOverrideFields(tt.base, tt.overridePaths) | ||
object, err := json.Marshal(got) | ||
if err != nil { | ||
t.Errorf("error marshalling object %v", err) | ||
} | ||
|
||
// unmarshal and marshal wanted value to be independent of formatting and oder of properties | ||
var wantobject interface{} | ||
err = json.Unmarshal([]byte(tt.want), &wantobject) | ||
if err != nil { | ||
t.Errorf("want is not valid json %v", err) | ||
} | ||
|
||
wantbyte, err := json.Marshal(wantobject) | ||
|
||
if err != nil { | ||
t.Errorf("error marshalling object %v", err) | ||
} | ||
|
||
gotString := string(object) | ||
wantString := string(wantbyte) | ||
if gotString != wantString { | ||
t.Errorf("Expaced object does not match got\n%v, want\n%v", gotString, wantString) | ||
} | ||
}) | ||
} | ||
} |