This commit is contained in:
Ian Gulliver
2023-07-04 21:05:32 +01:00
parent 2f811d96b5
commit 9a5ea79f8a
6 changed files with 39 additions and 1 deletions

View File

@@ -99,7 +99,6 @@ $match:
metadata:
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.
## Merge Behavior

23
bkl.go
View File

@@ -20,6 +20,7 @@ var (
ErrInvalidDirective = fmt.Errorf("invalid directive (%w)", Err)
ErrInvalidType = fmt.Errorf("invalid type (%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)
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
// (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 {
f, found := formatByExtension[ext]
if !found {
@@ -101,6 +104,26 @@ func (p *Parser) MergeIndexBytes(index int, doc []byte, ext string) error {
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)
if err != nil {
return err

7
tests/match-map/a.b.yaml Normal file
View File

@@ -0,0 +1,7 @@
$match:
b: 2
c: 3
---
$match:
a: 1
d: 4

3
tests/match-map/a.yaml Normal file
View File

@@ -0,0 +1,3 @@
a: 1
---
b: 2

1
tests/match-map/cmd Normal file
View File

@@ -0,0 +1 @@
bkl -f yaml a.b.yaml

5
tests/match-map/expected Normal file
View File

@@ -0,0 +1,5 @@
a: 1
d: 4
---
b: 2
c: 3