Custom codecs#92
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
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
Codecinterface with a defaultJSONCodecimplementation. - Extended
Cacheto hold a codec (defaulting toJSONCodec{}) and updatedCacheStructto use it for encode/decode. - Added a
WithCodeccache 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.
| @@ -178,20 +179,20 @@ func (c *Cache) CacheStruct(ctx context.Context, key string, ttl time.Duration, | |||
| return nil, err | |||
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a pluggable codec interface to the
anycachepackage, allowing users to customize how values are encoded and decoded when usingCacheStruct. The default remains JSON, but this makes it easy to swap in other serialization formats. The most important changes are:Codec abstraction and implementation
Codecinterface incodec.gothat definesEncodeandDecodemethods, along with a defaultJSONCodecimplementation using the standard library's JSON marshaling.Cachestruct to include acodecfield, which is initialized toJSONCodecby default inNew. [1] [2]Pluggable codec support
WithCodec(codec)option to allow users to override the default codec when constructing aCacheinstance. [1] [2]CacheStructto use the configured codec for encoding and decoding values, instead of hardcoding JSON serialization.Code cleanup
encoding/jsonfromanycache.go, since encoding responsibilities are now managed by the codec abstraction.