| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
"regexp" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/lib/templates" |
| 10 |
"github.com/emersion/go-message/mail" |
| 11 |
"github.com/go-ini/ini" |
| 12 |
) |
| 13 |
|
| 14 |
func MapToStruct(s *ini.Section, v any, useDefaults bool) error { |
| 15 |
typ := reflect.TypeOf(v) |
| 16 |
val := reflect.ValueOf(v) |
| 17 |
if typ.Kind() == reflect.Ptr { |
| 18 |
typ = typ.Elem() |
| 19 |
val = val.Elem() |
| 20 |
} else { |
| 21 |
panic("MapToStruct requires a pointer") |
| 22 |
} |
| 23 |
if typ.Kind() != reflect.Struct { |
| 24 |
panic("MapToStruct requires a pointer to a struct") |
| 25 |
} |
| 26 |
|
| 27 |
for i := 0; i < typ.NumField(); i++ { |
| 28 |
fieldVal := val.Field(i) |
| 29 |
fieldType := typ.Field(i) |
| 30 |
|
| 31 |
name := fieldType.Tag.Get("ini") |
| 32 |
if name == "" || name == "-" { |
| 33 |
continue |
| 34 |
} |
| 35 |
key, err := s.GetKey(name) |
| 36 |
if err != nil { |
| 37 |
defValue, found := fieldType.Tag.Lookup("default") |
| 38 |
if useDefaults && found { |
| 39 |
key, _ = s.NewKey(name, defValue) |
| 40 |
} else { |
| 41 |
continue |
| 42 |
} |
| 43 |
} |
| 44 |
err = setField(s, key, reflect.ValueOf(v), fieldVal, fieldType) |
| 45 |
if err != nil { |
| 46 |
return fmt.Errorf("[%s].%s: %w", s.Name(), name, err) |
| 47 |
} |
| 48 |
} |
| 49 |
return nil |
| 50 |
} |
| 51 |
|
| 52 |
func setField( |
| 53 |
s *ini.Section, key *ini.Key, struc reflect.Value, |
| 54 |
fieldVal reflect.Value, fieldType reflect.StructField, |
| 55 |
) error { |
| 56 |
var methodValue reflect.Value |
| 57 |
method := getParseMethod(s, key, struc, fieldType) |
| 58 |
if method.IsValid() { |
| 59 |
in := []reflect.Value{reflect.ValueOf(s), reflect.ValueOf(key)} |
| 60 |
out := method.Call(in) |
| 61 |
err, _ := out[1].Interface().(error) |
| 62 |
if err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
methodValue = out[0] |
| 66 |
} |
| 67 |
|
| 68 |
ft := fieldType.Type |
| 69 |
|
| 70 |
switch ft.Kind() { |
| 71 |
case reflect.String: |
| 72 |
if method.IsValid() { |
| 73 |
fieldVal.SetString(methodValue.String()) |
| 74 |
} else { |
| 75 |
fieldVal.SetString(key.String()) |
| 76 |
} |
| 77 |
case reflect.Bool: |
| 78 |
if method.IsValid() { |
| 79 |
fieldVal.SetBool(methodValue.Bool()) |
| 80 |
} else { |
| 81 |
boolVal, err := key.Bool() |
| 82 |
if err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
fieldVal.SetBool(boolVal) |
| 86 |
} |
| 87 |
case reflect.Int32: |
| 88 |
// impossible to differentiate rune from int32, they are aliases |
| 89 |
// this is an ugly hack but there is no alternative... |
| 90 |
if fieldType.Tag.Get("type") == "rune" { |
| 91 |
if method.IsValid() { |
| 92 |
fieldVal.Set(methodValue) |
| 93 |
} else { |
| 94 |
runes := []rune(key.String()) |
| 95 |
if len(runes) != 1 { |
| 96 |
return errors.New("value must be 1 character long") |
| 97 |
} |
| 98 |
fieldVal.Set(reflect.ValueOf(runes[0])) |
| 99 |
} |
| 100 |
return nil |
| 101 |
} |
| 102 |
fallthrough |
| 103 |
case reflect.Int64: |
| 104 |
// ParseDuration will not return err for `0`, so check the type name |
| 105 |
if ft.PkgPath() == "time" && ft.Name() == "Duration" { |
| 106 |
durationVal, err := key.Duration() |
| 107 |
if err != nil { |
| 108 |
return err |
| 109 |
} |
| 110 |
fieldVal.Set(reflect.ValueOf(durationVal)) |
| 111 |
return nil |
| 112 |
} |
| 113 |
fallthrough |
| 114 |
case reflect.Int, reflect.Int8, reflect.Int16: |
| 115 |
if method.IsValid() { |
| 116 |
fieldVal.SetInt(methodValue.Int()) |
| 117 |
} else { |
| 118 |
intVal, err := key.Int64() |
| 119 |
if err != nil { |
| 120 |
return err |
| 121 |
} |
| 122 |
fieldVal.SetInt(intVal) |
| 123 |
} |
| 124 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 125 |
if method.IsValid() { |
| 126 |
fieldVal.SetUint(methodValue.Uint()) |
| 127 |
} else { |
| 128 |
uintVal, err := key.Uint64() |
| 129 |
if err != nil { |
| 130 |
return err |
| 131 |
} |
| 132 |
fieldVal.SetUint(uintVal) |
| 133 |
} |
| 134 |
case reflect.Float32, reflect.Float64: |
| 135 |
if method.IsValid() { |
| 136 |
fieldVal.SetFloat(methodValue.Float()) |
| 137 |
} else { |
| 138 |
floatVal, err := key.Float64() |
| 139 |
if err != nil { |
| 140 |
return err |
| 141 |
} |
| 142 |
fieldVal.SetFloat(floatVal) |
| 143 |
} |
| 144 |
case reflect.Slice, reflect.Array: |
| 145 |
switch { |
| 146 |
case method.IsValid(): |
| 147 |
fieldVal.Set(methodValue) |
| 148 |
case ft.Elem().Kind() == reflect.Ptr && |
| 149 |
typePath(ft.Elem().Elem()) == "net/mail.Address": |
| 150 |
addrs, err := mail.ParseAddressList(key.String()) |
| 151 |
if err != nil { |
| 152 |
return err |
| 153 |
} |
| 154 |
fieldVal.Set(reflect.ValueOf(addrs)) |
| 155 |
case ft.Elem().Kind() == reflect.String: |
| 156 |
delim := fieldType.Tag.Get("delim") |
| 157 |
fieldVal.Set(reflect.ValueOf(key.Strings(delim))) |
| 158 |
default: |
| 159 |
panic(fmt.Sprintf("unsupported type []%s", typePath(ft.Elem()))) |
| 160 |
} |
| 161 |
case reflect.Struct: |
| 162 |
if method.IsValid() { |
| 163 |
fieldVal.Set(methodValue) |
| 164 |
} else { |
| 165 |
panic(fmt.Sprintf("unsupported type %s", typePath(ft))) |
| 166 |
} |
| 167 |
case reflect.Ptr: |
| 168 |
if method.IsValid() { |
| 169 |
fieldVal.Set(methodValue) |
| 170 |
} else { |
| 171 |
switch typePath(ft.Elem()) { |
| 172 |
case "net/mail.Address": |
| 173 |
addr, err := mail.ParseAddress(key.String()) |
| 174 |
if err != nil { |
| 175 |
return err |
| 176 |
} |
| 177 |
fieldVal.Set(reflect.ValueOf(addr)) |
| 178 |
case "regexp.Regexp": |
| 179 |
r, err := regexp.Compile(key.String()) |
| 180 |
if err != nil { |
| 181 |
return err |
| 182 |
} |
| 183 |
fieldVal.Set(reflect.ValueOf(r)) |
| 184 |
case "text/template.Template": |
| 185 |
t, err := templates.ParseTemplate(key.String(), key.String()) |
| 186 |
if err != nil { |
| 187 |
return err |
| 188 |
} |
| 189 |
fieldVal.Set(reflect.ValueOf(t)) |
| 190 |
default: |
| 191 |
panic(fmt.Sprintf("unsupported type %s", typePath(ft))) |
| 192 |
} |
| 193 |
} |
| 194 |
default: |
| 195 |
panic(fmt.Sprintf("unsupported type %s", typePath(ft))) |
| 196 |
} |
| 197 |
return nil |
| 198 |
} |
| 199 |
|
| 200 |
func getParseMethod( |
| 201 |
section *ini.Section, key *ini.Key, |
| 202 |
struc reflect.Value, typ reflect.StructField, |
| 203 |
) reflect.Value { |
| 204 |
methodName, found := typ.Tag.Lookup("parse") |
| 205 |
if !found { |
| 206 |
return reflect.Value{} |
| 207 |
} |
| 208 |
method := struc.MethodByName(methodName) |
| 209 |
if !method.IsValid() { |
| 210 |
panic(fmt.Sprintf("(*%s).%s: method not found", |
| 211 |
struc, methodName)) |
| 212 |
} |
| 213 |
|
| 214 |
if method.Type().NumIn() != 2 || |
| 215 |
method.Type().In(0) != reflect.TypeFor[*ini.Section]() || |
| 216 |
method.Type().In(1) != reflect.TypeFor[*ini.Key]() || |
| 217 |
method.Type().NumOut() != 2 { |
| 218 |
panic(fmt.Sprintf("(*%s).%s: invalid signature, expected %s", |
| 219 |
struc.Elem().Type().Name(), methodName, |
| 220 |
"func(*ini.Section, *ini.Key) (any, error)")) |
| 221 |
} |
| 222 |
|
| 223 |
return method |
| 224 |
} |
| 225 |
|
| 226 |
func typePath(t reflect.Type) string { |
| 227 |
var prefix string |
| 228 |
if t.Kind() == reflect.Ptr { |
| 229 |
t = t.Elem() |
| 230 |
prefix = "*" |
| 231 |
} |
| 232 |
return fmt.Sprintf("%s%s.%s", prefix, t.PkgPath(), t.Name()) |
| 233 |
} |