nix-amer/internal/cnf/json.go

138 lines
2.8 KiB
Go
Raw Normal View History

package cnf
import (
lib "encoding/json"
"io"
"strings"
)
type json struct {
data interface{}
parsed bool
}
// ReadFrom implements io.ReaderFrom
func (d *json) ReadFrom(_reader io.Reader) (int64, error) {
// 1. get json decoder
decoder := lib.NewDecoder(_reader)
err := decoder.Decode(&d.data)
if err != nil {
return 0, err
}
d.parsed = true
return 0, nil
}
// WriteTo implements io.WriterTo
func (d *json) WriteTo(_writer io.Writer) (int64, error) {
encoder := lib.NewEncoder(_writer)
encoder.SetIndent("", "\t")
return 0, encoder.Encode(&d.data)
}
2018-11-07 14:51:54 +00:00
// browse returns the target of a dot-separated path (as an interface{} chain where the last is the target if found)
func (d *json) browse(_path string) ([]interface{}, bool) {
// 1. extract path
path := strings.Split(_path, ".")
2018-11-07 14:51:54 +00:00
// 2. init output chain
current := d.data
2018-11-07 14:51:54 +00:00
chain := make([]interface{}, 0, len(path)+1)
chain = append(chain, current)
// 3. iterate over path / nested fields
for _, field := range path {
fmap, ok := current.(map[string]interface{})
if !ok { // incomplete path
2018-11-07 14:51:54 +00:00
return chain, false
}
child, ok := fmap[field]
if !ok { // incomplete path
2018-11-07 14:51:54 +00:00
return chain, false
}
current = child
2018-11-07 14:51:54 +00:00
chain = append(chain, current)
}
2018-11-07 14:51:54 +00:00
return chain, true
}
// Get returns the value of a dot-separated path, and if it exists
func (d *json) Get(_path string) (string, bool) {
2018-11-07 14:51:54 +00:00
// 1. browse path
chain, found := d.browse(_path)
if !found {
return "", false
}
// 2. return if string value
value, ok := chain[len(chain)-1].(string)
return value, ok
2018-11-07 14:51:54 +00:00
}
// Set the value of a dot-separated path, and creates it if not found
func (d *json) Set(_path, _value string) bool {
2018-11-07 14:51:54 +00:00
// 1. browse path + create it if does not exist
path := strings.Split(_path, ".")
lp := len(path)
2018-11-07 14:51:54 +00:00
chain, found := d.browse(_path)
// 2. if found -> set value
if found {
mapWrapper, ok := chain[len(chain)-2].(map[string]interface{})
if !ok { // impossible
return false
}
key := path[lp-1]
2018-11-07 14:51:54 +00:00
mapWrapper[key] = _value
return true
}
// 3. create path until the end to set value
root := make(map[string]interface{})
current := root
// create children until second to last
for i, l := len(chain)-1, lp-1; i < l; i++ {
2018-11-07 14:51:54 +00:00
child := make(map[string]interface{})
current[path[i]] = child
current = child
}
// set value
current[path[lp-1]] = _value
2018-11-07 14:51:54 +00:00
// replace whole object if empty
if len(chain) < 2 {
wrapper, ok := d.data.(map[string]interface{})
if !ok { // impossible
return false
}
key := path[0]
wrapper[key] = root[key] // store with key ; eitherway it will erase all brother keys
2018-11-07 14:51:54 +00:00
return true
}
// update last found object to add the value
wrapper, ok := chain[len(chain)-1].(map[string]interface{})
2018-11-07 14:51:54 +00:00
if !ok { // impossible
return false
}
// add each subkey
for subkey, subvalue := range root {
wrapper[subkey] = subvalue
}
2018-11-07 14:51:54 +00:00
return true
}