Skip to content

Commit

Permalink
fix: Refactored logic for viper and mapstructure workaround
Browse files Browse the repository at this point in the history
Now yaml or json parser that dedicated to transformer parameters parsing parses only transformer parameter

Fixes #85
  • Loading branch information
wwoytenko committed Apr 29, 2024
1 parent e2d52dd commit fc26fee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 13 additions & 0 deletions internal/domains/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,16 @@ type Table struct {
Transformers []*TransformerConfig `mapstructure:"transformers" yaml:"transformers" json:"transformers,omitempty"`
ColumnsTypeOverride map[string]string `mapstructure:"columns_type_override" yaml:"columns_type_override" json:"columns_type_override,omitempty"`
}

// DummyConfig - This is a dummy config to the viper workaround
// It is used to parse the transformation parameters manually only avoiding parsing other pars of the config
// The reason why is there https://github.com/GreenmaskIO/greenmask/discussions/85
type DummyConfig struct {
Dump struct {
Transformation []struct {
Transformers []struct {
Params map[string]interface{} `yaml:"params" json:"params"`
} `yaml:"transformers" json:"transformers"`
} `yaml:"transformation" json:"transformation"`
} `yaml:"dump" json:"dump"`
}
8 changes: 4 additions & 4 deletions internal/utils/config/viper_workaround.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import (
// To overcome this problem we need use default yaml and json parsers avoiding vaiper or mapstructure usage.
func ParseTransformerParamsManually(cfgFilePath string, cfg *domains.Config) error {
ext := path.Ext(cfgFilePath)
//cfgMap := make(map[string]any)
tmpCfg := domains.NewConfig()
tmpCfg := &domains.DummyConfig{}
f, err := os.Open(cfgFilePath)
if err != nil {
return err
Expand All @@ -57,12 +56,13 @@ func ParseTransformerParamsManually(cfgFilePath string, cfg *domains.Config) err

// setTransformerParams - get the value from domains.TransformerConfig.TempParams, marshall this value and store into
// domains.TransformerConfig.Params
func setTransformerParams(tmpCfg, cfg *domains.Config) (err error) {
func setTransformerParams(tmpCfg *domains.DummyConfig, cfg *domains.Config) (err error) {
for tableIdx, tableObj := range tmpCfg.Dump.Transformation {
for transformationIdx, transformationObj := range tableObj.Transformers {
transformer := cfg.Dump.Transformation[tableIdx].Transformers[transformationIdx]
transformerParametersHook := tmpCfg.Dump.Transformation[tableIdx].Transformers[transformationIdx]
paramsMap := make(map[string]toolkit.ParamsValue, len(transformationObj.Params))
for paramName, decodedValue := range transformer.TempParams {
for paramName, decodedValue := range transformerParametersHook.Params {
var encodedVal toolkit.ParamsValue
switch v := decodedValue.(type) {
case string:
Expand Down

0 comments on commit fc26fee

Please sign in to comment.