Skip to content

Commit

Permalink
feat(hog): empty, not empty (#22980)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Jun 14, 2024
1 parent 7710c62 commit fa00f35
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 19 deletions.
30 changes: 23 additions & 7 deletions hogvm/__tests__/__snapshots__/stl.hoge
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
["_h", 32, "-- empty, notEmpty, length, lower, upper, reverse --", 2, "print", 1, 35, 32, "234", 2, "notEmpty", 1, 32,
"", 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]
11, 40, 9, 32, "test", 2, "upper", 1, 2, "print", 1, 35, 32, "spinner", 2, "reverse", 1, 2, "print", 1, 35, 32, "", 2,
"print", 1, 35, 32, "-- encodeURLComponent, decodeURLComponent --", 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, "", 2, "print", 1, 35, 32,
"-- base64Encode, base64Decode --", 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,
32, "", 2, "print", 1, 35, 32, "-- empty --", 2, "print", 1, 35, 31, 2, "empty", 1, 2, "print", 1, 35, 33, 0, 2,
"empty", 1, 2, "print", 1, 35, 33, 1, 2, "empty", 1, 2, "print", 1, 35, 33, -1, 2, "empty", 1, 2, "print", 1, 35, 34,
0.0, 2, "empty", 1, 2, "print", 1, 35, 34, 0.01, 2, "empty", 1, 2, "print", 1, 35, 32, "", 2, "empty", 1, 2, "print", 1,
35, 32, "string", 2, "empty", 1, 2, "print", 1, 35, 32, "0", 2, "empty", 1, 2, "print", 1, 35, 43, 0, 2, "empty", 1, 2,
"print", 1, 35, 42, 0, 2, "empty", 1, 2, "print", 1, 35, 2, "tuple", 0, 2, "empty", 1, 2, "print", 1, 35, 33, 0, 2,
"tuple", 1, 2, "empty", 1, 2, "print", 1, 35, 33, 2, 33, 1, 2, "tuple", 2, 2, "empty", 1, 2, "print", 1, 35, 32, "", 2,
"print", 1, 35, 32, "-- notEmpty --", 2, "print", 1, 35, 31, 2, "notEmpty", 1, 2, "print", 1, 35, 33, 0, 2, "notEmpty",
1, 2, "print", 1, 35, 33, 1, 2, "notEmpty", 1, 2, "print", 1, 35, 33, -1, 2, "notEmpty", 1, 2, "print", 1, 35, 34, 0.0,
2, "notEmpty", 1, 2, "print", 1, 35, 34, 0.01, 2, "notEmpty", 1, 2, "print", 1, 35, 32, "", 2, "notEmpty", 1, 2,
"print", 1, 35, 32, "string", 2, "notEmpty", 1, 2, "print", 1, 35, 32, "0", 2, "notEmpty", 1, 2, "print", 1, 35, 43, 0,
2, "notEmpty", 1, 2, "print", 1, 35, 42, 0, 2, "notEmpty", 1, 2, "print", 1, 35, 2, "tuple", 0, 2, "notEmpty", 1, 2,
"print", 1, 35, 33, 0, 2, "tuple", 1, 2, "notEmpty", 1, 2, "print", 1, 35, 33, 2, 33, 1, 2, "tuple", 2, 2, "notEmpty",
1, 2, "print", 1, 35, 32, "", 2, "print", 1, 35, 32, "-- replaceAll, replaceOne --", 2, "print", 1, 35, 32, "L", 32,
"l", 32, "hello world", 2, "replaceAll", 3, 2, "print", 1, 35, 32, "L", 32, "l", 32, "hello world", 2, "replaceOne", 3,
2, "print", 1, 35]
40 changes: 40 additions & 0 deletions hogvm/__tests__/__snapshots__/stl.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,51 @@
3
TEST
rennips

-- encodeURLComponent, decodeURLComponent --
http%3A%2F%2Fwww.google.com
tom%20%26%20jerry
http://www.google.com
tom & jerry

-- base64Encode, base64Decode --
aHR0cDovL3d3dy5nb29nbGUuY29t
dG9tICYgamVycnk=
http://www.google.com
tom & jerry

-- empty --
true
true
false
false
true
false
true
false
false
true
true
true
false
false

-- notEmpty --
false
false
true
true
false
true
false
true
true
false
false
false
true
true

-- replaceAll, replaceOne --
heLLo worLd
heLlo world
12 changes: 6 additions & 6 deletions hogvm/__tests__/__snapshots__/tuples.hoge
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
["_h", 33, 1, 33, 2, 33, 3, 44, 3, 2, "print", 1, 35, 33, 1, 32, "2", 33, 3, 44, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33,
3, 44, 2, 33, 4, 44, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 2, "print", 1, 35,
33, 1, 33, 2, 33, 3, 44, 3, 36, 0, 33, 1, 45, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3,
33, 1, 45, 33, 1, 45, 33, 1, 45, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 1, 45,
33, 1, 45, 33, 1, 45, 2, "print", 1, 35, 33, 1, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 1, 45, 33,
1, 45, 33, 1, 45, 6, 2, "print", 1, 35, 35]
["_h", 2, "tuple", 0, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 44, 3, 2, "print", 1, 35, 33, 1, 32, "2", 33, 3, 44, 3, 2,
"print", 1, 35, 33, 1, 33, 2, 33, 3, 44, 2, 33, 4, 44, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2,
33, 5, 44, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 44, 3, 36, 0, 33, 1, 45, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3,
33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 1, 45, 33, 1, 45, 33, 1, 45, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44,
2, 44, 2, 33, 5, 44, 3, 33, 1, 45, 33, 1, 45, 33, 1, 45, 2, "print", 1, 35, 33, 1, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2,
44, 2, 33, 5, 44, 3, 33, 1, 45, 33, 1, 45, 33, 1, 45, 6, 2, "print", 1, 35, 35]
1 change: 1 addition & 0 deletions hogvm/__tests__/__snapshots__/tuples.stdout
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tuple()
(1, 2, 3)
(1, '2', 3)
(1, (2, 3), 4)
Expand Down
45 changes: 41 additions & 4 deletions hogvm/__tests__/stl.hog
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,52 @@ 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('')
print('-- encodeURLComponent, decodeURLComponent --')
print(encodeURLComponent('http://www.google.com'))
print(encodeURLComponent('tom & jerry'))

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

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

