small improvements

This commit is contained in:
uubulb 2025-01-16 18:21:00 +08:00
parent 844865e2c1
commit 1d8583ca8c
3 changed files with 13 additions and 14 deletions

View File

@ -70,7 +70,7 @@ func (ns *NotificationServerBundle) reqBody(message string) (string, error) {
return string(msgBytes)[1 : len(msgBytes)-1]
}), nil
case NotificationRequestTypeForm:
data, err := utils.GjsonParseStringMap(n.RequestBody)
data, err := utils.GjsonIter(n.RequestBody)
if err != nil {
return "", err
}
@ -98,7 +98,7 @@ func (n *Notification) setRequestHeader(req *http.Request) error {
if n.RequestHeader == "" {
return nil
}
m, err := utils.GjsonParseStringMap(n.RequestHeader)
m, err := utils.GjsonIter(n.RequestHeader)
if err != nil {
return err
}

View File

@ -77,7 +77,7 @@ func (provider *Provider) prepareRequest(ctx context.Context) (*http.Request, er
return nil, err
}
headers, err := utils.GjsonParseStringMap(
headers, err := utils.GjsonIter(
provider.formatWebhookString(provider.DDNSProfile.WebhookHeaders))
if err != nil {
return nil, err
@ -139,7 +139,7 @@ func (provider *Provider) reqBody() (string, error) {
case requestTypeJSON:
return provider.formatWebhookString(provider.DDNSProfile.WebhookRequestBody), nil
case requestTypeForm:
data, err := utils.GjsonParseStringMap(provider.DDNSProfile.WebhookRequestBody)
data, err := utils.GjsonIter(provider.DDNSProfile.WebhookRequestBody)
if err != nil {
return "", err
}

View File

@ -2,6 +2,7 @@ package utils
import (
"errors"
"iter"
"github.com/tidwall/gjson"
)
@ -20,21 +21,19 @@ func GjsonGet(json []byte, path string) (gjson.Result, error) {
return result, nil
}
func GjsonParseStringMap(jsonObject string) (map[string]string, error) {
if jsonObject == "" {
func GjsonIter(json string) (iter.Seq2[string, string], error) {
if json == "" {
return nil, 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 func(yield func(string, string) bool) {
result.ForEach(func(k, v gjson.Result) bool {
return yield(k.String(), v.String())
})
return ret, nil
}, nil
}