Skip to content

Conversation

@subomi
Copy link
Owner

@subomi subomi commented Jan 18, 2026

Summary

This PR introduces two major enhancements to the requestmigrations library:

  1. Context Propagation - New rm.For(r) pattern that makes request context required by design
  2. Runtime Type Detection - Support for migrations on interface{} fields where concrete types are determined at runtime

Changes

Context Propagation (rm.For(r) Pattern)

  • New Migrator type: A request-scoped handle created via rm.For(r) that holds the HTTP request context and user version
  • Updated API: Marshal/Unmarshal now live on Migrator instead of RequestMigration
  • Updated TypeMigration interface: Now accepts context.Context parameter for cancellation, logging, and tenant-specific logic
  • New helper function: UserVersionFromContext(ctx) to retrieve user version inside migrations

Before:

rm.WithUserVersion(r).Unmarshal(data, &v)  // Request was optional
rm.Marshal(v)  // Could skip migrations silently

After:

migrator, err := rm.For(r)  // Request is required
migrator.Unmarshal(data, &v)
migrator.Marshal(v)

Runtime Type Detection for Dynamic Fields

  • New BuildFromValue method: Inspects runtime types before JSON serialization to detect concrete types in interface{} fields
  • Enables migrations on wrapper types: Works with patterns like PagedResponse{Content: []EndpointResponse{}}
  • Cycle detection: Uses pointer-address tracking to handle recursive structures

Documentation

  • Added docs/context_propagation.md - Design doc for the rm.For(r) pattern
  • Added docs/runtime_type_detection.md - Design doc for dynamic field support
  • Added docs/bulk_registration.md - Design doc for bulk migration registration

Tests

  • Updated all existing tests to use new rm.For(r).Marshal/Unmarshal pattern
  • Added tests for interface{} field migrations
  • Added tests for nested interface slices
  • Added tests for context propagation and UserVersionFromContext()

Breaking Changes

  • TypeMigration interface signature changed: methods now require context.Context as first parameter
  • Marshal/Unmarshal moved from RequestMigration to Migrator
  • WithUserVersion() replaced by For()

Test Plan

  • All existing tests updated and passing
  • New tests for interface field migrations
  • New tests for context propagation
  • Examples updated to demonstrate new API

@subomi subomi merged commit dadbc02 into main Jan 18, 2026
3 checks passed
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