Skip to content

Commit

Permalink
Common: fix JS enrichment failing without return statement (close #295)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuwy committed Jul 22, 2020
1 parent d16e9c9 commit 3a5ec74
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ final case class JavascriptScriptEnrichment(schemaKey: SchemaKey, rawFunction: S

private val stringified = rawFunction + """
function getJavascriptContexts(event) {
return JSON.stringify(process(event));
var result = process(event);
if (result == null) {
return "[]"
} else {
return JSON.stringify(result);
}
}
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ import outputs.EnrichedEvent

class JavascriptScriptEnrichmentSpec extends Specification {
def is = s2"""
Javascript enrichment should fail if the function isn't valid $e1
Javascript enrichment should fail if the function doesn't return an array $e2
Javascript enrichment should fail if the function doesn't return an array of SDJs $e3
Javascript enrichment should be able to access the fields of the enriched event $e4
Javascript enrichment should be able to update the fields of the enriched event $e5
Javascript enrichment should be able to throw an exception $e6
Javascript enrichment should be able to return no new context $e7
Javascript enrichment should be able to return 2 new contexts $e8
Javascript enrichment should fail if the function isn't valid $e1
Javascript enrichment should fail if the function doesn't return an array $e2
Javascript enrichment should fail if the function doesn't return an array of SDJs $e3
Javascript enrichment should be able to access the fields of the enriched event $e4
Javascript enrichment should be able to update the fields of the enriched event $e5
Javascript enrichment should be able to throw an exception $e6
Javascript enrichment should be able to return no new context $e7
Javascript enrichment should be able to return 2 new contexts $e8
Javascript enrichment should be able to proceed without return statement $e9
Javascript enrichment should be able to proceed with return null $e10
Javascript enrichment should be able to update the fields without return statement $e11
"""

val schemaKey =
Expand Down Expand Up @@ -137,6 +140,36 @@ class JavascriptScriptEnrichmentSpec extends Specification {
}
}

def e9 = {
val function = s"""
function process(event) {
var a = 42 // no-op
}"""

JavascriptScriptEnrichment(schemaKey, function).process(buildEnriched()) must beRight(Nil)
}

def e10 = {
val function = s"""
function process(event) {
return null
}"""

JavascriptScriptEnrichment(schemaKey, function).process(buildEnriched()) must beRight(Nil)
}

def e11 = {
val appId = "greatApp"
val enriched = buildEnriched(appId)
val newAppId = "evenBetterApp"
val function = s"""
function process(event) {
event.setApp_id("$newAppId")
}"""
JavascriptScriptEnrichment(schemaKey, function).process(enriched)
enriched.app_id must beEqualTo(newAppId)
}

def buildEnriched(appId: String = "my super app"): EnrichedEvent = {
val e = new EnrichedEvent()
e.platform = "server"
Expand Down

0 comments on commit 3a5ec74

Please sign in to comment.