mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-22 09:29:38 +00:00
feat: add storage APi's and example
Imported from CityOfZion/neo-storm (86ac5c215a2c6ec710f2fd913e0ace63d6ea993e).
This commit is contained in:
parent
86715511d0
commit
9d983ec77b
3 changed files with 65 additions and 0 deletions
46
examples/storage/storage.go
Normal file
46
examples/storage/storage.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package storage_contract
|
||||
|
||||
import (
|
||||
"github.com/CityOfZion/neo-go-sc/interop/storage"
|
||||
)
|
||||
|
||||
func Main(operation string, args []interface{}) interface{} {
|
||||
var ctx = storage.GetContext()
|
||||
|
||||
// Puts value at key
|
||||
if operation == "put" {
|
||||
if checkArgs(args, 2) {
|
||||
key := args[0].([]byte)
|
||||
value := args[1].([]byte)
|
||||
storage.Put(ctx, key, value)
|
||||
return key
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the value at passed key
|
||||
if operation == "get" {
|
||||
if checkArgs(args, 1) {
|
||||
key := args[0].([]byte)
|
||||
return storage.Get(ctx, key)
|
||||
}
|
||||
}
|
||||
|
||||
// Deletes the value at passed key
|
||||
if operation == "delete" {
|
||||
key := args[0].([]byte)
|
||||
storage.Delete(ctx, key)
|
||||
return true
|
||||
}
|
||||
|
||||
// TODO: storage.Find()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func checkArgs(args []interface{}, length int) bool {
|
||||
if len(args) == length {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
19
interop/storage/storage.go
Normal file
19
interop/storage/storage.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package storage
|
||||
|
||||
// Context represents the storage context
|
||||
type Context interface{}
|
||||
|
||||
// GetContext returns the storage context
|
||||
func GetContext() interface{} { return nil }
|
||||
|
||||
// Put value at given key
|
||||
func Put(ctx interface{}, key interface{}, value interface{}) {}
|
||||
|
||||
// Get value matching given key
|
||||
func Get(ctx interface{}, key interface{}) interface{} { return 0 }
|
||||
|
||||
// Delete key value pair from storage
|
||||
func Delete(ctx interface{}, key interface{}) {}
|
||||
|
||||
// Find values stored on keys partially matching given key
|
||||
func Find(ctx interface{}, key interface{}) interface{} { return 0 }
|
Loading…
Reference in a new issue