diff --git a/model/notification.go b/model/notification.go index c6e1678..514d4d8 100644 --- a/model/notification.go +++ b/model/notification.go @@ -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 } diff --git a/pkg/ddns/webhook/webhook.go b/pkg/ddns/webhook/webhook.go index b74d434..af99ab6 100644 --- a/pkg/ddns/webhook/webhook.go +++ b/pkg/ddns/webhook/webhook.go @@ -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 } diff --git a/pkg/utils/gjson.go b/pkg/utils/gjson.go index 0063681..6059442 100644 --- a/pkg/utils/gjson.go +++ b/pkg/utils/gjson.go @@ -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 ret, nil + return func(yield func(string, string) bool) { + result.ForEach(func(k, v gjson.Result) bool { + return yield(k.String(), v.String()) + }) + }, nil }