generated from adrienaury/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: coherence source with templates (#16)
* feat: wip! coherence source with templates * feat: add sprig functions * test: add venom test + changelog
- Loading branch information
1 parent
a55ebb4
commit 0fcec45
Showing
10 changed files
with
190 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (C) 2023 CGI France | ||
// | ||
// This file is part of MIMO. | ||
// | ||
// MIMO is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// MIMO is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with MIMO. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package mimo | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"text/template" | ||
"unicode" | ||
|
||
"github.com/Masterminds/sprig/v3" | ||
"golang.org/x/text/runes" | ||
"golang.org/x/text/transform" | ||
"golang.org/x/text/unicode/norm" | ||
) | ||
|
||
func generateCoherentSource(tmplstring string, root DataRow, stack []any) (string, error) { | ||
funcmap := generateFuncMap() | ||
|
||
funcmap["Stack"] = generateStackFunc(stack) | ||
|
||
tmpl, err := template.New("template").Funcs(sprig.TxtFuncMap()).Funcs(funcmap).Parse(tmplstring) | ||
if err != nil { | ||
return "", fmt.Errorf("%w", err) | ||
} | ||
|
||
result := &strings.Builder{} | ||
err = tmpl.Execute(result, root) | ||
|
||
return result.String(), err | ||
} | ||
|
||
func generateFuncMap() template.FuncMap { | ||
funcMap := template.FuncMap{} | ||
|
||
funcMap["ToUpper"] = strings.ToUpper | ||
funcMap["ToLower"] = strings.ToLower | ||
funcMap["NoAccent"] = rmAcc | ||
|
||
return funcMap | ||
} | ||
|
||
func generateStackFunc(theStack []any) func(index int) any { | ||
return func(index int) any { | ||
if index > 0 { | ||
return theStack[index-1] | ||
} | ||
|
||
return theStack[len(theStack)+index-1] | ||
} | ||
} | ||
|
||
// rmAcc removes accents from string | ||
// Function derived from: http://blog.golang.org/normalization | ||
func rmAcc(s string) string { | ||
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC) | ||
result, _, _ := transform.String(t, s) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: "1" | ||
metrics: | ||
- name: "batchs.[].accounts.[].number" | ||
coherentSource: "{{$batch := Stack -3}}{{Stack 0}} + {{$batch.id}}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>MIMO Report</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1" /> | ||
<meta name="description" content="MIMO Report" /> | ||
</head> | ||
<body> | ||
<h1>MIMO Report</h1> | ||
<table border="1" cellspacing="0" cellpadding="5"> | ||
<thead> | ||
<th>Field</th> | ||
<th>Nil</th> | ||
<th>Empty</th> | ||
<th>Masked</th> | ||
<th>Missed</th> | ||
<th>Masking Rate</th> | ||
<th>Coherent Rate</th> | ||
<th>Identifiable Rate</th> | ||
<th>K</th> | ||
</thead> | ||
<tbody> | ||
|
||
<tr> | ||
<td>batchs.[].accounts.[].number</td> | ||
<td>0</td> | ||
<td>0</td> | ||
<td>4</td> | ||
<td>0</td> | ||
<td style="background-color: lightgreen">100.00 %</td> | ||
<td style="background-color: orange">66.67 %</td> | ||
<td style="background-color: lightgreen">100.00 %</td> | ||
<td style="background-color: orange">1</td> | ||
</tr> | ||
|
||
<tr> | ||
<td>batchs.[].id</td> | ||
<td>0</td> | ||
<td>0</td> | ||
<td>0</td> | ||
<td>3</td> | ||
<td style="background-color: orange">0.00 %</td> | ||
<td style="background-color: lightgreen">100.00 %</td> | ||
<td style="background-color: lightgreen">100.00 %</td> | ||
<td style="background-color: orange">1</td> | ||
</tr> | ||
|
||
</tbody> | ||
</table> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Venom Test Suite definition | ||
# Check Venom documentation for more information : https://github.com/ovh/venom | ||
name: use templates to compute coherent source | ||
testcases: | ||
- name: template with use of Stack object | ||
steps: | ||
- script: echo '{"batchs":[{"id":1,"accounts":[{"number":"01234"},{"number":"56789"}]}]}' > working/real.jsonl | ||
- script: echo '{"batchs":[{"id":1,"accounts":[{"number":"56789"}]},{"id":2,"accounts":[{"number":"01234"}]}]}' >> working/real.jsonl | ||
|
||
- script: echo '{"batchs":[{"id":1,"accounts":[{"number":"A"},{"number":"B"}]}]}' > working/masked.jsonl | ||
- script: echo '{"batchs":[{"id":1,"accounts":[{"number":"C"}]},{"id":2,"accounts":[{"number":"D"}]}]}' >> working/masked.jsonl | ||
|
||
- script: cat working/masked.jsonl | mimo --config ../configs/config_template.yaml -v3 working/real.jsonl | ||
assertions: | ||
- result.code ShouldEqual 0 | ||
- result.systemerr ShouldContainSubstring batchs.[].id count-empty=0 count-masked=0 count-missed=3 count-nil=0 field=batchs.[].id rate-coherence=1 rate-identifiable=1 rate-masking=0 | ||
- result.systemerr ShouldContainSubstring batchs.[].accounts.[].number count-empty=0 count-masked=4 count-missed=0 count-nil=0 field=batchs.[].accounts.[].number rate-coherence=0.6666666666666666 rate-identifiable=1 rate-masking=1 | ||
|
||
- script: mv report.html ../reports/report_template_source_stack.html |