Evaluate $output: false after $merge/$replace
This commit is contained in:
63
output.go
63
output.go
@@ -5,12 +5,12 @@ import (
|
||||
)
|
||||
|
||||
func findOutputs(obj any) (any, []any, error) {
|
||||
switch objType := obj.(type) {
|
||||
switch obj2 := obj.(type) {
|
||||
case map[string]any:
|
||||
return findOutputsMap(objType)
|
||||
return findOutputsMap(obj2)
|
||||
|
||||
case []any:
|
||||
return findOutputsList(objType)
|
||||
return findOutputsList(obj2)
|
||||
|
||||
default:
|
||||
return obj, []any{}, nil
|
||||
@@ -69,3 +69,60 @@ func findOutputsList(obj []any) (any, []any, error) {
|
||||
|
||||
return ret, outs, nil
|
||||
}
|
||||
|
||||
func filterOutput(obj any) (any, error) {
|
||||
switch obj2 := obj.(type) {
|
||||
case map[string]any:
|
||||
return filterOutputMap(obj2)
|
||||
|
||||
case []any:
|
||||
return filterOutputList(obj2)
|
||||
|
||||
default:
|
||||
return obj, nil
|
||||
}
|
||||
}
|
||||
|
||||
func filterOutputMap(obj map[string]any) (any, error) {
|
||||
output, obj := popMapBoolValue(obj, "$output", false)
|
||||
if output {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return filterMap(obj, func(k string, v any) (map[string]any, error) {
|
||||
v2, err := filterOutput(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if v2 == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return map[string]any{k: v2}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func filterOutputList(obj []any) (any, error) {
|
||||
output, obj, err := popListMapBoolValue(obj, "$output", false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if output {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return filterList(obj, func(v any) ([]any, error) {
|
||||
v2, err := filterOutput(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if v2 == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return []any{v2}, nil
|
||||
})
|
||||
}
|
||||
|
||||
23
parser.go
23
parser.go
@@ -39,12 +39,12 @@ import (
|
||||
//
|
||||
// Output phase 1 (process)
|
||||
// - $merge
|
||||
// - $replace: map
|
||||
// - $replace: string
|
||||
// - $output: false
|
||||
// - $encode
|
||||
//
|
||||
// Output phase 2 (output)
|
||||
// - $output: true
|
||||
// - $output
|
||||
type Parser struct {
|
||||
docs []any
|
||||
debug bool
|
||||
@@ -240,7 +240,24 @@ func (p *Parser) OutputDocumentsIndex(index int) ([]any, error) {
|
||||
outs = append(outs, obj)
|
||||
}
|
||||
|
||||
err = validate(obj)
|
||||
outs, err = filterList(outs, func(v any) ([]any, error) {
|
||||
v2, err := filterOutput(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if v2 == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
err = validate(v2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []any{v2}, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -44,11 +44,6 @@ func processMap(obj map[string]any, mergeFrom any, mergeFromDocs []any, depth in
|
||||
return processMapReplace(mergeFrom, mergeFromDocs, m, depth)
|
||||
}
|
||||
|
||||
output, found := getMapBoolValue(obj, "$output")
|
||||
if found && !output {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
encode := getMapStringValue(obj, "$encode")
|
||||
if encode != "" {
|
||||
delete(obj, "$encode")
|
||||
@@ -138,10 +133,6 @@ func processList(obj []any, mergeFrom any, mergeFromDocs []any, depth int) (any,
|
||||
return processListReplace(mergeFrom, mergeFromDocs, m, depth)
|
||||
}
|
||||
|
||||
if hasListMapBoolValue(obj, "$output", false) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
encode, obj, err := popListMapStringValue(obj, "$encode")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
5
tests/process-replace-before-output/a.yaml
Normal file
5
tests/process-replace-before-output/a.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
a:
|
||||
b: 2
|
||||
$output: false
|
||||
c:
|
||||
$replace: a.b
|
||||
1
tests/process-replace-before-output/cmd
Normal file
1
tests/process-replace-before-output/cmd
Normal file
@@ -0,0 +1 @@
|
||||
bkl a.yaml
|
||||
1
tests/process-replace-before-output/expected
Normal file
1
tests/process-replace-before-output/expected
Normal file
@@ -0,0 +1 @@
|
||||
c: 2
|
||||
7
util.go
7
util.go
@@ -233,7 +233,12 @@ func popListMapStringValue(l []any, k string) (string, []any, error) {
|
||||
func filterMap(m map[string]any, filter func(string, any) (map[string]any, error)) (map[string]any, error) {
|
||||
ret := map[string]any{}
|
||||
|
||||
for k, v := range m {
|
||||
ks := polyfill.MapsKeys(m)
|
||||
polyfill.SlicesSort(ks)
|
||||
|
||||
for _, k := range ks {
|
||||
v := m[k]
|
||||
|
||||
m2, err := filter(k, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user