small improvements (#958)

* small improvements

* fix: return empty iterator if no json present

* use time.Tick

* changes
This commit is contained in:
UUBulb
2025-01-19 21:22:00 +08:00
committed by GitHub
parent 844865e2c1
commit 4b1af369e3
10 changed files with 34 additions and 34 deletions

View File

@@ -2,6 +2,7 @@ package utils
import (
"errors"
"iter"
"github.com/tidwall/gjson"
)
@@ -11,6 +12,8 @@ var (
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() {
@@ -20,21 +23,19 @@ func GjsonGet(json []byte, path string) (gjson.Result, error) {
return result, nil
}
func GjsonParseStringMap(jsonObject string) (map[string]string, error) {
if jsonObject == "" {
return nil, nil
func GjsonIter(json string) (iter.Seq2[string, string], error) {
if json == "" {
return emptyIterator, nil
}
result := gjson.Parse(jsonObject)
result := gjson.Parse(json)
if !result.IsObject() {
return nil, ErrGjsonWrongType
}
ret := make(map[string]string)
result.ForEach(func(key, value gjson.Result) bool {
ret[key.String()] = value.String()
return true
})
return ret, nil
return func(yield func(string, string) bool) {
result.ForEach(func(k, v gjson.Result) bool {
return yield(k.String(), v.String())
})
}, nil
}