From 69ea157270dd33a64c77ca04af3a3946050932bf Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 17 Nov 2020 15:06:05 +0900 Subject: [PATCH] Refactor unsupported value for float64 --- encode_vm.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/encode_vm.go b/encode_vm.go index d71e032..1b043c5 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -35,6 +35,12 @@ func errUnsupportedValue(code *opcode, ptr uintptr) *UnsupportedValueError { } } +func errUnsupportedFloat(v float64) *UnsupportedValueError { + return &UnsupportedValueError{ + Value: reflect.ValueOf(v), + Str: strconv.FormatFloat(v, 'g', -1, 64), + } +} func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { recursiveLevel := 0 seenPtr := map[uintptr]struct{}{} @@ -140,10 +146,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { case opFloat64: v := e.ptrToFloat64(load(ctxptr, code.idx)) if math.IsInf(v, 0) || math.IsNaN(v) { - return &UnsupportedValueError{ - Value: reflect.ValueOf(v), - Str: strconv.FormatFloat(v, 'g', -1, 64), - } + return errUnsupportedFloat(v) } e.encodeFloat64(v) e.encodeByte(',') @@ -151,10 +154,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { case opFloat64Indent: v := e.ptrToFloat64(load(ctxptr, code.idx)) if math.IsInf(v, 0) || math.IsNaN(v) { - return &UnsupportedValueError{ - Value: reflect.ValueOf(v), - Str: strconv.FormatFloat(v, 'g', -1, 64), - } + return errUnsupportedFloat(v) } e.encodeFloat64(v) e.encodeBytes([]byte{',', '\n'})