From c48ab766978b735431a65033495c036a3af0341c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20R=C3=BChl?= Date: Mon, 28 Aug 2023 12:02:47 +0200 Subject: [PATCH] feat(encode): print written hex on debugCodec flag (#685) * feat(encode): print written hex on debugCodec flag --- ua/encode.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/ua/encode.go b/ua/encode.go index e02d1db2..2d6462f1 100644 --- a/ua/encode.go +++ b/ua/encode.go @@ -5,6 +5,7 @@ package ua import ( + "encoding/hex" "fmt" "math" "reflect" @@ -43,7 +44,7 @@ func encode(val reflect.Value, name string) ([]byte, error) { switch { case isBinaryEncoder(val): v := val.Interface().(BinaryEncoder) - return v.Encode() + return dump(v.Encode()) case isTime(val): buf.WriteTime(val.Convert(timeType).Interface().(time.Time)) @@ -78,18 +79,25 @@ func encode(val reflect.Value, name string) ([]byte, error) { if val.IsNil() { return nil, nil } - return encode(val.Elem(), name) + return dump(encode(val.Elem(), name)) case reflect.Struct: - return writeStruct(val, name) + return dump(writeStruct(val, name)) case reflect.Slice: - return writeSlice(val, name) + return dump(writeSlice(val, name)) case reflect.Array: - return writeArray(val, name) + return dump(writeArray(val, name)) default: return nil, errors.Errorf("unsupported type: %s", val.Type()) } } - return buf.Bytes(), buf.Error() + return dump(buf.Bytes(), buf.Error()) +} + +func dump(b []byte, err error) ([]byte, error) { + if debugCodec { + fmt.Printf("%s---\n", hex.Dump(b)) + } + return b, err } func writeStruct(val reflect.Value, name string) ([]byte, error) {