@@ -24,30 +24,55 @@ func sortByOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
24
24
for el := context .MatchingNodes .Front (); el != nil ; el = el .Next () {
25
25
candidate := el .Value .(* CandidateNode )
26
26
27
- if candidate .Kind != SequenceNode {
28
- return context , fmt .Errorf ("node at path [%v] is not an array (it's a %v)" , candidate .GetNicePath (), candidate .Tag )
29
- }
27
+ var sortableArray sortableNodeArray
28
+
29
+ if candidate .Kind == MappingNode {
30
+
31
+ sortableArray = make (sortableNodeArray , len (candidate .Content )/ 2 )
32
+ log .Warningf ("Sorting map: %v" , NodeToString (candidate ))
33
+ for i := 1 ; i < len (candidate .Content ); i = i + 2 {
30
34
31
- sortableArray := make (sortableNodeArray , len (candidate .Content ))
35
+ originalNode := candidate .Content [i ]
36
+ compareContext , err := d .GetMatchingNodes (context .SingleReadonlyChildContext (originalNode ), expressionNode .RHS )
37
+ if err != nil {
38
+ return Context {}, err
39
+ }
32
40
33
- for i , originalNode := range candidate . Content {
41
+ sortableArray [ i / 2 ] = sortableNode { Node : originalNode , CompareContext : compareContext , dateTimeLayout : context . GetDateTimeLayout ()}
34
42
35
- compareContext , err := d .GetMatchingNodes (context .SingleReadonlyChildContext (originalNode ), expressionNode .RHS )
36
- if err != nil {
37
- return Context {}, err
38
43
}
39
44
40
- sortableArray [i ] = sortableNode {Node : originalNode , CompareContext : compareContext , dateTimeLayout : context .GetDateTimeLayout ()}
45
+ } else if candidate .Kind == SequenceNode {
46
+ sortableArray = make (sortableNodeArray , len (candidate .Content ))
47
+
48
+ for i , originalNode := range candidate .Content {
49
+
50
+ compareContext , err := d .GetMatchingNodes (context .SingleReadonlyChildContext (originalNode ), expressionNode .RHS )
51
+ if err != nil {
52
+ return Context {}, err
53
+ }
54
+
55
+ sortableArray [i ] = sortableNode {Node : originalNode , CompareContext : compareContext , dateTimeLayout : context .GetDateTimeLayout ()}
41
56
57
+ }
58
+ } else {
59
+ return context , fmt .Errorf ("node at path [%v] is not an array or map (it's a %v)" , candidate .GetNicePath (), candidate .Tag )
42
60
}
43
61
44
62
sort .Stable (sortableArray )
45
63
46
- sortedList := candidate .CreateReplacementWithComments (SequenceNode , "!!seq" , candidate .Style )
47
-
48
- for _ , sortedNode := range sortableArray {
49
- sortedList .AddChild (sortedNode .Node )
64
+ sortedList := candidate .CopyWithoutContent ()
65
+ if candidate .Kind == MappingNode {
66
+ for _ , sortedNode := range sortableArray {
67
+ sortedList .AddKeyValueChild (sortedNode .Node .Key , sortedNode .Node )
68
+ }
69
+ } else if candidate .Kind == SequenceNode {
70
+ for _ , sortedNode := range sortableArray {
71
+ sortedList .AddChild (sortedNode .Node )
72
+ }
50
73
}
74
+
75
+ // convert array of value nodes back to map
51
76
results .PushBack (sortedList )
52
77
}
53
78
return context .ChildContext (results ), nil
0 commit comments