Skip to content

add: version ラッパー型の任意フィールドに omitempty を付与#47

Open
fujiwara wants to merge 1 commit into
sacloud:mainfrom
fujiwara:improve-version-dto-usability
Open

add: version ラッパー型の任意フィールドに omitempty を付与#47
fujiwara wants to merge 1 commit into
sacloud:mainfrom
fujiwara:improve-version-dto-usability

Conversation

@fujiwara

@fujiwara fujiwara commented May 29, 2026

Copy link
Copy Markdown

概要

apis/version の手書きラッパー型(CreateParams / VersionDetail / ExposedPort / EnvironmentVariable)の任意フィールドに omitempty を付与します。

  • 付与対象: ポインタ・スライスの任意フィールド、および任意の registryPasswordAction(enum)
  • 付与しない: 必須フィールド(cpu / memory / scalingMode / image / targetPort / key
  • 付与しない: bool フィールド(useLetsEncrypt / secret)。false は意味のある値なので常に出力し、「未設定」と区別できるようにします

動機

これらのラッパー型は、SDK 上のアプリケーションで定義ファイルの入出力(読み込み・レンダリング・差分表示など)にそのまま使えると便利です。しかし omitempty が無いと、未設定のフィールドがすべて null/ゼロ値として出力されます。

{
  "cpu": 100,
  "memory": 128,
  "scalingMode": "manual",
  "fixedScale": null,
  "minScale": null,
  "image": "nginx:latest",
  "cmd": null,
  "registryUsername": null,
  "registryPasswordAction": "",
  "exposedPorts": null,
  "envVars": null
}

差分表示などではノイズが大きく、利用側で omitempty 付きのミラー構造体を定義する動機になってしまいます。omitempty を付けると出力が最小限に保たれます。

{
  "cpu": 100,
  "memory": 128,
  "scalingMode": "manual",
  "image": "nginx:latest"
}

なお bool には omitempty を付けていません。false を省略すると「未設定」と区別できず分かりにくいため、常に出力します。

互換性

後方互換です。API リクエストのエンコードは into() 経由で v1(ogen)型を組み立てて行われるため、API へ送信される内容は変わりません。影響するのはラッパー型を直接 json.Marshal したときの出力のみです。null と欠落はどちらもゼロ値にデコードされるため、ラウンドトリップも変わりません。TestJSONTags を新しい挙動に合わせて更新しています。

Optional pointer and slice fields (and the optional registryPasswordAction
enum) in CreateParams, VersionDetail, ExposedPort and EnvironmentVariable now
carry omitempty, so marshaling a wrapper value to JSON omits unset fields
instead of emitting null/zero noise.

bool fields (useLetsEncrypt, secret) are intentionally left without omitempty:
false is a meaningful value, so it is always serialized and can be told apart
from omission. Required fields (cpu, memory, scalingMode, image, targetPort,
key) are unchanged.

This is backward compatible: API requests are encoded through into() and the
v1 (ogen) types, so the payload sent to the API is unaffected; only direct
json.Marshal of the wrapper types is changed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: fujiwara <fujiwara.shunichiro@gmail.com>
@fujiwara fujiwara force-pushed the improve-version-dto-usability branch from 8818020 to 8a241e3 Compare May 29, 2026 12:50

@shyouhei shyouhei left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

異議ありません

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants