mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-05-04 09:02:28 +00:00
stackitem: introduce Convertible interface
We have a lot of native contract types that are converted to stack items before serialization, then deserialized as stack items and converted back to regular structures. stackitem.Convertible allows to remove a lot of repetitive io.Serializable code. This also introduces to/from converter in testserdes which unfortunately required to change util tests to avoid circular references.
This commit is contained in:
parent
2d993d0da5
commit
aab18c3083
23 changed files with 223 additions and 339 deletions
|
@ -52,6 +52,12 @@ type Item interface {
|
|||
Convert(Type) (Item, error)
|
||||
}
|
||||
|
||||
// Convertible is something that can be converted to/from Item.
|
||||
type Convertible interface {
|
||||
ToStackItem() (Item, error)
|
||||
FromStackItem(Item) error
|
||||
}
|
||||
|
||||
var (
|
||||
// ErrInvalidConversion is returned on attempt to make an incorrect
|
||||
// conversion between item types.
|
||||
|
|
|
@ -254,3 +254,21 @@ func decodeBinary(r *io.BinReader, allowInvalid bool) Item {
|
|||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// SerializeConvertible serializes Convertible into a slice of bytes.
|
||||
func SerializeConvertible(conv Convertible) ([]byte, error) {
|
||||
item, err := conv.ToStackItem()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return Serialize(item)
|
||||
}
|
||||
|
||||
// DeserializeConvertible deserializes Convertible from a slice of bytes.
|
||||
func DeserializeConvertible(data []byte, conv Convertible) error {
|
||||
item, err := Deserialize(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return conv.FromStackItem(item)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue