| 1 | package formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ |
| 2 | |
| 3 | import ( |
| 4 | "net/urlscip-go gomod github.com/golang/go/src . `net/url`/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 6 | |
| 7 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 8 | ) |
| 9 | |
| 10 | // KeyPrefix is the prefix used to escape custom URL query keys that conflict with service config prop keys. |
| 11 | // It consists of two underscore characters ("__"). |
| 12 | const KeyPrefixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/KeyPrefix. = "__" |
| 13 | |
| 14 | // BuildQuery converts the fields of a config object to a delimited query string. |
| 15 | func BuildQueryscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/BuildQuery().(cqr typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ConfigQueryResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ConfigQueryResolver#) string { |
| 16 | return BuildQueryWithCustomFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/BuildQueryWithCustomFields().(cqr, urlscip-go gomod github.com/golang/go/src . `net/url`/.Valuesscip-go gomod github.com/golang/go/src . `net/url`/Values#{}).Encodescip-go gomod github.com/golang/go/src . `net/url`/Values#Encode().() |
| 17 | } |
| 18 | |
| 19 | // BuildQueryWithCustomFields converts the fields of a config object to a delimited query string, |
| 20 | // escaping any custom fields that share the same key as a config prop using a "__" prefix. |
| 21 | func BuildQueryWithCustomFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/BuildQueryWithCustomFields().(cqr typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ConfigQueryResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ConfigQueryResolver#, query urlscip-go gomod github.com/golang/go/src . `net/url`/.Valuesscip-go gomod github.com/golang/go/src . `net/url`/Values#) urlscip-go gomod github.com/golang/go/src . `net/url`/.Valuesscip-go gomod github.com/golang/go/src . `net/url`/Values# { |
| 22 | fields := cqr.QueryFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ConfigQueryResolver#QueryFields.() |
| 23 | skipEscape := len(query) < 1 |
| 24 | |
| 25 | pkr, isPkr := cqr.(*PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) |
| 26 | |
| 27 | for _, key := range fields { |
| 28 | if !skipEscape { |
| 29 | // Escape any webhook query keys using the same name as service props |
| 30 | escValues := query[key] |
| 31 | if len(escValues) > 0 { |
| 32 | query.Delscip-go gomod github.com/golang/go/src . `net/url`/Values#Del().(key) |
| 33 | query[EscapeKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EscapeKey().(key)] = escValues |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | if isPkr && !pkr.KeyIsPrimaryscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#KeyIsPrimary().(key) { |
| 38 | continue |
| 39 | } |
| 40 | |
| 41 | value, err := cqr.Getscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ConfigQueryResolver#Get.(key) |
| 42 | if err != nil || isPkr && pkr.IsDefaultscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#IsDefault().(key, value) { |
| 43 | continue |
| 44 | } |
| 45 | |
| 46 | query.Setscip-go gomod github.com/golang/go/src . `net/url`/Values#Set().(key, value) |
| 47 | } |
| 48 | |
| 49 | return query |
| 50 | } |
| 51 | |
| 52 | // SetConfigPropsFromQuery iterates over all the config prop keys and sets the config prop to the corresponding |
| 53 | // query value based on the key. It returns a non-nil url.Values query with all config prop keys removed, |
| 54 | // even if any of them could not be used to set a config field, and with any escaped keys unescaped. |
| 55 | // The error returned is the first error that occurred, subsequent errors are just discarded. |
| 56 | func SetConfigPropsFromQueryscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/SetConfigPropsFromQuery().(cqr typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ConfigQueryResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ConfigQueryResolver#, query urlscip-go gomod github.com/golang/go/src . `net/url`/.Valuesscip-go gomod github.com/golang/go/src . `net/url`/Values#) (urlscip-go gomod github.com/golang/go/src . `net/url`/.Valuesscip-go gomod github.com/golang/go/src . `net/url`/Values#, error) { |
| 57 | var firstError error |
| 58 | |
| 59 | if query == nil { |
| 60 | return urlscip-go gomod github.com/golang/go/src . `net/url`/.Valuesscip-go gomod github.com/golang/go/src . `net/url`/Values#{}, nil |
| 61 | } |
| 62 | |
| 63 | for _, key := range cqr.QueryFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ConfigQueryResolver#QueryFields.() { |
| 64 | // Retrieve the service-related prop value |
| 65 | values := query[key] |
| 66 | if len(values) > 0 { |
| 67 | if err := cqr.Setscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ConfigQueryResolver#Set.(key, values[0]); err != nil && firstError == nil { |
| 68 | firstError = err |
| 69 | } |
| 70 | } |
| 71 | // Remove it from the query Values |
| 72 | query.Delscip-go gomod github.com/golang/go/src . `net/url`/Values#Del().(key) |
| 73 | |
| 74 | // If an escaped version of the key exist, unescape it |
| 75 | escKey := EscapeKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EscapeKey().(key) |
| 76 | escValues := query[escKey] |
| 77 | |
| 78 | if len(escValues) > 0 { |
| 79 | query.Delscip-go gomod github.com/golang/go/src . `net/url`/Values#Del().(escKey) |
| 80 | query[key] = escValues |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return query, firstError |
| 85 | } |
| 86 | |
| 87 | // EscapeKey adds the KeyPrefix to custom URL query keys that conflict with service config prop keys. |
| 88 | func EscapeKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/EscapeKey().(key string) string { |
| 89 | return KeyPrefixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/KeyPrefix. + key |
| 90 | } |
| 91 | |
| 92 | // UnescapeKey removes the KeyPrefix from custom URL query keys that conflict with service config prop keys. |
| 93 | func UnescapeKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/UnescapeKey().(key string) string { |
| 94 | return stringsscip-go gomod github.com/golang/go/src . strings/.TrimPrefixscip-go gomod github.com/golang/go/src . strings/TrimPrefix().(key, KeyPrefixscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/KeyPrefix.) |
| 95 | } |