This repository has been archived by the owner on May 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
137 lines (110 loc) · 3.19 KB
/
core.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package goconf
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"reflect"
"strconv"
)
const (
TestMethodName = "RuntimeTest"
)
func LoadConfigFromFile(fileName string, configObject interface{}) {
configDataBytes, err := loadFile(fileName)
if err != nil {
log.Fatalf("Configuration file read error: %s", err)
}
LoadConfig(configDataBytes, configObject)
}
func LoadConfig(configBytes []byte, configObject interface{}) {
LoadDefaults(configObject)
err := json.Unmarshal(configBytes, &configObject)
if err != nil {
log.Fatalf("Configuration file marshal error: %s", err)
}
configBytes = nil
RuntimeTest(configObject)
}
func loadFile(fileName string) (fileBytes []byte, err error) {
configFilePath := getValidFullPath(fileName, getExecutePath())
_, err = os.Stat(configFilePath)
if err != nil {
return
}
fileBytes, err = ioutil.ReadFile(configFilePath)
if err != nil {
return
}
return
}
func LoadDefaults(configObject interface{}) {
v := reflect.Indirect(reflect.ValueOf(configObject))
for i := 0; i < v.NumField(); i++ {
typeField := v.Type().Field(i)
defaultField := typeField.Tag.Get("default")
if len(defaultField) == 0 {
continue
}
switch v.Field(i).Kind() {
case reflect.Bool:
boolVal, err := strconv.ParseBool(defaultField)
if err == nil {
v.Field(i).SetBool(boolVal)
}
case reflect.Int:
intVal, err := strconv.ParseInt(defaultField, 10, 0)
if err == nil {
v.Field(i).SetInt(intVal)
}
case reflect.String:
v.Field(i).SetString(defaultField)
}
}
}
func RuntimeTest(configObject interface{}) {
v := reflect.Indirect(reflect.ValueOf(configObject))
for i := 0; i < v.NumField(); i++ {
typeField := v.Type().Field(i)
if testable, err := strconv.ParseBool(typeField.Tag.Get("testable")); err != nil || testable == false {
continue
}
kindField := v.Field(i).Kind()
if kindField != reflect.Struct && kindField != reflect.Ptr {
log.Printf("Warning: Config property `%s` cannot be marked as testable, only `struct` and `ptr` allowed.\n", typeField.Name)
continue
}
if v.Field(i).IsNil() {
log.Fatalf("Warning: Config property `%s` marked as testable, but was not initialized.\n", typeField.Name)
continue
}
ptrField := v.Field(i)
if ptrField.Kind() == reflect.Struct {
ptrField = ptrField.Addr()
}
method := ptrField.MethodByName(TestMethodName)
if !method.IsValid() {
log.Printf("Warning: Config property `%s` marked as testable, but no method `%s` was found.\n", typeField.Name, TestMethodName)
continue
}
methodType, _ := ptrField.Type().MethodByName(TestMethodName)
methodTypeType := methodType.Type
if methodTypeType.NumOut() != 2 || methodTypeType.Out(0) != reflect.TypeOf((*error)(nil)).Elem() || methodTypeType.Out(1) != reflect.TypeOf((*bool)(nil)).Elem() {
log.Printf("Warning: Method `%s` in property `%s` has invalid parameters, should be (bool, error).", TestMethodName, typeField.Name)
continue
}
testOutput := method.Call([]reflect.Value{})
var err error = nil
if ie := testOutput[0].Interface(); ie != nil {
err = ie.(error)
}
fatal := testOutput[1].Interface().(bool)
if err != nil {
if fatal {
log.Fatalln(err)
} else {
log.Println(err)
}
}
}
}