fix: return empty iterator if no json present

This commit is contained in:
uubulb 2025-01-16 18:33:12 +08:00
parent 1d8583ca8c
commit 2df72c2e49

View File

@ -12,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() {
@ -23,7 +25,7 @@ func GjsonGet(json []byte, path string) (gjson.Result, error) {
func GjsonIter(json string) (iter.Seq2[string, string], error) {
if json == "" {
return nil, nil
return emptyIterator, nil
}
result := gjson.Parse(json)