Merge pull request #774 from nspcc-dev/fix/jsonint

smartcontract: marshal Integer values to JSON-strings
This commit is contained in:
Roman Khimov 2020-03-18 12:47:35 +03:00 committed by GitHub
commit 23919263e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 8 deletions

View file

@ -181,7 +181,7 @@ func adjustValToType(typ ParamType, val string) (interface{}, error) {
return nil, errors.New("invalid boolean value") return nil, errors.New("invalid boolean value")
} }
case IntegerType: case IntegerType:
return strconv.Atoi(val) return strconv.ParseInt(val, 10, 64)
case Hash160Type: case Hash160Type:
u, err := address.StringToUint160(val) u, err := address.StringToUint160(val)
if err == nil { if err == nil {

View file

@ -185,15 +185,15 @@ func TestAdjustValToType(t *testing.T) {
}, { }, {
typ: IntegerType, typ: IntegerType,
val: "0", val: "0",
out: 0, out: int64(0),
}, { }, {
typ: IntegerType, typ: IntegerType,
val: "42", val: "42",
out: 42, out: int64(42),
}, { }, {
typ: IntegerType, typ: IntegerType,
val: "-42", val: "-42",
out: -42, out: int64(-42),
}, { }, {
typ: IntegerType, typ: IntegerType,
val: "q", val: "q",

View file

@ -63,8 +63,16 @@ func (p *Parameter) MarshalJSON() ([]byte, error) {
resultErr error resultErr error
) )
switch p.Type { switch p.Type {
case BoolType, IntegerType, StringType, Hash256Type, Hash160Type: case BoolType, StringType, Hash256Type, Hash160Type:
resultRawValue, resultErr = json.Marshal(p.Value) resultRawValue, resultErr = json.Marshal(p.Value)
case IntegerType:
val, ok := p.Value.(int64)
if !ok {
resultErr = errors.New("invalid integer value")
break
}
valStr := strconv.FormatInt(val, 10)
resultRawValue = json.RawMessage(`"` + valStr + `"`)
case PublicKeyType, ByteArrayType, SignatureType: case PublicKeyType, ByteArrayType, SignatureType:
if p.Value == nil { if p.Value == nil {
resultRawValue = []byte("null") resultRawValue = []byte("null")

View file

@ -384,13 +384,13 @@ func TestNewParameterFromString(t *testing.T) {
out: Parameter{StringType, "qwerty"}, out: Parameter{StringType, "qwerty"},
}, { }, {
in: "42", in: "42",
out: Parameter{IntegerType, 42}, out: Parameter{IntegerType, int64(42)},
}, { }, {
in: "Hello, 世界", in: "Hello, 世界",
out: Parameter{StringType, "Hello, 世界"}, out: Parameter{StringType, "Hello, 世界"},
}, { }, {
in: `\4\2`, in: `\4\2`,
out: Parameter{IntegerType, 42}, out: Parameter{IntegerType, int64(42)},
}, { }, {
in: `\\4\2`, in: `\\4\2`,
out: Parameter{StringType, `\42`}, out: Parameter{StringType, `\42`},
@ -399,7 +399,7 @@ func TestNewParameterFromString(t *testing.T) {
out: Parameter{StringType, `\42`}, out: Parameter{StringType, `\42`},
}, { }, {
in: "int:42", in: "int:42",
out: Parameter{IntegerType, 42}, out: Parameter{IntegerType, int64(42)},
}, { }, {
in: "true", in: "true",
out: Parameter{BoolType, true}, out: Parameter{BoolType, true},