@@ -148,27 +148,6 @@ var associativeAndAtomicSchema = `types:
148
148
elementType:
149
149
scalar: string
150
150
`
151
- var atomicTypesSchema = `types:
152
- - name: myRoot
153
- map:
154
- fields:
155
- - name: atomicMap
156
- type:
157
- namedType: myAtomicMap
158
- - name: atomicList
159
- type:
160
- namedType: mySequence
161
- - name: myAtomicMap
162
- map:
163
- elementType:
164
- scalar: string
165
- elementRelationship: atomic
166
- - name: mySequence
167
- list:
168
- elementType:
169
- scalar: string
170
- elementRelationship: atomic
171
- `
172
151
173
152
var nestedTypesSchema = `types:
174
153
- name: type
@@ -906,3 +885,116 @@ func TestReversibleExtract(t *testing.T) {
906
885
})
907
886
}
908
887
}
888
+
889
+ type extractWithKeysTestCase struct {
890
+ name string
891
+ rootTypeName string
892
+ schema typed.YAMLObject
893
+ triplets []extractTriplet
894
+ }
895
+
896
+ type extractTriplet struct {
897
+ object typed.YAMLObject
898
+ set * fieldpath.Set
899
+ wantOutput interface {}
900
+ }
901
+
902
+ var extractWithKeysCases = []extractWithKeysTestCase {{
903
+ name : "associativeAndAtomicSchema" ,
904
+ rootTypeName : "myRoot" ,
905
+ schema : typed .YAMLObject (associativeAndAtomicSchema ),
906
+ triplets : []extractTriplet {
907
+ {
908
+ // extract with all key fields included
909
+ object : `{"list":[{"key":"nginx","id":1,"nv":2}]}` ,
910
+ set : _NS (
911
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "key" ),
912
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "id" ),
913
+ ),
914
+ wantOutput : typed .YAMLObject (`{"list":[{"key":"nginx","id":1}]}` ),
915
+ },
916
+ {
917
+ // extract no key field included
918
+ object : `{"list":[{"key":"nginx","id":1,"nv":2}]}` ,
919
+ set : _NS (
920
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "nv" ),
921
+ ),
922
+ wantOutput : typed .YAMLObject (`{"list":[{"key":"nginx","id":1, "nv":2}]}` ),
923
+ },
924
+ {
925
+ // extract with partial keys included
926
+ object : `{"list":[{"key":"nginx","id":1,"nv":2}]}` ,
927
+ set : _NS (
928
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "nv" ),
929
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "id" ),
930
+ ),
931
+ wantOutput : typed .YAMLObject (`{"list":[{"key":"nginx","id":1, "nv":2}]}` ),
932
+ },
933
+ {
934
+ // extract with null field value
935
+ object : `{"list":[{"key":"nginx","id":1,"nv":2}]}` ,
936
+ set : _NS (
937
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "value" ),
938
+ ),
939
+ wantOutput : map [string ]interface {}{
940
+ "list" : []interface {}{nil },
941
+ },
942
+ },
943
+ },
944
+ }}
945
+
946
+ func (tt extractWithKeysTestCase ) test (t * testing.T ) {
947
+ parser , err := typed .NewParser (tt .schema )
948
+ if err != nil {
949
+ t .Fatalf ("failed to create schema: %v" , err )
950
+ }
951
+ pt := parser .Type (tt .rootTypeName )
952
+
953
+ for i , triplet := range tt .triplets {
954
+ triplet := triplet
955
+ t .Run (fmt .Sprintf ("%v-valid-%v" , tt .name , i ), func (t * testing.T ) {
956
+ t .Parallel ()
957
+ // source typedValue obj
958
+ tv , err := pt .FromYAML (triplet .object )
959
+ if err != nil {
960
+ t .Fatal (err )
961
+ }
962
+ gotExtracted := tv .ExtractItems (triplet .set , typed .WithAppendKeyFields ())
963
+
964
+ switch triplet .wantOutput .(type ) {
965
+ case typed.YAMLObject :
966
+ wantOut , err := pt .FromYAML (triplet .wantOutput .(typed.YAMLObject ))
967
+ if err != nil {
968
+ t .Fatalf ("unable to parser/validate removeOutput yaml: %v\n %v" , err , triplet .wantOutput )
969
+ }
970
+
971
+ if ! value .Equals (gotExtracted .AsValue (), wantOut .AsValue ()) {
972
+ t .Errorf ("ExtractItems expected\n %v\n but got\n %v\n " ,
973
+ value .ToString (wantOut .AsValue ()), value .ToString (gotExtracted .AsValue ()),
974
+ )
975
+ }
976
+ default :
977
+ // The extracted result
978
+ wantOut := value .NewValueInterface (triplet .wantOutput )
979
+ if ! value .Equals (gotExtracted .AsValue (), wantOut ) {
980
+ t .Errorf ("ExtractItems expected\n %v\n but got\n %v\n " ,
981
+ value .ToString (wantOut ), value .ToString (gotExtracted .AsValue ()),
982
+ )
983
+ }
984
+ }
985
+ })
986
+ }
987
+ }
988
+
989
+ // TestExtractWithKeys ensures that when you extract
990
+ // items from an object with the AppendKeyField option,
991
+ // the key fields are also included in the output.
992
+ func TestExtractWithKeys (t * testing.T ) {
993
+ for _ , tt := range extractWithKeysCases {
994
+ tt := tt
995
+ t .Run (tt .name , func (t * testing.T ) {
996
+ t .Parallel ()
997
+ tt .test (t )
998
+ })
999
+ }
1000
+ }
0 commit comments