Skip to content

Commit

Permalink
feat(hog): generateUUIDv4 (#23006)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Jun 17, 2024
1 parent 1bc6300 commit d0a3395
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion hogvm/__tests__/__snapshots__/stl.hoge
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
"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]
2, "print", 1, 35, 32, "", 2, "print", 1, 35, 32, "-- generateUUIDv4 --", 2, "print", 1, 35, 2, "generateUUIDv4", 0, 2,
"length", 1, 2, "print", 1, 35]
3 changes: 3 additions & 0 deletions hogvm/__tests__/__snapshots__/stl.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ true
-- replaceAll, replaceOne --
heLLo worLd
heLlo world

-- generateUUIDv4 --
36
3 changes: 3 additions & 0 deletions hogvm/__tests__/stl.hog
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ print('')
print('-- replaceAll, replaceOne --')
print(replaceAll('hello world', 'l', 'L'))
print(replaceOne('hello world', 'l', 'L'))
print('')
print('-- generateUUIDv4 --')
print(length(generateUUIDv4()))

9 changes: 9 additions & 0 deletions hogvm/python/stl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ def replaceAll(name: str, args: list[Any], team: Optional["Team"], stdout: Optio
return args[0].replace(args[1], args[2])


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

return str(uuid.uuid4())


STL: dict[str, Callable[[str, list[Any], Optional["Team"], list[str] | None, int], Any]] = {
"concat": concat,
"match": match,
Expand All @@ -173,4 +181,5 @@ def replaceAll(name: str, args: list[Any], team: Optional["Team"], stdout: Optio
"decodeURLComponent": decodeURLComponent,
"replaceOne": replaceOne,
"replaceAll": replaceAll,
"generateUUIDv4": generateUUIDv4,
}
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.13",
"version": "1.0.14",
"description": "PostHog Hog Virtual Machine",
"types": "dist/index.d.ts",
"main": "dist/index.js",
Expand Down
7 changes: 7 additions & 0 deletions hogvm/typescript/src/stl/stl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ export const STL: Record<string, (args: any[], name: string, timeout: number) =>
replaceAll(args) {
return args[0].replaceAll(args[1], args[2])
},
generateUUIDv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
},
}

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

0 comments on commit d0a3395

Please sign in to comment.