Skip to content

Commit

Permalink
feat(hog): typeof (#24742)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Sep 2, 2024
1 parent 2138e3f commit c2c8b26
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 11 deletions.
5 changes: 5 additions & 0 deletions hogvm/__tests__/__snapshots__/typeof.hoge
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
["_H", 1, 52, "test", 1, 0, 11, 36, 0, 2, "typeof", 1, 2, "print", 1, 35, 31, 38, 53, 0, 32, "hello world", 36, 0, 54,
1, 35, 33, 123, 36, 0, 54, 1, 35, 34, 1.23, 36, 0, 54, 1, 35, 29, 36, 0, 54, 1, 35, 30, 36, 0, 54, 1, 35, 31, 36, 0, 54,
1, 35, 42, 0, 36, 0, 54, 1, 35, 43, 0, 36, 0, 54, 1, 35, 33, 1, 33, 2, 33, 3, 44, 3, 36, 0, 54, 1, 35, 52, "lambda", 0,
0, 6, 33, 2, 33, 1, 6, 38, 53, 0, 36, 0, 54, 1, 35, 32, "2021-01-01T00:00:00Z", 2, "toDateTime", 1, 36, 0, 54, 1, 35,
32, "2021-01-01", 2, "toDate", 1, 36, 0, 54, 1, 35, 32, "BigError", 32, "message", 2, "Error", 2, 36, 0, 54, 1, 35, 35]
13 changes: 13 additions & 0 deletions hogvm/__tests__/__snapshots__/typeof.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
string
integer
float
boolean
boolean
null
object
array
tuple
function
datetime
date
error
17 changes: 17 additions & 0 deletions hogvm/__tests__/typeof.hog
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fun test(obj) {
print(typeof(obj))
}

test('hello world')
test(123)
test(1.23)
test(true)
test(false)
test(null)
test({})
test([])
test((1,2,3))
test(() -> 1 + 2)
test(toDateTime('2021-01-01T00:00:00Z'))
test(toDate('2021-01-01'))
test(Error('BigError', 'message'))
29 changes: 29 additions & 0 deletions hogvm/python/stl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,34 @@ def _formatDateTime(args: list[Any], team: Optional["Team"], stdout: Optional[li
return formatDateTime(args[0], args[1], args[2] if len(args) > 2 else None)


def _typeof(args: list[Any], team: Optional["Team"], stdout: Optional[list[str]], timeout: float) -> str:
if args[0] is None:
return "null"
elif is_hog_datetime(args[0]):
return "datetime"
elif is_hog_date(args[0]):
return "date"
elif is_hog_error(args[0]):
return "error"
elif is_hog_callable(args[0]) or is_hog_closure(args[0]):
return "function"
elif isinstance(args[0], list):
return "array"
elif isinstance(args[0], tuple):
return "tuple"
elif isinstance(args[0], dict):
return "object"
elif args[0] is True or args[0] is False:
return "boolean"
elif isinstance(args[0], int):
return "integer"
elif isinstance(args[0], float):
return "float"
elif isinstance(args[0], str):
return "string"
return "unknown"


STL: dict[str, STLFunction] = {
"concat": STLFunction(
fn=lambda args, team, stdout, timeout: "".join(
Expand Down Expand Up @@ -424,6 +452,7 @@ def _formatDateTime(args: list[Any], team: Optional["Team"], stdout: Optional[li
minArgs=0,
maxArgs=2,
),
"typeof": STLFunction(fn=_typeof, minArgs=1, maxArgs=1),
# only in python, async function in nodejs
"sleep": STLFunction(fn=sleep, minArgs=1, maxArgs=1),
"run": STLFunction(fn=run, minArgs=1, maxArgs=1),
Expand Down
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.42",
"version": "1.0.43",
"description": "PostHog Hog Virtual Machine",
"types": "dist/index.d.ts",
"source": "src/index.ts",
Expand Down
31 changes: 31 additions & 0 deletions hogvm/typescript/src/stl/stl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,37 @@ export const STL: Record<string, STLFunction> = {
minArgs: 0,
maxArgs: 2,
},
typeof: {
fn: (args) => {
if (args[0] === null || args[0] === undefined) {
return 'null'
} else if (isHogDateTime(args[0])) {
return 'datetime'
} else if (isHogDate(args[0])) {
return 'date'
} else if (isHogError(args[0])) {
return 'error'
} else if (isHogCallable(args[0]) || isHogClosure(args[0])) {
return 'function'
} else if (Array.isArray(args[0])) {
if ((args[0] as any).__isHogTuple) {
return 'tuple'
}
return 'array'
} else if (typeof args[0] === 'object') {
return 'object'
} else if (typeof args[0] === 'number') {
return Number.isInteger(args[0]) ? 'integer' : 'float'
} else if (typeof args[0] === 'string') {
return 'string'
} else if (typeof args[0] === 'boolean') {
return 'boolean'
}
return 'unknown'
},
minArgs: 1,
maxArgs: 1,
},
}

export const ASYNC_STL: Record<string, AsyncSTLFunction> = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@medv/finder": "^3.1.0",
"@microlink/react-json-view": "^1.21.3",
"@monaco-editor/react": "4.6.0",
"@posthog/hogvm": "^1.0.42",
"@posthog/hogvm": "^1.0.43",
"@posthog/icons": "0.7.3",
"@posthog/plugin-scaffold": "^1.4.4",
"@react-hook/size": "^2.1.2",
Expand Down
2 changes: 1 addition & 1 deletion plugin-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@maxmind/geoip2-node": "^3.4.0",
"@posthog/clickhouse": "^1.7.0",
"@posthog/cyclotron": "file:../rust/cyclotron-node",
"@posthog/hogvm": "^1.0.42",
"@posthog/hogvm": "^1.0.43",
"@posthog/plugin-scaffold": "1.4.4",
"@sentry/node": "^7.49.0",
"@sentry/profiling-node": "^0.3.0",
Expand Down
8 changes: 4 additions & 4 deletions plugin-server/pnpm-lock.yaml

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

8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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

0 comments on commit c2c8b26

Please sign in to comment.