2018-03-23 20:36:59 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-11-21 14:42:02 +00:00
|
|
|
"encoding/hex"
|
2019-11-21 13:42:51 +00:00
|
|
|
"encoding/json"
|
2018-03-23 20:36:59 +00:00
|
|
|
"fmt"
|
2019-11-21 13:42:51 +00:00
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
"github.com/pkg/errors"
|
2018-03-23 20:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2019-10-22 14:56:03 +00:00
|
|
|
// Param represents a param either passed to
|
2018-03-23 20:36:59 +00:00
|
|
|
// the server or to send to a server using
|
|
|
|
// the client.
|
|
|
|
Param struct {
|
2019-11-21 13:42:51 +00:00
|
|
|
Type paramType
|
|
|
|
Value interface{}
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
2019-11-21 13:42:51 +00:00
|
|
|
|
|
|
|
paramType int
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultT paramType = iota
|
|
|
|
stringT
|
|
|
|
numberT
|
2019-11-21 14:42:02 +00:00
|
|
|
arrayT
|
2018-03-23 20:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (p Param) String() string {
|
2019-11-21 13:42:51 +00:00
|
|
|
return fmt.Sprintf("%v", p.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetString returns string value of the parameter.
|
|
|
|
func (p Param) GetString() string { return p.Value.(string) }
|
|
|
|
|
|
|
|
// GetInt returns int value of te parameter.
|
|
|
|
func (p Param) GetInt() int { return p.Value.(int) }
|
|
|
|
|
|
|
|
// GetUint256 returns Uint256 value of the parameter.
|
|
|
|
func (p Param) GetUint256() (util.Uint256, error) {
|
|
|
|
s, ok := p.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return util.Uint256{}, errors.New("must be a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.Uint256DecodeReverseString(s)
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:42:02 +00:00
|
|
|
// GetBytesHex returns []byte value of the parameter if
|
|
|
|
// it is a hex-encoded string.
|
|
|
|
func (p Param) GetBytesHex() ([]byte, error) {
|
|
|
|
s, ok := p.Value.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("must be a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.DecodeString(s)
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:42:51 +00:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
|
|
|
func (p *Param) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err == nil {
|
|
|
|
p.Type = stringT
|
|
|
|
p.Value = s
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var num float64
|
|
|
|
if err := json.Unmarshal(data, &num); err == nil {
|
|
|
|
p.Type = numberT
|
|
|
|
p.Value = int(num)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:42:02 +00:00
|
|
|
var ps []Param
|
|
|
|
if err := json.Unmarshal(data, &ps); err == nil {
|
|
|
|
p.Type = arrayT
|
|
|
|
p.Value = ps
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:42:51 +00:00
|
|
|
return errors.New("unknown type")
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|