Skip to content

Commit

Permalink
fixup - Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
adatzer committed Jun 20, 2024
1 parent d0308e0 commit 61776c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ transform {
use "jq" {
jq_command = <<JQEOT
{
my_api_key: "${env.TESTAPIKEY}",
my_app_id: .app_id,
my_nested_prop: {
playback_rate: .contexts_com_snowplowanalytics_snowplow_media_player_1[0].playbackRate
Expand Down
22 changes: 11 additions & 11 deletions pkg/transform/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ import (

// JQMapperConfig represents the configuration for the JQ transformation
type JQMapperConfig struct {
JQCommand string `hcl:"jq_command"`
RunTimeout int `hcl:"timeout_sec,optional"`
SpMode bool `hcl:"snowplow_mode,optional"`
JQCommand string `hcl:"jq_command"`
RunTimeoutMs int `hcl:"timeout_ms,optional"`
SpMode bool `hcl:"snowplow_mode,optional"`
}

// JQMapper handles jq generic mapping as a transformation
type jqMapper struct {
JQCode *gojq.Code
RunTimeout time.Duration
SpMode bool
JQCode *gojq.Code
RunTimeoutMs time.Duration
SpMode bool
}

// RunFunction runs a jq mapper transformation
Expand All @@ -46,7 +46,7 @@ func (jqm *jqMapper) RunFunction() TransformationFunction {
return nil, nil, message, nil
}

ctx, cancel := context.WithTimeout(context.Background(), jqm.RunTimeout)
ctx, cancel := context.WithTimeout(context.Background(), jqm.RunTimeoutMs)
defer cancel()

iter := jqm.JQCode.RunWithContext(ctx, input)
Expand Down Expand Up @@ -80,7 +80,7 @@ type jqMapperAdapter func(i interface{}) (interface{}, error)
// ProvideDefault implements the ComponentConfigurable interface
func (f jqMapperAdapter) ProvideDefault() (interface{}, error) {
return &JQMapperConfig{
RunTimeout: 15,
RunTimeoutMs: 100,
}, nil
}

Expand Down Expand Up @@ -114,9 +114,9 @@ func jqMapperConfigFunction(c *JQMapperConfig) (TransformationFunction, error) {
}

jq := &jqMapper{
JQCode: code,
RunTimeout: time.Duration(c.RunTimeout) * time.Second,
SpMode: c.SpMode,
JQCode: code,
RunTimeoutMs: time.Duration(c.RunTimeoutMs) * time.Millisecond,
SpMode: c.SpMode,
}

return jq.RunFunction(), nil
Expand Down
48 changes: 24 additions & 24 deletions pkg/transform/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func TestJQRunFunction_SpMode_true(t *testing.T) {
assert := assert.New(t)

jqConfig := &JQMapperConfig{
JQCommand: tt.JQCommand,
RunTimeout: 15,
SpMode: true,
JQCommand: tt.JQCommand,
RunTimeoutMs: 100,
SpMode: true,
}

transFun, err := jqMapperConfigFunction(jqConfig)
Expand Down Expand Up @@ -199,9 +199,9 @@ func TestJQRunFunction_SpMode_false(t *testing.T) {
assert := assert.New(t)

jqConfig := &JQMapperConfig{
JQCommand: tt.JQCommand,
RunTimeout: 15,
SpMode: false,
JQCommand: tt.JQCommand,
RunTimeoutMs: 100,
SpMode: false,
}

transFun, err := jqMapperConfigFunction(jqConfig)
Expand Down Expand Up @@ -256,9 +256,9 @@ func TestJQRunFunction_errors(t *testing.T) {
{
Scenario: "not_a_map_a",
JQConfig: &JQMapperConfig{
JQCommand: `.`,
RunTimeout: 5,
SpMode: false,
JQCommand: `.`,
RunTimeoutMs: 100,
SpMode: false,
},
InputMsg: &models.Message{
Data: []byte(`[]`),
Expand All @@ -279,9 +279,9 @@ func TestJQRunFunction_errors(t *testing.T) {
{
Scenario: "not_a_map_b",
JQConfig: &JQMapperConfig{
JQCommand: `.`,
RunTimeout: 5,
SpMode: false,
JQCommand: `.`,
RunTimeoutMs: 100,
SpMode: false,
},
InputMsg: &models.Message{
Data: []byte(`a`),
Expand All @@ -302,9 +302,9 @@ func TestJQRunFunction_errors(t *testing.T) {
{
Scenario: "not_snowplow_event_with_spMode_true",
JQConfig: &JQMapperConfig{
JQCommand: `.`,
RunTimeout: 5,
SpMode: true,
JQCommand: `.`,
RunTimeoutMs: 100,
SpMode: true,
},
InputMsg: &models.Message{
Data: []byte(`a`),
Expand All @@ -325,9 +325,9 @@ func TestJQRunFunction_errors(t *testing.T) {
{
Scenario: "deadline_exceeded",
JQConfig: &JQMapperConfig{
JQCommand: `{foo: .app_id}`,
RunTimeout: 0,
SpMode: true,
JQCommand: `{foo: .app_id}`,
RunTimeoutMs: 0,
SpMode: true,
},
InputMsg: &models.Message{
Data: SnowplowTsv1,
Expand All @@ -348,9 +348,9 @@ func TestJQRunFunction_errors(t *testing.T) {
{
Scenario: "no_output",
JQConfig: &JQMapperConfig{
JQCommand: `.foo[].value`,
RunTimeout: 5,
SpMode: false,
JQCommand: `.foo[].value`,
RunTimeoutMs: 100,
SpMode: false,
},
InputMsg: &models.Message{
Data: []byte(`{"foo": []}`),
Expand Down Expand Up @@ -440,9 +440,9 @@ func TestJQMapperConfigFunction(t *testing.T) {
assert := assert.New(t)

jqCfg := &JQMapperConfig{
JQCommand: tt.JQCommand,
RunTimeout: 5,
SpMode: false,
JQCommand: tt.JQCommand,
RunTimeoutMs: 100,
SpMode: false,
}

transFun, err := jqMapperConfigFunction(jqCfg)
Expand Down

0 comments on commit 61776c3

Please sign in to comment.