Refactor unsupported value error for float64

This commit is contained in:
Masaaki Goshima 2020-11-17 15:14:07 +09:00
parent ea96cc7811
commit 9802f67a9b
1 changed files with 16 additions and 64 deletions

View File

@ -1533,10 +1533,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
} else { } else {
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeByte('{') e.encodeByte('{')
e.encodeKey(code) e.encodeKey(code)
@ -1554,10 +1551,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
} else { } else {
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeKey(code) e.encodeKey(code)
e.encodeFloat64(v) e.encodeFloat64(v)
@ -2170,10 +2164,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
} else { } else {
v := e.ptrToFloat64(ptr) v := e.ptrToFloat64(ptr)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeBytes([]byte{'{', '\n'}) e.encodeBytes([]byte{'{', '\n'})
@ -2804,10 +2795,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
code = code.nextField code = code.nextField
} else { } else {
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeKey(code) e.encodeKey(code)
e.encodeFloat64(v) e.encodeFloat64(v)
@ -2831,10 +2819,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
code = code.nextField code = code.nextField
} else { } else {
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeKey(code) e.encodeKey(code)
e.encodeFloat64(v) e.encodeFloat64(v)
@ -3491,10 +3476,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
code = code.nextField code = code.nextField
} else { } else {
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeIndent(code.indent + 1) e.encodeIndent(code.indent + 1)
e.encodeKey(code) e.encodeKey(code)
@ -4026,10 +4008,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
e.encodeByte('{') e.encodeByte('{')
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeKey(code) e.encodeKey(code)
e.encodeString(fmt.Sprint(v)) e.encodeString(fmt.Sprint(v))
@ -4049,10 +4028,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
} else { } else {
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeKey(code) e.encodeKey(code)
e.encodeString(fmt.Sprint(v)) e.encodeString(fmt.Sprint(v))
@ -4596,10 +4572,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
e.encodeBytes([]byte{'{', '\n'}) e.encodeBytes([]byte{'{', '\n'})
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeIndent(code.indent + 1) e.encodeIndent(code.indent + 1)
e.encodeKey(code) e.encodeKey(code)
@ -4893,10 +4866,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
} }
v := e.ptrToFloat64(p) v := e.ptrToFloat64(p)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeFloat64(v) e.encodeFloat64(v)
e.encodeByte(',') e.encodeByte(',')
@ -4906,10 +4876,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
e.encodeKey(code) e.encodeKey(code)
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeFloat64(v) e.encodeFloat64(v)
e.encodeByte(',') e.encodeByte(',')
@ -5115,10 +5082,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
ptr := load(ctxptr, code.headIdx) ptr := load(ctxptr, code.headIdx)
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeFloat64(v) e.encodeFloat64(v)
e.encodeBytes([]byte{',', '\n'}) e.encodeBytes([]byte{',', '\n'})
@ -5369,10 +5333,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if v != 0 { if v != 0 {
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeKey(code) e.encodeKey(code)
e.encodeFloat64(v) e.encodeFloat64(v)
@ -5618,10 +5579,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if v != 0 { if v != 0 {
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeKey(code) e.encodeKey(code)
@ -5817,10 +5775,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
ptr := load(ctxptr, code.headIdx) ptr := load(ctxptr, code.headIdx)
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeKey(code) e.encodeKey(code)
e.encodeString(fmt.Sprint(v)) e.encodeString(fmt.Sprint(v))
@ -5973,10 +5928,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
ptr := load(ctxptr, code.headIdx) ptr := load(ctxptr, code.headIdx)
v := e.ptrToFloat64(ptr + code.offset) v := e.ptrToFloat64(ptr + code.offset)
if math.IsInf(v, 0) || math.IsNaN(v) { if math.IsInf(v, 0) || math.IsNaN(v) {
return &UnsupportedValueError{ return errUnsupportedFloat(v)
Value: reflect.ValueOf(v),
Str: strconv.FormatFloat(v, 'g', -1, 64),
}
} }
e.encodeIndent(code.indent) e.encodeIndent(code.indent)
e.encodeKey(code) e.encodeKey(code)