2021-05-09 03:59:23 +00:00
package yqlib
import (
"testing"
)
var entriesOperatorScenarios = [ ] expressionScenario {
2023-12-12 04:44:34 +00:00
{
description : "to_entries splat" ,
skipDoc : true ,
document : ` { a: 1, b: 2} ` ,
expression : ` to_entries[] ` ,
expected : [ ] string {
"D0, P[0], (!!map)::key: a\nvalue: 1\n" ,
"D0, P[1], (!!map)::key: b\nvalue: 2\n" ,
} ,
} ,
2023-11-30 03:04:54 +00:00
{
description : "to_entries, delete key" ,
skipDoc : true ,
document : ` { a: 1, b: 2} ` ,
expression : ` to_entries | map(del(.key)) ` ,
expected : [ ] string {
"D0, P[], (!!seq)::- value: 1\n- value: 2\n" ,
} ,
} ,
2021-05-09 03:59:23 +00:00
{
description : "to_entries Map" ,
2021-05-09 05:12:50 +00:00
document : ` { a: 1, b: 2} ` ,
expression : ` to_entries ` ,
2021-05-09 03:59:23 +00:00
expected : [ ] string {
"D0, P[], (!!seq)::- key: a\n value: 1\n- key: b\n value: 2\n" ,
} ,
} ,
{
description : "to_entries Array" ,
2021-05-09 05:12:50 +00:00
document : ` [a, b] ` ,
expression : ` to_entries ` ,
2021-05-09 03:59:23 +00:00
expected : [ ] string {
"D0, P[], (!!seq)::- key: 0\n value: a\n- key: 1\n value: b\n" ,
} ,
} ,
2021-05-10 00:42:43 +00:00
{
description : "to_entries null" ,
document : ` null ` ,
expression : ` to_entries ` ,
2021-05-14 05:01:44 +00:00
expected : [ ] string { } ,
2021-05-10 00:42:43 +00:00
} ,
2021-05-09 04:18:25 +00:00
{
description : "from_entries map" ,
2021-05-09 05:12:50 +00:00
document : ` { a: 1, b: 2} ` ,
expression : ` to_entries | from_entries ` ,
2021-05-09 04:18:25 +00:00
expected : [ ] string {
"D0, P[], (!!map)::a: 1\nb: 2\n" ,
} ,
} ,
{
2023-03-16 02:39:36 +00:00
description : "from_entries with numeric key indices" ,
2021-05-09 04:18:25 +00:00
subdescription : "from_entries always creates a map, even for numeric keys" ,
2021-05-09 05:12:50 +00:00
document : ` [a,b] ` ,
expression : ` to_entries | from_entries ` ,
2021-05-09 04:18:25 +00:00
expected : [ ] string {
"D0, P[], (!!map)::0: a\n1: b\n" ,
} ,
} ,
2021-05-09 05:12:50 +00:00
{
description : "Use with_entries to update keys" ,
document : ` { a: 1, b: 2} ` ,
2024-02-10 23:25:38 +00:00
// expression: `to_entries | with(.[]; .key |= "KEY_" + .) | from_entries`,
expression : ` with_entries(.key |= "KEY_" + .) ` ,
2021-05-09 05:12:50 +00:00
expected : [ ] string {
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n" ,
} ,
} ,
2025-03-03 04:10:42 +00:00
{
description : "Use with_entries to update keys recursively" ,
document : ` { a: 1, b: { b_a: nested, b_b: thing}} ` ,
expression : ` (.. | select(tag=="!!map")) |= with_entries(.key |= "KEY_" + .) ` ,
subdescription : "We use (.. | select(tag=\"map\")) to find all the maps in the doc, then |= to update each one of those maps. In the update, with_entries is used." ,
expected : [ ] string {
"D0, P[], (!!map)::{KEY_a: 1, KEY_b: {KEY_b_a: nested, KEY_b_b: thing}}\n" ,
} ,
} ,
2024-02-10 23:25:38 +00:00
{
skipDoc : true ,
description : "Use with_entries to update keys comment" ,
document : ` { a: 1, b: 2} ` ,
expression : ` with_entries(.key headComment= .value) ` ,
expected : [ ] string {
"D0, P[], (!!map)::# 1\na: 1\n# 2\nb: 2\n" ,
} ,
} ,
2022-10-17 04:03:47 +00:00
{
description : "Custom sort map keys" ,
subdescription : "Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back." ,
document : ` { a: 1, c: 3, b: 2} ` ,
expression : ` to_entries | sort_by(.key) | reverse | from_entries ` ,
expected : [ ] string {
"D0, P[], (!!map)::c: 3\nb: 2\na: 1\n" ,
} ,
} ,
2021-11-30 02:19:30 +00:00
{
skipDoc : true ,
document : ` { a: 1, b: 2} ` ,
document2 : ` { c: 1, d: 2} ` ,
expression : ` with_entries(.key |= "KEY_" + .) ` ,
expected : [ ] string {
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n" ,
"D0, P[], (!!map)::KEY_c: 1\nKEY_d: 2\n" ,
} ,
} ,
2022-05-31 06:28:53 +00:00
{
skipDoc : true ,
document : ` [ { a: 1, b: 2}, { c: 1, d: 2}] ` ,
expression : ` .[] | with_entries(.key |= "KEY_" + .) ` ,
expected : [ ] string {
"D0, P[], (!!map)::KEY_a: 1\nKEY_b: 2\n" ,
"D0, P[], (!!map)::KEY_c: 1\nKEY_d: 2\n" ,
} ,
} ,
2021-06-07 15:58:30 +00:00
{
2021-06-08 11:50:14 +00:00
description : "Use with_entries to filter the map" ,
document : ` { a: { b: bird }, c: { d: dog }} ` ,
expression : ` with_entries(select(.value | has("b"))) ` ,
2021-06-07 15:58:30 +00:00
expected : [ ] string {
2021-06-08 11:50:14 +00:00
"D0, P[], (!!map)::a: {b: bird}\n" ,
2021-06-07 15:58:30 +00:00
} ,
} ,
2022-05-31 06:28:53 +00:00
{
description : "Use with_entries to filter the map; head comment" ,
skipDoc : true ,
document : "# abc\n{a: { b: bird }, c: { d: dog }}\n# xyz" ,
expression : ` with_entries(select(.value | has("b"))) ` ,
expected : [ ] string {
"D0, P[], (!!map)::# abc\na: {b: bird}\n# xyz\n" ,
} ,
} ,
2021-05-09 03:59:23 +00:00
}
func TestEntriesOperatorScenarios ( t * testing . T ) {
for _ , tt := range entriesOperatorScenarios {
testScenario ( t , & tt )
}
2021-12-21 04:02:07 +00:00
documentOperatorScenarios ( t , "entries" , entriesOperatorScenarios )
2021-05-09 03:59:23 +00:00
}