Add Format::Truncated — enforce maximum byte size on serialized output#86
Merged
Conversation
b1d2a91 to
4ab1ca3
Compare
Fold maximum-size enforcement into Safe rather than a separate wrapper: - Rename `limit` to `depth_limit` (with a deprecated `limit:` alias) to clarify its purpose alongside the new `size_limit`. - Add `size_limit` (default 16 KiB, `nil` disables). When the fast serialization exceeds it, the record is rebuilt field-by-field, keeping as many top-level fields as fit. - Unify the failure and size diagnostics into a single `truncated` object mapping each degraded field to why: `true` (dropped for size) or its error (value recovered, could not serialize directly). Falls back to `truncated: true` when detail does not fit. - Serialize hash-like records via `to_hash`. Removes the separate Console::Format::Truncated class.
4ab1ca3 to
c3720ac
Compare
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.
Summary
Adds
Console::Format::Truncated, a format wrapper that enforces a maximum byte size on serialized log output.If the serialized record exceeds the limit, top-level fields are replaced with
"[TRUNCATED]"one at a time, largest first, until the record fits within the budget. If truncating every field is still insufficient, a minimal{"truncated":true}fallback is returned.Design
@format.dumpis called exactly once. All truncation is done by direct byte-range surgery on the already-serialized string; no Ruby objects are re-allocated or re-serialized.parse/scan_string/skip_value/skip_whitespace) locates top-level key→value byte ranges. It handles JSON whitespace correctly and is robust against any conformant JSON serializer, not justJSON.dump's compact output.,"truncated":truefragment is injected before the closing}so consumers can detect that the record was modified.Usage
Files
lib/console/format/truncated.rb— implementationlib/console/format.rb— addedrequire_relative "format/truncated"test/console/format/truncated.rb— 7 tests covering pass-through, largest-first truncation, multi-field truncation, skip-if-replacement-is-larger, fallback, and default size behaviour