-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
setters.go
53 lines (46 loc) · 1.33 KB
/
setters.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
package queryparam
import (
"fmt"
"reflect"
)
// GenericType is a reflect.Type we can use to identity generic value setters.
var GenericType = reflect.TypeOf(struct{}{})
// DefaultValueSetters returns a set of default value setters.
func DefaultValueSetters() map[reflect.Type]ValueSetter {
return map[reflect.Type]ValueSetter{
GenericType: GenericSetter,
reflect.TypeOf(int32(0)): Int32ValueSetter,
reflect.TypeOf(float32(0)): Float32ValueSetter,
}
}
// recoverPanic recovers from a panic and sets the panic value into the given error.
func recoverPanic(err *error) func() {
return func() {
rec := recover()
if rec != nil {
if e, ok := rec.(error); ok {
*err = e
} else {
*err = fmt.Errorf("%s", rec)
}
}
}
}
// GenericSetter sets the targets value.
func GenericSetter(value reflect.Value, target reflect.Value) (err error) {
defer recoverPanic(&err)()
target.Set(value)
return
}
// Int32ValueSetter sets the targets value to an int32.
func Int32ValueSetter(value reflect.Value, target reflect.Value) (err error) {
defer recoverPanic(&err)()
target.SetInt(value.Int())
return nil
}
// Float32ValueSetter sets the targets value to a float32.
func Float32ValueSetter(value reflect.Value, target reflect.Value) (err error) {
defer recoverPanic(&err)()
target.SetFloat(value.Float())
return nil
}