Skip to content

Commit

Permalink
fix: bug with contexts (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienaury authored Dec 23, 2023
1 parent c0c663a commit 919fdf0
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Types of changes

- `Added` new features `findInCSV` mask to get one or multiple csv lines which matched with Json entry value from CSV files.
- `Added` new flag `--serve` to start a HTTP server that apply masking to JSON body of requests.
- `Fixed` some bugs with selectors.

## [1.20.0]

Expand Down
2 changes: 1 addition & 1 deletion pkg/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (am MaskEngine) MaskContext(context model.Dictionary, key string, contexts
if !present {
if am.template != nil {
var output bytes.Buffer
if err := am.template.Execute(&output, contexts[0].UnpackAsDict().Unordered()); err != nil {
if err := am.template.Execute(&output, contexts[0].UnpackUnordered()); err != nil {
return context, err
}
context.Set(key, output.String())
Expand Down
2 changes: 1 addition & 1 deletion pkg/addtransient/add-transient.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (am MaskEngine) MaskContext(context model.Dictionary, key string, contexts
if !present {
if am.template != nil {
var output bytes.Buffer
if err := am.template.Execute(&output, contexts[0].UnpackAsDict().Unordered()); err != nil {
if err := am.template.Execute(&output, contexts[0].UnpackUnordered()); err != nil {
return context, err
}
context.Set(key, output.String())
Expand Down
9 changes: 4 additions & 5 deletions pkg/ff1/ff1.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ func (ff1m MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model.E

// Extract tweak from the Dictionary (context)
tweak := ""
if rootCtx, ctxExist := context[0].TryUnpackAsDict(); ctxExist {
if rootCtx.Get(ff1m.tweakField) != nil {
tweak = rootCtx.Get(ff1m.tweakField).(string)
}
tweakSelector := model.NewPackedPathSelector(ff1m.tweakField)
if value, found := tweakSelector.Read(context[0]); found {
tweak = value.(string)
}
// Get encryption key as byte array
envKey := os.Getenv(ff1m.keyFromEnv)
Expand Down Expand Up @@ -214,7 +213,7 @@ func fromFF1Domain(value string, domain string, preserved map[int]rune) string {

func executeTemplate(engine *template.Engine, contexts []model.Dictionary) (string, error) {
var output bytes.Buffer
if err := engine.Execute(&output, contexts[0].UnpackAsDict().Unordered()); err != nil {
if err := engine.Execute(&output, contexts[0].UnpackUnordered()); err != nil {
return "", err
}
return output.String(), nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/findincsv/findincsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (me *MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model.En
if len(context) == 0 {
context = []model.Dictionary{model.NewPackedDictionary()}
}
if err := me.templateURI.Execute(&filenameBuffer, context[0].UnpackAsDict().Unordered()); err != nil {
if err := me.templateURI.Execute(&filenameBuffer, context[0].UnpackUnordered()); err != nil {
return nil, err
}
filename := filenameBuffer.String()
Expand All @@ -158,7 +158,7 @@ func (me *MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model.En
// getJaccardMatchResults calculates Jaccard similarity for the given CSV filename and exact match results.
func (me *MaskEngine) getJaccardMatchResults(filename string, exactMatchResults []model.Entry, context []model.Dictionary) ([]model.Entry, error) {
var jaccardEntryBuffer bytes.Buffer
if err := me.temJaccardEntry.Execute(&jaccardEntryBuffer, context[0].UnpackAsDict().Unordered()); err != nil {
if err := me.temJaccardEntry.Execute(&jaccardEntryBuffer, context[0].UnpackUnordered()); err != nil {
return nil, err
}
jaccardEntryString := jaccardEntryBuffer.String()
Expand Down Expand Up @@ -212,7 +212,7 @@ func (me *MaskEngine) ExactMatch(filename string, context []model.Dictionary) (b
}

var exactEntryBuffer bytes.Buffer
if err := me.temExactMatchEntry.Execute(&exactEntryBuffer, context[0].UnpackAsDict().Unordered()); err != nil {
if err := me.temExactMatchEntry.Execute(&exactEntryBuffer, context[0].UnpackUnordered()); err != nil {
return false, nil, err
}
exactEntryString := exactEntryBuffer.String()
Expand Down
4 changes: 2 additions & 2 deletions pkg/hashcsv/hashcsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func (mrl MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model.En

var output bytes.Buffer
var tmplContext any
if len(context) == 0 || !context[0].CanUnpackAsDict() {
if len(context) == 0 {
tmplContext = nil
} else {
tmplContext = context[0].UnpackAsDict().Unordered()
tmplContext = context[0].UnpackUnordered()
}

if err := mrl.template.Execute(&output, tmplContext); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (p RepeaterUntilProcess) ProcessDictionary(dictionary Dictionary, out Colle
err = fmt.Errorf("Cannot execute template, error: %v", r)
}
}()
err = p.tmpl.Execute(&output, dictionary.UnpackAsDict().Untyped())
err = p.tmpl.Execute(&output, dictionary.UnpackUntyped())

if err != nil && skipLineOnError {
log.Warn().AnErr("error", err).Msg("Line skipped")
Expand Down
8 changes: 8 additions & 0 deletions pkg/model/ordered_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ func (d Dictionary) Unpack() Entry {
return d.Get(".")
}

func (d Dictionary) UnpackUnordered() Entry {
return d.Unordered()["."]
}

func (d Dictionary) UnpackUntyped() interface{} {
return d.Untyped()["."]
}

func (d Dictionary) UnpackAsDict() Dictionary {
return d.Get(".").(Dictionary)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/randomcsv/randomcsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (mrl MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model.En
if len(context) == 0 {
context = []model.Dictionary{model.NewPackedDictionary()}
}
if err := mrl.template.Execute(&output, context[0].UnpackAsDict().Unordered()); err != nil {
if err := mrl.template.Execute(&output, context[0].UnpackUnordered()); err != nil {
return nil, err
}
filename := output.String()
Expand Down
2 changes: 1 addition & 1 deletion pkg/randomuri/randomuri.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (mrl MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model.En
if len(context) == 0 {
context = []model.Dictionary{model.NewPackedDictionary()}
}
if err := mrl.template.Execute(&output, context[0].UnpackAsDict().Unordered()); err != nil {
if err := mrl.template.Execute(&output, context[0].UnpackUnordered()); err != nil {
return nil, err
}
filename := output.String()
Expand Down
2 changes: 1 addition & 1 deletion pkg/templatemask/templatemask.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewMask(text string, funcs tmpl.FuncMap, seed int64, seedField string) (Mas
func (tmpl MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model.Entry, error) {
log.Info().Msg("Mask template")
var output bytes.Buffer
err := tmpl.template.Execute(&output, context[0].UnpackAsDict().Unordered())
err := tmpl.template.Execute(&output, context[0].UnpackUnordered())
return output.String(), err
}

Expand Down
14 changes: 14 additions & 0 deletions test/suites/repeat-until-while.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,17 @@ testcases:
assertions:
- result.systemout ShouldStartWith '{"name":"Aaron"}'
- result.systemout ShouldEndWith '{"name":"Zazie"}'

- name: use repeat-while to extract content of referential
steps:
- script: |-
cat > script.sh <<EOF
pimo --empty-input --repeat-while '[[ eq "string" (printf "%T" .) ]]' --mask '.={fluxUri: "pimo://nameFR"}'
EOF
- script: sed -i "s/\[\[/\{\{/g" script.sh
- script: sed -i "s/\]\]/\}\}/g" script.sh
- script: chmod +x script.sh
- script: ./script.sh
assertions:
- result.systemout ShouldStartWith '"Aaron"'
- result.systemout ShouldEndWith '"Zazie"'

0 comments on commit 919fdf0

Please sign in to comment.