-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
config_decoder.go
114 lines (98 loc) · 2.88 KB
/
config_decoder.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
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package settings
import (
"strings"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/admpub/nging/v3/application/dbschema"
)
type Decoder func(v *dbschema.NgingConfig, dbschemaMap echo.H) error
var decoders = map[string]Decoder{}
func Decoders() map[string]Decoder {
return decoders
}
func GetDecoder(group string) Decoder {
ps, _ := decoders[group]
return ps
}
type Codec interface {
Decode(string, ...string) string
Encode(string, ...string) string
}
// RegisterDecoder 注册配置值解码器(用于从数据库读出来之后的解码操作)
// 名称支持"group"或"group.key"两种格式,例如:
// settings.RegisterDecoder(`sms`,...)对整个sms组的配置有效
// settings.RegisterDecoder(`sms.twilio`,...)对sms组内key为twilio的配置有效
func RegisterDecoder(group string, decoder Decoder) {
decoders[group] = decoder
}
func DecodeConfigValue(v *dbschema.NgingConfig, decoder Decoder) (echo.H, error) {
if v.Encrypted == `Y` {
v.Value = echo.Get(`DefaultConfig`).(Codec).Decode(v.Value)
}
r := echo.H(v.AsMap())
var err error
if decoder != nil {
err = decoder(v, r)
if err != nil {
return nil, err
}
}
if subDecoder := GetDecoder(v.Group + `.` + v.Key); subDecoder != nil {
err = subDecoder(v, r)
} else {
err = DefaultDecoder(v, r)
}
return r, err
}
func DecodeConfig(v *dbschema.NgingConfig, cfg echo.H, decoder Decoder) (echo.H, error) {
r, e := DecodeConfigValue(v, decoder)
if e != nil {
return cfg, e
}
cfg.Set(v.Key, r)
return cfg, nil
}
func DefaultDecoder(v *dbschema.NgingConfig, r echo.H) error {
if r.Has(`ValueObject`) {
return nil
}
switch v.Type {
case `json`:
jsonData := echo.H{}
if len(v.Value) > 0 {
err := com.JSONDecode([]byte(v.Value), &jsonData)
if err != nil {
return err
}
}
r[`ValueObject`] = jsonData
case `list`:
jsonData := echo.H{}
if len(v.Value) > 0 {
com.JSONDecode([]byte(v.Value), &jsonData)
}
v.Value = strings.Trim(v.Value, `,`)
if len(v.Value) > 0 {
r[`ValueObject`] = strings.Split(v.Value, `,`)
} else {
r[`ValueObject`] = []string{}
}
default:
r[`ValueObject`] = nil
}
return nil
}