@@ -906,3 +906,116 @@ func TestReversibleExtract(t *testing.T) {
906
906
})
907
907
}
908
908
}
909
+
910
+ type extractWithKeysTestCase struct {
911
+ name string
912
+ rootTypeName string
913
+ schema typed.YAMLObject
914
+ triplets []extractTriplet
915
+ }
916
+
917
+ type extractTriplet struct {
918
+ object typed.YAMLObject
919
+ set * fieldpath.Set
920
+ wantOutput interface {}
921
+ }
922
+
923
+ var extractWithKeysCases = []extractWithKeysTestCase {{
924
+ name : "associativeAndAtomicSchema" ,
925
+ rootTypeName : "myRoot" ,
926
+ schema : typed .YAMLObject (associativeAndAtomicSchema ),
927
+ triplets : []extractTriplet {
928
+ {
929
+ // extract with all key fields included
930
+ object : `{"list":[{"key":"nginx","id":1,"nv":2}]}` ,
931
+ set : _NS (
932
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "key" ),
933
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "id" ),
934
+ ),
935
+ wantOutput : typed .YAMLObject (`{"list":[{"key":"nginx","id":1}]}` ),
936
+ },
937
+ {
938
+ // extract no key field included
939
+ object : `{"list":[{"key":"nginx","id":1,"nv":2}]}` ,
940
+ set : _NS (
941
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "nv" ),
942
+ ),
943
+ wantOutput : typed .YAMLObject (`{"list":[{"key":"nginx","id":1, "nv":2}]}` ),
944
+ },
945
+ {
946
+ // extract with patial keys included
947
+ object : `{"list":[{"key":"nginx","id":1,"nv":2}]}` ,
948
+ set : _NS (
949
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "nv" ),
950
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "id" ),
951
+ ),
952
+ wantOutput : typed .YAMLObject (`{"list":[{"key":"nginx","id":1, "nv":2}]}` ),
953
+ },
954
+ {
955
+ // extract with null field value
956
+ object : `{"list":[{"key":"nginx","id":1,"nv":2}]}` ,
957
+ set : _NS (
958
+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "value" ),
959
+ ),
960
+ wantOutput : map [string ]interface {}{
961
+ "list" : []interface {}{nil },
962
+ },
963
+ },
964
+ },
965
+ }}
966
+
967
+ func (tt extractWithKeysTestCase ) test (t * testing.T ) {
968
+ parser , err := typed .NewParser (tt .schema )
969
+ if err != nil {
970
+ t .Fatalf ("failed to create schema: %v" , err )
971
+ }
972
+ pt := parser .Type (tt .rootTypeName )
973
+
974
+ for i , triplet := range tt .triplets {
975
+ triplet := triplet
976
+ t .Run (fmt .Sprintf ("%v-valid-%v" , tt .name , i ), func (t * testing.T ) {
977
+ t .Parallel ()
978
+ // source typedValue obj
979
+ tv , err := pt .FromYAML (triplet .object )
980
+ if err != nil {
981
+ t .Fatal (err )
982
+ }
983
+ gotExtracted := tv .ExtractItems (triplet .set , typed .AppendKeyField )
984
+
985
+ switch triplet .wantOutput .(type ) {
986
+ case typed.YAMLObject :
987
+ wantOut , err := pt .FromYAML (triplet .wantOutput .(typed.YAMLObject ))
988
+ if err != nil {
989
+ t .Fatalf ("unable to parser/validate removeOutput yaml: %v\n %v" , err , triplet .wantOutput )
990
+ }
991
+
992
+ if ! value .Equals (gotExtracted .AsValue (), wantOut .AsValue ()) {
993
+ t .Errorf ("ExtractItems expected\n %v\n but got\n %v\n " ,
994
+ value .ToString (wantOut .AsValue ()), value .ToString (gotExtracted .AsValue ()),
995
+ )
996
+ }
997
+ default :
998
+ // The extracted result
999
+ wantOut := value .NewValueInterface (triplet .wantOutput )
1000
+ if ! value .Equals (gotExtracted .AsValue (), wantOut ) {
1001
+ t .Errorf ("ExtractItems expected\n %v\n but got\n %v\n " ,
1002
+ value .ToString (wantOut ), value .ToString (gotExtracted .AsValue ()),
1003
+ )
1004
+ }
1005
+ }
1006
+ })
1007
+ }
1008
+ }
1009
+
1010
+ // TestExtractWithKeys ensures that when you extract
1011
+ // items from an object with the AppendKeyField option,
1012
+ // the key fields are also included in the output.
1013
+ func TestExtractWithKeys (t * testing.T ) {
1014
+ for _ , tt := range extractWithKeysCases {
1015
+ tt := tt
1016
+ t .Run (tt .name , func (t * testing.T ) {
1017
+ t .Parallel ()
1018
+ tt .test (t )
1019
+ })
1020
+ }
1021
+ }
0 commit comments