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

View file

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

View file

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

View file

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