Skip to content

feature: Add Weaver[Any] support for Map[String, Any] serialization#368

Merged
xerial merged 3 commits intomainfrom
feature/weaver-any
Jan 25, 2026
Merged

feature: Add Weaver[Any] support for Map[String, Any] serialization#368
xerial merged 3 commits intomainfrom
feature/weaver-any

Conversation

@xerial
Copy link
Copy Markdown
Member

@xerial xerial commented Jan 25, 2026

Summary

  • Add AnyWeaver to support serialization of generic collections like Map[String, Any] and Seq[Any]
  • Support knownWeavers parameter for registering custom type weavers (similar to airframe-codec's knownSurfaces)
  • Runtime type matching for primitives, collections, and Scala 3 enums
  • Falls back to toString for unknown types

Usage Example

case class Person(name: String, age: Int)

val anyWeaver = AnyWeaver(
  knownWeavers = Map(classOf[Person] -> Weaver.derived[Person])
)

val data: Map[String, Any] = Map(
  "user" -> Person("Alice", 30),
  "count" -> 42
)
val msgpack = anyWeaver.weave(data)

Test plan

  • Map[String, Any] with primitives (roundtrip)
  • JSON serialization of Map[String, Any]
  • Nested Map[String, Any] with inner maps and lists
  • Seq[Any] with mixed types
  • null values and Option[Any]
  • Scala 3 enum serialization
  • Array[Any] and Array[Byte] handling
  • knownWeavers with custom case class

🤖 Generated with Claude Code

Add AnyWeaver to support serialization of generic collections like
Map[String, Any] and Seq[Any], similar to airframe-codec's AnyCodec.

Key features:
- Runtime type matching for primitives, collections, and enums
- knownWeavers parameter for registering custom type weavers
- Falls back to toString for unknown types

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions github-actions Bot added doc Improvements or additions to documentation feature New feature labels Jan 25, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @xerial, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the uni-weaver library by introducing an AnyWeaver component, which provides a flexible and powerful mechanism for serializing and deserializing heterogeneous Scala data structures. The primary goal is to enable seamless handling of generic collections like Map[String, Any] and Seq[Any] by performing runtime type matching for common types, supporting custom type serializers through knownWeavers, and ensuring a consistent fallback for unhandled types. This improvement makes the library more adaptable for working with dynamically typed data, mirroring the functionality found in similar serialization frameworks.

Highlights

  • New AnyWeaver Implementation: Introduced a new AnyWeaver component to support serialization and deserialization of Any types, enabling flexible handling of heterogeneous data structures.
  • Generic Collection Support: Added robust support for serializing generic collections such as Map[String, Any] and Seq[Any], which can contain values of various types.
  • Custom Type Registration: Implemented a knownWeavers parameter, allowing users to register custom Weaver instances for specific types, ensuring proper serialization of domain-specific objects.
  • Runtime Type Matching and Fallback: The AnyWeaver performs runtime type matching for primitives, collections, and Scala 3 enums, with a graceful fallback to toString serialization for unknown types.
  • Comprehensive Test Coverage: A new test suite (AnyWeaverTest) has been added, covering a wide range of scenarios including nested collections, null handling, enum serialization, and custom type integration.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces AnyWeaver for serializing generic collections like Map[String, Any], which is a great addition. The implementation is comprehensive, covering primitives, collections, and custom types via knownWeavers. The test suite is also thorough and covers many edge cases.

I've found one high-severity bug in the error handling logic within AnyWeaverImpl's unpackMap method, which could lead to errors when unpacking malformed map data. I've also left a minor comment on the design document to improve its reusability for other developers.

Overall, this is a solid feature implementation. Once the identified issue is addressed, it should be ready for merging.

Comment on lines +156 to +159
while i < len do
u.skipValue
u.skipValue
i += 1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There's a bug in the error handling logic when unpacking a map key. If unpack(u, keyContext) fails, the unpacker's position will be at the corresponding value of the failed key. The current loop while i < len do ... attempts to skip len - i full key-value pairs. This is one value too many and will likely cause an error by reading past the end of the map.

The logic should be to first skip the value of the current failed pair, and then skip the remaining full pairs.

          u.skipValue // Skip value of the current pair
          i += 1
          while i < len do
            u.skipValue // key
            u.skipValue // value
            i += 1

Comment thread plans/2025-01-25-weaver-any.md Outdated
## Verification

```bash
cd /Users/leo/work/uni/.worktree/feature-weaver-any
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The cd command uses a hardcoded absolute path. This is specific to your machine and won't work for other contributors. Please remove this line. The following commands are expected to be run from the project root.

xerial and others added 2 commits January 25, 2026 08:58
When key unpacking fails, skip only the current value (key already consumed)
before skipping remaining pairs. This prevents misalignment of the unpacker.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@xerial xerial merged commit 8c5a21b into main Jan 25, 2026
13 checks passed
@xerial xerial deleted the feature/weaver-any branch January 25, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc Improvements or additions to documentation feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant