-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
func_first.go
34 lines (28 loc) · 706 Bytes
/
func_first.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package dasel
import (
"fmt"
"reflect"
)
var FirstFunc = BasicFunction{
name: "first",
runFn: func(c *Context, s *Step, args []string) (Values, error) {
if err := requireNoArgs("first", args); err != nil {
return nil, err
}
input := s.inputs()
res := make(Values, 0)
for _, val := range input {
switch val.Kind() {
case reflect.Slice, reflect.Array:
if val.Len() == 0 {
return nil, fmt.Errorf("index out of range: %w", &ErrIndexNotFound{Index: 0})
}
value := val.Index(0)
res = append(res, value)
default:
return nil, fmt.Errorf("cannot use first selector on non slice/array types: %w", &ErrIndexNotFound{Index: 0})
}
}
return res, nil
},
}