Skip to content

Commit

Permalink
Merge pull request #318 from onflow/fxamacker/change-map-get-to-retur…
Browse files Browse the repository at this point in the history
…n-value

Change `OrderedMap.Get()` to return `Value` instead of `Storable`
  • Loading branch information
fxamacker authored Jun 29, 2023
2 parents 8db67b9 + 8702f7d commit f54dc2f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 1 addition & 5 deletions cmd/stress/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,10 @@ func checkMapDataLoss(m *atree.OrderedMap, elements map[atree.Value]atree.Value)

// Check every element
for k, v := range elements {
storable, err := m.Get(compare, hashInputProvider, k)
convertedValue, err := m.Get(compare, hashInputProvider, k)
if err != nil {
return fmt.Errorf("failed to get element with key %s: %w", k, err)
}
convertedValue, err := storable.StoredValue(m.Storage)
if err != nil {
return fmt.Errorf("failed to convert storable to value with key %s: %w", k, err)
}
err = valueEqual(v, convertedValue)
if err != nil {
return fmt.Errorf("failed to compare %s and %s: %w", v, convertedValue, err)
Expand Down
20 changes: 18 additions & 2 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3606,7 +3606,7 @@ func NewMapWithRootID(storage SlabStorage, rootID StorageID, digestBuilder Diges
}

func (m *OrderedMap) Has(comparator ValueComparator, hip HashInputProvider, key Value) (bool, error) {
_, err := m.Get(comparator, hip, key)
_, err := m.get(comparator, hip, key)
if err != nil {
var knf *KeyNotFoundError
if errors.As(err, &knf) {
Expand All @@ -3618,7 +3618,23 @@ func (m *OrderedMap) Has(comparator ValueComparator, hip HashInputProvider, key
return true, nil
}

func (m *OrderedMap) Get(comparator ValueComparator, hip HashInputProvider, key Value) (Storable, error) {
func (m *OrderedMap) Get(comparator ValueComparator, hip HashInputProvider, key Value) (Value, error) {

storable, err := m.get(comparator, hip, key)
if err != nil {
// Don't need to wrap error as external error because err is already categorized by MapSlab.Get().
return nil, err
}

v, err := storable.StoredValue(m.Storage)
if err != nil {
// Wrap err as external error (if needed) because err is returned by Storable interface.
return nil, wrapErrorfAsExternalErrorIfNeeded(err, "failed to get storable's stored value")
}
return v, nil
}

func (m *OrderedMap) get(comparator ValueComparator, hip HashInputProvider, key Value) (Storable, error) {

keyDigest, err := m.digesterBuilder.Digest(hip, key)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ func verifyMap(

// Verify map elements
for k, v := range keyValues {
s, err := m.Get(compare, hashInputProvider, k)
require.NoError(t, err)

e, err := s.StoredValue(m.Storage)
e, err := m.Get(compare, hashInputProvider, k)
require.NoError(t, err)

valueEqual(t, typeInfoComparator, v, e)
Expand Down

0 comments on commit f54dc2f

Please sign in to comment.