-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
mody.go
83 lines (68 loc) · 1.63 KB
/
mody.go
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
// Package mody allows to modify fields in an object.
package mody
import (
"encoding/json"
"fmt"
"reflect"
"strings"
)
const delimiter = "."
// Update sets the value val of the named field inside
// the passed struct instance.
func Update(v interface{}, field string, val interface{}) (err error) {
return update(reflect.ValueOf(v), field, val, true)
}
// UpdateJson unmarshals valStr into a value and tries
// to apply it to the named field in v.
func UpdateJson(v interface{}, field, valStr string) (err error) {
var val interface{}
if err = json.Unmarshal([]byte(valStr), &val); err != nil {
return
}
return Update(v, field, val)
}
func update(
v reflect.Value,
field string, val interface{},
isTopLvl bool,
) (err error) {
split := strings.SplitN(field, delimiter, 2)
isPtr := v.Kind() == reflect.Ptr
if isTopLvl && !isPtr {
err = ErrTypeMustBePointer
return
}
if isPtr {
v = v.Elem()
}
fv := v.FieldByName(split[0])
if !fv.IsValid() {
err = ErrFieldNotExistent
return
}
if len(split) == 2 {
return update(fv, split[1], val, false)
}
valT := reflect.ValueOf(val)
if valT.Kind() != fv.Kind() {
if fv.Kind() == reflect.String || !valT.CanConvert(fv.Type()) {
err = ErrTypeMissmatch
return
}
valT = valT.Convert(fv.Type())
}
fv.Set(valT)
return
}
// Catch executes the passed function and recovers
// any occuring panic inside it which is then
// returned as error.
func Catch(fn func()) (err error) {
defer func() {
if rec := recover(); rec != nil {
err = fmt.Errorf("%v", rec)
}
}()
fn()
return
}