コンテンツにスキップ

コントラクト

型付きコントラクトを通じてサービスを呼び出し。スキーマ検証と非同期実行サポート付きでリモートAPI、ワークフロー、関数を呼び出し。

local contract = require("contract")

IDで直接バインディングを開く:

local greeter, err = contract.open("app.services:greeter")
if err then
return nil, err
end
local result, err = greeter:say_hello("Alice")

スコープコンテキストまたはクエリパラメータ付き:

-- スコープテーブル付き
local svc, err = contract.open("app.services:user", {
tenant_id = "acme",
region = "us-east"
})
-- クエリパラメータ付き(自動変換: "true"→bool, numbers→int/float)
local api, err = contract.open("app.services:api?debug=true&timeout=5000")
-- 呼び出しオプション付き(第3引数)
local inst, err = contract.open("app.services:flaky", nil, {
retry = { max_attempts = 5, initial_delay = 100 }
})
パラメータ説明
binding_idstringバインディングID、クエリパラメータをサポート
scopetableコンテキスト値(オプション、クエリパラメータをオーバーライド)
optionstable呼び出しオプション(オプション)— 例: retry.max_attempts, retry.initial_delay

戻り値: Instance, error

イントロスペクション用のコントラクト定義を取得:

local c, err = contract.get("app.services:greeter")
print(c:id()) -- "app.services:greeter"
local methods = c:methods()
for _, m in ipairs(methods) do
print(m.name, m.description)
end
local method, err = c:method("say_hello")
フィールド説明
namestringメソッド名
descriptionstringメソッドの説明
input_schemastable[]入力スキーマ定義
output_schemastable[]出力スキーマ定義

コントラクトを実装するすべてのバインディングをリスト:

local bindings, err = contract.find_implementations("app.services:greeter")
for _, binding_id in ipairs(bindings) do
print(binding_id)
end

またはコントラクトオブジェクト経由:

local c, err = contract.get("app.services:greeter")
local bindings, err = c:implementations()

インスタンスがコントラクトを実装しているかチェック:

if contract.is(instance, "app.services:greeter") then
instance:say_hello("World")
end

同期呼び出し - 完了までブロック:

local calc, err = contract.open("app.services:calculator")
local sum, err = calc:add(10, 20)
local product, err = calc:multiply(5, 6)

非同期実行には_asyncサフィックスを追加:

local processor, err = contract.open("app.services:processor")
local future, err = processor:process_async(large_dataset)
-- 他の処理を実行...
-- 結果を待機
local ch = future:response()
local payload, ok = ch:receive()
if ok then
local result = payload:data()
end

FutureメソッドについてはFutureを参照。

コントラクトオブジェクトを通じてバインディングを開く:

local c, err = contract.get("app.services:user")
-- デフォルトバインディング
local instance, err = c:open()
-- 特定のバインディング
local instance, err = c:open("app.services:user_impl")
-- スコープ付き
local instance, err = c:open(nil, {user_id = 123})
local instance, err = c:open("app.services:user_impl", {user_id = 123})

事前設定されたコンテキスト付きラッパーを作成:

local c, err = contract.get("app.services:user")
local wrapped = c:with_context({
request_id = ctx.get("request_id"),
user_id = current_user.id
})
local instance, err = wrapped:open()

with_options でリトライおよびその他の呼び出し動作を構成します:

local c, err = contract.get("app.services:flaky")
local inst, err = c
:with_options({ retry = { max_attempts = 5, initial_delay = 100 } })
:open("app.services:flaky_impl")
local result, err = inst:call()

オプションは返されるインスタンスのすべてのメソッド呼び出しに適用されます。リトライ可能なエラーのみがリトライをトリガーします。リトライ不可能なエラーは即座に返されます。with_contextwith_actorwith_scope とチェーン可能です。

オプション説明
retry.max_attemptsint最初を含む最大試行回数 (1 はリトライを無効化)
retry.initial_delayint/duration最初のリトライ前の遅延(ミリ秒または duration 文字列)

認可用のアクターとスコープを設定:

local security = require("security")
local c, err = contract.get("app.services:admin")
local secured = c:with_actor(security.actor()):with_scope(security.scope())
local admin, err = secured:open()
権限リソース関数
contract.getcontract idget()
contract.openbinding idopen()Contract:open()
contract.implementationscontract idfind_implementations()Contract:implementations()
contract.callmethod name同期および非同期メソッド呼び出し
contract.context”context”Contract:with_context()
contract.security”security”Contract:with_actor()Contract:with_scope()
条件種別
無効なバインディングIDフォーマットerrors.INVALID
コントラクトが見つからないerrors.NOT_FOUND
バインディングが見つからないerrors.NOT_FOUND
メソッドが見つからないerrors.NOT_FOUND
デフォルトバインディングがないerrors.NOT_FOUND
権限拒否errors.PERMISSION_DENIED
呼び出し失敗errors.INTERNAL