|
| 1 | +// Copyright 2024 Peter Olds <[email protected]> |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package functions |
| 16 | + |
| 17 | +import ( |
| 18 | + "cmp" |
| 19 | + "fmt" |
| 20 | + "reflect" |
| 21 | + "slices" |
| 22 | + "sort" |
| 23 | + |
| 24 | + "github.com/expr-lang/expr" |
| 25 | +) |
| 26 | + |
| 27 | +// IsSorted provides the isSorted function as an Expr function. It will verify that the provided type |
| 28 | +// is sorted ascending. It supports the following types: |
| 29 | +// - Injected types that support the sort.Interface |
| 30 | +// - []int |
| 31 | +// - []float64 |
| 32 | +// - []string |
| 33 | +// |
| 34 | +// Usage: |
| 35 | +// |
| 36 | +// // Inject into your environment. |
| 37 | +// _, err := expr.Compile(`foo`, expr.Env(nil), functions.ExprIsSorted()) |
| 38 | +// |
| 39 | +// Expression: |
| 40 | +// |
| 41 | +// isSorted([1, 2, 3]) |
| 42 | +// isSorted(["a", "b", "c"]) |
| 43 | +// isSorted([1.0, 2.0, 3.0]) |
| 44 | +// isSorted(myCustomType) // myCustomType must implement sort.Interface |
| 45 | +func IsSorted() expr.Option { |
| 46 | + return expr.Function("isSorted", func(params ...any) (any, error) { |
| 47 | + if len(params) != 1 { |
| 48 | + return false, fmt.Errorf("expected one parameter, got %d", len(params)) |
| 49 | + } |
| 50 | + return isSorted(params[0]) |
| 51 | + }, |
| 52 | + new(func(sort.Interface) (bool, error)), |
| 53 | + new(func([]any) (bool, error)), |
| 54 | + new(func([]int) (bool, error)), |
| 55 | + new(func([]float64) (bool, error)), |
| 56 | + new(func([]string) (bool, error)), |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +// isSorted attempts to determine if v is sortable, first by determine if it satisfies the sort.Interface interface, |
| 61 | +// then by checking if it is a slice of a sortable type. If the type is a slice of type []any pass it to the |
| 62 | +// isSliceSorted method which builds a new slice of the correct type and validates that it is sorted. |
| 63 | +func isSorted(v any) (any, error) { |
| 64 | + if v == nil { |
| 65 | + return false, nil |
| 66 | + } |
| 67 | + |
| 68 | + switch t := v.(type) { |
| 69 | + case sort.Interface: |
| 70 | + return sort.IsSorted(t), nil |
| 71 | + |
| 72 | + // There are cases where Expr is passing around an []any instead of a []int, []float64, or []string. |
| 73 | + // This logic will attempt to do its own sorting to determine if the slice is sorted. |
| 74 | + case []any: |
| 75 | + return isSliceSorted(t) |
| 76 | + case []int: |
| 77 | + return slices.IsSorted(t), nil |
| 78 | + case []float64: |
| 79 | + return slices.IsSorted(t), nil |
| 80 | + case []string: |
| 81 | + return slices.IsSorted(t), nil |
| 82 | + } |
| 83 | + return false, fmt.Errorf("type %s is not sortable", reflect.TypeOf(v)) |
| 84 | +} |
| 85 | + |
| 86 | +func convertTo[E cmp.Ordered](x any) (E, error) { |
| 87 | + var r E |
| 88 | + v, ok := x.(E) |
| 89 | + if !ok { |
| 90 | + return r, fmt.Errorf("mis-typed slice, expected %T, got %T", r, x) |
| 91 | + } |
| 92 | + return v, nil |
| 93 | +} |
| 94 | + |
| 95 | +func less[E cmp.Ordered](vv []any) (bool, error) { |
| 96 | + for i := len(vv) - 1; i > 0; i-- { |
| 97 | + l, err := convertTo[E](vv[i-1]) |
| 98 | + if err != nil { |
| 99 | + return false, err |
| 100 | + } |
| 101 | + h, err := convertTo[E](vv[i]) |
| 102 | + if err != nil { |
| 103 | + return false, err |
| 104 | + } |
| 105 | + if cmp.Less(h, l) { |
| 106 | + return false, nil |
| 107 | + } |
| 108 | + } |
| 109 | + return true, nil |
| 110 | +} |
| 111 | + |
| 112 | +// isSliceSorted attempts to determine if v is a slice of a sortable type. |
| 113 | +// Instead of building a slice it just walks the slice and validates that it is sorted. The first unsorted element |
| 114 | +// causes the function to return false. |
| 115 | +// Expr only supports int, float, and string types. |
| 116 | +func isSliceSorted(vv []any) (bool, error) { |
| 117 | + // We have to peek the first element to determine the type of the slice. |
| 118 | + switch t := vv[0].(type) { |
| 119 | + case int: |
| 120 | + return less[int](vv) |
| 121 | + case float64: |
| 122 | + return less[float64](vv) |
| 123 | + case string: |
| 124 | + return less[string](vv) |
| 125 | + default: |
| 126 | + return false, fmt.Errorf("unsupported type %T", t) |
| 127 | + } |
| 128 | +} |
0 commit comments