Skip to content

Commit

Permalink
Do not return error from Hash.Get
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik committed Aug 10, 2024
1 parent aacffeb commit 558d3a5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
7 changes: 2 additions & 5 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,7 @@ func (d *decoder) decodeMap(name string, v Value, result reflect.Value) error {
keys := hash.Keys()

for i, rbKey := range keys {
rbVal, err := hash.Get(rbKey)
if err != nil {
return err
}
rbVal := hash.Get(rbKey)

// Make the field name
fieldName := fmt.Sprintf("%s.<entry %d>", name, i)
Expand Down Expand Up @@ -476,7 +473,7 @@ func (d *decoder) decodeStruct(name string, v Value, result reflect.Value) error
func decodeStructHashGetter(grb *GRuby, h Hash) decodeStructGetter {
return func(key string) (Value, error) {
rbKey := ToRuby(grb, key)
return h.Get(rbKey)
return h.Get(rbKey), nil
}
}

Expand Down
19 changes: 10 additions & 9 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ type Hash struct {
// Delete deletes a key from the hash, returning its existing value,
// or nil if there wasn't a value.
func (h *Hash) Delete(key Value) Value {
keyVal := key.CValue()
result := C.mrb_hash_delete_key(h.GRuby().state, h.CValue(), keyVal)
result := C.mrb_hash_delete_key(h.GRuby().state, h.CValue(), key.CValue())

val := h.GRuby().value(result)
if val.Type() == TypeNil {
Expand All @@ -25,17 +24,19 @@ func (h *Hash) Delete(key Value) Value {
}

// Get reads a value from the hash.
func (h *Hash) Get(key Value) (Value, error) {
keyVal := key.CValue()
result := C.mrb_hash_get(h.GRuby().state, h.CValue(), keyVal)
return h.GRuby().value(result), nil
func (h *Hash) Get(key Value) Value {
result := C.mrb_hash_get(h.GRuby().state, h.CValue(), key.CValue())

val := h.GRuby().value(result)
if val.Type() == TypeNil {
return nil
}
return h.GRuby().value(result)
}

// Set sets a value on the hash
func (h *Hash) Set(key, val Value) {
keyVal := key.CValue()
valVal := val.CValue()
C.mrb_hash_set(h.GRuby().state, h.CValue(), keyVal, valVal)
C.mrb_hash_set(h.GRuby().state, h.CValue(), key.CValue(), val.CValue())
}

// Keys returns the array of keys that the Hash has as Values.
Expand Down
9 changes: 3 additions & 6 deletions hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,17 @@ func TestHash(t *testing.T) {
hash := gruby.ToGo[gruby.Hash](value)

// Get
value, err = hash.Get(gruby.ToRuby(grb, "foo"))
g.Expect(err).ToNot(HaveOccurred())
value = hash.Get(gruby.ToRuby(grb, "foo"))
g.Expect(value.String()).To(Equal("bar"))

// Get false type
value, err = hash.Get(gruby.ToRuby(grb, "baz"))
g.Expect(err).ToNot(HaveOccurred())
value = hash.Get(gruby.ToRuby(grb, "baz"))
g.Expect(value.Type()).To(Equal(gruby.TypeFalse))
g.Expect(value.String()).To(Equal("false"))

// Set
hash.Set(gruby.ToRuby(grb, "foo"), gruby.ToRuby(grb, "baz"))
value, err = hash.Get(gruby.ToRuby(grb, "foo"))
g.Expect(err).ToNot(HaveOccurred())
value = hash.Get(gruby.ToRuby(grb, "foo"))
g.Expect(value.String()).To(Equal("baz"))

// Keys
Expand Down
5 changes: 1 addition & 4 deletions typecast.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ func ToGoMap[K supportedComparables, V SupportedTypes](hash Hash) map[K]V {

for _, rbKey := range keys {
key := ToGo[K](rbKey)
val, err := hash.Get(rbKey)
if err != nil {
panic(err)
}
val := hash.Get(rbKey)
result[key] = ToGo[V](val)
}

Expand Down

0 comments on commit 558d3a5

Please sign in to comment.