print(base64Decode(base64Encode('http://www.google.com')))
print(base64Decode(base64Encode('tom & jerry')))
print('')
print('-- empty --')
print(empty(null))
print(empty(0))
print(empty(1))
print(empty(-1))
print(empty(0.0))
print(empty(0.01))
print(empty(''))
print(empty('string'))
print(empty('0'))
print(empty([]))
print(empty({}))
print(empty(tuple()))
print(empty(tuple(0)))
print(empty(tuple(1,2)))
print('')
print('-- notEmpty --')
print(notEmpty(null))
print(notEmpty(0))
print(notEmpty(1))
print(notEmpty(-1))
print(notEmpty(0.0))
print(notEmpty(0.01))
print(notEmpty(''))
print(notEmpty('string'))
print(notEmpty('0'))
print(notEmpty([]))
print(notEmpty({}))
print(notEmpty(tuple()))
print(notEmpty(tuple(0)))
print(notEmpty(tuple(1,2)))
print('')
print('-- replaceAll, replaceOne --')
print(replaceAll('hello world', 'l', 'L'))
print(replaceOne('hello world', 'l', 'L'))

1 change: 1 addition & 0 deletions hogvm/__tests__/tuples.hog
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
print(tuple())
print((1, 2, 3))
print((1, '2', 3))
print((1, (2, 3), 4))
Expand Down
15 changes: 15 additions & 0 deletions hogvm/python/stl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def notEmpty(name: str, args: list[Any], team: Optional["Team"], stdout: Optiona
return bool(args[0])


def _tuple(name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int):
return tuple(args)


def lower(name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int):
return args[0].lower()

Expand Down Expand Up @@ -135,6 +139,14 @@ def decodeURLComponent(
return urllib.parse.unquote(args[0])


def replaceOne(name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int) -> str:
return args[0].replace(args[1], args[2], 1)


def replaceAll(name: str, args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: int) -> str:
return args[0].replace(args[1], args[2])


STL: dict[str, Callable[[str, list[Any], Optional["Team"], list[str] | None, int], Any]] = {
"concat": concat,
"match": match,
Expand All @@ -146,6 +158,7 @@ def decodeURLComponent(
"length": length,
"empty": empty,
"notEmpty": notEmpty,
"tuple": _tuple,
"lower": lower,
"upper": upper,
"reverse": reverse,
Expand All @@ -158,4 +171,6 @@ def decodeURLComponent(
"base64Decode": base64Decode,
"encodeURLComponent": encodeURLComponent,
"decodeURLComponent": decodeURLComponent,
"replaceOne": replaceOne,
"replaceAll": replaceAll,
}
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.12",
"version": "1.0.13",
"description": "PostHog Hog Virtual Machine",
"types": "dist/index.d.ts",
"main": "dist/index.js",
Expand Down
3 changes: 3 additions & 0 deletions hogvm/typescript/src/stl/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export function escapeIdentifier(identifier: string | number): string {
export function printHogValue(obj: any): string {
if (Array.isArray(obj)) {
if ((obj as any).__isHogTuple) {
if (obj.length < 2) {
return `tuple(${obj.map(printHogValue).join(', ')})`
}
return `(${obj.map(printHogValue).join(', ')})`
} else {
return `[${obj.map(printHogValue).join(', ')}]`
Expand Down
24 changes: 23 additions & 1 deletion hogvm/typescript/src/stl/stl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,26 @@ export const STL: Record<string, (args: any[], name: string, timeout: number) =>
return args[0].length
},
empty: (args) => {
if (typeof args[0] === 'object') {
if (Array.isArray(args[0])) {
return args[0].length === 0
} else if (args[0] === null) {
return true
} else if (args[0] instanceof Map) {
return args[0].size === 0
} else {
return Object.keys(args[0]).length === 0
}
}
return !args[0]
},
notEmpty: (args) => {
return !!args[0]
return !STL.empty(args, 'empty', 0)
},
tuple: (args) => {
const tuple = args.slice()
;(tuple as any).__isHogTuple = true
return tuple
},
lower: (args) => {
return args[0].toLowerCase()
Expand Down Expand Up @@ -105,6 +121,12 @@ export const STL: Record<string, (args: any[], name: string, timeout: number) =>
decodeURLComponent(args) {
return decodeURIComponent(args[0])
},
replaceOne(args) {
return args[0].replace(args[1], args[2])
},
replaceAll(args) {
return args[0].replaceAll(args[1], args[2])
},
}

export const ASYNC_STL: Record<string, (args: any[], name: string, timeout: number) => Promise<any>> = {
Expand Down

0 comments on commit fa00f35

Please sign in to comment.