Skip to content

Add context.Context support to all API methods - #9

Merged
utahta merged 5 commits into
mainfrom
add-context
Jul 18, 2026
Merged

utahta merged 5 commits into
mainfrom
add-context

Conversation

@utahta

@utahta utahta commented Jul 18, 2026 •

Copy link
Copy Markdown
Owner

概要

全APIメソッドの第一引数に context.Context を追加し、呼び出し単位でのキャンセル・タイムアウト制御を可能にする。あわせて、E2Eテストで発覚した終値のみ配信指数の null 四本値問題を修正する。

変更内容

context.Context 対応(破壊的変更)

  • 全97サービスメソッドと client.HTTPClient.DoRequest の第一引数に ctx context.Context を追加
  • http.NewRequestWithContext によりキャンセル・deadline を HTTP リクエストまで伝播(ページネーションループの途中でも即座に効く)
  • キャッシュ有効時の singleflight は sf.DoChan + context.WithoutCancel に変更:待機中の呼び出し元が1つキャンセルしても他の待機者は巻き込まれず、進行中のリクエストは完走してキャッシュに保存される。キャンセルした呼び出し元だけが即座に context.Canceled で戻る
  • MockClient もキャンセル済み ctx を尊重
  • 既存コードの移行は各メソッド呼び出しに context.Background() を追加するだけ

終値のみ配信指数の null 四本値対応(破壊的変更)

  • 指数四本値APIは終値のみ配信の指数(0504, 6000, 6095, 6096, B507 等)の O/H/L を null で返すが、非ポインタ float64 のため 0 に化けていた
  • Index.O/H/L を *float64 に変更(C はレコードが存在する以上必ず値があるため float64 のまま)
  • E2Eテストは O/H/L が揃っている場合のみ四本値検証を行うよう修正。GetIndices_HistoricalData は API 仕様(code または date が必須)に合わせて code を指定し、エラーを握りつぶさないよう変更

ドキュメント

  • README のコード例に ctx を追加、実在しないメソッド名を修正
  • README / CLAUDE.md からコードから自明な記述を削除

財務情報の空文字→0化けの修正(4コミット目で追加)

  • v2 の /fins/summary は未設定の数値フィールドを null ではなく空文字で返すが、*types.Float64String 経由では「非nilポインタで値0」にデコードされ、「非開示」と「0円」が区別できなかった
  • RawStatement の数値フィールドを types.Float64StringWithDash(空文字・-・null をすべて nil として扱える)に切替
  • Float64StringWithDash の JSON null が &0 になる欠陥も修正し、回帰テストを追加
  • 実APIの当日データ107行で空文字→nil を確認済み

テスト

  • 単体テスト・golangci-lint: すべてパス
  • singleflight のキャンセル挙動を検証する TestClient_DoRequest_ContextCancellation を追加(-race 付きで確認済み)
  • 終値のみ指数の回帰テスト TestIndicesService_GetIndices_CloseOnlyIndex を追加
  • make test-e2e-v: 実APIに対して全テストパス

utahta added 5 commits July 18, 2026 22:01
- Add ctx as the first parameter to all service methods and
  client.HTTPClient.DoRequest, propagated via http.NewRequestWithContext
- Detach shared singleflight requests from the caller's cancellation
  (DoChan + context.WithoutCancel) so that one caller canceling does not
  fail the flight for other waiters; the completed flight still
  populates the cache while the canceled caller returns immediately
- MockClient honors a canceled context
- README: add ctx to all code examples, fix method names that no longer
  exist (GetInfo -> GetAllListedInfo etc.), document the cache
  cancellation semantics in the cache section
- CLAUDE.md: drop sections derivable from the code (directory layout,
  test pattern details) and keep only guidance that changes behavior
The indices API returns null O/H/L for indices that are distributed
with a closing price only (e.g. codes 0504, 6000, 6095, 6096, B507),
which previously decoded to 0 and broke E2E validation.

- Change Index.O/H/L to *float64 (nil when close-only); C stays float64
  since every published record has a closing price
- Validate OHLC in the E2E test only when present, and require O/H/L to
  be all present or all nil
- GetIndices_HistoricalData now specifies a code since the API requires
  code or date, and fails loudly instead of swallowing errors
The v2 /fins/summary API returns empty strings for unset numeric
fields (e.g. dividend forecasts), not null. Float64String leaves the
value at zero for empty strings, so pointer fields decoded to a
non-nil pointer to 0 -- making "not set" indistinguishable from a
real 0.

- Switch RawStatement numeric fields from *types.Float64String to
  types.Float64StringWithDash, which models absent (""), undetermined
  ("-") and present values correctly
- Guard Float64StringWithDash.UnmarshalJSON against JSON null, which
  previously slipped through the float64 branch and recorded 0
- Add Float64StringWithDash.ToInt64Ptr for share-count fields
- Fix a unit test that asserted the buggy behavior (empty string -> 0)
The output path is intentionally provided by the CLI user, so path
traversal does not apply. G703 is a new rule in recent gosec versions
and started failing CI without any code change.
@utahta
utahta merged commit ab23291 into main Jul 18, 2026
4 checks passed
@utahta
utahta deleted the add-context branch July 18, 2026 13:33
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