-
Notifications
You must be signed in to change notification settings - Fork 38
/
patch.go
44 lines (35 loc) · 1.4 KB
/
patch.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
package utils
import (
"regexp"
)
// Patch is the patch object to provider a converter function
type Patch interface {
Patch([]byte) []byte
}
// RegexpPatcher a patch object to provider a converter function from regular expression
type RegexpPatcher struct {
pattern *regexp.Regexp
replacement string
}
// NewRegexpPatcher will return a patch object to provider a converter function from regular expression
func NewRegexpPatcher(regex string, repl string) *RegexpPatcher {
return &RegexpPatcher{
pattern: regexp.MustCompile(regex),
replacement: repl,
}
}
// Patch will convert a bytes to another bytes with patch rules
func (p *RegexpPatcher) Patch(body []byte) []byte {
// TODO: ensure why the pattern will be disabled when there are multiple goroutines for bytes replacement
return []byte(p.PatchString(string(body)))
}
// PatchString will convert a string to another string with patch rules
func (p *RegexpPatcher) PatchString(body string) string {
return p.pattern.ReplaceAllString(body, p.replacement)
}
// RetCodePatcher will convert `RetCode` as integer
var RetCodePatcher = NewRegexpPatcher(`"RetCode":\s?"(\d+)"`, `"RetCode": $1`)
// PortPatcher will convert `Port` as integer
var PortPatcher = NewRegexpPatcher(`"Port":\s?"(\d+)"`, `"Port": $1`)
// FrequencePatcher will convert `Frequence` as float64
var FrequencePatcher = NewRegexpPatcher(`"Frequence":\s?"([\d.]+)"`, `"Frequence": $1`)