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] return string(msgBytes)[1 : len(msgBytes)-1]
}), nil }), nil
case NotificationRequestTypeForm: case NotificationRequestTypeForm:
data, err := utils.GjsonParseStringMap(n.RequestBody) data, err := utils.GjsonIter(n.RequestBody)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -98,7 +98,7 @@ func (n *Notification) setRequestHeader(req *http.Request) error {
if n.RequestHeader == "" { if n.RequestHeader == "" {
return nil return nil
} }
m, err := utils.GjsonParseStringMap(n.RequestHeader) m, err := utils.GjsonIter(n.RequestHeader)
if err != nil { if err != nil {
return err return err
} }

View File

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

View File

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