更新时间:2026-01-07 GMT+08:00
仓颉函数开发指南(使用Visual Studio Code)
- 仓颉函数仅支持在Linux环境下开发。
- 当前语言编写函数时,只支持上传zip包形式,所需依赖均已合入zip包中。
- 创建函数工程。
- 创建Cangjie工程。
图1 创建Cangjie工程
- 添加工程依赖。
- 配置依赖。
在用户函数的module.json文件中,依赖下载的sdk。
{ "cjc_version": "0.xx.xx", "organization": "yuanrong", "name": "user_func_test", "description": "user func demo", "version": "1.0.0", "build_dir": "", "requires": {}, "dev_requires": {}, "package_requires": { "path_option":["./yuanrong_runtime_sdk"] }, "foreign_requires": {}, "output_type": "dynamic", "command_option": "", "condition_option": { "bind_now": "--link-options \"-z now\"" }, "link_option": "", "cross_compile_configuration": {}, "scripts": {}, "package_configuration": {} }
- 创建Cangjie工程。
- 创建本地函数。
- 创建文件夹funcdemo,并在包下创建success_invoke类。
图3 创建success_invoke类
- 在success_invoke.cj中定义函数运行入口,示例代码如下:
package funcdemo from yuanrong_runtime_sdk import yuanrong_sdk.Context from yuanrong_runtime_sdk import yuanrong_sdk.APIGTriggerEvent from yuanrong_runtime_sdk import yuanrong_sdk.APIGTriggerResponse from yuanrong_runtime_sdk import yuanrong_sdk.FuncRegister from yuanrong_runtime_sdk import entry_macro.Entry from encoding import json.* from std import collection.* from std import os.* from serialization import serialization.* @Entry public func handleRequest(args: APIGTriggerEvent, context: Context): APIGTriggerResponse { var contextRunningTime: String = context.getRunningTimeInSeconds().toString() println("args is ${args.getBody()}") var resp = APIGTriggerResponse() resp.setBody(args.getBody()) resp.setStatusCode(200) resp }
- 创建文件夹funcdemo,并在包下创建success_invoke类。
- 工程打包。
使用cjpm build 构建工程。
- 创建Cangjie函数,使用zip包的方式上传代码包,具体创建请参见《用户指南》的“使用空白模板创建函数”章节。
把生成出来的.so文件打包并上传到元戎环境。

测试调用函数
- 在函数详情页面中,继续选择“代码 > 配置测试事件”,在弹出窗口中选择“创建新的测试事件 > 空白模板”。
测试用例内容可用如下:
{ "isBase64Encoded": false, "httpMethod": "POST", "path": "", "body": "test_cangjie" }
这里需要按照元戎sdk里定义的APIGTriggerEvent的类传递参数,否则可能会导致序列化、反序列化异常。public open class APIGTriggerEvent { private var isBase64Encoded: Bool = false; private var httpMethod: String = "POST"; private var path: String = ""; private var body: String = ""; } - 测试事件配置完成后,单击“测试”。
父主题: 仓颉函数开发指南
