neo-go/examples/storage/storage.go
Anna Shaleva db8cb752e3 examples: update methods signatures
Update methods signatures in order to generate correct manifest files
with full description of available methods.
2020-07-07 15:57:12 +03:00

82 lines
1.6 KiB
Go

package storagecontract
import (
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
"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)
}
if operation == "get" {
return Get(args)
}
if operation == "delete" {
return Delete(args)
}
if operation == "find" {
return Find(args)
}
return false
}
// 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
}
// 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
}
// Delete deletes the value at passed key.
func Delete(args []interface{}) interface{} {
ctx := storage.GetContext()
key := args[0].([]byte)
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
}
return false
}
func checkArgs(args []interface{}, length int) bool {
if len(args) == length {
return true
}
return false
}