| 1 |
package format |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/url" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"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 KeyPrefix = "__" |
| 13 |
|
| 14 |
// BuildQuery converts the fields of a config object to a delimited query string. |
| 15 |
func BuildQuery(cqr types.ConfigQueryResolver) string { |
| 16 |
return BuildQueryWithCustomFields(cqr, 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 BuildQueryWithCustomFields(cqr types.ConfigQueryResolver, query url.Values) url.Values { |
| 22 |
fields := cqr.QueryFields() |
| 23 |
skipEscape := len(query) < 1 |
| 24 |
|
| 25 |
pkr, isPkr := cqr.(*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.Del(key) |
| 33 |
query[EscapeKey(key)] = escValues |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
if isPkr && !pkr.KeyIsPrimary(key) { |
| 38 |
continue |
| 39 |
} |
| 40 |
|
| 41 |
value, err := cqr.Get(key) |
| 42 |
if err != nil || isPkr && pkr.IsDefault(key, value) { |
| 43 |
continue |
| 44 |
} |
| 45 |
|
| 46 |
query.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 SetConfigPropsFromQuery(cqr types.ConfigQueryResolver, query url.Values) (url.Values, error) { |
| 57 |
var firstError error |
| 58 |
|
| 59 |
if query == nil { |
| 60 |
return url.Values{}, nil |
| 61 |
} |
| 62 |
|
| 63 |
for _, key := range cqr.QueryFields() { |
| 64 |
// Retrieve the service-related prop value |
| 65 |
values := query[key] |
| 66 |
if len(values) > 0 { |
| 67 |
if err := cqr.Set(key, values[0]); err != nil && firstError == nil { |
| 68 |
firstError = err |
| 69 |
} |
| 70 |
} |
| 71 |
// Remove it from the query Values |
| 72 |
query.Del(key) |
| 73 |
|
| 74 |
// If an escaped version of the key exist, unescape it |
| 75 |
escKey := EscapeKey(key) |
| 76 |
escValues := query[escKey] |
| 77 |
|
| 78 |
if len(escValues) > 0 { |
| 79 |
query.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 EscapeKey(key string) string { |
| 89 |
return KeyPrefix + key |
| 90 |
} |
| 91 |
|
| 92 |
// UnescapeKey removes the KeyPrefix from custom URL query keys that conflict with service config prop keys. |
| 93 |
func UnescapeKey(key string) string { |
| 94 |
return strings.TrimPrefix(key, KeyPrefix) |
| 95 |
} |