/
config.go
122 lines (116 loc) · 2.94 KB
/
config.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
package main
import (
"errors"
"io/ioutil"
"reflect"
"strconv"
"strings"
)
// OracleConfig mysql server config
type OracleConfig struct {
Host string `ini:"host"`
Port int `ini:"port"`
Username string `ini:"username"`
Password string `ini:"password"`
Sid string `ini:"sid"`
}
// Config ...
type Config struct {
OracleConfig `ini:"oracle"`
}
func checkIni(filename string, data interface{}) (lines []string, err error) {
t := reflect.TypeOf(data)
if t.Kind() != reflect.Ptr {
err = errors.New("data should be a pointer")
return
}
if t.Elem().Kind() != reflect.Struct {
err = errors.New("data should be a struct")
return
}
buf, err := ioutil.ReadFile(filename)
if err != nil {
return
}
lines = strings.Split(string(buf), "\r\n")
return
}
// LoadIni load ini file
func LoadIni(filename string, data interface{}) (err error) {
t := reflect.ValueOf(data).Type().Elem()
v := reflect.ValueOf(data).Elem()
lines, err := checkIni(filename, data)
var structName string
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, ";") || strings.HasPrefix(line, "#") || len(line) == 0 {
continue
}
if strings.HasPrefix(line, "[") {
if line[0] != '[' || line[len(line)-1] != ']' {
err = errors.New("syntax error")
return
}
sectionName := strings.TrimSpace(line[1 : len(line)-1])
if len(sectionName) == 0 {
err = errors.New("syntax error")
return
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if sectionName == field.Tag.Get("ini") {
structName = field.Name
// fmt.Println("sec:", structName)
}
}
} else {
if strings.Index(line, "=") == -1 || strings.HasPrefix(line, "=") {
err = errors.New("syntax error")
return
}
index := strings.Index(line, "=")
iniKey := strings.TrimSpace(line[:index])
iniValue := strings.TrimSpace(line[index+1:])
if t.Kind() != reflect.Struct {
err = errors.New("data should be a struct")
return
}
var fieldType reflect.StructField
sValue := v.FieldByName(structName)
for i := 0; i < sValue.NumField(); i++ {
field := sValue.Type().Field(i)
if field.Tag.Get("ini") == iniKey {
fieldType = field
break
}
}
fieldObj := sValue.FieldByName(fieldType.Name)
switch fieldType.Type.Kind() {
case reflect.String:
fieldObj.SetString(iniValue)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var valueInt int64
valueInt, err = strconv.ParseInt(iniValue, 10, 64)
if err != nil {
return
}
fieldObj.SetInt(valueInt)
case reflect.Bool:
var valueBool bool
valueBool, err = strconv.ParseBool(iniValue)
if err != nil {
return
}
fieldObj.SetBool(valueBool)
case reflect.Float32, reflect.Float64:
var valueFloat float64
valueFloat, err = strconv.ParseFloat(iniValue, 64)
if err != nil {
return
}
fieldObj.SetFloat(valueFloat)
}
}
}
return
}