Skip to content

refactor: TypedHandler[T] — typed params for all task handlers#48

Merged
bdchatham merged 3 commits intomainfrom
refactor/typed-handlers
Mar 31, 2026
Merged

refactor: TypedHandler[T] — typed params for all task handlers#48
bdchatham merged 3 commits intomainfrom
refactor/typed-handlers

Conversation

@bdchatham
Copy link
Copy Markdown
Contributor

Summary

Introduces engine.TypedHandler[T] — a generic helper that gives every task handler compile-time typed params without changing the engine, store, server, or client.

All 17 handlers converted. Manual parse functions eliminated. Net -216 lines.

How it works

// Before: fragile type assertions
func (r *SnapshotRestorer) Handler() engine.TaskHandler {
    return func(ctx context.Context, params map[string]any) error {
        bucket, _ := params["bucket"].(string)  // silent zero on mismatch
        ...
    }
}

// After: typed params with JSON struct tags
func (r *SnapshotRestorer) Handler() engine.TaskHandler {
    return engine.TypedHandler(func(ctx context.Context, cfg SnapshotConfig) error {
        if cfg.Bucket == "" { return fmt.Errorf("missing bucket") }
        return r.Restore(ctx, cfg)
    })
}

What's removed

All manual parse functions: parseSnapshotConfig, parseUploadConfig, parseExportConfig, parseAssembleConfig, parseNodeNames, parseArtifactUploadConfig, parseGenesisPeersConfig, parseGenesisS3Config, intentFromParams, extractStringMap, parseSources, parseEC2TagsSource, parseStaticSource, toStringMap, toStringSlice, parseTargetHeight

What's unchanged

Engine, SQLite store, HTTP server, client, OpenAPI spec, serve.go registration.

🤖 Generated with Claude Code

bdchatham and others added 3 commits March 31, 2026 10:25
Introduce engine.TypedHandler[T] — a generic helper that wraps typed
handler functions into TaskHandler. Each handler now receives its own
typed params struct instead of parsing map[string]any.

The TypedHandler bridges map[string]any → json.Marshal → json.Unmarshal
→ typed struct, giving handlers compile-time type safety without
changing the engine, store, server, or client.

All manual parse functions removed:
  parseSnapshotConfig, parseUploadConfig, parseExportConfig,
  parseAssembleConfig, parseNodeNames, parseArtifactUploadConfig,
  parseGenesisPeersConfig, parseGenesisS3Config, intentFromParams,
  extractStringMap, parseSources, parseEC2TagsSource, parseStaticSource,
  toStringMap, toStringSlice, parseTargetHeight

The float64/int64/json.Number type switches in await_condition and
statesync are eliminated — json.Unmarshal into int64 handles it.

20 files changed, net -241 lines.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
… tests

Rename all task param structs from xConfig/xParams to xRequest:
  SnapshotConfig → SnapshotRestoreRequest
  SnapshotUploadConfig → SnapshotUploadRequest
  configPatchParams → ConfigPatchRequest
  ResultExportConfig → ResultExportRequest
  StateSyncParams → StateSyncRequest
  awaitConditionParams → AwaitConditionRequest
  genesisParams → ConfigureGenesisRequest
  genesisPeersConfig → SetGenesisPeersRequest
  identityParams → GenerateIdentityRequest
  gentxParams → GenerateGentxRequest
  assembleConfig → AssembleGenesisRequest
  artifactUploadConfig → UploadArtifactsRequest
  peerDiscoverParams → DiscoverPeersRequest
  configReloadParams → ConfigReloadRequest
  peerSourceConfig → PeerSourceEntry

All structs now exported for external consumers.

Add exhaustive TypedHandler tests:
  - Happy path, malformed JSON, nil params, nested structs, float64→int64
  - Integration tests for all 15 handler deserialization paths

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bdchatham bdchatham marked this pull request as ready for review March 31, 2026 18:52
@bdchatham bdchatham merged commit 4822d57 into main Mar 31, 2026
2 checks passed
@bdchatham bdchatham deleted the refactor/typed-handlers branch March 31, 2026 18:52
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