$output: true in lists
This commit is contained in:
@@ -859,6 +859,19 @@ $ <cmd>bkl</cmd> <focus><string>c.d.yaml</string></focus> # a.yaml + a.b.yaml
|
||||
<code><key>a</key>: <number>1</number>
|
||||
<key>b</key>: <number>1</number></code>
|
||||
|
||||
<vSpace class="span3"></vSpace>
|
||||
|
||||
<code><key>foo</key>:
|
||||
<key>bar</key>:
|
||||
- <focus><key>$output</key>: <bool>true</bool></focus>
|
||||
- <key>a</key>: <number>1</number>
|
||||
- <key>b</key>: <number>2</number></code>
|
||||
|
||||
<op>=</op>
|
||||
|
||||
<code>- <key>a</key>: <number>1</number>
|
||||
- <key>b</key>: <number>1</number></code>
|
||||
|
||||
</split3a>
|
||||
|
||||
<vSpace></vSpace>
|
||||
|
||||
@@ -5,38 +5,67 @@ import (
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
func findOutputs(obj any) []any {
|
||||
func findOutputs(obj any) (any, []any) {
|
||||
switch objType := obj.(type) {
|
||||
case map[string]any:
|
||||
ret := []any{}
|
||||
return findOutputsMap(objType)
|
||||
|
||||
if v, found := objType["$output"]; found {
|
||||
case []any:
|
||||
return findOutputsList(objType)
|
||||
|
||||
default:
|
||||
return obj, []any{}
|
||||
}
|
||||
}
|
||||
|
||||
func findOutputsMap(obj map[string]any) (any, []any) {
|
||||
ret := map[string]any{}
|
||||
outs := []any{}
|
||||
|
||||
keys := maps.Keys(obj)
|
||||
slices.Sort(keys)
|
||||
|
||||
for _, k := range keys {
|
||||
v := obj[k]
|
||||
|
||||
if k == "$output" {
|
||||
if v2, ok := v.(bool); ok && v2 {
|
||||
delete(objType, "$output")
|
||||
|
||||
ret = append(ret, obj)
|
||||
outs = append(outs, ret)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
keys := maps.Keys(objType)
|
||||
slices.Sort(keys)
|
||||
|
||||
for _, k := range keys {
|
||||
ret = append(ret, findOutputs(objType[k])...)
|
||||
}
|
||||
|
||||
return ret
|
||||
|
||||
case []any:
|
||||
ret := []any{}
|
||||
|
||||
for _, v := range objType {
|
||||
ret = append(ret, findOutputs(v)...)
|
||||
}
|
||||
|
||||
return ret
|
||||
|
||||
default:
|
||||
return []any{}
|
||||
vNew, subOuts := findOutputs(v)
|
||||
outs = append(outs, subOuts...)
|
||||
ret[k] = vNew
|
||||
}
|
||||
|
||||
return ret, outs
|
||||
}
|
||||
|
||||
func findOutputsList(obj []any) (any, []any) {
|
||||
ret := []any{}
|
||||
outs := []any{}
|
||||
output := false
|
||||
|
||||
for _, v := range obj {
|
||||
if vMap, ok := v.(map[string]any); ok {
|
||||
if v2, found := vMap["$output"]; found {
|
||||
if v3, ok := v2.(bool); ok && v3 {
|
||||
output = true
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vNew, subOuts := findOutputs(v)
|
||||
outs = append(outs, subOuts...)
|
||||
ret = append(ret, vNew)
|
||||
}
|
||||
|
||||
if output {
|
||||
outs = append(outs, any(ret))
|
||||
}
|
||||
|
||||
return ret, outs
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ func (p *Parser) OutputIndex(index int, ext string) ([][]byte, error) {
|
||||
return [][]byte{}, nil
|
||||
}
|
||||
|
||||
outs := findOutputs(obj)
|
||||
obj, outs := findOutputs(obj)
|
||||
if len(outs) == 0 {
|
||||
outs = append(outs, obj)
|
||||
}
|
||||
|
||||
+1
-1
@@ -116,7 +116,7 @@ func processMap(root any, obj map[string]any) (any, bool, error) {
|
||||
func processList(root any, obj []any) (any, bool, error) {
|
||||
ret := []any{}
|
||||
|
||||
// TODO: Support $merge, $replace, $output: true
|
||||
// TODO: Support $merge, $replace
|
||||
|
||||
encode := ""
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
foo:
|
||||
bar:
|
||||
- $output: true
|
||||
- a: 1
|
||||
- b: 2
|
||||
@@ -0,0 +1 @@
|
||||
bkl -f yaml a.yaml
|
||||
@@ -0,0 +1,2 @@
|
||||
- a: 1
|
||||
- b: 2
|
||||
Reference in New Issue
Block a user