examples: update examples

Closes #1234
This commit is contained in:
Anna Shaleva 2020-08-10 13:42:02 +03:00
parent d8db85ef55
commit ae3f15523c
7 changed files with 165 additions and 260 deletions

View file

@ -5,78 +5,39 @@ import (
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
// Main is a very useful function.
func Main(operation string, args []interface{}) interface{} {
if operation == "put" {
return Put(args)
}
// ctx holds storage context for contract methods
var ctx storage.Context
if operation == "get" {
return Get(args)
}
if operation == "delete" {
return Delete(args)
}
if operation == "find" {
return Find(args)
}
return false
// init inits storage context before any other contract method is called
func init() {
ctx = storage.GetContext()
}
// Put puts value at key.
func Put(args []interface{}) interface{} {
ctx := storage.GetContext()
if checkArgs(args, 2) {
key := args[0].([]byte)
value := args[1].([]byte)
storage.Put(ctx, key, value)
return key
}
return false
func Put(key, value []byte) []byte {
storage.Put(ctx, key, value)
return key
}
// Get returns the value at passed key.
func Get(args []interface{}) interface{} {
ctx := storage.GetContext()
if checkArgs(args, 1) {
key := args[0].([]byte)
return storage.Get(ctx, key)
}
return false
func Get(key []byte) interface{} {
return storage.Get(ctx, key)
}
// Delete deletes the value at passed key.
func Delete(args []interface{}) interface{} {
ctx := storage.GetContext()
key := args[0].([]byte)
func Delete(key []byte) bool {
storage.Delete(ctx, key)
return true
}
// Find returns an array of key-value pairs with key that matched the passed value.
func Find(args []interface{}) interface{} {
ctx := storage.GetContext()
if checkArgs(args, 1) {
value := args[0].([]byte)
iter := storage.Find(ctx, value)
result := []string{}
for iterator.Next(iter) {
val := iterator.Value(iter)
key := iterator.Key(iter)
result = append(result, key.(string)+":"+val.(string))
}
return result
// Find returns an array of key-value pairs with key that matched the passed value
func Find(value []byte) []string {
iter := storage.Find(ctx, value)
result := []string{}
for iterator.Next(iter) {
val := iterator.Value(iter)
key := iterator.Key(iter)
result = append(result, key.(string)+":"+val.(string))
}
return false
}
func checkArgs(args []interface{}, length int) bool {
if len(args) == length {
return true
}
return false
return result
}