forked from corestoreio/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modifier.go
228 lines (196 loc) · 6.46 KB
/
modifier.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed 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.
//go:generate easyjson -build_tags "csall json http proto" $GOFILE
package observer
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"sync"
"github.com/corestoreio/errors"
"github.com/corestoreio/pkg/config"
"github.com/corestoreio/pkg/util/bufferpool"
"github.com/corestoreio/pkg/util/gzippool"
"github.com/corestoreio/pkg/util/hashpool"
)
// ModifierFn defines the function signature for altering the data.
type ModifierFn func(config.Path, []byte) ([]byte, error)
type modReg struct {
sync.RWMutex
pool map[string]ModifierFn
}
var modifierRegistry = &modReg{
pool: map[string]ModifierFn{
"upper": toUpper,
"lower": toLower,
"trim": trim,
"title": toTitle,
"base64_encode": base64Encode,
"base64_decode": base64Decode,
"hex_encode": hexEncode,
"hex_decode": hexDecode,
"sha256": hash256, // as an example, but must register
"gzip": dataGzip,
"gunzip": dataGunzip,
},
}
// RegisterModifier adds a new modification function to the global registry
// and might overwrite previously set entries. Access to the global registry can
// be achieved via function NewModifier.
func RegisterModifier(typeName string, h ModifierFn) {
modifierRegistry.Lock()
defer modifierRegistry.Unlock()
modifierRegistry.pool[typeName] = h
}
func availableModifiers(ret ...string) []string {
modifierRegistry.RLock()
defer modifierRegistry.RUnlock()
for n := range modifierRegistry.pool {
ret = append(ret, n)
}
return ret
}
// ModifierArg defines the modifiers to use to alter a string received from the
// config.Service.
//easyjson:json
type ModifierArg struct {
// Funcs defines a list of function names. Currently supported: upper,
// lower, trim, title, base64_encode, base64_decode, sha256 (must one time
// be registered in hashpool package), gzip, gunzip. Additional all other
// custom modifier functions registered via RegisterModifier are
// supported.
Funcs []string `json:"funcs,omitempty"`
}
// NewModifier creates a new type specific modifier.
func NewModifier(data ModifierArg) (config.Observer, error) {
ia := &modifiers{
opType: append([]string{}, data.Funcs...), // copy data
opFns: make([]ModifierFn, 0, len(data.Funcs)),
}
modifierRegistry.RLock()
defer modifierRegistry.RUnlock()
for _, mod := range data.Funcs {
h, ok := modifierRegistry.pool[mod]
if !ok || h == nil {
return nil, errors.NotSupported.Newf("[config/observer] Modifier %q not found in list %v.", mod, availableModifiers())
}
ia.opFns = append(ia.opFns, h)
}
return ia, nil
}
// MustNewModifier same as NewModifier but panics on error.
func MustNewModifier(data ModifierArg) config.Observer {
o, err := NewModifier(data)
if err != nil {
panic(err)
}
return o
}
// modifiers must be used to prevent race conditions during initialization.
// That is the reason we have a separate struct for JSON handling. Having two
// structs allows to refrain from using Locks.
type modifiers struct {
opType []string
opFns []ModifierFn
}
// Observe validates the given rawData value. This functions runs in a hot path.
func (v *modifiers) Observe(p config.Path, rawData []byte, found bool) (rawData2 []byte, err error) {
rawData2 = rawData
for idx, valFn := range v.opFns {
if rawData2, err = valFn(p, rawData2); err != nil {
return nil, errors.Interrupted.New(err, "[config/observer] Function %q interrupted", v.opType[idx])
}
}
return rawData2, nil
}
// as long as we don't see a use case for those modifiers in other packages,
// they stay private. might be refactored later.
func trim(_ config.Path, data []byte) ([]byte, error) {
return bytes.TrimSpace(data), nil
}
func toUpper(_ config.Path, data []byte) ([]byte, error) {
return bytes.ToUpper(data), nil
}
func toLower(_ config.Path, data []byte) ([]byte, error) {
return bytes.ToLower(data), nil
}
func toTitle(_ config.Path, data []byte) ([]byte, error) {
return bytes.Title(data), nil
}
func base64Encode(_ config.Path, src []byte) (dst []byte, _ error) {
dst = make([]byte, base64.StdEncoding.EncodedLen(len(src)))
base64.StdEncoding.Encode(dst, src)
return
}
func base64Decode(_ config.Path, src []byte) (dst []byte, err error) {
dst = make([]byte, base64.StdEncoding.DecodedLen(len(src)))
_, err = base64.StdEncoding.Decode(dst, src)
return
}
func hexEncode(_ config.Path, src []byte) (dst []byte, _ error) {
dst = make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src)
return dst, nil
}
func hexDecode(_ config.Path, src []byte) (dst []byte, err error) {
dst = make([]byte, hex.DecodedLen(len(src)))
_, err = hex.Decode(dst, src)
return
}
// hash256 prefix the fully qualified path to src and then hashes it. Higher
// security.
func hash256(p config.Path, src []byte) ([]byte, error) {
tnk, err := hashpool.FromRegistry("sha256")
if err != nil {
return nil, errors.WithStack(err)
}
buf := bufferpool.Get()
defer bufferpool.Put(buf)
if err := p.AppendFQ(buf); err != nil {
return nil, errors.Wrapf(err, "[config/observer] SHA256 with path %q", p.String())
}
buf.Write(src)
var dst [sha256.Size]byte
return tnk.Sum(buf.Bytes(), dst[:0]), nil
}
func dataGzip(_ config.Path, src []byte) (dst []byte, _ error) {
var buf bytes.Buffer
buf.Grow(len(src) * 9 / 10) // *0.9
zw := gzippool.GetWriter(&buf)
defer gzippool.PutWriter(zw)
if _, err := zw.Write(src); err != nil {
return nil, errors.WithStack(err)
}
if err := zw.Close(); err != nil {
return nil, errors.WithStack(err)
}
return buf.Bytes(), nil
}
func dataGunzip(_ config.Path, src []byte) (dst []byte, _ error) {
r := bufferpool.GetReader(src)
zr := gzippool.GetReader(r)
defer func() {
bufferpool.PutReader(r)
gzippool.PutReader(zr)
}()
var buf bytes.Buffer
if _, err := buf.ReadFrom(zr); err != nil {
return nil, errors.WithStack(err)
}
if err := zr.Close(); err != nil {
return nil, errors.WithStack(err)
}
return buf.Bytes(), nil
}