Overview
Functions are implemented in Go and exposed to the Otto JavaScript VM runtime ininternal/functions/.
Function Registry
Functions are registered ininternal/functions/otto_runtime.go:
Steps to Add a Function
1. Implement the Function
Add to an appropriate file ininternal/functions/:
2. Register the Function
Updateinternal/functions/otto_runtime.go:
3. Add to Function List
Updateinternal/functions/registry.go for ListFunctions():
4. Write Tests
Add tointernal/functions/registry_test.go:
Return Types
String
Boolean
Number
Array
Object/Map
Working with Context
Functions can access the execution context:Example: Hash Function
Example: HTTP Fetch Function
Best Practices
- Validate arguments - Check argument count and types
- Handle errors gracefully - Return undefined for errors
- Document the function - Add to ListFunctions()
- Write tests - Cover happy path and edge cases
- Keep functions pure - Minimize side effects
- Support optional arguments - Use sensible defaults
Function Categories
Organize functions by category:| Category | File | Functions |
|---|---|---|
file | file_functions.go | fileExists, fileLength, readFile, writeFile |
string | string_functions.go | trim, split, join, replace, contains |
util | util_functions.go | log_info, getEnvVar, exit |
http | http_functions.go | http_get, http_post |
db | db_functions.go | db_select, db_select_assets |
jq | jq.go | jq |
Next Steps
- Adding Step Types - Custom step executors
- Functions Reference - All functions
- Functions Overview - Usage guide