Skip to content

Commit

Permalink
fix(hogvm): if without else, add base64 and url encoding/decoding (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Jun 14, 2024
1 parent 7e3a365 commit 919d017
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 4 deletions.
3 changes: 3 additions & 0 deletions hogvm/__tests__/__snapshots__/ifJump.hoge
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
["_h", 42, 0, 36, 0, 32, "email", 45, 32, "", 36, 1, 11, 40, 12, 32, "ERROR - Email not found!", 2, "print", 1, 35, 32,
"3", 2, "print", 1, 35, 32, "1", 2, "print", 1, 35, 32, "", 36, 1, 11, 40, 14, 32, "ERROR - Email not found!", 2,
"print", 1, 35, 32, "3", 2, "print", 1, 35, 39, 6, 32, "else", 2, "print", 1, 35, 32, "1", 2, "print", 1, 35, 35, 35]
3 changes: 3 additions & 0 deletions hogvm/__tests__/__snapshots__/ifJump.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1
else
1
10 changes: 8 additions & 2 deletions hogvm/__tests__/__snapshots__/stl.hoge
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
["_h", 32, "-- empty, notEmpty, length, lower, upper, reverse --", 2, "print", 1, 35, 32, "234", 2, "notEmpty", 1, 32,
"", 2, "empty", 1, 3, 2, 40, 11, 32, "123", 2, "length", 1, 2, "print", 1, 35, 32, "tdd4gh", 32, "Tdd4gh", 2, "lower",
1, 11, 40, 11, 32, "test", 2, "upper", 1, 2, "print", 1, 35, 32, "spinner", 2, "reverse", 1, 2, "print", 1, 35]
"", 2, "empty", 1, 3, 2, 40, 9, 32, "123", 2, "length", 1, 2, "print", 1, 35, 32, "tdd4gh", 32, "Tdd4gh", 2, "lower", 1,
11, 40, 9, 32, "test", 2, "upper", 1, 2, "print", 1, 35, 32, "spinner", 2, "reverse", 1, 2, "print", 1, 35, 32,
"http://www.google.com", 2, "encodeURLComponent", 1, 2, "print", 1, 35, 32, "tom & jerry", 2, "encodeURLComponent", 1,
2, "print", 1, 35, 32, "http://www.google.com", 2, "encodeURLComponent", 1, 2, "decodeURLComponent", 1, 2, "print", 1,
35, 32, "tom & jerry", 2, "encodeURLComponent", 1, 2, "decodeURLComponent", 1, 2, "print", 1, 35, 32,
"http://www.google.com", 2, "base64Encode", 1, 2, "print", 1, 35, 32, "tom & jerry", 2, "base64Encode", 1, 2, "print",
1, 35, 32, "http://www.google.com", 2, "base64Encode", 1, 2, "base64Decode", 1, 2, "print", 1, 35, 32, "tom & jerry", 2,
"base64Encode", 1, 2, "base64Decode", 1, 2, "print", 1, 35]
8 changes: 8 additions & 0 deletions hogvm/__tests__/__snapshots__/stl.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
3
TEST
rennips
http%3A%2F%2Fwww.google.com
tom%20%26%20jerry
http://www.google.com
tom & jerry
aHR0cDovL3d3dy5nb29nbGUuY29t
dG9tICYgamVycnk=
http://www.google.com
tom & jerry
20 changes: 20 additions & 0 deletions hogvm/__tests__/ifJump.hog
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let props := {
}
let email := props.email

if (email == '') {
print('ERROR - Email not found!')
print('3')
}

print('1')


if (email == '') {
print('ERROR - Email not found!')
print('3')
} else {
print('else')
}

print('1')
12 changes: 12 additions & 0 deletions hogvm/__tests__/stl.hog
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@ print('-- empty, notEmpty, length, lower, upper, reverse --')
if (empty('') and notEmpty('234')) print(length('123'))
if (lower('Tdd4gh') == 'tdd4gh') print(upper('test'))
print(reverse('spinner'))

print(encodeURLComponent('http://www.google.com'))
print(encodeURLComponent('tom & jerry'))

print(decodeURLComponent(encodeURLComponent('http://www.google.com')))
print(decodeURLComponent(encodeURLComponent('tom & jerry')))

print(base64Encode('http://www.google.com'))
print(base64Encode('tom & jerry'))

print(base64Decode(base64Encode('http://www.google.com')))
print(base64Decode(base64Encode('tom & jerry')))
32 changes: 32 additions & 0 deletions hogvm/python/stl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ def jsonStringify(name: str, args: list[Any], team: Optional["Team"], stdout: Op
return json.dumps(args[0])


def base64Encode(name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int) -> str:
import base64

return base64.b64encode(args[0].encode()).decode()


def base64Decode(name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int) -> str:
import base64

return base64.b64decode(args[0].encode()).decode()


def encodeURLComponent(
name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int
) -> str:
import urllib.parse

return urllib.parse.quote(args[0], safe="")


def decodeURLComponent(
name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int
) -> str:
import urllib.parse

return urllib.parse.unquote(args[0])


STL: dict[str, Callable[[str, list[Any], Optional["Team"], list[str] | None, int], Any]] = {
"concat": concat,
"match": match,
Expand All @@ -126,4 +154,8 @@ def jsonStringify(name: str, args: list[Any], team: Optional["Team"], stdout: Op
"run": run,
"jsonParse": jsonParse,
"jsonStringify": jsonStringify,
"base64Encode": base64Encode,
"base64Decode": base64Decode,
"encodeURLComponent": encodeURLComponent,
"decodeURLComponent": decodeURLComponent,
}
2 changes: 1 addition & 1 deletion hogvm/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@posthog/hogvm",
"version": "1.0.11",
"version": "1.0.12",
"description": "PostHog Hog Virtual Machine",
"types": "dist/index.d.ts",
"main": "dist/index.js",
Expand Down
19 changes: 19 additions & 0 deletions hogvm/typescript/src/stl/stl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ export const STL: Record<string, (args: any[], name: string, timeout: number) =>
}
return JSON.stringify(convert(args[0]))
},
base64Encode: (args) => {
return Buffer.from(args[0]).toString('base64')
},
base64Decode: (args) => {
return Buffer.from(args[0], 'base64').toString()
},
tryBase64Decode: (args) => {
try {
return Buffer.from(args[0], 'base64').toString()
} catch (e) {
return ''
}
},
encodeURLComponent(args) {
return encodeURIComponent(args[0])
},
decodeURLComponent(args) {
return decodeURIComponent(args[0])
},
}

export const ASYNC_STL: Record<string, (args: any[], name: string, timeout: number) => Promise<any>> = {
Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql/bytecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def visit_if_statement(self, node: ast.IfStatement):

response = []
response.extend(expr)
response.extend([Operation.JUMP_IF_FALSE, len(then) + 2]) # + else's OP_JUMP + count
response.extend([Operation.JUMP_IF_FALSE, len(then) + (2 if else_ else 0)])
response.extend(then)
if else_:
response.extend([Operation.JUMP, len(else_)])
Expand Down

0 comments on commit 919d017

Please sign in to comment.