-
Notifications
You must be signed in to change notification settings - Fork 493
/
config_util.go
141 lines (129 loc) · 4.23 KB
/
config_util.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package helper
import (
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"reflect"
)
// DecodeStruct validates `input` struct with `validator` and set it into viper
// `tag` represent the fields when setting config, and the fields with `tag` shall prevail.
// `input` must be a pointer
func DecodeStruct(output *viper.Viper, input interface{}, data map[string]interface{}, tag string) error {
// update fields from request body
err := mapstructure.Decode(data, input)
if err != nil {
return err
}
// validate fields with `validate` tag
vld := validator.New()
err = vld.Struct(input)
if err != nil {
return err
}
err = validator.New().Struct(input)
if err != nil {
return err
}
vf := reflect.ValueOf(input)
if vf.Kind() != reflect.Ptr {
panic("input is not a pointer")
}
tf := reflect.Indirect(vf).Type()
fieldTags := make([]string, 0)
fieldNames := make([]string, 0)
fieldTypes := make([]reflect.Type, 0)
walkFields(tf, &fieldNames, &fieldTypes, &fieldTags, tag)
length := len(fieldNames)
for i := 0; i < length; i++ {
fieldName := fieldNames[i]
fieldType := fieldTypes[i]
fieldTag := fieldTags[i]
// Check if the first letter is uppercase (indicates a public element, accessible)
ascii := rune(fieldName[0])
if int(ascii) < int('A') || int(ascii) > int('Z') {
continue
}
// View their tags in order to filter out members who don't have a valid tag set
if fieldTag == "" {
continue
}
vfField := vf.Elem().FieldByName(fieldName)
switch fieldType.String() {
case "string":
output.Set(fieldTag, vfField.String())
case "int", "int64":
output.Set(fieldTag, vfField.Int())
case "float64":
output.Set(fieldTag, vfField.Float())
default:
}
}
return nil
}
// EncodeStruct encodes struct from viper
// `tag` represent the fields when setting config, and the fields with `tag` shall prevail.
// `object` must be a pointer
func EncodeStruct(input *viper.Viper, output interface{}, tag string) error {
vf := reflect.ValueOf(output)
if vf.Kind() != reflect.Ptr {
panic("output is not a pointer")
}
tf := reflect.Indirect(vf).Type()
fieldTags := make([]string, 0)
fieldNames := make([]string, 0)
fieldTypes := make([]reflect.Type, 0)
walkFields(tf, &fieldNames, &fieldTypes, &fieldTags, tag)
length := len(fieldNames)
for i := 0; i < length; i++ {
fieldName := fieldNames[i]
fieldType := fieldTypes[i]
fieldTag := fieldTags[i]
// Check if the first letter is uppercase (indicates a public element, accessible)
ascii := rune(fieldName[0])
if int(ascii) < int('A') || int(ascii) > int('Z') {
continue
}
// View their tags in order to filter out members who don't have a valid tag set
if fieldTag == "" {
continue
}
vfField := vf.Elem().FieldByName(fieldName)
switch fieldType.String() {
case "string":
vfField.SetString(input.GetString(fieldTag))
case "int", "int64":
vfField.SetInt(input.GetInt64(fieldTag))
case "float64":
vfField.SetFloat(input.GetFloat64(fieldTag))
default:
}
}
return nil
}
func walkFields(t reflect.Type, fieldNames *[]string, fieldTypes *[]reflect.Type, fieldTags *[]string, tag string) {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Type.Kind() == reflect.Struct {
walkFields(field.Type, fieldNames, fieldTypes, fieldTags, tag)
} else {
fieldTag := field.Tag.Get(tag)
*fieldNames = append(*fieldNames, field.Name)
*fieldTypes = append(*fieldTypes, field.Type)
*fieldTags = append(*fieldTags, fieldTag)
}
}
}