From acee1ef8bafc8c3119da25deaab486e361842646 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 15 Sep 2020 20:48:02 +0900 Subject: [PATCH] Refactor errUnsupportedValue --- encode_vm.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/encode_vm.go b/encode_vm.go index 29ddb30..bd732cc 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -21,6 +21,17 @@ func store(base uintptr, idx uintptr, p uintptr) { *(*uintptr)(unsafe.Pointer(base + idx)) = p } +func errUnsupportedValue(code *opcode, ptr uintptr) *UnsupportedValueError { + v := *(*interface{})(unsafe.Pointer(&interfaceHeader{ + typ: code.typ, + ptr: unsafe.Pointer(ptr), + })) + return &UnsupportedValueError{ + Value: reflect.ValueOf(v), + Str: fmt.Sprintf("encountered a cycle via %s", code.typ), + } +} + func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { recursiveLevel := 0 seenPtr := map[uintptr]struct{}{} @@ -613,14 +624,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error { if ptr != 0 { if recursiveLevel > startDetectingCyclesAfter { if _, exists := seenPtr[ptr]; exists { - v := *(*interface{})(unsafe.Pointer(&interfaceHeader{ - typ: code.typ, - ptr: unsafe.Pointer(ptr), - })) - return &UnsupportedValueError{ - Value: reflect.ValueOf(v), - Str: fmt.Sprintf("encountered a cycle via %s", code.typ), - } + return errUnsupportedValue(code, ptr) } } }