Add unix expr helper (#1952)
* Add unix expr helper * Add original value not parsed error * return early if cannot parse * Add tests * Fix negative value
This commit is contained in:
parent
e4463c412b
commit
401739b036
|
@ -68,6 +68,7 @@ func GetExprEnv(ctx map[string]interface{}) map[string]interface{} {
|
||||||
"GetDecisionsCount": GetDecisionsCount,
|
"GetDecisionsCount": GetDecisionsCount,
|
||||||
"GetDecisionsSinceCount": GetDecisionsSinceCount,
|
"GetDecisionsSinceCount": GetDecisionsSinceCount,
|
||||||
"Sprintf": fmt.Sprintf,
|
"Sprintf": fmt.Sprintf,
|
||||||
|
"ParseUnix": ParseUnix,
|
||||||
}
|
}
|
||||||
for k, v := range ctx {
|
for k, v := range ctx {
|
||||||
ExprLib[k] = v
|
ExprLib[k] = v
|
||||||
|
@ -290,3 +291,13 @@ func LookupHost(value string) []string {
|
||||||
}
|
}
|
||||||
return addresses
|
return addresses
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseUnix(value string) string {
|
||||||
|
//Splitting string here as some unix timestamp may have milliseconds and break ParseInt
|
||||||
|
i, err := strconv.ParseInt(strings.Split(value, ".")[0], 10, 64)
|
||||||
|
if err != nil || i <= 0 {
|
||||||
|
log.Errorf("Unable to parse %s as unix timestamp.", value)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return time.Unix(i, 0).Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
|
|
@ -971,3 +971,63 @@ func TestGetDecisionsSinceCount(t *testing.T) {
|
||||||
log.Printf("test '%s' : OK", test.name)
|
log.Printf("test '%s' : OK", test.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseUnix(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
env map[string]interface{}
|
||||||
|
code string
|
||||||
|
result string
|
||||||
|
err string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "ParseUnix() test: valid value with milli",
|
||||||
|
env: map[string]interface{}{
|
||||||
|
"unix": "1672239773.3590894",
|
||||||
|
"ParseUnix": ParseUnix,
|
||||||
|
},
|
||||||
|
code: "ParseUnix(unix)",
|
||||||
|
result: "2022-12-28T15:02:53Z",
|
||||||
|
err: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ParseUnix() test: valid value without milli",
|
||||||
|
env: map[string]interface{}{
|
||||||
|
"unix": "1672239773",
|
||||||
|
"ParseUnix": ParseUnix,
|
||||||
|
},
|
||||||
|
code: "ParseUnix(unix)",
|
||||||
|
result: "2022-12-28T15:02:53Z",
|
||||||
|
err: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ParseUnix() test: invalid input",
|
||||||
|
env: map[string]interface{}{
|
||||||
|
"unix": "AbcDefG!#",
|
||||||
|
"ParseUnix": ParseUnix,
|
||||||
|
},
|
||||||
|
code: "ParseUnix(unix)",
|
||||||
|
result: "",
|
||||||
|
err: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ParseUnix() test: negative value",
|
||||||
|
env: map[string]interface{}{
|
||||||
|
"unix": "-1000",
|
||||||
|
"ParseUnix": ParseUnix,
|
||||||
|
},
|
||||||
|
code: "ParseUnix(unix)",
|
||||||
|
result: "",
|
||||||
|
err: "",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
program, err := expr.Compile(test.code, expr.Env(test.env))
|
||||||
|
require.NoError(t, err)
|
||||||
|
output, err := expr.Run(program, test.env)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, test.result, output)
|
||||||
|
log.Printf("test '%s' : OK", test.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue