| 1 |
// Copyright 2017 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package collections |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"errors" |
| 19 |
"reflect" |
| 20 |
"sort" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/common/hmaps" |
| 24 |
"github.com/gohugoio/hugo/common/hreflect" |
| 25 |
"github.com/gohugoio/hugo/langs" |
| 26 |
"github.com/gohugoio/hugo/tpl/compare" |
| 27 |
"github.com/spf13/cast" |
| 28 |
) |
| 29 |
|
| 30 |
// Sort returns a sorted copy of the list l. |
| 31 |
func (ns *Namespace) Sort(ctx context.Context, l any, args ...any) (any, error) { |
| 32 |
if l == nil { |
| 33 |
return nil, errors.New("sequence must be provided") |
| 34 |
} |
| 35 |
|
| 36 |
seqv, isNil := hreflect.Indirect(reflect.ValueOf(l)) |
| 37 |
if isNil { |
| 38 |
return nil, errors.New("can't iterate over a nil value") |
| 39 |
} |
| 40 |
|
| 41 |
ctxv := reflect.ValueOf(ctx) |
| 42 |
|
| 43 |
var sliceType reflect.Type |
| 44 |
switch seqv.Kind() { |
| 45 |
case reflect.Array, reflect.Slice: |
| 46 |
sliceType = seqv.Type() |
| 47 |
case reflect.Map: |
| 48 |
sliceType = reflect.SliceOf(seqv.Type().Elem()) |
| 49 |
default: |
| 50 |
return nil, errors.New("can't sort " + reflect.ValueOf(l).Type().String()) |
| 51 |
} |
| 52 |
|
| 53 |
collator := langs.GetCollator1(ns.deps.Conf.Language().(*langs.Language)) |
| 54 |
|
| 55 |
// Create a list of pairs that will be used to do the sort |
| 56 |
p := pairList{Collator: collator, sortComp: ns.sortComp, SortAsc: true, SliceType: sliceType, |
| 57 |
Pairs: make([]pair, seqv.Len())} |
| 58 |
|
| 59 |
var sortByField string |
| 60 |
for i, l := range args { |
| 61 |
dStr, err := cast.ToStringE(l) |
| 62 |
switch { |
| 63 |
case i == 0 && err != nil: |
| 64 |
sortByField = "" |
| 65 |
case i == 0 && err == nil: |
| 66 |
sortByField = dStr |
| 67 |
case i == 1 && err == nil && dStr == "desc": |
| 68 |
p.SortAsc = false |
| 69 |
case i == 1: |
| 70 |
p.SortAsc = true |
| 71 |
} |
| 72 |
} |
| 73 |
path := strings.Split(strings.Trim(sortByField, "."), ".") |
| 74 |
|
| 75 |
switch seqv.Kind() { |
| 76 |
case reflect.Array, reflect.Slice: |
| 77 |
for i := range seqv.Len() { |
| 78 |
p.Pairs[i].Value = seqv.Index(i) |
| 79 |
if sortByField == "" || sortByField == "value" { |
| 80 |
p.Pairs[i].Key = p.Pairs[i].Value |
| 81 |
} else { |
| 82 |
v := p.Pairs[i].Value |
| 83 |
var err error |
| 84 |
for i, elemName := range path { |
| 85 |
v, err = evaluateSubElem(ctxv, v, elemName) |
| 86 |
if err != nil { |
| 87 |
return nil, err |
| 88 |
} |
| 89 |
if !v.IsValid() { |
| 90 |
continue |
| 91 |
} |
| 92 |
// Special handling of lower cased maps. |
| 93 |
if params, ok := v.Interface().(hmaps.Params); ok { |
| 94 |
v = reflect.ValueOf(params.GetNested(path[i+1:]...)) |
| 95 |
break |
| 96 |
} |
| 97 |
} |
| 98 |
p.Pairs[i].Key = v |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
case reflect.Map: |
| 103 |
|
| 104 |
iter := seqv.MapRange() |
| 105 |
i := 0 |
| 106 |
for iter.Next() { |
| 107 |
key := iter.Key() |
| 108 |
value := iter.Value() |
| 109 |
p.Pairs[i].Value = value |
| 110 |
if sortByField == "" { |
| 111 |
p.Pairs[i].Key = key |
| 112 |
} else if sortByField == "value" { |
| 113 |
p.Pairs[i].Key = p.Pairs[i].Value |
| 114 |
} else { |
| 115 |
v := p.Pairs[i].Value |
| 116 |
var err error |
| 117 |
for j, elemName := range path { |
| 118 |
v, err = evaluateSubElem(ctxv, v, elemName) |
| 119 |
if err != nil { |
| 120 |
return nil, err |
| 121 |
} |
| 122 |
if !v.IsValid() { |
| 123 |
continue |
| 124 |
} |
| 125 |
// Special handling of lower cased hmaps. |
| 126 |
if params, ok := v.Interface().(hmaps.Params); ok { |
| 127 |
v = reflect.ValueOf(params.GetNested(path[j+1:]...)) |
| 128 |
break |
| 129 |
} |
| 130 |
} |
| 131 |
p.Pairs[i].Key = v |
| 132 |
} |
| 133 |
i++ |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
collator.Lock() |
| 138 |
defer collator.Unlock() |
| 139 |
|
| 140 |
return p.sort(), nil |
| 141 |
} |
| 142 |
|
| 143 |
// Credit for pair sorting method goes to Andrew Gerrand |
| 144 |
// https://groups.google.com/forum/#!topic/golang-nuts/FT7cjmcL7gw |
| 145 |
// A data structure to hold a key/value pair. |
| 146 |
type pair struct { |
| 147 |
Key reflect.Value |
| 148 |
Value reflect.Value |
| 149 |
} |
| 150 |
|
| 151 |
// A slice of pairs that implements sort.Interface to sort by Value. |
| 152 |
type pairList struct { |
| 153 |
Collator *langs.Collator |
| 154 |
sortComp *compare.Namespace |
| 155 |
Pairs []pair |
| 156 |
SortAsc bool |
| 157 |
SliceType reflect.Type |
| 158 |
} |
| 159 |
|
| 160 |
func (p pairList) Swap(i, j int) { p.Pairs[i], p.Pairs[j] = p.Pairs[j], p.Pairs[i] } |
| 161 |
func (p pairList) Len() int { return len(p.Pairs) } |
| 162 |
func (p pairList) Less(i, j int) bool { |
| 163 |
iv := p.Pairs[i].Key |
| 164 |
jv := p.Pairs[j].Key |
| 165 |
|
| 166 |
if iv.IsValid() { |
| 167 |
if jv.IsValid() { |
| 168 |
// can only call Interface() on valid reflect Values |
| 169 |
return p.sortComp.LtCollate(p.Collator, iv.Interface(), jv.Interface()) |
| 170 |
} |
| 171 |
|
| 172 |
// if j is invalid, test i against i's zero value |
| 173 |
return p.sortComp.LtCollate(p.Collator, iv.Interface(), reflect.Zero(iv.Type())) |
| 174 |
} |
| 175 |
|
| 176 |
if jv.IsValid() { |
| 177 |
// if i is invalid, test j against j's zero value |
| 178 |
return p.sortComp.LtCollate(p.Collator, reflect.Zero(jv.Type()), jv.Interface()) |
| 179 |
} |
| 180 |
|
| 181 |
return false |
| 182 |
} |
| 183 |
|
| 184 |
// sorts a pairList and returns a slice of sorted values |
| 185 |
func (p pairList) sort() any { |
| 186 |
if p.SortAsc { |
| 187 |
sort.Stable(p) |
| 188 |
} else { |
| 189 |
sort.Stable(sort.Reverse(p)) |
| 190 |
} |
| 191 |
sorted := reflect.MakeSlice(p.SliceType, len(p.Pairs), len(p.Pairs)) |
| 192 |
for i, v := range p.Pairs { |
| 193 |
sorted.Index(i).Set(v.Value) |
| 194 |
} |
| 195 |
|
| 196 |
return sorted.Interface() |
| 197 |
} |