@@ -941,6 +941,92 @@ var extractWithKeysCases = []extractWithKeysTestCase{{
941941 },
942942 },
943943 },
944+ }, {
945+ name : "atomicStructures" ,
946+ rootTypeName : "myRoot" ,
947+ schema : typed .YAMLObject (associativeAndAtomicSchema ),
948+ triplets : []extractTriplet {
949+ {
950+ // extract from atomic list should return entire list
951+ object : `{"atomicList":["a", "b", "c"]}` ,
952+ set : _NS (
953+ _P ("atomicList" , _V ("b" )),
954+ ),
955+ wantOutput : typed .YAMLObject (`{"atomicList":["a", "b", "c"]}` ),
956+ },
957+ {
958+ // extract from atomic map should return entire map
959+ object : `{"atomicMap":{"key1": "value1", "key2": "value2"}}` ,
960+ set : _NS (
961+ _P ("atomicMap" , "key1" ),
962+ ),
963+ wantOutput : typed .YAMLObject (`{"atomicMap":{"key1": "value1", "key2": "value2"}}` ),
964+ },
965+ {
966+ // extract with both atomic and associative structures
967+ object : `{"list":[{"key":"nginx","id":1,"nv":2}], "atomicList":["x", "y"]}` ,
968+ set : _NS (
969+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "nv" ),
970+ _P ("atomicList" , _V ("x" )),
971+ ),
972+ wantOutput : typed .YAMLObject (`{"list":[{"key":"nginx","id":1, "nv":2}], "atomicList":["x", "y"]}` ),
973+ },
974+ },
975+ }, {
976+ name : "compositeKeysExtraction" ,
977+ rootTypeName : "myRoot" ,
978+ schema : typed .YAMLObject (associativeAndAtomicSchema ),
979+ triplets : []extractTriplet {
980+ {
981+ // extract with composite keys - partial field extraction
982+ object : `{"list":[{"key":"a","id":1,"nv":2,"bv":true},{"key":"a","id":2,"nv":3}]}` ,
983+ set : _NS (
984+ _P ("list" , _KBF ("key" , "a" , "id" , 1 ), "nv" ),
985+ ),
986+ wantOutput : typed .YAMLObject (`{"list":[{"key":"a","id":1,"nv":2}]}` ),
987+ },
988+ {
989+ // This test case specifically catches the bug where WithAppendKeyFields
990+ // would duplicate items when extracting with both item key and field paths
991+ object : `{"list":[{"key":"nginx","id":1,"nv":2,"bv":true},{"key":"apache","id":2,"nv":3}]}` ,
992+ set : _NS (
993+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 )),
994+ _P ("list" , _KBF ("key" , "nginx" , "id" , 1 ), "nv" ),
995+ ),
996+ wantOutput : typed .YAMLObject (`{"list":[{"key":"nginx","id":1,"nv":2}]}` ),
997+ },
998+ {
999+ // extract multiple items with composite keys
1000+ object : `{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1,"nv":4}]}` ,
1001+ set : _NS (
1002+ _P ("list" , _KBF ("key" , "a" , "id" , 1 )),
1003+ _P ("list" , _KBF ("key" , "b" , "id" , 1 )),
1004+ ),
1005+ wantOutput : typed .YAMLObject (`{"list":[{"key":"a","id":1},{"key":"b","id":1}]}` ),
1006+ },
1007+ },
1008+ }, {
1009+ name : "nestedListsPartialExtraction" ,
1010+ rootTypeName : "type" ,
1011+ schema : typed .YAMLObject (nestedTypesSchema ),
1012+ triplets : []extractTriplet {
1013+ {
1014+ // extract single field from nested list
1015+ object : `{"listOfMaps":[{"name":"a","value":{"x":"1","y":"2"}},{"name":"b","value":{"z":"3"}}]}` ,
1016+ set : _NS (
1017+ _P ("listOfMaps" , _KBF ("name" , "a" ), "value" , "x" ),
1018+ ),
1019+ wantOutput : typed .YAMLObject (`{"listOfMaps":[{"name":"a","value":{"x":"1"}}]}` ),
1020+ },
1021+ {
1022+ // extract from deeply nested structure
1023+ object : `{"mapOfMapsRecursive":{"a":{"b":{"c":null,"d":{"e":null}}}}}` ,
1024+ set : _NS (
1025+ _P ("mapOfMapsRecursive" , "a" , "b" , "c" ),
1026+ ),
1027+ wantOutput : typed .YAMLObject (`{"mapOfMapsRecursive":{"a":{"b":{"c":null}}}}` ),
1028+ },
1029+ },
9441030}}
9451031
9461032func (tt extractWithKeysTestCase ) test (t * testing.T ) {
0 commit comments