This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
params.go
72 lines (63 loc) · 2.12 KB
/
params.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
package main
import (
"bytes"
"fmt"
"regexp"
"strings"
)
// Params holds the parameter mappings specified by user
type Params map[string]string
var pNamePattern = `([A-Za-z_][0-9A-Za-z_]*)`
var pNameRegex = regexp.MustCompile(`^` + pNamePattern + `$`)
var paramDecRegex = regexp.MustCompile(`{{ *` + pNamePattern + ` *}}`)
var paramRegex = regexp.MustCompile(pNamePattern + `=(.+?)`)
// specific Params -> String transformation
func (params *Params) String() string {
return fmt.Sprint(*params)
}
// tell `flag` package how to parse/store param args
func (params *Params) Set(input string) error {
// for each `--param` flag...
for _, p := range strings.Split(input, ",") {
// split params on ,
pElems := strings.SplitN(p, "=", 2)
// check number of elements in param arg
switch len(pElems) {
case 0:
return fmt.Errorf("Missing argument to `--param` flag.")
case 1:
return fmt.Errorf("Missing value in parameter arg: %s", p)
}
// validate string as PName identifer
pName, pValue := pElems[0], pElems[1]
(*params)[pName] = pValue
}
return nil
}
/* resolve all param names (keys in `params` map) to their resp. values */
func replaceParams(fileData []byte, params Params) ([]byte, error) {
// get all unique param declarations from test file
matches := uniqueParamDeclarations(fileData)
for pDeclaration, pName := range matches {
// check whether the reference parameter is defined
if pVal, ok := params[pName]; ok {
// parameter found, replace all occurrences with its value
fileData = bytes.Replace(fileData, []byte(pDeclaration), []byte(pVal), -1)
} else {
// parameter not found, fail
return []byte{}, fmt.Errorf("Parameter %s not specified", pName)
}
}
return fileData, nil
}
/* returns all unique parameter declarations in a test file */
func uniqueParamDeclarations(fileData []byte) map[string]string {
// get all regex results
regexResults := paramDecRegex.FindAllSubmatch(fileData, -1)
// matchSet is map from '{{ <varName> }}' to '<varName>'
matchSet := make(map[string]string)
for _, result := range regexResults {
matchSet[string(result[0])] = string(result[1])
}
return matchSet
}