Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support array in experiment engine json #375

Merged
merged 6 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions engines/experiment/pkg/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetValueFromHTTPRequest(
case PayloadFieldSource:
return getValueFromJSONPayload(bodyBytes, field)
case HeaderFieldSource:
value := reqHeader.Get(field)
value := strings.Join(reqHeader.Values(field), ",")
leonlnj marked this conversation as resolved.
Show resolved Hide resolved
if value == "" {
// key not found in header
return "", fmt.Errorf("Field %s not found in the request header", field)
Expand Down Expand Up @@ -112,19 +112,18 @@ func GetValueFromUPIRequest(

func getValueFromJSONPayload(body []byte, key string) (string, error) {
// Retrieve value using JSON path
value, typez, _, _ := jsonparser.Get(body, strings.Split(key, ".")...)
value, dataType, _, _ := jsonparser.Get(body, strings.Split(key, ".")...)

switch typez {
case jsonparser.String, jsonparser.Number, jsonparser.Boolean:
switch dataType {
case jsonparser.String, jsonparser.Number, jsonparser.Boolean, jsonparser.Array, jsonparser.Object:
// See: https://github.com/buger/jsonparser/blob/master/bytes_unsafe.go#L31
return *(*string)(unsafe.Pointer(&value)), nil
case jsonparser.Null:
return "", nil
case jsonparser.NotExist:
return "", errors.Errorf("Field %s not found in the request payload: Key path not found", key)
default:
return "", errors.Errorf(
"Field %s can not be parsed as string value, unsupported type: %s", key, typez.String())
return "", errors.Errorf("Field %s can not be parsed as string value, unsupported type: %s", key, dataType.String())
}
}

Expand Down
33 changes: 31 additions & 2 deletions engines/experiment/pkg/request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ func TestGetValueFromHTTPRequest(t *testing.T) {
}(),
expected: "123",
},
"success | header multiple value": {
field: "CustomerID",
fieldSrc: request.HeaderFieldSource,
header: func() http.Header {
header := http.Header{}
header.Set("CustomerID", "123")
header.Add("CustomerID", "321")
return header
}(),
expected: "123,321",
},
leonlnj marked this conversation as resolved.
Show resolved Hide resolved
"success | nested payload": {
field: "customer.id",
fieldSrc: request.PayloadFieldSource,
Expand All @@ -62,6 +73,24 @@ func TestGetValueFromHTTPRequest(t *testing.T) {
body: []byte(`{"is_premium_customer": true}`),
expected: "true",
},
"success | array string": {
field: "customers",
fieldSrc: request.PayloadFieldSource,
body: []byte(`{"customers": ["123","321"]}`),
expected: `["123","321"]`,
},
"success | array int": {
field: "customers",
fieldSrc: request.PayloadFieldSource,
body: []byte(`{"customers": [123,321]}`),
expected: `[123,321]`,
},
"success | object": {
field: "customers",
fieldSrc: request.PayloadFieldSource,
body: []byte(`{"customers": {"a":"b"}}`),
expected: `{"a":"b"}`,
},
"success | payload null field": {
field: "session_id",
fieldSrc: request.PayloadFieldSource,
Expand All @@ -87,8 +116,8 @@ func TestGetValueFromHTTPRequest(t *testing.T) {
"failure | payload unsupported type": {
field: "customer",
fieldSrc: request.PayloadFieldSource,
body: []byte(`{"customer": {"id": 42, "email": "test@test.com"}`),
err: "Field customer can not be parsed as string value, unsupported type: object",
body: []byte(`{"customer": !!!`),
err: "Field customer can not be parsed as string value, unsupported type: unknown",
},
"failure | unknown source": {
field: "CustomerID",
Expand Down
Loading