File: key.go

package info (click to toggle)
golang-github-caspr-io-yamlpath 0.0~git20200722.502e8d1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116 kB
  • sloc: makefile: 11
file content (38 lines) | stat: -rw-r--r-- 602 bytes parent folder | download
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
35
36
37
38
package segments

import (
	"fmt"
)

type Key struct {
	key string
}

func ParseKeySegment(s string) (YamlPathSegment, error) {
	return &Key{
		key: s,
	}, nil
}

func (s *Key) NavigateMap(m map[string]interface{}) (interface{}, error) {
	if v, ok := m[s.key]; ok {
		return v, nil
	}

	return nil, fmt.Errorf("could not find key '%s' in yaml", s.key)
}

func (s *Key) NavigateArray(l []interface{}) (interface{}, error) {
	result := []interface{}{}

	for _, v := range l {
		r, err := NavigateYaml(v, s)
		if err != nil {
			return nil, err
		}

		result = append(result, r)
	}

	return result, nil
}