Skip to content

Commit 4dfe52d

Browse files
committed
Sort by works on map
1 parent 08092fa commit 4dfe52d

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

examples/data1.yaml

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
# block comments come through
2-
person: # neither do comments on maps
3-
name: Mike Wazowski # comments on values appear
4-
pets:
5-
- cat # comments on array values appear
6-
- dog # comments on array values appear
7-
- things:
8-
- frog
9-
food: [pizza] # comments on arrays do not
10-
emptyArray: []
11-
emptyMap: []
1+
Foo: 3
2+
apple: 1
3+
bar: 2

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,4 @@ require (
2929
golang.org/x/sys v0.28.0 // indirect
3030
)
3131

32-
go 1.21.0
33-
34-
toolchain go1.22.5
32+
go 1.23.0

pkg/yqlib/candidate_node.go

+23
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,29 @@ func (n *CandidateNode) SetParent(parent *CandidateNode) {
198198
n.Parent = parent
199199
}
200200

201+
type ValueVisitor func(*CandidateNode) error
202+
203+
func (n *CandidateNode) VisitValues(visitor ValueVisitor) error {
204+
if n.Kind == MappingNode {
205+
for i := 1; i < len(n.Content); i = i + 2 {
206+
if err := visitor(n.Content[i]); err != nil {
207+
return err
208+
}
209+
}
210+
} else if n.Kind == SequenceNode {
211+
for i := 0; i < len(n.Content); i = i + 1 {
212+
if err := visitor(n.Content[i]); err != nil {
213+
return err
214+
}
215+
}
216+
}
217+
return nil
218+
}
219+
220+
func (n *CandidateNode) CanVisitValues() bool {
221+
return n.Kind == MappingNode || n.Kind == SequenceNode
222+
}
223+
201224
func (n *CandidateNode) AddKeyValueChild(rawKey *CandidateNode, rawValue *CandidateNode) (*CandidateNode, *CandidateNode) {
202225
key := rawKey.Copy()
203226
key.SetParent(n)

pkg/yqlib/operator_sort.go

+10-24
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,19 @@ func sortByOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
2626

2727
var sortableArray sortableNodeArray
2828

29-
if candidate.Kind == MappingNode {
30-
31-
sortableArray = make(sortableNodeArray, len(candidate.Content)/2)
32-
for i := 1; i < len(candidate.Content); i = i + 2 {
33-
34-
originalNode := candidate.Content[i]
35-
compareContext, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(originalNode), expressionNode.RHS)
29+
if candidate.CanVisitValues() {
30+
sortableArray = make(sortableNodeArray, 0)
31+
visitor := func(valueNode *CandidateNode) error {
32+
compareContext, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(valueNode), expressionNode.RHS)
3633
if err != nil {
37-
return Context{}, err
34+
return err
3835
}
39-
40-
sortableArray[i/2] = sortableNode{Node: originalNode, CompareContext: compareContext, dateTimeLayout: context.GetDateTimeLayout()}
41-
36+
sortableNode := sortableNode{Node: valueNode, CompareContext: compareContext, dateTimeLayout: context.GetDateTimeLayout()}
37+
sortableArray = append(sortableArray, sortableNode)
38+
return nil
4239
}
43-
44-
} else if candidate.Kind == SequenceNode {
45-
sortableArray = make(sortableNodeArray, len(candidate.Content))
46-
47-
for i, originalNode := range candidate.Content {
48-
49-
compareContext, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(originalNode), expressionNode.RHS)
50-
if err != nil {
51-
return Context{}, err
52-
}
53-
54-
sortableArray[i] = sortableNode{Node: originalNode, CompareContext: compareContext, dateTimeLayout: context.GetDateTimeLayout()}
55-
40+
if err := candidate.VisitValues(visitor); err != nil {
41+
return context, err
5642
}
5743
} else {
5844
return context, fmt.Errorf("node at path [%v] is not an array or map (it's a %v)", candidate.GetNicePath(), candidate.Tag)

0 commit comments

Comments
 (0)