-
Notifications
You must be signed in to change notification settings - Fork 180
/
options.go
179 lines (141 loc) · 3.6 KB
/
options.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) 2019-2022. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells 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.
*
* Pydio Cells 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 Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package configx
import (
"context"
"github.com/spf13/cast"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
yaml "gopkg.in/yaml.v2"
json "github.com/pydio/cells/v4/common/utils/jsonx"
)
type Unmarshaler interface {
Unmarshal([]byte, interface{}) error
}
type Marshaller interface {
Marshal(interface{}) ([]byte, error)
}
type Encrypter interface {
Encrypt([]byte) (string, error)
}
type Decrypter interface {
Decrypt(string) ([]byte, error)
}
type Option func(*Options)
type Options struct {
InitData interface{}
Unmarshaler
Marshaller
Encrypter
Decrypter
// Additional callback after Set is called
SetCallback func([]string, interface{}) error
// Do not create any resources
ReadOnly bool
// Used to pass other potential options
Context context.Context
}
func WithInitData(data interface{}) Option {
return func(o *Options) {
o.InitData = data
}
}
type jsonReader struct{}
func (j *jsonReader) Unmarshal(data []byte, out interface{}) error {
var err error
switch v := out.(type) {
case proto.Message:
err = protojson.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(data, v)
default:
err = json.Unmarshal(data, &v)
}
return err
}
type jsonWriter struct{}
func (j *jsonWriter) Marshal(in interface{}) ([]byte, error) {
return json.Marshal(in)
}
func WithJSON() Option {
return func(o *Options) {
o.Unmarshaler = &jsonReader{}
o.Marshaller = &jsonWriter{}
}
}
type yamlReader struct{}
func (j *yamlReader) Unmarshal(data []byte, out interface{}) error {
return yaml.Unmarshal(data, out)
}
type yamlWriter struct{}
func (j *yamlWriter) Marshal(in interface{}) ([]byte, error) {
return yaml.Marshal(in)
}
func WithYAML() Option {
return func(o *Options) {
o.Unmarshaler = &yamlReader{}
o.Marshaller = &yamlWriter{}
}
}
type stringReader struct{}
func (j *stringReader) Unmarshal(data []byte, out interface{}) error {
if v, ok := out.(*interface{}); ok {
*v = cast.ToString(data)
}
return nil
}
type stringWriter struct{}
func (j *stringWriter) Marshal(in interface{}) ([]byte, error) {
return []byte(cast.ToString(in)), nil
}
func WithString() Option {
return func(o *Options) {
o.Unmarshaler = &stringReader{}
o.Marshaller = &stringWriter{}
}
}
func WithMarshaller(m Marshaller) Option {
return func(o *Options) {
o.Marshaller = m
}
}
func WithUnmarshaler(u Unmarshaler) Option {
return func(o *Options) {
o.Unmarshaler = u
}
}
func WithEncrypt(e Encrypter) Option {
return func(o *Options) {
o.Encrypter = e
}
}
func WithDecrypt(d Decrypter) Option {
return func(o *Options) {
o.Decrypter = d
}
}
func WithReadOnly() Option {
return func(options *Options) {
options.ReadOnly = true
}
}
func WithSetCallback(f func([]string, interface{}) error) Option {
return func(o *Options) {
o.SetCallback = f
}
}