stackitem: use Bool item directly

It is always copied.

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-08-03 19:37:06 +03:00
parent 5aff82aef4
commit cff8b1c24e
6 changed files with 40 additions and 44 deletions

View file

@ -17,7 +17,7 @@ func ParameterFromStackItem(i stackitem.Item, seen map[stackitem.Item]bool) Para
Type: IntegerType, Type: IntegerType,
Value: i.Value().(*big.Int).Int64(), Value: i.Value().(*big.Int).Int64(),
} }
case *stackitem.Bool: case stackitem.Bool:
return Parameter{ return Parameter{
Type: BoolType, Type: BoolType,
Value: i.Value().(bool), Value: i.Value().(bool),

View file

@ -195,7 +195,7 @@ func compareItems(t *testing.T, a, b stackitem.Item) {
require.Equal(t, val, ac.Value().(*big.Int).Int64()) require.Equal(t, val, ac.Value().(*big.Int).Int64())
case *stackitem.ByteArray: case *stackitem.ByteArray:
require.Equal(t, val, bigint.FromBytes(ac.Value().([]byte)).Int64()) require.Equal(t, val, bigint.FromBytes(ac.Value().([]byte)).Int64())
case *stackitem.Bool: case stackitem.Bool:
if ac.Value().(bool) { if ac.Value().(bool) {
require.Equal(t, val, int64(1)) require.Equal(t, val, int64(1))
} else { } else {

View file

@ -126,9 +126,7 @@ func Make(v interface{}) Item {
value: []byte(val), value: []byte(val),
} }
case bool: case bool:
return &Bool{ return Bool(val)
value: val,
}
case []Item: case []Item:
return &Array{ return &Array{
value: val, value: val,
@ -485,76 +483,72 @@ func (i *BigInteger) MarshalJSON() ([]byte, error) {
} }
// Bool represents a boolean Item. // Bool represents a boolean Item.
type Bool struct { type Bool bool
value bool
}
// NewBool returns an new Bool object. // NewBool returns an new Bool object.
func NewBool(val bool) *Bool { func NewBool(val bool) Bool {
return &Bool{ return Bool(val)
value: val,
}
} }
// Value implements Item interface. // Value implements Item interface.
func (i *Bool) Value() interface{} { func (i Bool) Value() interface{} {
return i.value return bool(i)
} }
// MarshalJSON implements the json.Marshaler interface. // MarshalJSON implements the json.Marshaler interface.
func (i *Bool) MarshalJSON() ([]byte, error) { func (i Bool) MarshalJSON() ([]byte, error) {
return json.Marshal(i.value) return json.Marshal(bool(i))
} }
func (i *Bool) String() string { func (i Bool) String() string {
return "Boolean" return "Boolean"
} }
// Dup implements Item interface. // Dup implements Item interface.
func (i *Bool) Dup() Item { func (i Bool) Dup() Item {
return &Bool{i.value} return i
} }
// TryBool implements Item interface. // TryBool implements Item interface.
func (i *Bool) TryBool() (bool, error) { return i.value, nil } func (i Bool) TryBool() (bool, error) { return bool(i), nil }
// Bytes converts Bool to bytes. // Bytes converts Bool to bytes.
func (i *Bool) Bytes() []byte { func (i Bool) Bytes() []byte {
if i.value { if i {
return []byte{1} return []byte{1}
} }
return []byte{0} return []byte{0}
} }
// TryBytes implements Item interface. // TryBytes implements Item interface.
func (i *Bool) TryBytes() ([]byte, error) { func (i Bool) TryBytes() ([]byte, error) {
return i.Bytes(), nil return i.Bytes(), nil
} }
// TryInteger implements Item interface. // TryInteger implements Item interface.
func (i *Bool) TryInteger() (*big.Int, error) { func (i Bool) TryInteger() (*big.Int, error) {
if i.value { if i {
return big.NewInt(1), nil return big.NewInt(1), nil
} }
return big.NewInt(0), nil return big.NewInt(0), nil
} }
// Equals implements Item interface. // Equals implements Item interface.
func (i *Bool) Equals(s Item) bool { func (i Bool) Equals(s Item) bool {
if i == s { if i == s {
return true return true
} else if s == nil { } else if s == nil {
return false return false
} }
val, ok := s.(*Bool) val, ok := s.(Bool)
return ok && i.value == val.value return ok && i == val
} }
// Type implements Item interface. // Type implements Item interface.
func (i *Bool) Type() Type { return BooleanT } func (i Bool) Type() Type { return BooleanT }
// Convert implements Item interface. // Convert implements Item interface.
func (i *Bool) Convert(typ Type) (Item, error) { func (i Bool) Convert(typ Type) (Item, error) {
return convertPrimitive(i, typ) return convertPrimitive(i, typ)
} }
@ -861,7 +855,7 @@ func (i *Map) Drop(index int) {
// key. // key.
func IsValidMapKey(key Item) error { func IsValidMapKey(key Item) error {
switch key.(type) { switch key.(type) {
case *Bool, *BigInteger: case Bool, *BigInteger:
return nil return nil
case *ByteArray: case *ByteArray:
size := len(key.Value().([]byte)) size := len(key.Value().([]byte))
@ -1171,8 +1165,8 @@ func deepCopy(item Item, seen map[Item]Item) Item {
return NewByteArray(slice.Copy(it.value)) return NewByteArray(slice.Copy(it.value))
case *Buffer: case *Buffer:
return NewBuffer(slice.Copy(it.value)) return NewBuffer(slice.Copy(it.value))
case *Bool: case Bool:
return NewBool(it.value) return it
case *Pointer: case *Pointer:
return NewPointerWithHash(it.pos, it.script, it.hash) return NewPointerWithHash(it.pos, it.script, it.hash)
case *Interop: case *Interop:

View file

@ -63,11 +63,11 @@ var makeStackItemTestCases = []struct {
}, },
{ {
input: true, input: true,
result: &Bool{value: true}, result: Bool(true),
}, },
{ {
input: false, input: false,
result: &Bool{value: false}, result: Bool(false),
}, },
{ {
input: []Item{&BigInteger{value: big.NewInt(3)}, &ByteArray{value: []byte{1, 2, 3}}}, input: []Item{&BigInteger{value: big.NewInt(3)}, &ByteArray{value: []byte{1, 2, 3}}},
@ -459,8 +459,8 @@ func TestMarshalJSON(t *testing.T) {
switch testCase.input.(type) { switch testCase.input.(type) {
case *BigInteger: case *BigInteger:
actual, err = testCase.input.(*BigInteger).MarshalJSON() actual, err = testCase.input.(*BigInteger).MarshalJSON()
case *Bool: case Bool:
actual, err = testCase.input.(*Bool).MarshalJSON() actual, err = testCase.input.(Bool).MarshalJSON()
case *ByteArray: case *ByteArray:
actual, err = testCase.input.(*ByteArray).MarshalJSON() actual, err = testCase.input.(*ByteArray).MarshalJSON()
case *Array: case *Array:
@ -532,7 +532,9 @@ func TestDeepCopy(t *testing.T) {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
actual := DeepCopy(tc.item) actual := DeepCopy(tc.item)
require.Equal(t, tc.item, actual) require.Equal(t, tc.item, actual)
require.False(t, actual == tc.item) if tc.item.Type() != BooleanT {
require.False(t, actual == tc.item)
}
}) })
} }

View file

@ -120,8 +120,8 @@ func toJSON(data []byte, seen map[Item]sliceNoPointer, item Item) ([]byte, error
return nil, err return nil, err
} }
data = append(data, raw...) data = append(data, raw...)
case *Bool: case Bool:
if it.value { if it {
data = append(data, "true"...) data = append(data, "true"...)
} else { } else {
data = append(data, "false"...) data = append(data, "false"...)
@ -288,8 +288,8 @@ func toJSONWithTypes(item Item, seen map[Item]bool) (interface{}, error) {
} }
value = arr value = arr
delete(seen, item) delete(seen, item)
case *Bool: case Bool:
value = it.value value = bool(it)
case *Buffer, *ByteArray: case *Buffer, *ByteArray:
value = base64.StdEncoding.EncodeToString(it.Value().([]byte)) value = base64.StdEncoding.EncodeToString(it.Value().([]byte))
case *BigInteger: case *BigInteger:

View file

@ -103,9 +103,9 @@ func (w *serContext) serialize(item Item) error {
data := t.Value().([]byte) data := t.Value().([]byte)
w.appendVarUint(uint64(len(data))) w.appendVarUint(uint64(len(data)))
w.data = append(w.data, data...) w.data = append(w.data, data...)
case *Bool: case Bool:
w.data = append(w.data, byte(BooleanT)) w.data = append(w.data, byte(BooleanT))
if t.Value().(bool) { if t {
w.data = append(w.data, 1) w.data = append(w.data, 1)
} else { } else {
w.data = append(w.data, 0) w.data = append(w.data, 0)