-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgosh.go
160 lines (133 loc) · 2.59 KB
/
gosh.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package gosh
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
capi "github.com/hashicorp/consul/api"
)
// Config consul 地址配置
type Config struct {
Address string
}
// DefaultConfig ...
func DefaultConfig() *Config {
c := &Config{}
c.Address = "127.0.0.1:8500"
return c
}
// Gosh ...
type Gosh struct {
client *capi.Client
}
// NewClient ...
func NewClient(c *Config) (*Gosh, error) {
config := capi.DefaultConfig()
config.Address = c.Address
consul, err := capi.NewClient(config)
if err != nil {
return nil, err
}
g := &Gosh{}
g.client = consul
return g, nil
}
// Unmarshal ...
func (g *Gosh) Unmarshal(v interface{}) error {
var ops capi.KVTxnOps
rv := reflect.ValueOf(v).Elem()
if !rv.IsValid() {
return errors.New("参数未初始化")
}
rt := reflect.TypeOf(v).Elem()
ndx := 0
dt := make(map[int]string)
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
key := field.Tag.Get("consul")
if key != "" {
op := capi.KVTxnOp{
Verb: capi.KVGet,
Key: key,
}
ops = append(ops, &op)
dt[ndx] = field.Name
ndx++
}
}
ok, res, _, err := g.client.KV().Txn(ops, nil)
if err != nil {
return err
}
if !ok {
str := ""
for i := 0; i < len(res.Errors); i++ {
str += fmt.Sprintf("%v\n", res.Errors[i].What)
}
return errors.New(str)
}
obj := make(map[string]interface{})
for i, each := range res.Results {
key := dt[i]
switch f := rv.FieldByName(key); f.Kind() {
case reflect.String:
obj[key] = string(each.Value)
case
reflect.Interface,
reflect.Map,
reflect.Slice,
reflect.Ptr,
reflect.Struct,
reflect.Bool:
var x interface{}
err := json.Unmarshal(each.Value, &x)
if err != nil {
return err
}
obj[key] = x
case
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
n, err := strconv.ParseInt(string(each.Value), 10, 64)
if err != nil || f.OverflowInt(n) {
return err
}
obj[key] = n
case
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr:
n, err := strconv.ParseUint(string(each.Value), 10, 64)
if err != nil || f.OverflowUint(n) {
return err
}
obj[key] = n
case
reflect.Float32,
reflect.Float64:
n, err := strconv.ParseFloat(string(each.Value), f.Type().Bits())
if err != nil || f.OverflowFloat(n) {
return err
}
obj[key] = n
default:
return fmt.Errorf("not support %v", f.Type())
}
}
b, err := json.Marshal(obj)
if err != nil {
return nil
}
err = json.Unmarshal(b, &v)
if err != nil {
return err
}
return nil
}