-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask.go
75 lines (67 loc) · 1.85 KB
/
mask.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
package main
import (
"fmt"
"regexp"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
driver "github.com/pingcap/tidb/types/parser_driver"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
)
type MaskRule struct {
Columns []string `cty:"columns"`
Surrogate string `cty:"surrogate"`
PatternString string `cty:"pattern"`
Pattern *regexp.Regexp
}
var maskRuleDefaultSpec = hcldec.ObjectSpec{
"columns": columnSpec,
"surrogate": &hcldec.DefaultSpec{
Primary: &hcldec.AttrSpec{
Name: "surrogate",
Type: cty.String,
},
Default: &hcldec.LiteralSpec{Value: cty.StringVal("*")},
},
"pattern": &hcldec.DefaultSpec{
Primary: &hcldec.AttrSpec{
Name: "pattern",
Type: cty.String,
},
Default: &hcldec.LiteralSpec{Value: cty.StringVal(`[^\s]`)},
},
}
func (r *MaskRule) Apply(row *Row) error {
for _, columnName := range r.Columns {
column, ok := row.Table.Columns[columnName]
if !ok {
continue
}
currentValueExpr := (*row.Values)[column.Position-1]
if expr, ok := currentValueExpr.(*driver.ValueExpr); ok {
s, _ := expr.Datum.ToString()
expr.Datum.SetValue(r.Pattern.ReplaceAllString(s, r.Surrogate), &expr.Type)
}
}
return nil
}
func NewMaskRule(block *hcl.Block, ctx *hcl.EvalContext) (*MaskRule, hcl.Diagnostics) {
rule := &MaskRule{}
decodedSpec, diagnostics := hcldec.Decode(block.Body, maskRuleDefaultSpec, ctx)
if diagnostics.HasErrors() {
return nil, diagnostics
}
err := gocty.FromCtyValue(decodedSpec, &rule)
if err != nil {
attrRange := block.Body.MissingItemRange()
return nil, hcl.Diagnostics{
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("error while configuring %s rule: %v", "@TODO", err.Error()),
Subject: &attrRange,
},
}
}
rule.Pattern = regexp.MustCompile(rule.PatternString)
return rule, diagnostics
}