Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit11dadd8

Browse files
committed
Return resolved offset from Offset on Decoder
1 parent3fb9e74 commit11dadd8

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

‎CHANGELOG.md‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
#Changes
22

3-
##Unreleased
4-
5-
- Fixed`Offset()` method to correctly return the resolved data offset when
6-
positioned at a pointer. Previously,`Offset()` would return the offset after
7-
the data's control bytes rather than where the control bytes begin, which
8-
could cause incorrect cache behavior in custom unmarshalers. Now`Offset()`
9-
properly resolves pointer chains and returns the offset where the actual
10-
data's control bytes start, ensuring that multiple pointers to the same data
11-
return the same offset value.
3+
##2.1.0 - 2025-11-04
4+
5+
- Updated`Offset` method on`Decoder` to return the resolved data offset
6+
when positioned at a pointer. This makes more useful for caching values,
7+
which is its stated purpose.
128

139
##2.0.0 - 2025-10-18
1410

‎internal/decoder/decoder.go‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,36 @@ func (d *Decoder) PeekKind() (Kind, error) {
352352
}
353353

354354
// Offset returns the current offset position in the database.
355-
// This can be used by custom unmarshalers for caching purposes.
355+
// If the current position points to a pointer, this method resolves the
356+
// pointer chain and returns the offset of the actual data. This ensures
357+
// that multiple pointers to the same data return the same offset, which
358+
// is important for caching purposes.
356359
func (d*Decoder)Offset()uint {
357-
returnd.offset
360+
// Follow pointer chain to get resolved data location
361+
dataOffset:=d.offset
362+
for {
363+
kindNum,size,ctrlEndOffset,err:=d.d.decodeCtrlData(dataOffset)
364+
iferr!=nil {
365+
// Return original offset to avoid breaking the public API.
366+
// Offset() returns uint (not (uint, error)), so we can't propagate errors.
367+
// In practice, errors here are rare and the original offset is still valid.
368+
returnd.offset
369+
}
370+
ifkindNum!=KindPointer {
371+
// dataOffset is now pointing at the actual data (not a pointer)
372+
// Return this offset, which is where the data's control bytes start
373+
break
374+
}
375+
// Follow the pointer to get the target offset
376+
dataOffset,_,err=d.d.decodePointer(size,ctrlEndOffset)
377+
iferr!=nil {
378+
// Return original offset to avoid breaking the public API.
379+
// The caller will encounter the same error when they try to read.
380+
returnd.offset
381+
}
382+
// dataOffset is now the pointer target; loop to check if it's also a pointer
383+
}
384+
returndataOffset
358385
}
359386

360387
func (d*Decoder)reset(offsetuint) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp