Skip to content

feat: add HiveConverter for custom TypeAdapter field conversion - #313

Draft
Rexios80 wants to merge 4 commits into
mainfrom
cursor/hive-type-converters-d829
Draft

feat: add HiveConverter for custom TypeAdapter field conversion#313
Rexios80 wants to merge 4 commits into
mainfrom
cursor/hive-type-converters-d829

Conversation

@Rexios80

@Rexios80 Rexios80 commented Jul 29, 2026

Copy link
Copy Markdown
Member

Summary

Adds a json_serializable-style type converter API so third-party types can be handled without hardcoding them into the generator (see #286).

User API

class UriConverter implements HiveConverter<Uri, String> {
  const UriConverter();

  @override
  Uri fromHive(String hive) => Uri.parse(hive);

  @override
  String toHive(Uri object) => object.toString();
}

@GenerateAdapters(
  [AdapterSpec<Website>()],
  converters: [UriConverter()],
)
class Website {
  final Uri url;
}

Converters are registered only via GenerateAdapters.converters. Matching is by field type (including simple generic converters like IListConverter<T>IList<String>).

Design notes

  • User-facing shape mirrors JsonConverter (fromHive / toHive)
  • Implementation is intentionally small: no field/class annotations, no AdapterSpec.converters
  • Nested collections apply converters to elements on read/write

Test plan

  • GenerateAdapters.converters generation
  • Generic converter type-argument inference
  • Nested collection conversion
  • Nullable fields
  • Example project with UriConverter
  • CI green
Open in Web Open in Cursor 

Add a JsonConverter-style API for transforming field values during
TypeAdapter generation. Converters can be applied via field/class
annotations, AdapterSpec.converters, or GenerateAdapters.converters,
including generic converters with inferred type arguments.

Co-authored-by: Rexios <rexios@rexios.dev>
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.37%. Comparing base (e98c319) to head (f0df1c6).

Files with missing lines Patch % Lines
hive/lib/src/annotations/hive_converter.dart 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #313      +/-   ##
==========================================
- Coverage   93.40%   93.37%   -0.04%     
==========================================
  Files          64       65       +1     
  Lines        2625     2626       +1     
==========================================
  Hits         2452     2452              
- Misses        173      174       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

cursoragent and others added 3 commits July 29, 2026 20:48
Remove AdapterSpec.converters and field/class annotation support.
App-local Hive serialization only needs one converter per type, so
GenerateAdapters.converters is sufficient.

Co-authored-by: Rexios <rexios@rexios.dev>
DateTime is already supported by Hive natively, so Uri is a clearer
demonstration of when a converter is needed.

Co-authored-by: Rexios <rexios@rexios.dev>
Keep the json_serializable-like user API, but drop annotation/revive
edge cases and replace recursive type unification with a smaller
exact + identity-generic matcher.

Co-authored-by: Rexios <rexios@rexios.dev>
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