Skip to content

Commit

Permalink
feat: Support non-string parameters. Closes #1236 (#2317)
Browse files Browse the repository at this point in the history
Signed-off-by: Aalok <[email protected]>
  • Loading branch information
AalokAhluwalia authored and whynowy committed Dec 12, 2022
1 parent 9e467ff commit 7aa2d0a
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 283 deletions.
4 changes: 4 additions & 0 deletions api/jsonschema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4116,6 +4116,10 @@
"description": "DependencyName refers to the name of the dependency. The event which is stored for this dependency is used as payload for the parameterization. Make sure to refer to one of the dependencies you have defined under Dependencies list.",
"type": "string"
},
"useRawData": {
"description": "UseRawData indicates if the value in an event at data key should be used without converting to string. When true, a number, boolean, json or string parameter may be extracted. When the field is unspecified, or explicitly false, the behavior is to turn the extracted field into a string. (e.g. when set to true, the parameter 123 will resolve to the numerical type, but when false, or not provided, the string \"123\" will be resolved)",
"type": "boolean"
},
"value": {
"description": "Value is the default literal value to use for this parameter source This is only used if the DataKey is invalid. If the DataKey is invalid and this is not defined, this param source will produce an error.",
"type": "string"
Expand Down
4 changes: 4 additions & 0 deletions api/openapi-spec/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions api/sensor.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions api/sensor.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions docs/tutorials/02-parameterization.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ to update the trigger resource on the fly.
**_Note_**: If you define both the `contextKey` and `dataKey` within a parameter, then
the `dataKey` takes the precedence.

**_Note_**: When `useRawData` is not specified or explicitly set to false, the parameter
will resolve to a string type. When `useRawData` is set to true, a number, boolean, json
or string parameter may be resolved.

### Default Values

Each parameter comes with an option to configure the default value. This is specially
Expand Down
576 changes: 304 additions & 272 deletions pkg/apis/sensor/v1alpha1/generated.pb.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions pkg/apis/sensor/v1alpha1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/apis/sensor/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/apis/sensor/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,12 @@ type TriggerParameterSource struct {
// This is only used if the DataKey is invalid.
// If the DataKey is invalid and this is not defined, this param source will produce an error.
Value *string `json:"value,omitempty" protobuf:"bytes,6,opt,name=value"`
// UseRawData indicates if the value in an event at data key should be used without converting to string.
// When true, a number, boolean, json or string parameter may be extracted. When the field is unspecified, or explicitly
// false, the behavior is to turn the extracted field into a string. (e.g. when set to true, the parameter
// 123 will resolve to the numerical type, but when false, or not provided, the string "123" will be resolved)
// +optional
UseRawData bool `json:"useRawData,omitempty" protobuf:"bytes,7,opt,name=useRawData"`
}

// TriggerPolicy dictates the policy for the trigger retries
Expand Down
9 changes: 4 additions & 5 deletions sensors/triggers/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func ConstructPayload(events map[string]*v1alpha1.Event, parameters []v1alpha1.T
if err != nil {
return nil, err
}
if typ == jsonType {
if typ != stringType && parameter.Src.UseRawData {
tmp, err := sjson.SetRawBytes(payload, parameter.Dest, []byte(*value))
if err != nil {
return nil, err
Expand Down Expand Up @@ -120,6 +120,7 @@ func ApplyParams(jsonObj []byte, params []v1alpha1.TriggerParameter, events map[
current := gjson.GetBytes(jsonObj, param.Dest)

if current.Exists() {
typ = stringType
if op == v1alpha1.TriggerParameterOpAppend {
*value = current.String() + *value
} else {
Expand All @@ -132,7 +133,7 @@ func ApplyParams(jsonObj []byte, params []v1alpha1.TriggerParameter, events map[
return nil, fmt.Errorf("unsupported trigger parameter operation: %+v", op)
}
// now let's set the value
if typ == jsonType {
if typ != stringType && param.Src.UseRawData {
tmp, err := sjson.SetRawBytes(jsonObj, param.Dest, []byte(*value))
if err != nil {
return nil, err
Expand Down Expand Up @@ -297,9 +298,7 @@ func getValueWithTemplate(value []byte, templString string) (string, error) {
func getValueByKey(value []byte, key string) (string, string, error) {
res := gjson.GetBytes(value, key)
if res.Exists() {
if res.Type.String() == stringType {
return res.String(), res.Type.String(), nil
} else if res.Type.String() == jsonType {
if res.Type.String() == jsonType {
return res.Raw, res.Type.String(), nil
}
return res.String(), res.Type.String(), nil
Expand Down
137 changes: 131 additions & 6 deletions sensors/triggers/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ type Details struct {
}

type Payload struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Details Details `json:"details"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Age int `json:"age"`
IsActive bool `json:"isActive"`
TypelessAge string `json:"typelessAge"`
TypelessIsActive string `json:"typelessIsActive"`
Details Details `json:"details"`
}

func TestConstructPayload(t *testing.T) {
Expand All @@ -98,6 +102,16 @@ func TestConstructPayload(t *testing.T) {
},
Data: []byte("{\"lastName\": \"foo\"}"),
},
"use-event-data-type": {
Context: &v1alpha1.EventContext{
ID: "3",
Type: "calendar",
Source: "calendar-gateway",
DataContentType: common.MediaTypeJSON,
Subject: "example-1",
},
Data: []byte("{\"age\": 100, \"isActive\": false, \"countries\": [\"ca\", \"us\", \"mx\"]}"),
},
}

defaultFirstName := "faker"
Expand All @@ -120,6 +134,36 @@ func TestConstructPayload(t *testing.T) {
},
Dest: "lastName",
},
{
Src: &v1alpha1.TriggerParameterSource{
DependencyName: "use-event-data-type",
DataKey: "age",
UseRawData: true,
},
Dest: "age",
},
{
Src: &v1alpha1.TriggerParameterSource{
DependencyName: "use-event-data-type",
DataKey: "isActive",
UseRawData: true,
},
Dest: "isActive",
},
{
Src: &v1alpha1.TriggerParameterSource{
DependencyName: "use-event-data-type",
DataKey: "age",
},
Dest: "typelessAge",
},
{
Src: &v1alpha1.TriggerParameterSource{
DependencyName: "use-event-data-type",
DataKey: "isActive",
},
Dest: "typelessIsActive",
},
}

payloadBytes, err := ConstructPayload(testEvents, parameters)
Expand All @@ -131,6 +175,10 @@ func TestConstructPayload(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "fake", p.FirstName)
assert.Equal(t, "foo", p.LastName)
assert.Equal(t, 100, p.Age)
assert.Equal(t, false, p.IsActive)
assert.Equal(t, "100", p.TypelessAge)
assert.Equal(t, "false", p.TypelessIsActive)

parameters[0].Src.DataKey = "unknown"
parameters[1].Src.DataKey = "unknown"
Expand All @@ -155,7 +203,7 @@ func TestResolveParamValue(t *testing.T) {
ID: "1",
Time: metav1.Time{Time: time.Now().UTC()},
},
Data: []byte("{\"name\": {\"first\": \"fake\", \"last\": \"user\"} }"),
Data: []byte("{\"name\": {\"first\": \"fake\", \"last\": \"user\"}, \"reviews\": 8, \"rating\": 4.5, \"isActive\" : true, \"isVerified\" : false, \"countries\": [\"ca\", \"us\", \"mx\"]}"),
}
eventBody, err := json.Marshal(event)
assert.Nil(t, err)
Expand Down Expand Up @@ -270,6 +318,33 @@ func TestResolveParamValue(t *testing.T) {
},
result: "fake",
},
{
name: "UseRawData set to true - string",
source: &v1alpha1.TriggerParameterSource{
DependencyName: "fake-dependency",
DataKey: "name.first",
UseRawData: true,
},
result: "fake",
},
{
name: "UseRawData set to true - json",
source: &v1alpha1.TriggerParameterSource{
DependencyName: "fake-dependency",
DataKey: "name",
UseRawData: true,
},
result: "{\"first\": \"fake\", \"last\": \"user\"}",
},
{
name: "UseRawData set to true - list",
source: &v1alpha1.TriggerParameterSource{
DependencyName: "fake-dependency",
DataKey: "countries",
UseRawData: true,
},
result: "[\"ca\", \"us\", \"mx\"]",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -325,7 +400,7 @@ func TestApplyParams(t *testing.T) {
ID: "1",
Time: metav1.Time{Time: time.Now().UTC()},
},
Data: []byte("{\"name\": {\"first\": \"fake\", \"last\": \"user\"} }"),
Data: []byte("{\"name\": {\"first\": \"fake\", \"last\": \"user\"}, \"age\": 100, \"countries\": [\"ca\", \"us\", \"mx\"] }"),
}

events := map[string]*v1alpha1.Event{
Expand Down Expand Up @@ -399,7 +474,7 @@ func TestApplyParams(t *testing.T) {
result: []byte("{\"name\": \"fake\"}"),
},
{
name: "apply block parameters with overwrite operation",
name: "apply block parameters with overwrite operation - useRawDataValue false",
params: []v1alpha1.TriggerParameter{
{
Src: &v1alpha1.TriggerParameterSource{
Expand All @@ -411,8 +486,58 @@ func TestApplyParams(t *testing.T) {
},
},
jsonObj: []byte("{\"name\": \"faker\"}"),
result: []byte("{\"name\": \"{\\\"first\\\": \\\"fake\\\", \\\"last\\\": \\\"user\\\"}\"}"),
},
{
name: "apply block parameters with overwrite operation - useRawDataValue true",
params: []v1alpha1.TriggerParameter{
{
Src: &v1alpha1.TriggerParameterSource{
DependencyName: "fake-dependency",
DataKey: "name",
UseRawData: true,
},
Dest: "name",
Operation: v1alpha1.TriggerParameterOpOverwrite,
},
},
jsonObj: []byte("{\"name\": \"faker\"}"),
result: []byte("{\"name\": {\"first\": \"fake\", \"last\": \"user\"}}"),
},
{
name: "Use raw data types",
params: []v1alpha1.TriggerParameter{
{
Src: &v1alpha1.TriggerParameterSource{
DependencyName: "fake-dependency",
DataKey: "age",
UseRawData: true,
},
Dest: "age",
Operation: v1alpha1.TriggerParameterOpOverwrite,
},
{
Src: &v1alpha1.TriggerParameterSource{
DependencyName: "fake-dependency",
DataKey: "age",
UseRawData: true,
},
Dest: "ageWithYears",
Operation: v1alpha1.TriggerParameterOpAppend,
},
{
Src: &v1alpha1.TriggerParameterSource{
DependencyName: "fake-dependency",
DataKey: "countries",
UseRawData: true,
},
Dest: "countries",
Operation: v1alpha1.TriggerParameterOpAppend,
},
},
jsonObj: []byte("{\"age\": \"this-gets-over-written\", \"ageWithYears\": \"Years: \"}"),
result: []byte("{\"age\": 100, \"ageWithYears\": \"Years: 100\",\"countries\":[\"ca\", \"us\", \"mx\"]}"),
},
}

for _, test := range tests {
Expand Down

0 comments on commit 7aa2d0a

Please sign in to comment.