set sets the value of field f in cfg to value.
(f configField, value string)
| 223 | |
| 224 | // set sets the value of field f in cfg to value. |
| 225 | func (cfg *config) set(f configField, value string) error { |
| 226 | switch ptr := cfg.fieldPtr(f).(type) { |
| 227 | case *string: |
| 228 | if len(f.choices) > 0 { |
| 229 | // Verify that value is one of the allowed choices. |
| 230 | if slices.Contains(f.choices, value) { |
| 231 | *ptr = value |
| 232 | return nil |
| 233 | } |
| 234 | return fmt.Errorf("invalid %q value %q", f.name, value) |
| 235 | } |
| 236 | *ptr = value |
| 237 | case *int: |
| 238 | v, err := strconv.Atoi(value) |
| 239 | if err != nil { |
| 240 | return err |
| 241 | } |
| 242 | *ptr = v |
| 243 | case *float64: |
| 244 | v, err := strconv.ParseFloat(value, 64) |
| 245 | if err != nil { |
| 246 | return err |
| 247 | } |
| 248 | *ptr = v |
| 249 | case *bool: |
| 250 | v, err := stringToBool(value) |
| 251 | if err != nil { |
| 252 | return err |
| 253 | } |
| 254 | *ptr = v |
| 255 | default: |
| 256 | panic(fmt.Sprintf("unsupported config field type %v", f.field.Type)) |
| 257 | } |
| 258 | return nil |
| 259 | } |
| 260 | |
| 261 | // isConfigurable returns true if name is either the name of a config field, or |
| 262 | // a valid value for a multi-choice config field. |
no test coverage detected