Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler value retrieving from KVStore for primitive types #1499

Closed
mossid opened this issue Jul 2, 2018 · 0 comments
Closed

Simpler value retrieving from KVStore for primitive types #1499

mossid opened this issue Jul 2, 2018 · 0 comments

Comments

@mossid
Copy link
Contributor

mossid commented Jul 2, 2018

For now, if we want to get primitive types (such as int, uint, bool) from KVStores, we have to UnmarshalBinary it manually. But since the primitive types don't need codec to be unmarshaled, we can make it simpler.

We can declare a wrapper type for byte slices:

type StoreValue []byte

KVStores returns StoreValue instead of raw []byte when Get is called on it

type KVStore interface {
    //...
    Get([]byte) StoreValue
    //...
}

It has the following methods:

func (v StoreValue) Int64() int64
func (v StoreValue) Uint64() uint64
func (v StoreValue) Bool() bool
//...

StoreValue can still be treated as byte slice for the other situations, but if the caller want to unmarshal it to one of the primitive types, they can just call the methods.

Futhermore, we can also support amino types when they are being used with MustUnmarshalBinary.

func (v StoreValue) Amino(cdc *wire.Codec, ptr interface{}) 

One problem here is if the value is empty then we have to check it before calling the methods. It will be better if we can handle it in one line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant