Skip to content

Commit 2a1862d

Browse files
committed
fix nit
1 parent bbe53c2 commit 2a1862d

File tree

2 files changed

+7
-52
lines changed

2 files changed

+7
-52
lines changed

api/filters/replacement/replacement_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4511,6 +4511,8 @@ func TestValueInlineStructuredData(t *testing.T) {
45114511
kind: ConfigMap
45124512
metadata:
45134513
name: target-configmap
4514+
annotations:
4515+
hostname: www.example.com
45144516
data:
45154517
config.json: |-
45164518
{
@@ -4524,7 +4526,7 @@ data:
45244526
- source:
45254527
kind: ConfigMap
45264528
name: target-configmap
4527-
fieldPath: metadata.name
4529+
fieldPath: metadata.annotations.hostname
45284530
targets:
45294531
- select:
45304532
kind: ConfigMap
@@ -4535,11 +4537,13 @@ data:
45354537
kind: ConfigMap
45364538
metadata:
45374539
name: target-configmap
4540+
annotations:
4541+
hostname: www.example.com
45384542
data:
45394543
config.json: |-
45404544
{
45414545
"config": {
4542-
"hostname": "target-configmap",
4546+
"hostname": "www.example.com",
45434547
"id": "42"
45444548
}
45454549
}`,

kyaml/yaml/match.go

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// PathMatcher returns all RNodes matching the path wrapped in a SequenceNode.
17-
// Lists may have multiple elements matching the path, and each matching element
17+
// Lists may have multiple elements matching the pafunc cleanPath(path []string) []string {g element
1818
// is added to the return result.
1919
// If Path points to a SequenceNode, the SequenceNode is wrapped in another SequenceNode
2020
// If Path does not contain any lists, the result is still wrapped in a SequenceNode of len == 1
@@ -138,12 +138,6 @@ func (p *PathMatcher) doField(rn *RNode) (*RNode, error) {
138138
// lookup the field
139139
field, err := rn.Pipe(Get(p.Path[0]))
140140
if err != nil {
141-
// check error is an invalid kind error
142-
invalidKindErr := &InvalidNodeKindError{}
143-
if errors.As(err, &invalidKindErr) {
144-
// if the field is valid json or yaml, continue to lookup the next part of the path
145-
fmt.Print("-----------------------------\nOUTPUT: ", err)
146-
}
147141
return nil, err
148142
}
149143

@@ -347,25 +341,6 @@ func cleanPath(path []string) []string {
347341
return p
348342
}
349343

350-
// structuredDataNode is a wrapper around RNode that handles serialization back to scalar fields
351-
// when the underlying structured data is modified
352-
type structuredDataNode struct {
353-
RNode
354-
structuredRoot *RNode // The root of the structured data
355-
scalarField *RNode // The original scalar field containing the structured data
356-
}
357-
358-
// Override SetYNode to handle structured data serialization
359-
func (s *structuredDataNode) SetYNode(node *yaml.Node) {
360-
// Call the original SetYNode
361-
s.RNode.SetYNode(node)
362-
363-
// Serialize the modified structured data back to the scalar field
364-
if modifiedData, err := s.structuredRoot.String(); err == nil {
365-
s.scalarField.YNode().Value = strings.TrimSpace(modifiedData)
366-
}
367-
}
368-
369344
// handleStructuredDataInScalar processes a scalar field that contains structured data (JSON/YAML)
370345
// and allows path navigation within that structured data
371346
func (p *PathMatcher) handleStructuredDataInScalar(scalarField *RNode) (*RNode, error) {
@@ -386,29 +361,5 @@ func (p *PathMatcher) handleStructuredDataInScalar(scalarField *RNode) (*RNode,
386361
}
387362
p.Matches = pm.Matches
388363

389-
// For structured data, we need to create a special result node that knows how to
390-
// serialize back to the original scalar when modified
391-
if result != nil && len(result.YNode().Content) > 0 {
392-
// Wrap the result with structured data context
393-
for _, node := range result.YNode().Content {
394-
wrappedNode := NewRNode(node)
395-
// Add a custom method to handle setting values back to structured data
396-
wrappedNode = p.wrapWithStructuredDataHandler(wrappedNode, structuredField, scalarField)
397-
result.YNode().Content[0] = wrappedNode.YNode()
398-
}
399-
}
400-
401364
return result, nil
402365
}
403-
404-
// wrapWithStructuredDataHandler creates a wrapper that will serialize structured data back
405-
// to the scalar field when the node is modified
406-
func (p *PathMatcher) wrapWithStructuredDataHandler(targetNode, structuredRoot, scalarField *RNode) *RNode {
407-
// Create a custom node that overrides SetYNode to handle structured data serialization
408-
wrapper := &structuredDataNode{
409-
RNode: *targetNode,
410-
structuredRoot: structuredRoot,
411-
scalarField: scalarField,
412-
}
413-
return &wrapper.RNode
414-
}

0 commit comments

Comments
 (0)