$match
This commit is contained in:
@@ -99,7 +99,6 @@ $match:
|
|||||||
metadata:
|
metadata:
|
||||||
name: myService
|
name: myService
|
||||||
```
|
```
|
||||||
* K8s paths: If `kind` and `metadata.name` are present, they are used as match keys.
|
|
||||||
* Ordering: Stream position is used to match documents.
|
* Ordering: Stream position is used to match documents.
|
||||||
|
|
||||||
## Merge Behavior
|
## Merge Behavior
|
||||||
|
|||||||
23
bkl.go
23
bkl.go
@@ -20,6 +20,7 @@ var (
|
|||||||
ErrInvalidDirective = fmt.Errorf("invalid directive (%w)", Err)
|
ErrInvalidDirective = fmt.Errorf("invalid directive (%w)", Err)
|
||||||
ErrInvalidType = fmt.Errorf("invalid type (%w)", Err)
|
ErrInvalidType = fmt.Errorf("invalid type (%w)", Err)
|
||||||
ErrMissingFile = fmt.Errorf("missing file (%w)", Err)
|
ErrMissingFile = fmt.Errorf("missing file (%w)", Err)
|
||||||
|
ErrNoMatchFound = fmt.Errorf("no document matched $match (%w)", Err)
|
||||||
ErrRequiredField = fmt.Errorf("required field not set (%w)", Err)
|
ErrRequiredField = fmt.Errorf("required field not set (%w)", Err)
|
||||||
ErrUnknownFormat = fmt.Errorf("unknown format (%w)", Err)
|
ErrUnknownFormat = fmt.Errorf("unknown format (%w)", Err)
|
||||||
|
|
||||||
@@ -90,6 +91,8 @@ func (p *Parser) MergePatch(index int, patch any) error {
|
|||||||
|
|
||||||
// MergeIndexBytes parses the supplied doc bytes as the format specified by ext
|
// MergeIndexBytes parses the supplied doc bytes as the format specified by ext
|
||||||
// (file extension), then calls [MergePatch()].
|
// (file extension), then calls [MergePatch()].
|
||||||
|
//
|
||||||
|
// index is taken as a hint but can be overridden by $match.
|
||||||
func (p *Parser) MergeIndexBytes(index int, doc []byte, ext string) error {
|
func (p *Parser) MergeIndexBytes(index int, doc []byte, ext string) error {
|
||||||
f, found := formatByExtension[ext]
|
f, found := formatByExtension[ext]
|
||||||
if !found {
|
if !found {
|
||||||
@@ -101,6 +104,26 @@ func (p *Parser) MergeIndexBytes(index int, doc []byte, ext string) error {
|
|||||||
return fmt.Errorf("%w / %w", err, ErrDecode)
|
return fmt.Errorf("%w / %w", err, ErrDecode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if patchMap, ok := CanonicalizeType(patch).(map[string]any); ok {
|
||||||
|
m, found := patchMap["$match"]
|
||||||
|
if found {
|
||||||
|
delete(patchMap, "$match")
|
||||||
|
|
||||||
|
index = -1
|
||||||
|
|
||||||
|
for i, doc := range p.docs {
|
||||||
|
if Match(doc, m) {
|
||||||
|
index = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if index == -1 {
|
||||||
|
return fmt.Errorf("%#v: %w", m, ErrNoMatchFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = p.MergePatch(index, patch)
|
err = p.MergePatch(index, patch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
7
tests/match-map/a.b.yaml
Normal file
7
tests/match-map/a.b.yaml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
$match:
|
||||||
|
b: 2
|
||||||
|
c: 3
|
||||||
|
---
|
||||||
|
$match:
|
||||||
|
a: 1
|
||||||
|
d: 4
|
||||||
3
tests/match-map/a.yaml
Normal file
3
tests/match-map/a.yaml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
a: 1
|
||||||
|
---
|
||||||
|
b: 2
|
||||||
1
tests/match-map/cmd
Normal file
1
tests/match-map/cmd
Normal file
@@ -0,0 +1 @@
|
|||||||
|
bkl -f yaml a.b.yaml
|
||||||
5
tests/match-map/expected
Normal file
5
tests/match-map/expected
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
a: 1
|
||||||
|
d: 4
|
||||||
|
---
|
||||||
|
b: 2
|
||||||
|
c: 3
|
||||||
Reference in New Issue
Block a user