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