Skip to content

Commit

Permalink
wire/value: Combine numbers into a single field
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav committed Jun 1, 2016
1 parent 18a02f6 commit bcfbbfd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
80 changes: 41 additions & 39 deletions wire/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package wire

import (
"fmt"
"math"
"strings"
)

Expand All @@ -31,12 +32,7 @@ import (
type Value struct {
typ Type

tbool bool
tdouble float64
ti8 int8
ti16 int16
ti32 int32
ti64 int64
tnumber uint64
tbinary []byte
tstruct Struct
tcoll interface{} // set/map/list
Expand All @@ -51,104 +47,110 @@ func (v *Value) Type() Type {
func (v *Value) Get() interface{} {
switch v.typ {
case TBool:
return v.tbool
return v.GetBool()
case TI8:
return v.ti8
return v.GetI8()
case TDouble:
return v.tdouble
return v.GetDouble()
case TI16:
return v.ti16
return v.GetI16()
case TI32:
return v.ti32
return v.GetI32()
case TI64:
return v.ti64
return v.GetI64()
case TBinary:
return v.tbinary
return v.GetBinary()
case TStruct:
return v.tstruct
case TMap, TSet, TList:
return v.tcoll
return v.GetStruct()
case TMap:
return v.GetMap()
case TSet, TList:
return v.GetList()
default:
panic(fmt.Sprintf("Unknown value type %v", v.typ))
}
}

// NewValueBool constructs a new Value that contains a boolean.
func NewValueBool(v bool) Value {
var n uint64 = 0
if v {
n = 1
}
return Value{
typ: TBool,
tbool: v,
typ: TBool,
tnumber: n,
}
}

// GetBool gets the Bool value from a Value.
func (v *Value) GetBool() bool {
return v.tbool
return v.tnumber != 0
}

// NewValueI8 constructs a new Value that contains a byte
func NewValueI8(v int8) Value {
return Value{
typ: TI8,
ti8: v,
typ: TI8,
tnumber: uint64(v),
}
}

// GetI8 gets the I8 value from a Value.
func (v *Value) GetI8() int8 {
return v.ti8
return int8(v.tnumber)
}

// NewValueDouble constructs a new Value that contains a double.
func NewValueDouble(v float64) Value {
return Value{
typ: TDouble,
tdouble: v,
tnumber: math.Float64bits(v),
}
}

// GetDouble gets the Double value from a Value.
func (v *Value) GetDouble() float64 {
return v.tdouble
return math.Float64frombits(v.tnumber)
}

// NewValueI16 constructs a new Value that contains a 16-bit integer.
func NewValueI16(v int16) Value {
return Value{
typ: TI16,
ti16: v,
typ: TI16,
tnumber: uint64(v),
}
}

// GetI16 gets the I16 value from a Value.
func (v *Value) GetI16() int16 {
return v.ti16
return int16(v.tnumber)
}

// NewValueI32 constructs a new Value that contains a 32-bit integer.
func NewValueI32(v int32) Value {
return Value{
typ: TI32,
ti32: v,
typ: TI32,
tnumber: uint64(v),
}
}

// GetI32 gets the I32 value from a Value.
func (v *Value) GetI32() int32 {
return v.ti32
return int32(v.tnumber)
}

// NewValueI64 constructs a new Value that contains a 64-bit integer.
func NewValueI64(v int64) Value {
return Value{
typ: TI64,
ti64: v,
typ: TI64,
tnumber: uint64(v),
}
}

// GetI64 gets the I64 value from a Value.
func (v *Value) GetI64() int64 {
return v.ti64
return int64(v.tnumber)
}

// NewValueBinary constructs a new Value that contains a binary string.
Expand Down Expand Up @@ -232,17 +234,17 @@ func (v *Value) GetList() ValueList {
func (v Value) String() string {
switch v.typ {
case TBool:
return fmt.Sprintf("TBool(%v)", v.tbool)
return fmt.Sprintf("TBool(%v)", v.GetBool())
case TI8:
return fmt.Sprintf("TI8(%v)", v.ti8)
return fmt.Sprintf("TI8(%v)", v.GetI8())
case TDouble:
return fmt.Sprintf("TDouble(%v)", v.tdouble)
return fmt.Sprintf("TDouble(%v)", v.GetDouble())
case TI16:
return fmt.Sprintf("TI16(%v)", v.ti16)
return fmt.Sprintf("TI16(%v)", v.GetI16())
case TI32:
return fmt.Sprintf("TI32(%v)", v.ti32)
return fmt.Sprintf("TI32(%v)", v.GetI32())
case TI64:
return fmt.Sprintf("TI64(%v)", v.ti64)
return fmt.Sprintf("TI64(%v)", v.GetI64())
case TBinary:
return fmt.Sprintf("TBinary(%v)", v.tbinary)
case TStruct:
Expand Down
12 changes: 6 additions & 6 deletions wire/value_equals.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ func ValuesAreEqual(left, right Value) bool {

switch left.typ {
case TBool:
return left.tbool == right.tbool
return left.GetBool() == right.GetBool()
case TI8:
return left.ti8 == right.ti8
return left.GetI8() == right.GetI8()
case TDouble:
return left.tdouble == right.tdouble
return left.GetDouble() == right.GetDouble()
case TI16:
return left.ti16 == right.ti16
return left.GetI16() == right.GetI16()
case TI32:
return left.ti32 == right.ti32
return left.GetI32() == right.GetI32()
case TI64:
return left.ti64 == right.ti64
return left.GetI64() == right.GetI64()
case TBinary:
return bytes.Equal(left.tbinary, right.tbinary)
case TStruct:
Expand Down

0 comments on commit bcfbbfd

Please sign in to comment.