icp-cli: bindgen record fields with opt are undefined when empty, not null

オープン 初心者向け
#409 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 6 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
ドキュメント
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript

調査の方向性

skills/icp-cli/references/binding-generation.md:67-69 と、「Candid opt T representation in bindgen」という名前の既存のevalを読んでください。単純なopt値とoptionalなrecordフィールドを区別し、バージョンに依存しない空値チェックを推奨し、recordフィールドのケース用のevalを追加するよう、ドキュメントを更新してください。修正されたマッピングが文書化され、新しいevalが合格すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

skills/icp-cli/references/binding-generation.md:67-69 says bindgen's wrapper turns opt T into T | null and tells agents to "always use T | null". That holds for a plain opt, but not for a record field: an opt T field is an optional property that is undefined when empty. An agent following the rule writes if (r.value !== null), which is true for an empty field. The certified-variables helper had exactly this bug: it rejected a valid proof of absence (fixed in #407).

Verified against the @icp-sdk/bindgen 0.4.0 and 0.4.1 wrappers:

Candid position Wrapper type Empty value
argument, return value, vec element, variant payload: opt T T | null null
record field f : opt T f?: T undefined (field omitted)
nested opt opt T (not in a record) Some<T | null> | None None

Needed regardless of the bindgen PRs

These hold on 0.4.1 and after dfinity/icp-js-bindgen#197, dfinity/icp-js-bindgen#198 and dfinity/icp-js-bindgen#201:

  • Correct the section in binding-generation.md. T | null applies to a plain opt. A record field is optional and undefined when empty. To send an absent field, omit it or pass undefined.
  • Recommend == null or ?? for "is it empty". It is correct in every position and version, so an agent doesn't need to know which position it is in.
  • Add an eval for the record-field case. The existing case ("Candid opt T representation in bindgen") covers only a plain return value, and its expected result !== null stays correct.
  • Fix it in place, in icp-cli. #406 moves binding-generation.md's opt section into canister-calls later; the move then carries the corrected text. #406's own Candid↔TS table had the same gap (opt T → T | null only) and is corrected there, since it is the long-term home of the full mapping.

After the bindgen fixes are released

  • dfinity/icp-js-bindgen#197 (present but falsy optionals): until it ships, 0, false and "" in an optional record field or variant payload are sent as absent. No skill change is needed once it ships. If it is skipped, add a pitfall: send such values only in a plain opt argument, or avoid falsy values in optional fields.

  • dfinity/icp-js-bindgen#198 (opt opt record fields): once released, document f?: T | null with its three states (omitted, null for opt null, a value), pinned to that bindgen version. If it is skipped, the skill must say instead that opt opt record fields do not match their declared type (they decode to Some/None objects, and null cannot be sent), and point to the raw declarations for those fields.

  • dfinity/icp-js-bindgen#201 (actor class name): once released, generating the actor files for a .did file named after one of its own types (governance.did with type Governance, swap.did with type Swap) fails with a hint to rename the file. Where the skill shows vendoring NNS or SNS interfaces, if anywhere, use a distinct file name (e.g. nns_governance.did). The same advice applies if it is skipped, since the class then merges with the type without an error.

  • dfinity/icp-js-bindgen#203 (docs for --actor-interface-file): no skill change. It documents the .d.ts as a reference that leaves out the actor class, which is how caffeine-app (--actor-interface-file) and writing-motoko ("use the generated backend.d.ts as the source of truth") already use it; neither imports it.

  • dfinity/icp-js-bindgen#172 (proposed, no PR yet): if adopted, an inline unit variant becomes a string-literal union instead of a Variant_<tags> or borrowed enum. No skill references Variant_<tags> today, so nothing breaks; the mapping in #406 then gains that row.

Until the bindgen PRs are decided, document only the first section.

主要言語
Python
スター
39
フォーク
19
平均マージ
5日 3時間
マージ済み PR(30日)
19

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dfinity/icskills のほかの issue

dfinity/icskills の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。