mirror of
https://github.com/nezhahq/nezha.git
synced 2025-03-11 08:18:13 -04:00

* feat: support listening https * refactor * modernize * support snake case in config * more precise control of config fields * update goreleaser config * remove kubeyaml * fix: expose agent_secret * chore
40 lines
768 B
Go
40 lines
768 B
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"iter"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
var (
|
|
ErrGjsonNotFound = errors.New("specified path does not exist")
|
|
ErrGjsonWrongType = errors.New("wrong type")
|
|
)
|
|
|
|
var emptyIterator = func(yield func(string, string) bool) {}
|
|
|
|
func GjsonGet(json []byte, path string) (gjson.Result, error) {
|
|
result := gjson.GetBytes(json, path)
|
|
if !result.Exists() {
|
|
return result, ErrGjsonNotFound
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func GjsonIter(json string) (iter.Seq2[string, string], error) {
|
|
if json == "" {
|
|
return emptyIterator, nil
|
|
}
|
|
|
|
result := gjson.Parse(json)
|
|
if !result.IsObject() {
|
|
return nil, ErrGjsonWrongType
|
|
}
|
|
|
|
return ConvertSeq2(result.ForEach, func(k, v gjson.Result) (string, string) {
|
|
return k.String(), v.String()
|
|
}), nil
|
|
}
|