-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathconfig.go
More file actions
181 lines (162 loc) · 4.11 KB
/
Copy pathconfig.go
File metadata and controls
181 lines (162 loc) · 4.11 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package eosc
import (
"fmt"
"reflect"
"strings"
)
type RequireId string
var (
_RequireTypeName = TypeNameOf(RequireId(""))
_RequireSliceTypeName = TypeNameOf([]RequireId{})
)
func TypeNameOf(v interface{}) string {
return TypeName(reflect.TypeOf(v))
}
func TypeName(t reflect.Type) string {
if t.Kind() == reflect.Ptr {
return fmt.Sprint("*", TypeName(t.Elem()))
}
return fmt.Sprintf("%s.%s", t.PkgPath(), t.String())
}
func CheckConfig(v interface{}, workers IWorkers) (map[RequireId]interface{}, error) {
value := reflect.ValueOf(v)
ws, err := checkConfig(value, workers)
if err != nil {
return nil, fmt.Errorf("%s:%w", TypeNameOf(v), err)
}
if ws == nil {
ws = make(map[RequireId]interface{})
}
return ws, nil
}
func checkConfig(v reflect.Value, workers IWorkers) (map[RequireId]interface{}, error) {
kind := v.Kind()
switch kind {
case reflect.Ptr:
if v.IsNil() {
return nil, nil
}
return checkConfig(v.Elem(), workers)
case reflect.Struct:
t := v.Type()
n := v.NumField()
requires := make(map[RequireId]interface{})
for i := 0; i < n; i++ {
if ws, err := checkField(t.Field(i), v.Field(i), workers); err != nil {
return nil, err
} else {
requires = merge(requires, ws)
}
}
return requires, nil
case reflect.Slice:
n := v.Len()
requires := make(map[RequireId]interface{})
for i := 0; i < n; i++ {
if ws, err := checkConfig(v.Index(i), workers); err != nil {
return nil, err
} else {
requires = merge(requires, ws)
}
}
return requires, nil
case reflect.Map:
it := v.MapRange()
requires := make(map[RequireId]interface{})
for it.Next() {
if ws, err := checkConfig(it.Value(), workers); err != nil {
return nil, err
} else {
requires = merge(requires, ws)
}
}
return requires, nil
default:
return nil, nil
}
return nil, ErrorConfigFieldUnknown
}
func checkField(f reflect.StructField, v reflect.Value, workers IWorkers) (map[RequireId]interface{}, error) {
typeName := TypeName(v.Type())
switch typeName {
case _RequireTypeName:
{
id := v.String()
if id == "" {
require, has := f.Tag.Lookup("require")
if !has || strings.ToLower(require) != "false" {
return nil, fmt.Errorf("%s:%w", f.Name, ErrorRequire)
}
return nil, nil
}
target, has := workers.Get(id)
if !has {
require, has := f.Tag.Lookup("require")
if !has || strings.ToLower(require) != "false" {
return nil, fmt.Errorf("require %s:%w", id, ErrorWorkerNotExits)
}
return nil, nil
}
skill, has := f.Tag.Lookup("skill")
if !has {
return nil, fmt.Errorf("field %s type %s :%w", f.Name, typeName, ErrorNotGetSillForRequire)
}
if !target.CheckSkill(skill) {
return nil, fmt.Errorf(" %s type %s value %s:%w", f.Name, typeName, id, ErrorTargetNotImplementSkill)
}
return map[RequireId]interface{}{RequireId(id): target}, nil
}
case _RequireSliceTypeName:
{
skill, has := f.Tag.Lookup("skill")
if !has {
return nil, fmt.Errorf("field %s type %s :%w", f.Name, typeName, ErrorNotGetSillForRequire)
}
require, requireHas := f.Tag.Lookup("require")
n := v.Len()
requires := make(map[RequireId]interface{})
for i := 0; i < n; i++ {
id := v.Index(i).String()
if id == "" {
continue
}
target, has := workers.Get(id)
if !has {
if !requireHas || strings.ToLower(require) != "false" {
return nil, fmt.Errorf("require %s:%w", id, ErrorWorkerNotExits)
}
}
if !target.CheckSkill(skill) {
return nil, fmt.Errorf(" %s type %s value %s:%w", f.Name, typeName, id, ErrorTargetNotImplementSkill)
}
requires[RequireId(id)] = target
}
return requires, nil
}
default:
{
return checkConfig(v, workers)
}
}
}
func merge(dist map[RequireId]interface{}, source map[RequireId]interface{}) map[RequireId]interface{} {
if dist == nil && source == nil {
return nil
}
if source == nil {
return dist
}
if dist == nil {
return source
}
for k, v := range source {
dist[k] = v
}
return dist
}
func newConfig(t reflect.Type) interface{} {
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
return reflect.New(t).Interface()
}