From f7814e4cd07dfb73cf47d002b9d7bb3147eca164 Mon Sep 17 00:00:00 2001 From: Viktor Pakhuchyi Date: Sat, 13 Jan 2024 18:37:23 +0200 Subject: [PATCH] feat: [CN-33] add a support of uintptr type (#23) --- doc.go | 6 ++-- docs/index.md | 8 ++--- internal/formatter/format.go | 4 +-- internal/formatter/format_test.go | 37 +++++++++++++++------ internal/formatter/integer.go | 2 +- internal/formatter/map.go | 2 +- internal/formatter/struct_test.go | 4 +-- internal/parser/int.go | 4 +-- internal/parser/int_test.go | 9 ++++++ internal/parser/interface.go | 2 +- internal/parser/interface_test.go | 2 +- internal/parser/map.go | 4 +-- internal/parser/map_test.go | 8 ++--- internal/parser/pointer.go | 2 +- internal/parser/pointer_test.go | 2 +- internal/parser/slice.go | 2 +- internal/parser/slice_test.go | 4 +-- internal/parser/struct.go | 2 +- internal/parser/struct_test.go | 53 +++++++++++++++---------------- processor.go | 2 +- processor_test.go | 22 ++++++------- 21 files changed, 98 insertions(+), 83 deletions(-) diff --git a/doc.go b/doc.go index 463ce15..72ba78c 100644 --- a/doc.go +++ b/doc.go @@ -25,7 +25,7 @@ - string, bool - float64/float32, complex64/complex128 - int/int8/int16/int32/int64/rune - - uint/uint8/uint16/uint32/uint64/byte + - uint/uint8/uint16/uint32/uint64/uintptr/byte - Customizable configuration: Offers flexibility in configuration through the use of a `.yaml` file or by directly passing a `config.Config` struct. @@ -97,9 +97,7 @@ Unsupported Types: |------------|------------|------------| - | Chan | Uintptr | Func | - |------------|------------|------------| - | UnsafePtr | | | + | Chan | UnsafePtr | Func | |------------|------------|------------| If a value of an unsupported type is provided, a string value with the following format will be returned: diff --git a/docs/index.md b/docs/index.md index b16ff5d..4d0411d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -23,7 +23,7 @@ go get -u github.com/vpakhuchyi/censor - [x] Wide range of supported types: - `struct`, `map`, `slice`, `array`, `pointer`, `string`, - `float64/float32`, `int/int8/int16/int32/int64/rune`, - - `uint/uint8/uint16/uint32/uint64/byte`, `bool`, + - `uint/uint8/uint16/uint32/uint64/uintptr/byte`, `bool`, - `interface`, `complex64/complex128`. - [x] Support encoding.TextMarshaler interface for custom types. - [x] Customizable configuration: @@ -632,7 +632,7 @@ func main() { There is no specific formatting for the following types: - Int/Int8/Int16/Int32/Int64 -- Uint/Uint8/Byte/Uint16/Uint32/Uint64 +- Uint/Uint8/Byte/Uint16/Uint32/Uint64/Uintptr - Bool Rules of fmt package are applied to them. @@ -655,7 +655,6 @@ type s struct { Chan chan int `censor:"display"` Func func() `censor:"display"` UnsafePointer unsafe.Pointer `censor:"display"` - UintPtr uintptr `censor:"display"` } func main() { @@ -663,12 +662,11 @@ func main() { Chan: make(chan int), Func: func() {}, UnsafePointer: unsafe.Pointer(uintptr(1)), - UintPtr: uintptr(1), } slog.Info("Request", "payload", censor.Format(v)) // Here is what we'll see in the log: - //Output: `2038/10/25 12:00:01 INFO Request payload={Chan: [Unsupported type: chan], Func: [Unsupported type: func], UnsafePointer: [Unsupported type: unsafe.Pointer], UintPtr: [Unsupported type: uintptr]}` + //Output: `2038/10/25 12:00:01 INFO Request payload={Chan: [Unsupported type: chan], Func: [Unsupported type: func], UnsafePointer: [Unsupported type: unsafe.Pointer]}` } ``` \ No newline at end of file diff --git a/internal/formatter/format.go b/internal/formatter/format.go index ad0da3d..8740eae 100644 --- a/internal/formatter/format.go +++ b/internal/formatter/format.go @@ -65,7 +65,7 @@ func (f *Formatter) writeValue(buf *strings.Builder, v models.Value) { case reflect.String: buf.WriteString(f.String(v)) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: buf.WriteString(f.Integer(v)) case reflect.Float32, reflect.Float64: buf.WriteString(f.Float(v)) @@ -94,7 +94,7 @@ func (f *Formatter) writeField(field models.Field, buf *strings.Builder) { case reflect.String: buf.WriteString(formatField(field.Name, f.String(field.Value))) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: buf.WriteString(formatField(field.Name, f.Integer(field.Value))) case reflect.Float32, reflect.Float64: buf.WriteString(formatField(field.Name, f.Float(field.Value))) diff --git a/internal/formatter/format_test.go b/internal/formatter/format_test.go index 142a689..7709ca3 100644 --- a/internal/formatter/format_test.go +++ b/internal/formatter/format_test.go @@ -134,6 +134,16 @@ func TestFormatter_writeValue(t *testing.T) { }) }) + t.Run("uintptr", func(t *testing.T) { + t.Cleanup(func() { buf.Reset() }) + require.NotPanics(t, func() { + v := models.Value{Value: uintptr(824634330992), Kind: reflect.Uint64} + f.writeValue(&buf, v) + exp := "824634330992" + require.Equal(t, exp, buf.String()) + }) + }) + t.Run("byte", func(t *testing.T) { t.Cleanup(func() { buf.Reset() }) require.NotPanics(t, func() { @@ -370,16 +380,6 @@ func TestFormatter_writeValue(t *testing.T) { }) }) - t.Run("unsupported_type_uintptr", func(t *testing.T) { - t.Cleanup(func() { buf.Reset() }) - require.NotPanics(t, func() { - v := models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr} - f.writeValue(&buf, v) - exp := `[Unsupported type: uintptr]` - require.Equal(t, exp, buf.String()) - }) - }) - t.Run("unsupported_type_unsafe_pointer", func(t *testing.T) { t.Cleanup(func() { buf.Reset() }) require.NotPanics(t, func() { @@ -579,6 +579,23 @@ func TestFormatter_writeField(t *testing.T) { }) }) + t.Run("uintptr", func(t *testing.T) { + t.Cleanup(func() { buf.Reset() }) + require.NotPanics(t, func() { + var b uintptr = 824634330992 + field := models.Field{ + Name: "Age", + Value: models.Value{ + Value: b, + Kind: reflect.Uintptr, + }, + } + f.writeField(field, &buf) + exp := `Age: 824634330992` + require.Equal(t, exp, buf.String()) + }) + }) + t.Run("rune", func(t *testing.T) { t.Cleanup(func() { buf.Reset() }) require.NotPanics(t, func() { diff --git a/internal/formatter/integer.go b/internal/formatter/integer.go index 16b843e..82abb97 100644 --- a/internal/formatter/integer.go +++ b/internal/formatter/integer.go @@ -14,7 +14,7 @@ import ( func (f *Formatter) Integer(v models.Value) string { switch v.Kind { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return fmt.Sprintf(`%d`, v.Value) default: panic("provided value is not an integer") diff --git a/internal/formatter/map.go b/internal/formatter/map.go index 23f0ab9..48315c6 100644 --- a/internal/formatter/map.go +++ b/internal/formatter/map.go @@ -16,7 +16,7 @@ import ( // [basic types] // - string // - int, int8, int16, int32, int64 -// - uint, uint8, uint16, uint32, uint64 +// - uint, uint8, uint16, uint32, uint64, uintptr // - float32, float64 // - bool // - byte - represented as uint8 diff --git a/internal/formatter/struct_test.go b/internal/formatter/struct_test.go index fb470e6..0de6e18 100644 --- a/internal/formatter/struct_test.go +++ b/internal/formatter/struct_test.go @@ -97,12 +97,10 @@ func TestFormatter_Struct(t *testing.T) { {Name: "Func", Value: models.Value{Value: "[Unsupported type: func]", Kind: reflect.Func}, Opts: options.FieldOptions{Display: false}}, {Name: "UnsafeWithCensorTag", Value: models.Value{Value: "[Unsupported type: unsafe.Pointer]", Kind: reflect.UnsafePointer}, Opts: options.FieldOptions{Display: true}}, {Name: "Unsafe", Value: models.Value{Value: "[Unsupported type: unsafe.Pointer]", Kind: reflect.UnsafePointer}, Opts: options.FieldOptions{Display: false}}, - {Name: "UintPtrWithCensorTag", Value: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, Opts: options.FieldOptions{Display: true}}, - {Name: "UintPtr", Value: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, Opts: options.FieldOptions{Display: false}}, }, } got := f.Struct(v) - exp := `{ChanWithCensorTag: [Unsupported type: chan], Chan: [CENSORED], FuncWithCensorTag: [Unsupported type: func], Func: [CENSORED], UnsafeWithCensorTag: [Unsupported type: unsafe.Pointer], Unsafe: [CENSORED], UintPtrWithCensorTag: [Unsupported type: uintptr], UintPtr: [CENSORED]}` + exp := `{ChanWithCensorTag: [Unsupported type: chan], Chan: [CENSORED], FuncWithCensorTag: [Unsupported type: func], Func: [CENSORED], UnsafeWithCensorTag: [Unsupported type: unsafe.Pointer], Unsafe: [CENSORED]}` require.Equal(t, exp, got) }) }) diff --git a/internal/parser/int.go b/internal/parser/int.go index ce69aa3..5b894e8 100644 --- a/internal/parser/int.go +++ b/internal/parser/int.go @@ -6,14 +6,14 @@ import ( "github.com/vpakhuchyi/censor/internal/models" ) -// Integer parses an integer (int/int8/int16/int32/int64/uint/uint8/uint16/uint32/uint64/byte/rune) and returns a models.Value. +// Integer parses an integer (int/int8/int16/int32/int64/uint/uint8/uint16/uint32/uint64/uintptr/byte/rune) and returns a models.Value. // Note: this method panics if the provided value is not an integer. // //nolint:exhaustive func (p *Parser) Integer(rv reflect.Value) models.Value { switch k := rv.Kind(); k { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return models.Value{ Value: rv.Interface(), Kind: k, diff --git a/internal/parser/int_test.go b/internal/parser/int_test.go index 950b1dd..ce9b88b 100644 --- a/internal/parser/int_test.go +++ b/internal/parser/int_test.go @@ -105,6 +105,15 @@ func TestParser_Integer(t *testing.T) { }) }) + t.Run("successful_uintptr", func(t *testing.T) { + require.NotPanics(t, func() { + var v uintptr = 824634330992 + got := p.Integer(reflect.ValueOf(v)) + exp := models.Value{Value: uintptr(824634330992), Kind: reflect.Uintptr} + require.Equal(t, exp, got) + }) + }) + t.Run("successful_byte", func(t *testing.T) { require.NotPanics(t, func() { var v byte = 122 diff --git a/internal/parser/interface.go b/internal/parser/interface.go index 67145d9..f0f4a21 100644 --- a/internal/parser/interface.go +++ b/internal/parser/interface.go @@ -30,7 +30,7 @@ func (p *Parser) Interface(rv reflect.Value) models.Value { case reflect.String: return p.String(rv.Elem()) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return p.Integer(rv.Elem()) case reflect.Float32, reflect.Float64: return p.Float(rv.Elem()) diff --git a/internal/parser/interface_test.go b/internal/parser/interface_test.go index c13d924..bdc1346 100644 --- a/internal/parser/interface_test.go +++ b/internal/parser/interface_test.go @@ -361,7 +361,7 @@ func TestParser_Interface(t *testing.T) { { Name: "Names", Value: models.Value{ - Value: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, + Value: models.Value{Value: uintptr(56784757), Kind: reflect.Uintptr}, Kind: reflect.Interface, }, Opts: options.FieldOptions{Display: false}, diff --git a/internal/parser/map.go b/internal/parser/map.go index 5021fda..65bebbc 100644 --- a/internal/parser/map.go +++ b/internal/parser/map.go @@ -41,7 +41,7 @@ func (p *Parser) Map(rv reflect.Value) models.Map { case reflect.String: pair.Key = p.String(key) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: pair.Key = p.Integer(key) case reflect.Float32, reflect.Float64: pair.Key = p.Float(key) @@ -68,7 +68,7 @@ func (p *Parser) Map(rv reflect.Value) models.Map { case reflect.String: pair.Value = p.String(value) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: pair.Value = p.Integer(value) case reflect.Float32, reflect.Float64: pair.Value = p.Float(value) diff --git a/internal/parser/map_test.go b/internal/parser/map_test.go index c81d18f..cf812cc 100644 --- a/internal/parser/map_test.go +++ b/internal/parser/map_test.go @@ -353,8 +353,8 @@ func TestParser_Map(t *testing.T) { exp := models.Map{ Type: "map[string]uintptr", Values: []models.KV{ - {Key: models.Value{Value: "key1", Kind: reflect.String}, Value: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, SortValue: "key1"}, - {Key: models.Value{Value: "key2", Kind: reflect.String}, Value: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, SortValue: "key2"}, + {Key: models.Value{Value: "key1", Kind: reflect.String}, Value: models.Value{Value: uintptr(4563456346), Kind: reflect.Uintptr}, SortValue: "key1"}, + {Key: models.Value{Value: "key2", Kind: reflect.String}, Value: models.Value{Value: uintptr(7586784657), Kind: reflect.Uintptr}, SortValue: "key2"}, }, } @@ -628,8 +628,8 @@ func TestParser_Map(t *testing.T) { exp := models.Map{ Type: "map[uintptr]string", Values: []models.KV{ - {SortValue: "4563456346", Key: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, Value: models.Value{Value: "value1", Kind: reflect.String}}, - {SortValue: "7586784657", Key: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, Value: models.Value{Value: "value2", Kind: reflect.String}}, + {SortValue: "4563456346", Key: models.Value{Value: uintptr(4563456346), Kind: reflect.Uintptr}, Value: models.Value{Value: "value1", Kind: reflect.String}}, + {SortValue: "7586784657", Key: models.Value{Value: uintptr(7586784657), Kind: reflect.Uintptr}, Value: models.Value{Value: "value2", Kind: reflect.String}}, }, } diff --git a/internal/parser/pointer.go b/internal/parser/pointer.go index 194446e..488eefa 100644 --- a/internal/parser/pointer.go +++ b/internal/parser/pointer.go @@ -37,7 +37,7 @@ func (p *Parser) Ptr(rv reflect.Value) models.Ptr { case reflect.String: return models.Ptr(p.String(rv.Elem())) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return models.Ptr(p.Integer(rv.Elem())) case reflect.Float32, reflect.Float64: return models.Ptr(p.Float(rv.Elem())) diff --git a/internal/parser/pointer_test.go b/internal/parser/pointer_test.go index d637848..f3e05e6 100644 --- a/internal/parser/pointer_test.go +++ b/internal/parser/pointer_test.go @@ -227,7 +227,7 @@ func TestParser_Pointer(t *testing.T) { require.NotPanics(t, func() { v := uintptr(0) got := p.Ptr(reflect.ValueOf(&v)) - exp := models.Ptr{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr} + exp := models.Ptr{Value: uintptr(0), Kind: reflect.Uintptr} require.Equal(t, exp, got) }) diff --git a/internal/parser/slice.go b/internal/parser/slice.go index 61d7ec5..fcd95e0 100644 --- a/internal/parser/slice.go +++ b/internal/parser/slice.go @@ -36,7 +36,7 @@ func (p *Parser) Slice(rv reflect.Value) models.Slice { case reflect.String: slice.Values = append(slice.Values, p.String(elem)) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: slice.Values = append(slice.Values, p.Integer(elem)) case reflect.Float32, reflect.Float64: slice.Values = append(slice.Values, p.Float(elem)) diff --git a/internal/parser/slice_test.go b/internal/parser/slice_test.go index 07dbbe7..f1056e6 100644 --- a/internal/parser/slice_test.go +++ b/internal/parser/slice_test.go @@ -327,8 +327,8 @@ func TestParser_Slice(t *testing.T) { got := p.Slice(reflect.ValueOf(v)) exp := models.Slice{ Values: []models.Value{ - {Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, - {Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, + {Value: uintptr(1), Kind: reflect.Uintptr}, + {Value: uintptr(2), Kind: reflect.Uintptr}, }, } diff --git a/internal/parser/struct.go b/internal/parser/struct.go index 976c319..daa0ebc 100644 --- a/internal/parser/struct.go +++ b/internal/parser/struct.go @@ -71,7 +71,7 @@ func (p *Parser) Struct(rv reflect.Value) models.Struct { case reflect.Float32, reflect.Float64: field.Value = p.Float(f) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: field.Value = p.Integer(f) case reflect.Complex64, reflect.Complex128: field.Value = p.Complex(f) diff --git a/internal/parser/struct_test.go b/internal/parser/struct_test.go index f5fd97f..0b43ea0 100644 --- a/internal/parser/struct_test.go +++ b/internal/parser/struct_test.go @@ -56,22 +56,23 @@ func TestParser_Struct(t *testing.T) { t.Run("struct_with_integers", func(t *testing.T) { type integers struct { - Int int `json:"int" censor:"display"` - Int8 int8 `json:"int8" censor:"display"` - Int16 int16 `json:"int16" censor:"display"` - Int32 int32 `json:"int32" censor:"display"` - Int64 int64 `json:"int64" censor:"display"` - Uint uint `json:"uint" censor:"display"` - Uint8 uint8 `json:"uint8" censor:"display"` - Uint16 uint16 `json:"uint16" censor:"display"` - Uint32 uint32 `json:"uint32" censor:"display"` - Uint64 uint64 `json:"uint64" censor:"display"` - Byte byte `json:"byte" censor:"display"` - Rune rune `json:"rune" censor:"display"` + Int int `json:"int" censor:"display"` + Int8 int8 `json:"int8" censor:"display"` + Int16 int16 `json:"int16" censor:"display"` + Int32 int32 `json:"int32" censor:"display"` + Int64 int64 `json:"int64" censor:"display"` + Uint uint `json:"uint" censor:"display"` + Uint8 uint8 `json:"uint8" censor:"display"` + Uint16 uint16 `json:"uint16" censor:"display"` + Uint32 uint32 `json:"uint32" censor:"display"` + Uint64 uint64 `json:"uint64" censor:"display"` + Uintptr uintptr `json:"uintptr" censor:"display"` + Byte byte `json:"byte" censor:"display"` + Rune rune `json:"rune" censor:"display"` } require.NotPanics(t, func() { - v := integers{Int: 1, Int8: 2, Int16: 3, Int32: 4, Int64: 5, Uint: 6, Uint8: 7, Uint16: 8, Uint32: 9, Uint64: 10, Byte: 11, Rune: 'y'} + v := integers{Int: 1, Int8: 2, Int16: 3, Int32: 4, Int64: 5, Uint: 6, Uint8: 7, Uint16: 8, Uint32: 9, Uint64: 10, Uintptr: 11, Byte: 12, Rune: 'y'} got := p.Struct(reflect.ValueOf(v)) exp := models.Struct{ Name: "parser.integers", @@ -86,7 +87,8 @@ func TestParser_Struct(t *testing.T) { {Name: "Uint16", Value: models.Value{Value: uint16(8), Kind: reflect.Uint16}, Opts: options.FieldOptions{Display: true}}, {Name: "Uint32", Value: models.Value{Value: uint32(9), Kind: reflect.Uint32}, Opts: options.FieldOptions{Display: true}}, {Name: "Uint64", Value: models.Value{Value: uint64(10), Kind: reflect.Uint64}, Opts: options.FieldOptions{Display: true}}, - {Name: "Byte", Value: models.Value{Value: byte(11), Kind: reflect.Uint8}, Opts: options.FieldOptions{Display: true}}, + {Name: "Uintptr", Value: models.Value{Value: uintptr(11), Kind: reflect.Uintptr}, Opts: options.FieldOptions{Display: true}}, + {Name: "Byte", Value: models.Value{Value: byte(12), Kind: reflect.Uint8}, Opts: options.FieldOptions{Display: true}}, {Name: "Rune", Value: models.Value{Value: rune(121), Kind: reflect.Int32}, Opts: options.FieldOptions{Display: true}}, }, } @@ -575,22 +577,19 @@ func TestParser_Struct(t *testing.T) { t.Run("struct_with_unsupported_types", func(t *testing.T) { type structWithUnsupportedTypes struct { - ChanWithCensorTag chan int `censor:"display"` - Chan chan int - FuncWithCensorTag func() `censor:"display"` - Func func() - UnsafeWithCensorTag unsafe.Pointer `censor:"display"` - Unsafe unsafe.Pointer - UintPtrWithCensorTag uintptr `censor:"display"` - UintPtr uintptr + ChanWithCensorTag chan int `censor:"display"` + Chan chan int + FuncWithCensorTag func() `censor:"display"` + Func func() + UnsafeWithCensorTag unsafe.Pointer `censor:"display"` + Unsafe unsafe.Pointer } require.NotPanics(t, func() { v := structWithUnsupportedTypes{ - Chan: make(chan int), - Func: func() {}, - Unsafe: unsafe.Pointer(&mainPkgStruct), - UintPtr: uintptr(unsafe.Pointer(&mainPkgStruct)), + Chan: make(chan int), + Func: func() {}, + Unsafe: unsafe.Pointer(&mainPkgStruct), } got := p.Struct(reflect.ValueOf(v)) @@ -603,8 +602,6 @@ func TestParser_Struct(t *testing.T) { {Name: "Func", Value: models.Value{Value: "[Unsupported type: func]", Kind: reflect.Func}, Opts: options.FieldOptions{Display: false}}, {Name: "UnsafeWithCensorTag", Value: models.Value{Value: "[Unsupported type: unsafe.Pointer]", Kind: reflect.UnsafePointer}, Opts: options.FieldOptions{Display: true}}, {Name: "Unsafe", Value: models.Value{Value: "[Unsupported type: unsafe.Pointer]", Kind: reflect.UnsafePointer}, Opts: options.FieldOptions{Display: false}}, - {Name: "UintPtrWithCensorTag", Value: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, Opts: options.FieldOptions{Display: true}}, - {Name: "UintPtr", Value: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, Opts: options.FieldOptions{Display: false}}, }, } require.Equal(t, exp, got) diff --git a/processor.go b/processor.go index 95e26f5..d863a84 100644 --- a/processor.go +++ b/processor.go @@ -151,7 +151,7 @@ func (p *Processor) parse(v reflect.Value) any { case reflect.Float32, reflect.Float64: return p.parser.Float(v) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return p.parser.Integer(v) case reflect.Bool: return p.parser.Bool(v) diff --git a/processor_test.go b/processor_test.go index 1dbbfce..46a19e0 100644 --- a/processor_test.go +++ b/processor_test.go @@ -273,6 +273,16 @@ func TestProcessor_parse(t *testing.T) { }) }) + t.Run("uintptr", func(t *testing.T) { + require.NotPanics(t, func() { + val := uintptr(824634330992) + exp := models.Value{Value: uintptr(824634330992), Kind: reflect.Uintptr} + + got := p.parse(reflect.ValueOf(val)) + require.Equal(t, exp, got) + }) + }) + t.Run("rune", func(t *testing.T) { require.NotPanics(t, func() { val := 'U' @@ -342,18 +352,6 @@ func TestProcessor_parse(t *testing.T) { require.Equal(t, exp, got) }) }) - - t.Run("unsupported_type_uintptr", func(t *testing.T) { - require.NotPanics(t, func() { - - var val uintptr = 1374389890440 - - exp := models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr} - - got := p.parse(reflect.ValueOf(val)) - require.Equal(t, exp, got) - }) - }) } func TestProcessor_format(t *testing.T) {