-
Notifications
You must be signed in to change notification settings - Fork 8
/
random_bool.go
116 lines (97 loc) · 3.2 KB
/
random_bool.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
// Copyright 2023 Greenmask
//
// 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.
package transformers
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/greenmaskio/greenmask/internal/db/postgres/transformers/utils"
"github.com/greenmaskio/greenmask/pkg/toolkit"
)
var RandomBoolTransformerDefinition = utils.NewDefinition(
utils.NewTransformerProperties(
"RandomBool",
"Generate random bool",
),
NewRandomBoolTransformer,
toolkit.MustNewParameter(
"column",
"column name",
).SetIsColumn(toolkit.NewColumnProperties().
SetAffected(true).
SetAllowedColumnTypes("bool"),
).SetRequired(true),
toolkit.MustNewParameter(
"keep_null",
"indicates that NULL values must not be replaced with transformed values",
).SetDefaultValue(toolkit.ParamsValue("true")),
)
type RandomBoolTransformer struct {
columnName string
keepNull bool
rand *rand.Rand
affectedColumns map[int]string
columnIdx int
}
func NewRandomBoolTransformer(ctx context.Context, driver *toolkit.Driver, parameters map[string]*toolkit.Parameter) (utils.Transformer, toolkit.ValidationWarnings, error) {
var columnName string
var keepNull bool
p := parameters["column"]
if _, err := p.Scan(&columnName); err != nil {
return nil, nil, fmt.Errorf(`unable to scan "column" param: %w`, err)
}
idx, _, ok := driver.GetColumnByName(columnName)
if !ok {
return nil, nil, fmt.Errorf("column with name %s is not found", columnName)
}
affectedColumns := make(map[int]string)
affectedColumns[idx] = columnName
p = parameters["keep_null"]
if _, err := p.Scan(&keepNull); err != nil {
return nil, nil, fmt.Errorf(`unable to scan "keep_null" param: %w`, err)
}
return &RandomBoolTransformer{
columnName: columnName,
keepNull: keepNull,
rand: rand.New(rand.NewSource(time.Now().UnixMicro())),
affectedColumns: affectedColumns,
columnIdx: idx,
}, nil, nil
}
func (rbt *RandomBoolTransformer) GetAffectedColumns() map[int]string {
return rbt.affectedColumns
}
func (rbt *RandomBoolTransformer) Init(ctx context.Context) error {
return nil
}
func (rbt *RandomBoolTransformer) Done(ctx context.Context) error {
return nil
}
func (rbt *RandomBoolTransformer) Transform(ctx context.Context, r *toolkit.Record) (*toolkit.Record, error) {
val, err := r.GetRawColumnValueByIdx(rbt.columnIdx)
if err != nil {
return nil, fmt.Errorf("unable to scan value: %w", err)
}
if val.IsNull && rbt.keepNull {
return r, nil
}
if err := r.SetColumnValueByIdx(rbt.columnIdx, utils.RandomBool(rbt.rand)); err != nil {
return nil, fmt.Errorf("unable to set new value: %w", err)
}
return r, nil
}
func init() {
utils.DefaultTransformerRegistry.MustRegister(RandomBoolTransformerDefinition)
}