| 1 | package executionscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/ |
| 2 | |
| 3 | import ( |
| 4 | "contextscip-go gomod github.com/golang/go/src go1.25 context/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/" |
| 8 | "github.com/tomwright/dasel/v3/selector/astscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/selector/ast`/" |
| 9 | ) |
| 10 | |
| 11 | func groupByExprExecutorscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/groupByExprExecutor().(e astscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/selector/ast`/.GroupByExprscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/selector/ast`/GroupByExpr#) (expressionExecutorscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/expressionExecutor#, error) { |
| 12 | return func(ctx contextscip-go gomod github.com/golang/go/src go1.25 context/.Contextscip-go gomod github.com/golang/go/src go1.25 context/Context#, options *Optionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/Options#, data *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) (*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#, error) { |
| 13 | ctx = WithExecutorIDscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/WithExecutorID().(ctx, "groupByExpr") |
| 14 | if !data.IsSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsSlice().() { |
| 15 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("cannot group by on non-slice data") |
| 16 | } |
| 17 | |
| 18 | res := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewMapValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewMapValue().() |
| 19 | |
| 20 | if err := data.RangeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeSlice().(func(i int, item *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error { |
| 21 | restore := withKeyVarscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/withKeyVar().(options, modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewIntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewIntValue().(int64(i))) |
| 22 | defer restore() |
| 23 | |
| 24 | keyVal, err := ExecuteASTscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/ExecuteAST().(ctx, e.Exprscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/selector/ast`/GroupByExpr#Expr., item, options) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | |
| 29 | key, err := valueToStringscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/valueToString().(keyVal) |
| 30 | if err != nil { |
| 31 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("cannot use %s as group key: %w", keyVal.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().(), err) |
| 32 | } |
| 33 | |
| 34 | exists, err := res.MapKeyExistsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeyExists().(key) |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | |
| 39 | if exists { |
| 40 | group, err := res.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().(key) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | if err := group.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(item); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | } else { |
| 48 | group := modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.NewSliceValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/NewSliceValue().() |
| 49 | if err := group.Appendscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Append().(item); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | if err := res.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().(key, group); err != nil { |
| 53 | return err |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return nil |
| 58 | }); err != nil { |
| 59 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error ranging over slice: %w", err) |
| 60 | } |
| 61 | |
| 62 | return res, nil |
| 63 | }, nil |
| 64 | } |
| 65 | |
| 66 | func valueToStringscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/valueToString().(v *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) (string, error) { |
| 67 | switch v.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().() { |
| 68 | case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeStringscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeString.: |
| 69 | return v.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().() |
| 70 | case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeIntscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeInt.: |
| 71 | i, err := v.IntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IntValue().() |
| 72 | if err != nil { |
| 73 | return "", err |
| 74 | } |
| 75 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25 fmt/Sprintf().("%d", i), nil |
| 76 | case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeFloatscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeFloat.: |
| 77 | f, err := v.FloatValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#FloatValue().() |
| 78 | if err != nil { |
| 79 | return "", err |
| 80 | } |
| 81 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25 fmt/Sprintf().("%g", f), nil |
| 82 | case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeBoolscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeBool.: |
| 83 | b, err := v.BoolValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#BoolValue().() |
| 84 | if err != nil { |
| 85 | return "", err |
| 86 | } |
| 87 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25 fmt/Sprintf().("%v", b), nil |
| 88 | default: |
| 89 | return "", fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("cannot convert %s to string for use as group key", v.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().()) |
| 90 | } |
| 91 | } |