Skip to content

Custom codecs#92

Merged
ksysoev merged 5 commits into
mainfrom
custom_codecs
Jul 1, 2026
Merged

Custom codecs#92
ksysoev merged 5 commits into
mainfrom
custom_codecs

Conversation

@ksysoev

@ksysoev ksysoev commented Jun 30, 2026

Copy link
Copy Markdown
Owner

This pull request introduces a pluggable codec interface to the anycache package, allowing users to customize how values are encoded and decoded when using CacheStruct. The default remains JSON, but this makes it easy to swap in other serialization formats. The most important changes are:

Codec abstraction and implementation

  • Added a new Codec interface in codec.go that defines Encode and Decode methods, along with a default JSONCodec implementation using the standard library's JSON marshaling.
  • Updated the Cache struct to include a codec field, which is initialized to JSONCodec by default in New. [1] [2]

Pluggable codec support

  • Added a WithCodec(codec) option to allow users to override the default codec when constructing a Cache instance. [1] [2]
  • Refactored CacheStruct to use the configured codec for encoding and decoding values, instead of hardcoding JSON serialization.

Code cleanup

  • Removed the direct import of encoding/json from anycache.go, since encoding responsibilities are now managed by the codec abstraction.

@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.23%. Comparing base (fed8f69) to head (4fa0ea4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #92      +/-   ##
==========================================
+ Coverage   88.22%   89.23%   +1.01%     
==========================================
  Files           9       10       +1     
  Lines         501      511      +10     
==========================================
+ Hits          442      456      +14     
+ Misses         34       32       -2     
+ Partials       25       23       -2     

☔ 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.

@ksysoev ksysoev linked an issue Jun 30, 2026 that may be closed by this pull request
@ksysoev ksysoev requested a review from Copilot June 30, 2026 15:04

Copilot AI left a comment

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.

Pull request overview

Introduces a pluggable codec abstraction for CacheStruct, allowing callers to override the default JSON (encoding/json) serialization/deserialization behavior while keeping JSON as the default.

Changes:

  • Added a Codec interface with a default JSONCodec implementation.
  • Extended Cache to hold a codec (defaulting to JSONCodec{}) and updated CacheStruct to use it for encode/decode.
  • Added a WithCodec cache option and accompanying unit tests, plus README option documentation.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
README.md Documents the new WithCodec cache-level option.
codec.go Adds the Codec interface and the default JSONCodec.
codec_test.go Adds unit tests for JSONCodec encode/decode and error cases.
cache_options.go Adds WithCodec option to configure the cache codec.
cache_options_test.go Adds tests validating WithCodec behavior and nil-guard panic.
anycache.go Wires the codec into Cache default initialization and CacheStruct encode/decode.
anycache_test.go Adds tests asserting CacheStruct uses custom codec and propagates codec errors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread codec.go
Comment thread cache_options.go
Comment thread anycache.go
@@ -178,20 +179,20 @@ func (c *Cache) CacheStruct(ctx context.Context, key string, ttl time.Duration,
return nil, err
ksysoev and others added 2 commits July 1, 2026 08:26
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@ksysoev ksysoev merged commit 060a63c into main Jul 1, 2026
2 checks passed
@ksysoev ksysoev deleted the custom_codecs branch July 1, 2026 01:49
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.

Custom encoder and decoder for complex data structures.

2 participants