File: get_paths.go

package info (click to toggle)
golang-github-smallfish-simpleyaml 0.0~git20170911.a320310-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 80 kB
  • sloc: makefile: 2
file content (62 lines) | stat: -rw-r--r-- 1,486 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package util

import (
	"errors"
	"github.com/smallfish/simpleyaml"
  "strconv"
  "bytes"
)

var ArrayOfPaths = make([]string, 0)

func GetAllExistingPaths(y *simpleyaml.Yaml, PathSlice []string) ([]string, error) {
	if y.IsMap() {
		keys, err := y.GetMapKeys()
		if err != nil {
			return nil, errors.New("Retrieving map keys failed")
		}
		for k, _ := range keys {
			if k != 0 {
				PathSlice = PathSlice[:len(PathSlice)-1]
			}
			PathSlice = append(PathSlice, keys[k])
			GetAllExistingPaths(y.Get(keys[k]), PathSlice)
		}
	} else if y.IsArray() {
		arr, err := y.Array()
		if err != nil {
			return nil, errors.New("Retrieving array failed")
		}
		for k, _ := range arr {
			if k != 0 {
				PathSlice = PathSlice[:len(PathSlice)-1]
			}
			PathSlice = append(PathSlice, strconv.Itoa(k))
			GetAllExistingPaths(y.GetIndex(k), PathSlice)
		}
	} else {
		var buffer bytes.Buffer
		for k, _ := range PathSlice {
				if k == len(PathSlice)-1 {
				  buffer.WriteString(PathSlice[k])
				}else{
				  buffer.WriteString(PathSlice[k]+"/")
				}
		}
		ArrayOfPaths = append(ArrayOfPaths, buffer.String())
	}
	return ArrayOfPaths, nil
}

// GetAllPaths retrieves all possible paths in the YAML file
//
// Example:
//      util.GetAllPaths(*Yaml)
func GetAllPaths(y *simpleyaml.Yaml) ([]string, error)  {
	InitialPath := make([]string, 0)
	AllPaths, err :=  GetAllExistingPaths(y, InitialPath)
	if err != nil {
		return nil, errors.New("Retrieving paths failed")
	}
	return AllPaths, nil
}