File: nested.go

package info (click to toggle)
golang-github-knqyf263-nested 0.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 96 kB
  • sloc: makefile: 2
file content (167 lines) | stat: -rw-r--r-- 2,893 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package nested

import (
	"errors"
	"strings"
)

var (
	ErrNoSuchKey     = errors.New("no such key")
	ErrUnmatchedType = errors.New("unmatched type")
)

type Nested map[string]interface{}

func (n Nested) GetString(keys []string) (value string, err error) {
	v, err := n.Get(keys)
	if err != nil {
		return "", err
	}
	value, ok := v.(string)
	if !ok {
		return "", ErrUnmatchedType
	}
	return value, nil
}

func (n Nested) GetInt(keys []string) (value int, err error) {
	v, err := n.Get(keys)
	if err != nil {
		return 0, err
	}
	value, ok := v.(int)
	if !ok {
		return 0, ErrUnmatchedType
	}
	return value, nil
}

func (n Nested) GetBool(keys []string) (value bool, err error) {
	v, err := n.Get(keys)
	if err != nil {
		return false, err
	}
	value, ok := v.(bool)
	if !ok {
		return false, ErrUnmatchedType
	}
	return value, nil
}

func (n Nested) GetByString(key, sep string) (value interface{}, err error) {
	key = strings.TrimPrefix(key, sep)
	return n.Get(strings.Split(key, sep))
}

func (n Nested) Get(keys []string) (value interface{}, err error) {
	var ok bool
	m := n
	for i, k := range keys {
		value, ok = m[k]
		if !ok {
			return nil, ErrNoSuchKey
		}
		if i == len(keys)-1 {
			break
		}

		m, ok = value.(map[string]interface{})
		if !ok {
			return nil, ErrNoSuchKey
		}

	}
	return value, nil
}

func (n Nested) SetByString(key, sep string, value interface{}) {
	key = strings.TrimPrefix(key, sep)
	n.Set(strings.Split(key, sep), value)
}

func (n Nested) Set(keys []string, value interface{}) {
	m := n
	for i, k := range keys {
		if i == len(keys)-1 {
			m[k] = value
			return
		}

		v, ok := m[k]
		if ok {
			temp, ok := v.(map[string]interface{})
			if ok {
				m = temp
				continue
			}
		}
		newMap := map[string]interface{}{}
		m[k] = newMap
		m = newMap
	}
	return
}

func (n Nested) DeleteByString(key, sep string) error {
	key = strings.TrimPrefix(key, sep)
	return n.Delete(strings.Split(key, sep))
}

func (n Nested) Delete(keys []string) error {
	m := n
	for i, k := range keys {
		value, ok := m[k]
		if !ok {
			return ErrNoSuchKey
		}

		if i == len(keys)-1 {
			delete(m, k)
			return nil
		}

		m, ok = value.(map[string]interface{})
		if !ok {
			return ErrNoSuchKey
		}
	}
	return nil
}

var SkipKey = errors.New("skip this key")

type WalkFunc func(keys []string, value interface{}) error

func (n Nested) Walk(walkFn WalkFunc) error {
	for k, v := range n {
		err := walk([]string{k}, v, walkFn)
		if err == SkipKey {
			continue
		} else if err != nil {
			return err
		}
	}
	return nil
}

func walk(keys []string, value interface{}, walkFn WalkFunc) error {
	err := walkFn(keys, value)
	if err == SkipKey {
		return nil
	} else if err != nil {
		return err
	}

	m, ok := value.(map[string]interface{})
	if !ok {
		return nil
	}

	for k, v := range m {
		err = walk(append(keys, k), v, walkFn)
		if err != nil {
			return err
		}
	}
	return nil
}