Simplify variables

This commit is contained in:
Tobie Morgan Hitchcock 2016-10-14 07:14:05 +01:00
parent dd79f3c440
commit fdb639e517
4 changed files with 129 additions and 130 deletions

View file

@ -90,99 +90,99 @@ func (d *decoder) Decode(items ...interface{}) {
*value = d.r.FindNumberFloat64() *value = d.r.FindNumberFloat64()
case *[]time.Time: case *[]time.Time:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindTime()) *value = append(*value, d.r.FindTime())
} }
} }
case *[]bool: case *[]bool:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindBool()) *value = append(*value, d.r.FindBool())
} }
} }
case *[]string: case *[]string:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindString()) *value = append(*value, d.r.FindString())
} }
} }
case *[]int: case *[]int:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberInt()) *value = append(*value, d.r.FindNumberInt())
} }
} }
case *[]int8: case *[]int8:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberInt8()) *value = append(*value, d.r.FindNumberInt8())
} }
} }
case *[]int16: case *[]int16:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberInt16()) *value = append(*value, d.r.FindNumberInt16())
} }
} }
case *[]int32: case *[]int32:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberInt32()) *value = append(*value, d.r.FindNumberInt32())
} }
} }
case *[]int64: case *[]int64:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberInt64()) *value = append(*value, d.r.FindNumberInt64())
} }
} }
case *[]uint: case *[]uint:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberUint()) *value = append(*value, d.r.FindNumberUint())
} }
} }
case *[]uint16: case *[]uint16:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberUint16()) *value = append(*value, d.r.FindNumberUint16())
} }
} }
case *[]uint32: case *[]uint32:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberUint32()) *value = append(*value, d.r.FindNumberUint32())
} }
} }
case *[]uint64: case *[]uint64:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberUint64()) *value = append(*value, d.r.FindNumberUint64())
} }
} }
case *[]float32: case *[]float32:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberFloat32()) *value = append(*value, d.r.FindNumberFloat32())
} }
} }
case *[]float64: case *[]float64:
if d.r.ReadNext(cARRAY) { if d.r.ReadNext(cARR) {
for !d.r.ReadNext(cTERM) { for !d.r.ReadNext(cEND) {
*value = append(*value, d.r.FindNumberFloat64()) *value = append(*value, d.r.FindNumberFloat64())
} }
} }
@ -195,17 +195,17 @@ func (d *decoder) Decode(items ...interface{}) {
switch fnd := d.r.FindNext(); fnd { switch fnd := d.r.FindNext(); fnd {
default: default:
*value = d.r.FindAny() *value = d.r.FindAny()
case cNILL: case cNIL:
*value = d.r.FindNull() *value = d.r.FindNull()
case cBOOL: case cVAL:
*value = d.r.FindBool() *value = d.r.FindBool()
case cTIME: case cTME:
*value = d.r.FindTime() *value = d.r.FindTime()
case cNUMBER: case cNEG, cPOS:
*value = d.r.FindNumber() *value = d.r.FindNumber()
case cSTRING, cPREFIX, cSUFFIX: case cSTR, cPRE, cSUF:
*value = d.r.FindString() *value = d.r.FindString()
case cARRAY: case cARR:
*value = d.r.FindArray() *value = d.r.FindArray()
} }

View file

@ -45,29 +45,29 @@ func (e *encoder) Encode(items ...interface{}) {
case nil: case nil:
e.w.Write(bNILL) e.w.Write(bNIL)
e.w.Write(bTERM) e.w.Write(bEND)
case bool: case bool:
e.w.Write(bBOOL) e.w.Write(bVAL)
if value { if value {
e.w.Write(bBOOL) e.w.Write(bVAL)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case time.Time: case time.Time:
e.w.Write(bTIME) e.w.Write(bTME)
e.w.Write(value) e.w.Write(value)
e.w.Write(bTERM) e.w.Write(bEND)
case []byte: case []byte:
e.w.Write(bSTRING) e.w.Write(bSTR)
e.w.Write(value) e.w.Write(value)
e.w.Write(bTERM) e.w.Write(bEND)
e.w.Write(bTERM) e.w.Write(bEND)
case string: case string:
@ -76,159 +76,156 @@ func (e *encoder) Encode(items ...interface{}) {
} }
if value == Prefix { if value == Prefix {
e.w.Write(bPREFIX) e.w.Write(bPRE)
e.w.Write(bTERM) e.w.Write(bEND)
break break
} }
if value == Suffix { if value == Suffix {
e.w.Write(bSUFFIX) e.w.Write(bSUF)
e.w.Write(bTERM) e.w.Write(bEND)
break break
} }
e.w.Write(bSTRING) e.w.Write(bSTR)
e.w.Write(value) e.w.Write(value)
e.w.Write(bTERM) e.w.Write(bEND)
e.w.Write(bTERM) e.w.Write(bEND)
case float32, float64: case float32, float64:
e.w.Write(bNUMBER)
e.w.Write(value) e.w.Write(value)
e.w.Write(bTERM) e.w.Write(bEND)
case int, int8, int16, int32, int64: case int, int8, int16, int32, int64:
e.w.Write(bNUMBER)
e.w.Write(value) e.w.Write(value)
e.w.Write(bTERM) e.w.Write(bEND)
case uint, uint8, uint16, uint32, uint64: case uint, uint8, uint16, uint32, uint64:
e.w.Write(bNUMBER)
e.w.Write(value) e.w.Write(value)
e.w.Write(bTERM) e.w.Write(bEND)
case []time.Time: case []time.Time:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []bool: case []bool:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []string: case []string:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []int: case []int:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []int8: case []int8:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []int16: case []int16:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []int32: case []int32:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []int64: case []int64:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []uint: case []uint:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []uint16: case []uint16:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []uint32: case []uint32:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []uint64: case []uint64:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []float32: case []float32:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []float64: case []float64:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
case []interface{}: case []interface{}:
e.w.Write(bARRAY) e.w.Write(bARR)
for _, val := range value { for _, val := range value {
e.Encode(val) e.Encode(val)
} }
e.w.Write(bTERM) e.w.Write(bEND)
} }

View file

@ -39,27 +39,29 @@ var (
) )
var ( var (
cTERM = uint8(0x00) cEND = byte(0x00)
cPREFIX = uint8(0x01) cPRE = byte(0x01)
cNILL = uint8(0x02) cNIL = byte(0x02)
cBOOL = uint8(0x03) cVAL = byte(0x03)
cTIME = uint8(0x04) cTME = byte(0x04)
cNUMBER = uint8(0x05) cNEG = byte(0x05)
cSTRING = uint8(0x06) cPOS = byte(0x06)
cARRAY = uint8(0x07) cSTR = byte(0x07)
cSUFFIX = uint8(0x08) cARR = byte(0x08)
cSUF = byte(0x09)
) )
var ( var (
bTERM = []byte("\x00") bEND = []byte{cEND}
bPREFIX = []byte("\x01") bPRE = []byte{cPRE}
bNILL = []byte("\x02") bNIL = []byte{cNIL}
bBOOL = []byte("\x03") bVAL = []byte{cVAL}
bTIME = []byte("\x04") bTME = []byte{cTME}
bNUMBER = []byte("\x05") bNEG = []byte{cNEG}
bSTRING = []byte("\x06") bPOS = []byte{cPOS}
bARRAY = []byte("\x07") bSTR = []byte{cSTR}
bSUFFIX = []byte("\x08") bARR = []byte{cARR}
bSUF = []byte{cSUF}
) )
const ( const (

View file

@ -74,50 +74,50 @@ func (r *reader) FindNext() (byt byte) {
} }
func (r *reader) FindAny() (val interface{}) { func (r *reader) FindAny() (val interface{}) {
return r.ReadUpto(cTERM) return r.ReadUpto(cEND)
} }
func (r *reader) FindNull() (val interface{}) { func (r *reader) FindNull() (val interface{}) {
if r.ReadNext(cNILL) { if r.ReadNext(cNIL) {
r.ReadNext(cTERM) r.ReadNext(cEND)
} }
return return
} }
func (r *reader) FindTime() (val time.Time) { func (r *reader) FindTime() (val time.Time) {
if r.ReadNext(cTIME) { if r.ReadNext(cTME) {
var out int64 var out int64
binary.Read(r.Reader, binary.BigEndian, &out) binary.Read(r.Reader, binary.BigEndian, &out)
val = time.Unix(0, out).UTC() val = time.Unix(0, out).UTC()
r.ReadNext(cTERM) r.ReadNext(cEND)
} }
return return
} }
func (r *reader) FindBool() (val bool) { func (r *reader) FindBool() (val bool) {
if r.ReadNext(cBOOL) { if r.ReadNext(cVAL) {
val = r.ReadNext(cBOOL) val = r.ReadNext(cVAL)
r.ReadNext(cTERM) r.ReadNext(cEND)
} }
return return
} }
func (r *reader) FindBytes() (val []byte) { func (r *reader) FindBytes() (val []byte) {
if r.ReadNext(cSTRING) { if r.ReadNext(cSTR) {
val = r.ReadUpto(cTERM, cTERM) val = r.ReadUpto(cEND, cEND)
} }
return return
} }
func (r *reader) FindString() (val string) { func (r *reader) FindString() (val string) {
if r.ReadNext(cPREFIX) { if r.ReadNext(cSTR) {
val = string(r.ReadUpto(cEND, cEND))
} else if r.ReadNext(cPRE) {
val = Prefix val = Prefix
r.ReadNext(cTERM) r.ReadNext(cEND)
} else if r.ReadNext(cSUFFIX) { } else if r.ReadNext(cSUF) {
val = Suffix val = Suffix
r.ReadNext(cTERM) r.ReadNext(cEND)
} else if r.ReadNext(cSTRING) {
val = string(r.ReadUpto(cTERM, cTERM))
} }
return return
} }
@ -185,26 +185,26 @@ func (r *reader) FindNumberFloat64() (val float64) {
} }
func (r *reader) FindArray() (val []interface{}) { func (r *reader) FindArray() (val []interface{}) {
if r.ReadNext(cARRAY) { if r.ReadNext(cARR) {
for !r.ReadNext(cTERM) { for !r.ReadNext(cEND) {
switch fnd := r.FindNext(); fnd { switch fnd := r.FindNext(); fnd {
default: default:
val = append(val, []interface{}{r.FindAny()}...) val = append(val, []interface{}{r.FindAny()}...)
case cNILL: case cNIL:
val = append(val, []interface{}{r.FindNull()}...) val = append(val, []interface{}{r.FindNull()}...)
case cBOOL: case cVAL:
val = append(val, []interface{}{r.FindBool()}...) val = append(val, []interface{}{r.FindBool()}...)
case cTIME: case cTME:
val = append(val, []interface{}{r.FindTime()}...) val = append(val, []interface{}{r.FindTime()}...)
case cNUMBER: case cNEG, cPOS:
val = append(val, []interface{}{r.FindNumber()}...) val = append(val, []interface{}{r.FindNumber()}...)
case cSTRING, cPREFIX, cSUFFIX: case cSTR, cPRE, cSUF:
val = append(val, []interface{}{r.FindString()}...) val = append(val, []interface{}{r.FindString()}...)
case cARRAY: case cARR:
val = append(val, []interface{}{r.FindArray()}...) val = append(val, []interface{}{r.FindArray()}...)
} }
} }
r.ReadNext(cTERM) r.ReadNext(cEND)
} }
return return
} }