| 1 | package formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src . errors/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 6 | "reflectscip-go gomod github.com/golang/go/src . reflect/" |
| 7 | "sortscip-go gomod github.com/golang/go/src . sort/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 9 | |
| 10 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 11 | ) |
| 12 | |
| 13 | // PropKeyResolver implements the ConfigQueryResolver interface for services that uses key tags for query props. |
| 14 | type PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver# struct { |
| 15 | confValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#confValue. reflectscip-go gomod github.com/golang/go/src . reflect/.Valuescip-go gomod github.com/golang/go/src . reflect/Value# |
| 16 | keyFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keyFields. map[string]*FieldInfoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo# |
| 17 | keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keys. []string |
| 18 | } |
| 19 | |
| 20 | var ( |
| 21 | ErrInvalidConfigKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ErrInvalidConfigKey. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("not a valid config key") |
| 22 | ErrInvalidValueForTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ErrInvalidValueForType. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("invalid value for type") |
| 23 | ) |
| 24 | |
| 25 | // NewPropKeyResolver creates a new PropKeyResolver and initializes it using the provided config. |
| 26 | func NewPropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/NewPropKeyResolver().(config typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ServiceConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ServiceConfig#) PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver# { |
| 27 | configNode := GetConfigFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/GetConfigFormat().(config) |
| 28 | items := configNode.Itemsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ContainerNode#Items. |
| 29 | |
| 30 | keyFields := make(map[string]*FieldInfoscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#, len(items)) |
| 31 | keys := make([]string, 0, len(items)) |
| 32 | |
| 33 | for _, item := range items { |
| 34 | field := *item.Fieldscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/Node#Field.() |
| 35 | if len(field.Keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Keys.) == 0 { |
| 36 | continue // Skip fields without explicit 'key' tags |
| 37 | } |
| 38 | |
| 39 | for _, key := range field.Keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Keys. { |
| 40 | key = stringsscip-go gomod github.com/golang/go/src . strings/.ToLowerscip-go gomod github.com/golang/go/src . strings/ToLower().(key) |
| 41 | if key != "" { |
| 42 | keys = append(keys, key) |
| 43 | keyFields[key] = &field |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | sortscip-go gomod github.com/golang/go/src . sort/.Stringsscip-go gomod github.com/golang/go/src . sort/Strings().(keys) |
| 49 | |
| 50 | confValue := reflectscip-go gomod github.com/golang/go/src . reflect/.ValueOfscip-go gomod github.com/golang/go/src . reflect/ValueOf().(config) |
| 51 | if confValue.Kindscip-go gomod github.com/golang/go/src . reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src . reflect/.Pointerscip-go gomod github.com/golang/go/src . reflect/Pointer. { |
| 52 | confValue = confValue.Elemscip-go gomod github.com/golang/go/src . reflect/Value#Elem().() |
| 53 | } |
| 54 | |
| 55 | return PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#{ |
| 56 | keyFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keyFields.: keyFields, |
| 57 | confValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#confValue.: confValue, |
| 58 | keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keys.: keys, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // GetConfigQueryResolver returns the ConfigQueryResolver interface for the config if it implements it, |
| 63 | // otherwise it creates and returns a PropKeyResolver that implements it. |
| 64 | func GetConfigQueryResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/GetConfigQueryResolver().(config typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ServiceConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ServiceConfig#) 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# { |
| 65 | var resolver 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# |
| 66 | |
| 67 | var ok bool |
| 68 | if resolver, ok = config.(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#); !ok { |
| 69 | pkr := NewPropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/NewPropKeyResolver().(config) |
| 70 | resolver = &pkr |
| 71 | } |
| 72 | |
| 73 | return resolver |
| 74 | } |
| 75 | |
| 76 | // Bind creates a new instance of the PropKeyResolver with the internal config reference |
| 77 | // set to the provided config. This should only be used for configs of the same type. |
| 78 | func (pkr *PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) Bindscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#Bind().(config typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ServiceConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ServiceConfig#) PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver# { |
| 79 | bound := *pkr |
| 80 | bound.confValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#confValue. = configValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/configValue().(config) |
| 81 | |
| 82 | return bound |
| 83 | } |
| 84 | |
| 85 | // Get returns the value of a config property tagged with the corresponding key. |
| 86 | func (pkr *PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) Getscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#Get().(key string) (string, error) { |
| 87 | if field, found := pkr.keyFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keyFields.[stringsscip-go gomod github.com/golang/go/src . strings/.ToLowerscip-go gomod github.com/golang/go/src . strings/ToLower().(key)]; found { |
| 88 | return GetConfigFieldStringscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/GetConfigFieldString().(pkr.confValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#confValue., field) |
| 89 | } |
| 90 | |
| 91 | return "", fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %v", ErrInvalidConfigKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ErrInvalidConfigKey., key) |
| 92 | } |
| 93 | |
| 94 | // IsDefault returns whether the specified key value is the default value. |
| 95 | func (pkr *PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) IsDefaultscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#IsDefault().(key, value string) bool { |
| 96 | return pkr.keyFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keyFields.[key].DefaultValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#DefaultValue. == value |
| 97 | } |
| 98 | |
| 99 | // KeyIsPrimary returns whether the key is the primary (and not an alias). |
| 100 | func (pkr *PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) KeyIsPrimaryscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#KeyIsPrimary().(key string) bool { |
| 101 | return pkr.keyFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keyFields.[key].Keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#Keys.[0] == key |
| 102 | } |
| 103 | |
| 104 | // QueryFields returns a list of tagged keys. |
| 105 | func (pkr *PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) QueryFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#QueryFields().() []string { |
| 106 | return pkr.keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keys. |
| 107 | } |
| 108 | |
| 109 | // Set sets the value of its bound struct's property, tagged with the corresponding key. |
| 110 | func (pkr *PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) Setscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#Set().(key, value string) error { |
| 111 | return pkr.setscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#set().(pkr.confValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#confValue., key, value) |
| 112 | } |
| 113 | |
| 114 | // SetDefaultProps mutates the provided config, setting the tagged fields with their default values. |
| 115 | // If the provided config is nil, the internal config will be updated instead. |
| 116 | // The error returned is the first error that occurred; subsequent errors are discarded. |
| 117 | func (pkr *PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) SetDefaultPropsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#SetDefaultProps().(config typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ServiceConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ServiceConfig#) error { |
| 118 | var firstError error |
| 119 | |
| 120 | confValue := pkr.configValueOrInternalscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#configValueOrInternal().(config) |
| 121 | for key, info := range pkr.keyFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keyFields. { |
| 122 | if err := pkr.setscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#set().(confValue, key, info.DefaultValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/FieldInfo#DefaultValue.); err != nil && firstError == nil { |
| 123 | firstError = err |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return firstError |
| 128 | } |
| 129 | |
| 130 | // UpdateConfigFromParams mutates the provided config, updating the values from its corresponding params. |
| 131 | // If the provided config is nil, the internal config will be updated instead. |
| 132 | // The error returned is the first error that occurred; subsequent errors are discarded. |
| 133 | func (pkr *PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) UpdateConfigFromParamsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#UpdateConfigFromParams().( |
| 134 | config typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ServiceConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ServiceConfig#, |
| 135 | params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#, |
| 136 | ) error { |
| 137 | var firstError error |
| 138 | |
| 139 | confValue := pkr.configValueOrInternalscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#configValueOrInternal().(config) |
| 140 | |
| 141 | if params != nil { |
| 142 | for key, val := range *params { |
| 143 | if err := pkr.setscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#set().(confValue, key, val); err != nil && firstError == nil { |
| 144 | firstError = err |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return firstError |
| 150 | } |
| 151 | |
| 152 | // configValueOrInternal returns the reflect.Value of the config, or the internal config if config is nil. |
| 153 | func (pkr *PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) configValueOrInternalscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#configValueOrInternal().(config typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ServiceConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ServiceConfig#) reflectscip-go gomod github.com/golang/go/src . reflect/.Valuescip-go gomod github.com/golang/go/src . reflect/Value# { |
| 154 | if config != nil { |
| 155 | return configValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/configValue().(config) |
| 156 | } |
| 157 | |
| 158 | return pkr.confValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#confValue. |
| 159 | } |
| 160 | |
| 161 | // set sets the value of a target struct tagged with the corresponding key. |
| 162 | func (pkr *PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#) setscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#set().(target reflectscip-go gomod github.com/golang/go/src . reflect/.Valuescip-go gomod github.com/golang/go/src . reflect/Value#, key, value string) error { |
| 163 | if field, found := pkr.keyFieldsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keyFields.[stringsscip-go gomod github.com/golang/go/src . strings/.ToLowerscip-go gomod github.com/golang/go/src . strings/ToLower().(key)]; found { |
| 164 | valid, err := SetConfigFieldscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/SetConfigField().(target, field, value) |
| 165 | if !valid && err == nil { |
| 166 | return ErrInvalidValueForTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ErrInvalidValueForType. |
| 167 | } |
| 168 | |
| 169 | return err |
| 170 | } |
| 171 | |
| 172 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %v (valid keys: %v)", ErrInvalidConfigKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/ErrInvalidConfigKey., key, pkr.keysscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#keys.) |
| 173 | } |
| 174 | |
| 175 | // configValue returns the indirect reflect.Value of the config. |
| 176 | func configValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/configValue().(config typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.ServiceConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/ServiceConfig#) reflectscip-go gomod github.com/golang/go/src . reflect/.Valuescip-go gomod github.com/golang/go/src . reflect/Value# { |
| 177 | return reflectscip-go gomod github.com/golang/go/src . reflect/.Indirectscip-go gomod github.com/golang/go/src . reflect/Indirect().(reflectscip-go gomod github.com/golang/go/src . reflect/.ValueOfscip-go gomod github.com/golang/go/src . reflect/ValueOf().(config)) |
| 178 | } |