| 1 | package format |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "reflect" |
| 7 | "sort" |
| 8 | "strings" |
| 9 | |
| 10 | "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 PropKeyResolver struct { |
| 15 | confValue reflect.Value |
| 16 | keyFields map[string]*FieldInfo |
| 17 | keys []string |
| 18 | } |
| 19 | |
| 20 | var ( |
| 21 | ErrInvalidConfigKey = errors.New("not a valid config key") |
| 22 | ErrInvalidValueForType = errors.New("invalid value for type") |
| 23 | ) |
| 24 | |
| 25 | // NewPropKeyResolver creates a new PropKeyResolver and initializes it using the provided config. |
| 26 | func NewPropKeyResolver(config types.ServiceConfig) PropKeyResolver { |
| 27 | configNode := GetConfigFormat(config) |
| 28 | items := configNode.Items |
| 29 | |
| 30 | keyFields := make(map[string]*FieldInfo, len(items)) |
| 31 | keys := make([]string, 0, len(items)) |
| 32 | |
| 33 | for _, item := range items { |
| 34 | field := *item.Field() |
| 35 | if len(field.Keys) == 0 { |
| 36 | continue // Skip fields without explicit 'key' tags |
| 37 | } |
| 38 | |
| 39 | for _, key := range field.Keys { |
| 40 | key = strings.ToLower(key) |
| 41 | if key != "" { |
| 42 | keys = append(keys, key) |
| 43 | keyFields[key] = &field |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | sort.Strings(keys) |
| 49 | |
| 50 | confValue := reflect.ValueOf(config) |
| 51 | if confValue.Kind() == reflect.Pointer { |
| 52 | confValue = confValue.Elem() |
| 53 | } |
| 54 | |
| 55 | return PropKeyResolver{ |
| 56 | keyFields: keyFields, |
| 57 | confValue: confValue, |
| 58 | 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 GetConfigQueryResolver(config types.ServiceConfig) types.ConfigQueryResolver { |
| 65 | var resolver types.ConfigQueryResolver |
| 66 | |
| 67 | var ok bool |
| 68 | if resolver, ok = config.(types.ConfigQueryResolver); !ok { |
| 69 | pkr := 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 *PropKeyResolver) Bind(config types.ServiceConfig) PropKeyResolver { |
| 79 | bound := *pkr |
| 80 | bound.confValue = 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 *PropKeyResolver) Get(key string) (string, error) { |
| 87 | if field, found := pkr.keyFields[strings.ToLower(key)]; found { |
| 88 | return GetConfigFieldString(pkr.confValue, field) |
| 89 | } |
| 90 | |
| 91 | return "", fmt.Errorf("%w: %v", ErrInvalidConfigKey, key) |
| 92 | } |
| 93 | |
| 94 | // IsDefault returns whether the specified key value is the default value. |
| 95 | func (pkr *PropKeyResolver) IsDefault(key, value string) bool { |
| 96 | return pkr.keyFields[key].DefaultValue == value |
| 97 | } |
| 98 | |
| 99 | // KeyIsPrimary returns whether the key is the primary (and not an alias). |
| 100 | func (pkr *PropKeyResolver) KeyIsPrimary(key string) bool { |
| 101 | return pkr.keyFields[key].Keys[0] == key |
| 102 | } |
| 103 | |
| 104 | // QueryFields returns a list of tagged keys. |
| 105 | func (pkr *PropKeyResolver) QueryFields() []string { |
| 106 | return pkr.keys |
| 107 | } |
| 108 | |
| 109 | // Set sets the value of its bound struct's property, tagged with the corresponding key. |
| 110 | func (pkr *PropKeyResolver) Set(key, value string) error { |
| 111 | return pkr.set(pkr.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 *PropKeyResolver) SetDefaultProps(config types.ServiceConfig) error { |
| 118 | var firstError error |
| 119 | |
| 120 | confValue := pkr.configValueOrInternal(config) |
| 121 | for key, info := range pkr.keyFields { |
| 122 | if err := pkr.set(confValue, key, info.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 *PropKeyResolver) UpdateConfigFromParams( |
| 134 | config types.ServiceConfig, |
| 135 | params *types.Params, |
| 136 | ) error { |
| 137 | var firstError error |
| 138 | |
| 139 | confValue := pkr.configValueOrInternal(config) |
| 140 | |
| 141 | if params != nil { |
| 142 | for key, val := range *params { |
| 143 | if err := pkr.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 *PropKeyResolver) configValueOrInternal(config types.ServiceConfig) reflect.Value { |
| 154 | if config != nil { |
| 155 | return configValue(config) |
| 156 | } |
| 157 | |
| 158 | return pkr.confValue |
| 159 | } |
| 160 | |
| 161 | // set sets the value of a target struct tagged with the corresponding key. |
| 162 | func (pkr *PropKeyResolver) set(target reflect.Value, key, value string) error { |
| 163 | if field, found := pkr.keyFields[strings.ToLower(key)]; found { |
| 164 | valid, err := SetConfigField(target, field, value) |
| 165 | if !valid && err == nil { |
| 166 | return ErrInvalidValueForType |
| 167 | } |
| 168 | |
| 169 | return err |
| 170 | } |
| 171 | |
| 172 | return fmt.Errorf("%w: %v (valid keys: %v)", ErrInvalidConfigKey, key, pkr.keys) |
| 173 | } |
| 174 | |
| 175 | // configValue returns the indirect reflect.Value of the config. |
| 176 | func configValue(config types.ServiceConfig) reflect.Value { |
| 177 | return reflect.Indirect(reflect.ValueOf(config)) |
| 178 | } |