Skip to content

Commit

Permalink
feat: conditional transformer initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wwoytenko committed Oct 25, 2024
1 parent d58dfb7 commit 66accc5
Show file tree
Hide file tree
Showing 40 changed files with 153 additions and 64 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Masterminds/sprig/v3 v3.3.0
github.com/aws/aws-sdk-go v1.55.5
github.com/dchest/siphash v1.2.3
github.com/expr-lang/expr v1.16.7
github.com/ggwhite/go-masker v1.1.0
github.com/go-faker/faker/v4 v4.5.0
github.com/google/uuid v1.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/siphash v1.2.3 h1:QXwFc8cFOR2dSa/gE6o/HokBMWtLUaNDVd+22aKHeEA=
github.com/dchest/siphash v1.2.3/go.mod h1:0NvQU092bT0ipiFN++/rXm69QG9tVxLAlQHIXMPAkHc=
github.com/expr-lang/expr v1.16.7 h1:gCIiHt5ODA0xIaDbD0DPKyZpM9Drph3b3lolYAYq2Kw=
github.com/expr-lang/expr v1.16.7/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Expand Down
2 changes: 1 addition & 1 deletion internal/db/postgres/context/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func validateAndBuildTablesConfig(
// InitTransformation toolkit
if len(tableCfg.Transformers) > 0 {
for _, tc := range tableCfg.Transformers {
transformer, initWarnings, err := initTransformer(ctx, driver, tc, registry, types)
transformer, initWarnings, err := initTransformer(ctx, driver, tc, registry)
if len(initWarnings) > 0 {
for _, w := range initWarnings {
// Enriching the tables context into meta
Expand Down
4 changes: 2 additions & 2 deletions internal/db/postgres/context/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func initTransformer(
ctx context.Context, d *toolkit.Driver,
c *domains.TransformerConfig,
r *transformersUtils.TransformerRegistry,
types []*toolkit.Type,
) (*transformersUtils.TransformerContext, toolkit.ValidationWarnings, error) {
// TODO: Create
var totalWarnings toolkit.ValidationWarnings
td, ok := r.Get(c.Name)
if !ok {
Expand All @@ -42,7 +42,7 @@ func initTransformer(
}))
return nil, totalWarnings, nil
}
transformer, warnings, err := td.Instance(ctx, d, c.Params, c.DynamicParams)
transformer, warnings, err := td.Instance(ctx, d, c.Params, c.DynamicParams, c.When)
if err != nil {
return nil, nil, fmt.Errorf("unable to init transformer: %w", err)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/db/postgres/dumpers/transformation_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,15 @@ func (tp *TransformationPipeline) Init(ctx context.Context) error {

func (tp *TransformationPipeline) TransformSync(ctx context.Context, r *toolkit.Record) (*toolkit.Record, error) {
var err error
var needTransform bool
for _, t := range tp.table.TransformersContext {
needTransform, err = t.EvaluateWhen()
if err != nil {
return nil, NewDumpError(tp.table.Schema, tp.table.Name, tp.line, fmt.Errorf("error evaluating when condition: %w", err))
}
if !needTransform {
continue
}
_, err = t.Transformer.Transform(ctx, r)
if err != nil {
return nil, NewDumpError(tp.table.Schema, tp.table.Name, tp.line, err)
Expand Down
4 changes: 2 additions & 2 deletions internal/db/postgres/transformers/column_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func (cc *ColumnContext) GetValue() (any, error) {
}

func (cc *ColumnContext) GetRawValue() (any, error) {
return cc.rc.GetRawColumnValue(cc.columnName)
return cc.rc.GetColumnRawValue(cc.columnName)
}

func (cc *ColumnContext) GetColumnValue(name string) (any, error) {
return cc.rc.GetColumnValue(name)
}

func (cc *ColumnContext) GetColumnRawValue(name string) (any, error) {
return cc.rc.GetRawColumnValue(name)
return cc.rc.GetColumnRawValue(name)
}

func (cc *ColumnContext) EncodeValue(v any) (any, error) {
Expand Down
6 changes: 6 additions & 0 deletions internal/db/postgres/transformers/dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestDictTransformer_Transform_with_fail(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -85,6 +86,7 @@ func TestDictTransformer_Transform_validation_error(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.NotEmpty(t, warnings)
Expand All @@ -101,6 +103,7 @@ func TestDictTransformer_Transform_validation_error(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.NotEmpty(t, warnings)
Expand All @@ -121,6 +124,7 @@ func TestDictTransformer_Transform_error_not_matched(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -149,6 +153,7 @@ func TestDictTransformer_Transform_use_default(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -180,6 +185,7 @@ func TestDictTransformer_Transform_with_int_values(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
10 changes: 5 additions & 5 deletions internal/db/postgres/transformers/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type EmailTransformer struct {
originalDomain []byte
randomBytesBuf []byte
hexEncodedRandomBytesBuf []byte
rrctx *RoRecordContext
rctx *toolkit.RecordContext
}

func NewEmailTransformer(ctx context.Context, driver *toolkit.Driver, parameters map[string]toolkit.Parameterizer) (utils.Transformer, toolkit.ValidationWarnings, error) {
Expand Down Expand Up @@ -142,7 +142,7 @@ func NewEmailTransformer(ctx context.Context, driver *toolkit.Driver, parameters
return nil, nil, fmt.Errorf(`unable to scan "domain_part_template" param: %w`, err)
}

rrctx := NewRoRecordContext()
rrctx := toolkit.NewRecordContext()
funcMap := toolkit.FuncMap()
if localPartTemplate != "" || domainTemplate != "" {
for _, c := range driver.Table.Columns {
Expand Down Expand Up @@ -208,8 +208,8 @@ func NewEmailTransformer(ctx context.Context, driver *toolkit.Driver, parameters
domainTemplate: domainTmpl,
validate: validate,
buf: bytes.NewBuffer(nil),
hexEncodedRandomBytesBuf: make([]byte, hex.EncodedLen(maxLength)),
rrctx: rrctx,
hexEncodedRandomBytesBuf: make([]byte, hex.EncodedLen(emailTransformerGeneratorSize)),
rctx: rrctx,
}, nil, nil
}

Expand Down Expand Up @@ -264,7 +264,7 @@ func (rit *EmailTransformer) setupTemplateContext(originalEmail []byte, r *toolk
if rit.localPartTemplate == nil && rit.domainTemplate == nil && !rit.keepOriginalDomain {
return nil
}
rit.rrctx.setRecord(r)
rit.rctx.SetRecord(r)

originalLocalPart, originalDomain, err := EmailParse(originalEmail)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/db/postgres/transformers/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestHashTransformer_Transform_all_functions(t *testing.T) {
context.Background(),
driver, tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -154,6 +155,7 @@ func TestHashTransformer_Transform_length_truncation(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -184,6 +186,7 @@ func TestHashTransformer_Transform_multiple_iterations(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
2 changes: 1 addition & 1 deletion internal/db/postgres/transformers/json_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (jc *JsonContext) GetColumnValue(name string) (any, error) {
}

func (jc *JsonContext) GetColumnRawValue(name string) (any, error) {
return jc.rc.GetRawColumnValue(name)
return jc.rc.GetColumnRawValue(name)
}

func (jc *JsonContext) EncodeValueByColumn(name string, v any) (any, error) {
Expand Down
3 changes: 3 additions & 0 deletions internal/db/postgres/transformers/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestJsonTransformer_Transform(t *testing.T) {
]`),
},
nil,
"",
)
require.NoError(t, err)
assert.Empty(t, warnings)
Expand Down Expand Up @@ -86,6 +87,7 @@ func TestJsonTransformer_Transform_with_template(t *testing.T) {
"operations": opsData,
},
nil,
"",
)
require.NoError(t, err)
assert.Empty(t, warnings)
Expand Down Expand Up @@ -130,6 +132,7 @@ func TestJsonTransformer_Transform_null(t *testing.T) {
"keep_null": toolkit.ParamsValue("false"),
},
nil,
"",
)
require.NoError(t, err)
assert.Empty(t, warnings)
Expand Down
2 changes: 2 additions & 0 deletions internal/db/postgres/transformers/masking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func TestMaskingTransformer_Transform(t *testing.T) {
context.Background(),
driver, tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -109,6 +110,7 @@ func TestMaskingTransformer_type_validation(t *testing.T) {
"type": toolkit.ParamsValue("unknown"),
},
nil,
"",
)
require.NoError(t, err)
assert.Len(t, warnings, 1)
Expand Down
1 change: 1 addition & 0 deletions internal/db/postgres/transformers/noise_date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestNoiseDateTransformer_Transform(t *testing.T) {
context.Background(),
driver, tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
1 change: 1 addition & 0 deletions internal/db/postgres/transformers/noise_float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func TestNoiseFloatTransformer_Transform(t *testing.T) {
driver,
tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
1 change: 1 addition & 0 deletions internal/db/postgres/transformers/noise_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func TestNoiseIntTransformer_Transform(t *testing.T) {
context.Background(),
driver, tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
1 change: 1 addition & 0 deletions internal/db/postgres/transformers/noise_numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func TestNoiseNumericTransformer_Transform(t *testing.T) {
driver,
tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
1 change: 1 addition & 0 deletions internal/db/postgres/transformers/random_bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestRandomBoolTransformer_Transform(t *testing.T) {
driver,
tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
3 changes: 3 additions & 0 deletions internal/db/postgres/transformers/random_choice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestRandomChoiceTransformer_Transform_with_fail(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -57,6 +58,7 @@ func TestRandomChoiceTransformer_Transform_validation_error(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.NotEmpty(t, warnings)
Expand All @@ -78,6 +80,7 @@ func TestRandomChoiceTransformer_Transform_json(t *testing.T) {
context.Background(),
driver, params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
1 change: 1 addition & 0 deletions internal/db/postgres/transformers/random_date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func TestTimestampTransformer_Transform(t *testing.T) {
context.Background(),
driver, tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
1 change: 1 addition & 0 deletions internal/db/postgres/transformers/random_float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func TestRandomFloatTransformer_Transform(t *testing.T) {
driver,
tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
3 changes: 3 additions & 0 deletions internal/db/postgres/transformers/random_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func TestRandomIntTransformer_Transform_random_static(t *testing.T) {
driver,
tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -205,6 +206,7 @@ func TestRandomIntTransformer_Transform_random_dynamic(t *testing.T) {
driver,
tt.params,
tt.dynamicParams,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -284,6 +286,7 @@ func TestRandomIntTransformer_Transform_deterministic_dynamic(t *testing.T) {
driver,
tt.params,
tt.dynamicParams,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
1 change: 1 addition & 0 deletions internal/db/postgres/transformers/random_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func TestRandomIpTransformer_Transform_random_dynamic(t *testing.T) {
driver,
tt.params,
tt.dynamicParams,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
1 change: 1 addition & 0 deletions internal/db/postgres/transformers/random_mac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func TestRandomMacTransformer_Transform_random(t *testing.T) {
driver,
tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
3 changes: 3 additions & 0 deletions internal/db/postgres/transformers/random_numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestBigIntTransformer_Transform_random_static(t *testing.T) {
driver,
tt.params,
nil,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -175,6 +176,7 @@ func TestBigIntTransformer_Transform_random_dynamic(t *testing.T) {
driver,
tt.params,
tt.dynamicParams,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down Expand Up @@ -254,6 +256,7 @@ func TestBigIntTransformer_Transform_deterministic_dynamic(t *testing.T) {
driver,
tt.params,
tt.dynamicParams,
"",
)
require.NoError(t, err)
require.Empty(t, warnings)
Expand Down
Loading

0 comments on commit 66accc5

Please sign in to comment.