Skip to content

Support anonymous struct in ToStruct type resolution#5

Merged
tomoemon merged 3 commits into
mainfrom
feat/anonymous-struct-tostruct
Apr 2, 2026
Merged

Support anonymous struct in ToStruct type resolution#5
tomoemon merged 3 commits into
mainfrom
feat/anonymous-struct-tostruct

Conversation

@tomoemon

@tomoemon tomoemon commented Apr 2, 2026

Copy link
Copy Markdown
Owner

Summary

  • var v struct { ... } のような無名構造体で row.ToStruct(&v) するパターンに対応
  • findVarTypeNamefindVarType にリネームし、戻り値を (string, *ast.StructType) に変更
  • README に無名構造体の使用例を追加

設計の背景

row.ToStruct では名前付き型だけでなく、無名構造体を使うパターンも一般的。従来の findVarTypeName は型名(string)のみ返す設計だったため、型名を持たない無名構造体を扱えなかった。

戻り値を (string, *ast.StructType) に拡張することで、無名構造体の場合は AST の StructType ノードから直接タグを抽出できるようにした。既存の extractTagsFromStructType をそのまま再利用しており、変更は最小限に抑えられている。

Test plan

  • TestScan_ToStructAnonymousStruct: 無名構造体 + row.ToStruct でマッチ
  • TestScan_ToStructAnonymousStructMismatch: 無名構造体 + SELECT カラム不一致でエラー
  • 既存テスト全件パス (go test ./... -count=1)

🤖 Generated with Claude Code

tomoemon and others added 3 commits April 2, 2026 14:41
Rename findVarTypeName to findVarType, returning (string, *ast.StructType)
to handle both named types and anonymous structs. When an anonymous struct
is found, extract spanner tags directly from the AST StructType node.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…lved

When row.ToStruct(&v) is detected but the variable's type cannot be
resolved (e.g. declared outside the callback), show a specific message
instead of the generic "could not detect" error.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tomoemon
tomoemon merged commit e8f3e1a into main Apr 2, 2026
2 checks passed
@tomoemon
tomoemon deleted the feat/anonymous-struct-tostruct branch April 2, 2026 05:50
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