From eecb91a42d071e44f670d91b750261a4391bef12 Mon Sep 17 00:00:00 2001 From: Faye Amacker <33205765+fxamacker@users.noreply.github.com> Date: Wed, 18 Oct 2023 17:58:24 -0500 Subject: [PATCH] Add ContainerStorable.HasPointer() --- array.go | 4 ++-- map.go | 4 ++-- storable.go | 19 +++++++++---------- storable_test.go | 19 +++++++++++++++---- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/array.go b/array.go index 4211128c..0170c7d9 100644 --- a/array.go +++ b/array.go @@ -819,7 +819,7 @@ func (a *ArrayDataSlab) Encode(enc *Encoder) error { return NewEncodingError(err) } - if a.hasPointer() { + if a.HasPointer() { h.setHasPointers() } @@ -1002,7 +1002,7 @@ func (a *ArrayDataSlab) Uninline(storage SlabStorage) error { return nil } -func (a *ArrayDataSlab) hasPointer() bool { +func (a *ArrayDataSlab) HasPointer() bool { for _, e := range a.elements { if hasPointer(e) { return true diff --git a/map.go b/map.go index f053da44..3fb56e88 100644 --- a/map.go +++ b/map.go @@ -2705,7 +2705,7 @@ func (m *MapDataSlab) Encode(enc *Encoder) error { return NewEncodingError(err) } - if m.hasPointer() { + if m.HasPointer() { h.setHasPointers() } @@ -3031,7 +3031,7 @@ func (m *MapDataSlab) canBeEncodedAsCompactMap() ([]Digest, []ComparableStorable return elements.hkeys, keys, values, true } -func (m *MapDataSlab) hasPointer() bool { +func (m *MapDataSlab) HasPointer() bool { return m.elements.hasPointer() } diff --git a/storable.go b/storable.go index dc46cfc0..c1c83620 100644 --- a/storable.go +++ b/storable.go @@ -59,16 +59,12 @@ type ContainerStorable interface { Storable EncodeAsElement(*Encoder, InlinedExtraData) error -} - -type containerStorable interface { - Storable - hasPointer() bool + HasPointer() bool } func hasPointer(storable Storable) bool { - if cs, ok := storable.(containerStorable); ok { - return cs.hasPointer() + if cs, ok := storable.(ContainerStorable); ok { + return cs.HasPointer() } return false } @@ -95,10 +91,9 @@ const ( type SlabIDStorable SlabID -var _ Storable = SlabIDStorable{} -var _ containerStorable = SlabIDStorable{} +var _ ContainerStorable = SlabIDStorable{} -func (v SlabIDStorable) hasPointer() bool { +func (v SlabIDStorable) HasPointer() bool { return true } @@ -155,6 +150,10 @@ func (v SlabIDStorable) Encode(enc *Encoder) error { return nil } +func (v SlabIDStorable) EncodeAsElement(enc *Encoder, _ InlinedExtraData) error { + return v.Encode(enc) +} + func (v SlabIDStorable) ByteSize() uint32 { // tag number (2 bytes) + byte string header (1 byte) + slab id (16 bytes) return 2 + 1 + slabIDSize diff --git a/storable_test.go b/storable_test.go index 12b732f2..539dd9e3 100644 --- a/storable_test.go +++ b/storable_test.go @@ -698,11 +698,11 @@ type SomeStorable struct { Storable Storable } -var _ Storable = SomeStorable{} +var _ ContainerStorable = SomeStorable{} -func (v SomeStorable) hasPointer() bool { - if ms, ok := v.Storable.(containerStorable); ok { - return ms.hasPointer() +func (v SomeStorable) HasPointer() bool { + if ms, ok := v.Storable.(ContainerStorable); ok { + return ms.HasPointer() } return false } @@ -723,6 +723,17 @@ func (v SomeStorable) Encode(enc *Encoder) error { return v.Storable.Encode(enc) } +func (v SomeStorable) EncodeAsElement(enc *Encoder, inlinedExtraData InlinedExtraData) error { + err := enc.CBOR.EncodeRawBytes([]byte{ + // tag number + 0xd8, cborTagSomeValue, + }) + if err != nil { + return err + } + return EncodeStorableAsElement(enc, v.Storable, inlinedExtraData) +} + func (v SomeStorable) ChildStorables() []Storable { return []Storable{v.Storable} }