Skip to content

Commit

Permalink
add jsonExtractUnescape Helper (#962)
Browse files Browse the repository at this point in the history
* add jsonExtractUnescape Helper
  • Loading branch information
he2ss committed Sep 10, 2021
1 parent e67fad0 commit e651379
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/exprhelpers/exprlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ func Upper(s string) string {

func GetExprEnv(ctx map[string]interface{}) map[string]interface{} {
var ExprLib = map[string]interface{}{
"Atof": Atof,
"JsonExtract": JsonExtract,
"JsonExtractLib": JsonExtractLib,
"File": File,
"RegexpInFile": RegexpInFile,
"Upper": Upper,
"IpInRange": IpInRange,
"TimeNow": TimeNow,
"Atof": Atof,
"JsonExtract": JsonExtract,
"JsonExtractUnescape": JsonExtractUnescape,
"JsonExtractLib": JsonExtractLib,
"File": File,
"RegexpInFile": RegexpInFile,
"Upper": Upper,
"IpInRange": IpInRange,
"TimeNow": TimeNow,
}
for k, v := range ctx {
ExprLib[k] = v
Expand Down
19 changes: 19 additions & 0 deletions pkg/exprhelpers/jsonextract.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ func JsonExtractLib(jsblob string, target ...string) string {
return strvalue
}

func JsonExtractUnescape(jsblob string, target ...string) string {
value, err := jsonparser.GetString(
jsonparser.StringToBytes(jsblob),
target...,
)

if err != nil {
if err == jsonparser.KeyPathNotFoundError {
log.Debugf("%+v doesn't exist", target)
return ""
}
log.Errorf("JsonExtractUnescape : %+v : %s", target, err)
return ""
}
log.Tracef("extract path %+v", target)
strvalue := string(value)
return strvalue
}

func JsonExtract(jsblob string, target string) string {
if !strings.HasPrefix(target, "[") {
target = strings.Replace(target, "[", ".[", -1)
Expand Down
40 changes: 40 additions & 0 deletions pkg/exprhelpers/jsonextract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,43 @@ func TestJsonExtract(t *testing.T) {
}

}
func TestJsonExtractUnescape(t *testing.T) {
if err := Init(); err != nil {
log.Fatalf(err.Error())
}

err := FileInit(TestFolder, "test_data_re.txt", "regex")
if err != nil {
log.Fatalf(err.Error())
}

tests := []struct {
name string
jsonBlob string
targetField string
expectResult string
}{
{
name: "basic json extract",
jsonBlob: `{"log" : "\"GET /JBNwtQ6i.blt HTTP/1.1\" 200 13 \"-\" \"Craftbot\""}`,
targetField: "log",
expectResult: "\"GET /JBNwtQ6i.blt HTTP/1.1\" 200 13 \"-\" \"Craftbot\"",
},
{
name: "basic json extract with non existing field",
jsonBlob: `{"test" : "1234"}`,
targetField: "non_existing_field",
expectResult: "",
},
}

for _, test := range tests {
result := JsonExtractUnescape(test.jsonBlob, test.targetField)
isOk := assert.Equal(t, test.expectResult, result)
if !isOk {
t.Fatalf("test '%s' failed", test.name)
}
log.Printf("test '%s' : OK", test.name)
}

}

0 comments on commit e651379

Please sign in to comment.