-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
113 lines (103 loc) · 2.74 KB
/
helpers.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
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
package gioc
import (
"errors"
"reflect"
"strconv"
)
func createFactoryFromInterface(factory interface{}) *Factory {
var factoryObj *Factory
if reflect.TypeOf(factory) == reflect.TypeOf(Factory{}) {
castedFactory := factory.(Factory)
factoryObj = &castedFactory
} else {
factoryObj = &Factory{
Create: factory,
}
}
checkFactoryMethod(factoryObj.Create)
return factoryObj
}
func checkFactoryMethod(factoryMethod interface{}) {
factoryMethodType := reflect.TypeOf(factoryMethod)
if factoryMethodType.Kind() != reflect.Func {
panic("Invalid kind of factory method. Factory method can be only a function")
}
// factory method must return only one parameter - pointer to new service instance
if factoryMethodType.NumOut() != 1 {
panic("Factory must return only one parameter")
} else if factoryMethodType.Out(0).Kind() != reflect.Ptr && factoryMethodType.Out(0).Kind() != reflect.Interface {
panic("Factory must return pointer")
}
}
func getArgumentValueFromString(kind reflect.Kind, stringValue string) interface{} {
errorProcessor := func(err error, kind reflect.Kind, stringValue string) {
if err != nil {
panic("Failed to convert '" + stringValue + "' to " + kind.String())
}
}
switch kind {
case reflect.String:
return stringValue
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
intVal, conversionError := strconv.ParseInt(stringValue, 0, 64)
errorProcessor(conversionError, kind, stringValue)
switch kind {
case reflect.Int:
return int(intVal)
case reflect.Int8:
return int8(intVal)
case reflect.Int16:
return int16(intVal)
case reflect.Int32:
return int32(intVal)
}
return intVal
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
intVal, conversionError := strconv.ParseUint(stringValue, 0, 64)
errorProcessor(conversionError, kind, stringValue)
switch kind {
case reflect.Uint:
return uint(intVal)
case reflect.Uint8:
return uint8(intVal)
case reflect.Uint16:
return uint16(intVal)
case reflect.Uint32:
return uint32(intVal)
}
return intVal
case reflect.Float32:
fallthrough
case reflect.Float64:
floatVal, conversionError := strconv.ParseFloat(stringValue, 64)
errorProcessor(conversionError, kind, stringValue)
switch kind {
case reflect.Float32:
return float32(floatVal)
}
return floatVal
case reflect.Bool:
boolVal, conversionError := strconv.ParseBool(stringValue)
errorProcessor(conversionError, kind, stringValue)
return boolVal
default:
errorProcessor(errors.New("no conversion logic found"), kind, stringValue)
}
return nil
}