Skip to content

Add SQL parameter validation to lint-scan#7

Merged
tomoemon merged 2 commits into
mainfrom
feat/lint-scan-params-check
Apr 2, 2026
Merged

Add SQL parameter validation to lint-scan#7
tomoemon merged 2 commits into
mainfrom
feat/lint-scan-params-check

Conversation

@tomoemon

@tomoemon tomoemon commented Apr 2, 2026

Copy link
Copy Markdown
Owner

Summary

  • lint-scan に SQL パラメータ (@param) と Params マップキーの整合性チェックを追加
  • SQL にあるが Params にないパラメータ、Params にあるが SQL にないキーの両方をエラーとして報告
  • Params が変数参照の場合はマップリテラルを要求するエラーを報告(//nolint:spantool で抑制可能)
  • コールバックがない場合でもパラメータチェックは動作
  • README に型互換性チェックは行わない旨を明記

設計の背景

既存の ToStruct チェックと同様に双方向のエラー報告を採用した。ToStruct では struct にあるが SELECT にないフィールドもエラーとして報告しており、Params でも未使用キーを警告ではなくエラーとすることで一貫性を保っている。

Params が変数参照の場合はエラーを報告し、マップリテラルの使用を促す。静的解析不可能なケースでも //nolint:spantool コメントで明示的に抑制できる。

検討した代替案

  • SQL の二重パース回避: extractSQLParamsextractSelectInfo が同じ SQL を個別にパースしているが、lint ツールの実行速度への影響は軽微なため、関心の分離を優先して現状のまま据え置いた
  • extractMapLiteralKeyslint.goextractMapKeys の共通化: 戻り値の型([]string vs []mapKey)やエラー処理方針が異なるため、無理に統合せず個別に実装した

Test plan

  • go test ./... 全テスト通過
  • パラメータ一致(正常系)
  • SQL にあるが Params にないパラメータ(エラー報告)
  • Params にあるが SQL にないキー(エラー報告)
  • Params が変数参照(エラー報告)
  • Params が変数参照 + //nolint:spantool(抑制)
  • Params フィールド未指定 + SQL にパラメータあり(スキップ)
  • パラメータなし + Params なし(正常)
  • 空マップ + SQL にパラメータあり(エラー報告)
  • コールバックなしでもパラメータチェック動作

🤖 Generated with Claude Code

tomoemon and others added 2 commits April 2, 2026 17:04
Validate that SQL parameters (@param) in spanner.Statement match the
keys in the Params map literal. Reports errors for both missing and
unused parameters, consistent with existing ToStruct bidirectional
checking. Also document that type compatibility is not checked.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Instead of silently skipping variable references in Params, report an
error requiring a map literal for static analysis. Support
//nolint:spantool comment to suppress the error.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tomoemon
tomoemon merged commit 3661553 into main Apr 2, 2026
2 checks passed
@tomoemon
tomoemon deleted the feat/lint-scan-params-check branch April 2, 2026 08:10
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.

1 participant