2020-05-22 16:12:33 +00:00
|
|
|
package exprhelpers
|
|
|
|
|
|
|
|
import (
|
2022-06-08 10:15:29 +00:00
|
|
|
"encoding/json"
|
2022-11-29 08:16:07 +00:00
|
|
|
"errors"
|
2022-06-08 10:15:29 +00:00
|
|
|
"fmt"
|
2020-05-22 16:12:33 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/buger/jsonparser"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-03-28 08:49:01 +00:00
|
|
|
// func JsonExtractLib(jsblob string, target ...string) string {
|
|
|
|
func JsonExtractLib(params ...any) (any, error) {
|
|
|
|
jsblob := params[0].(string)
|
|
|
|
target := params[1].([]string)
|
2020-05-22 16:12:33 +00:00
|
|
|
value, dataType, _, err := jsonparser.Get(
|
|
|
|
jsonparser.StringToBytes(jsblob),
|
|
|
|
target...,
|
|
|
|
)
|
2021-06-02 12:27:34 +00:00
|
|
|
|
2020-05-22 16:12:33 +00:00
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
if errors.Is(err, jsonparser.KeyPathNotFoundError) {
|
2021-06-02 12:27:34 +00:00
|
|
|
log.Debugf("%+v doesn't exist", target)
|
2023-03-28 08:49:01 +00:00
|
|
|
return "", nil
|
2021-06-02 12:27:34 +00:00
|
|
|
}
|
2020-05-23 11:22:43 +00:00
|
|
|
log.Errorf("jsonExtractLib : %+v : %s", target, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
return "", nil
|
2020-05-22 16:12:33 +00:00
|
|
|
}
|
|
|
|
if dataType == jsonparser.NotExist {
|
|
|
|
log.Debugf("%+v doesn't exist", target)
|
2023-03-28 08:49:01 +00:00
|
|
|
return "", nil
|
2020-05-22 16:12:33 +00:00
|
|
|
}
|
|
|
|
strvalue := string(value)
|
2023-03-28 08:49:01 +00:00
|
|
|
return strvalue, nil
|
2020-05-22 16:12:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 08:49:01 +00:00
|
|
|
// func JsonExtractUnescape(jsblob string, target ...string) string {
|
|
|
|
func JsonExtractUnescape(params ...any) (any, error) {
|
|
|
|
var value string
|
|
|
|
var err error
|
|
|
|
jsblob := params[0].(string)
|
|
|
|
switch v := params[1].(type) {
|
|
|
|
case string:
|
|
|
|
target := v
|
|
|
|
value, err = jsonparser.GetString(
|
|
|
|
jsonparser.StringToBytes(jsblob),
|
|
|
|
target,
|
|
|
|
)
|
|
|
|
case []string:
|
|
|
|
target := v
|
|
|
|
value, err = jsonparser.GetString(
|
|
|
|
jsonparser.StringToBytes(jsblob),
|
|
|
|
target...,
|
|
|
|
)
|
|
|
|
}
|
2021-09-10 10:43:11 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
if errors.Is(err, jsonparser.KeyPathNotFoundError) {
|
2023-03-28 08:49:01 +00:00
|
|
|
log.Debugf("%+v doesn't exist", params[1])
|
|
|
|
return "", nil
|
2021-09-10 10:43:11 +00:00
|
|
|
}
|
2023-03-28 08:49:01 +00:00
|
|
|
log.Errorf("JsonExtractUnescape : %+v : %s", params[1], err)
|
|
|
|
return "", nil
|
2021-09-10 10:43:11 +00:00
|
|
|
}
|
2023-03-28 08:49:01 +00:00
|
|
|
log.Tracef("extract path %+v", params[1])
|
|
|
|
return value, nil
|
2021-09-10 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 08:49:01 +00:00
|
|
|
// func JsonExtract(jsblob string, target string) string {
|
|
|
|
func JsonExtract(params ...any) (any, error) {
|
|
|
|
jsblob := params[0].(string)
|
|
|
|
target := params[1].(string)
|
2020-05-23 11:22:43 +00:00
|
|
|
if !strings.HasPrefix(target, "[") {
|
2022-11-07 09:36:50 +00:00
|
|
|
target = strings.ReplaceAll(target, "[", ".[")
|
2020-05-23 11:22:43 +00:00
|
|
|
}
|
2020-05-22 16:12:33 +00:00
|
|
|
fullpath := strings.Split(target, ".")
|
2020-05-23 11:22:43 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
log.Tracef("extract path %+v", fullpath)
|
2023-03-28 08:49:01 +00:00
|
|
|
return JsonExtractLib(jsblob, fullpath)
|
2020-05-22 16:12:33 +00:00
|
|
|
}
|
2022-06-08 10:15:29 +00:00
|
|
|
|
|
|
|
func jsonExtractType(jsblob string, target string, t jsonparser.ValueType) ([]byte, error) {
|
|
|
|
if !strings.HasPrefix(target, "[") {
|
2022-11-07 09:36:50 +00:00
|
|
|
target = strings.ReplaceAll(target, "[", ".[")
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
|
|
|
fullpath := strings.Split(target, ".")
|
|
|
|
|
|
|
|
log.Tracef("extract path %+v", fullpath)
|
|
|
|
|
|
|
|
value, dataType, _, err := jsonparser.Get(
|
|
|
|
jsonparser.StringToBytes(jsblob),
|
|
|
|
fullpath...,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
if errors.Is(err, jsonparser.KeyPathNotFoundError) {
|
2022-06-08 10:15:29 +00:00
|
|
|
log.Debugf("Key %+v doesn't exist", target)
|
|
|
|
return nil, fmt.Errorf("key %s does not exist", target)
|
|
|
|
}
|
|
|
|
log.Errorf("jsonExtractType : %s : %s", target, err)
|
|
|
|
return nil, fmt.Errorf("jsonExtractType: %s : %w", target, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dataType != t {
|
|
|
|
log.Errorf("jsonExtractType : expected type %s for target %s but found %s", t, target, dataType.String())
|
|
|
|
return nil, fmt.Errorf("jsonExtractType: expected type %s for target %s but found %s", t, target, dataType.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 08:49:01 +00:00
|
|
|
// func JsonExtractSlice(jsblob string, target string) []interface{} {
|
|
|
|
func JsonExtractSlice(params ...any) (any, error) {
|
|
|
|
jsblob := params[0].(string)
|
|
|
|
target := params[1].(string)
|
2022-06-08 10:15:29 +00:00
|
|
|
value, err := jsonExtractType(jsblob, target, jsonparser.Array)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("JsonExtractSlice : %s", err)
|
2023-03-28 08:49:01 +00:00
|
|
|
return []interface{}(nil), nil
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := make([]interface{}, 0)
|
|
|
|
|
|
|
|
err = json.Unmarshal(value, &s)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("JsonExtractSlice: could not convert '%s' to slice: %s", value, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
return []interface{}(nil), nil
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
2023-03-28 08:49:01 +00:00
|
|
|
return s, nil
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 08:49:01 +00:00
|
|
|
// func JsonExtractObject(jsblob string, target string) map[string]interface{} {
|
|
|
|
func JsonExtractObject(params ...any) (any, error) {
|
|
|
|
jsblob := params[0].(string)
|
|
|
|
target := params[1].(string)
|
2022-06-08 10:15:29 +00:00
|
|
|
value, err := jsonExtractType(jsblob, target, jsonparser.Object)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("JsonExtractObject: %s", err)
|
2023-03-28 08:49:01 +00:00
|
|
|
return map[string]interface{}(nil), nil
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := make(map[string]interface{})
|
|
|
|
|
|
|
|
err = json.Unmarshal(value, &s)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("JsonExtractObject: could not convert '%s' to map[string]interface{}: %s", value, err)
|
2023-03-28 08:49:01 +00:00
|
|
|
return map[string]interface{}(nil), nil
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
2023-03-28 08:49:01 +00:00
|
|
|
return s, nil
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 08:49:01 +00:00
|
|
|
// func ToJson(obj interface{}) string {
|
|
|
|
func ToJson(params ...any) (any, error) {
|
|
|
|
obj := params[0]
|
2022-06-08 10:15:29 +00:00
|
|
|
b, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("ToJson : %s", err)
|
2023-03-28 08:49:01 +00:00
|
|
|
return "", nil
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
2023-03-28 08:49:01 +00:00
|
|
|
return string(b), nil
|
2022-06-08 10:15:29 +00:00
|
|
|
}
|
2023-05-12 07:43:01 +00:00
|
|
|
|
|
|
|
// Func UnmarshalJSON(jsonBlob []byte, target interface{}) error {
|
|
|
|
func UnmarshalJSON(params ...any) (any, error) {
|
|
|
|
jsonBlob := params[0].(string)
|
|
|
|
target := params[1].(map[string]interface{})
|
|
|
|
key := params[2].(string)
|
|
|
|
|
|
|
|
var out interface{}
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(jsonBlob), &out)
|
|
|
|
if err != nil {
|
2023-08-09 08:42:08 +00:00
|
|
|
log.WithField("line", jsonBlob).Errorf("UnmarshalJSON : %s", err)
|
2023-06-16 17:34:55 +00:00
|
|
|
return nil, err
|
2023-05-12 07:43:01 +00:00
|
|
|
}
|
|
|
|
target[key] = out
|
2023-06-16 17:34:55 +00:00
|
|
|
return nil, nil
|
2023-05-12 07:43:01 +00:00
|
|
|
}
|