Skip to content

Commit

Permalink
feat: [CN-32] remove redundant Kind field from models.Field structure (
Browse files Browse the repository at this point in the history
  • Loading branch information
vpakhuchyi authored Jan 11, 2024
1 parent 203fddf commit 167ae34
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 172 deletions.
4 changes: 2 additions & 2 deletions internal/formatter/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (f *Formatter) writeValue(buf *strings.Builder, v models.Value) {

//nolint:exhaustive,gocyclo
func (f *Formatter) writeField(field models.Field, buf *strings.Builder) {
switch field.Kind {
switch field.Value.Kind {
case reflect.String:
buf.WriteString(formatField(field.Name, f.String(field.Value)))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
Expand All @@ -113,6 +113,6 @@ func (f *Formatter) writeField(field models.Field, buf *strings.Builder) {
case reflect.Interface:
buf.WriteString(formatField(field.Name, f.Interface(field.Value)))
default:
buf.WriteString(formatField(field.Name, fmt.Sprintf(config.UnsupportedTypeTmpl, field.Kind)))
buf.WriteString(formatField(field.Name, fmt.Sprintf(config.UnsupportedTypeTmpl, field.Value.Kind)))
}
}
28 changes: 0 additions & 28 deletions internal/formatter/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ func TestFormatter_writeValue(t *testing.T) {
Value: "Kholodetsʹ",
Kind: reflect.String,
},
Kind: reflect.String,
},
{
Name: "Ingredients",
Expand All @@ -295,7 +294,6 @@ func TestFormatter_writeValue(t *testing.T) {
},
Kind: reflect.Slice,
},
Kind: reflect.Slice,
Opts: options.FieldOptions{Display: true},
},
},
Expand Down Expand Up @@ -410,7 +408,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: "Kholodetsʹ",
Kind: reflect.String,
},
Kind: reflect.String,
}
f.writeField(field, &buf)
exp := `Name: Kholodetsʹ`
Expand All @@ -427,7 +424,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: 44,
Kind: reflect.Int,
},
Kind: reflect.Int,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -444,7 +440,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: int8(44),
Kind: reflect.Int8,
},
Kind: reflect.Int8,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -461,7 +456,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: int16(44),
Kind: reflect.Int16,
},
Kind: reflect.Int16,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -478,7 +472,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: int32(44),
Kind: reflect.Int32,
},
Kind: reflect.Int32,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -495,7 +488,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: int64(44),
Kind: reflect.Int64,
},
Kind: reflect.Int64,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -512,7 +504,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: uint(44),
Kind: reflect.Uint,
},
Kind: reflect.Uint,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -530,7 +521,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: b,
Kind: reflect.Uint8,
},
Kind: reflect.Uint8,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -548,7 +538,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: b,
Kind: reflect.Uint16,
},
Kind: reflect.Uint16,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -566,7 +555,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: b,
Kind: reflect.Uint32,
},
Kind: reflect.Uint32,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -584,7 +572,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: b,
Kind: reflect.Uint64,
},
Kind: reflect.Uint64,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -602,7 +589,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: r,
Kind: reflect.Int32,
},
Kind: reflect.Int32,
}
f.writeField(field, &buf)
exp := `Letter: 65`
Expand All @@ -620,7 +606,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: b,
Kind: reflect.Uint8,
},
Kind: reflect.Uint8,
}
f.writeField(field, &buf)
exp := `Age: 44`
Expand All @@ -638,7 +623,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: fl,
Kind: reflect.Float32,
},
Kind: reflect.Float32,
}
f.writeField(field, &buf)
exp := `Float: 3.1111112`
Expand All @@ -656,7 +640,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: fl,
Kind: reflect.Float64,
},
Kind: reflect.Float64,
}
f.writeField(field, &buf)
exp := `Float: 3.11111111111111`
Expand All @@ -674,7 +657,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: c,
Kind: reflect.Complex64,
},
Kind: reflect.Complex64,
}
f.writeField(field, &buf)
exp := `Test: (3.111111+3.111111i)`
Expand All @@ -692,7 +674,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: c,
Kind: reflect.Complex128,
},
Kind: reflect.Complex128,
}
f.writeField(field, &buf)
exp := `Test: (3.11111111111111+3.11111111111111i)`
Expand All @@ -709,7 +690,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: true,
Kind: reflect.Bool,
},
Kind: reflect.Bool,
}
f.writeField(field, &buf)
exp := `IsAwesome: true`
Expand All @@ -734,7 +714,6 @@ func TestFormatter_writeField(t *testing.T) {
},
Kind: reflect.Interface,
},
Kind: reflect.Interface,
}
f.writeField(field, &buf)
exp := `Dishes: [Kholodetsʹ, Halushky]`
Expand All @@ -756,7 +735,6 @@ func TestFormatter_writeField(t *testing.T) {
},
Kind: reflect.Slice,
},
Kind: reflect.Slice,
}
f.writeField(field, &buf)
exp := `Dishes: [Kholodetsʹ, Halushky]`
Expand All @@ -778,7 +756,6 @@ func TestFormatter_writeField(t *testing.T) {
},
Kind: reflect.Slice,
},
Kind: reflect.Array,
}
f.writeField(field, &buf)
exp := `Dishes: [Kholodetsʹ, Halushky]`
Expand All @@ -800,7 +777,6 @@ func TestFormatter_writeField(t *testing.T) {
Value: "Kholodetsʹ",
Kind: reflect.String,
},
Kind: reflect.String,
},
{
Name: "Ingredients",
Expand All @@ -816,14 +792,12 @@ func TestFormatter_writeField(t *testing.T) {
},
Kind: reflect.Slice,
},
Kind: reflect.Slice,
Opts: options.FieldOptions{Display: true},
},
},
},
Kind: reflect.Struct,
},
Kind: reflect.Struct,
}
f.writeField(field, &buf)
exp := `Dish: {Name: [CENSORED], Ingredients: [Pork, Garlic, Black pepper, Bay leaf, Salt]}`
Expand Down Expand Up @@ -854,7 +828,6 @@ func TestFormatter_writeField(t *testing.T) {
},
Kind: reflect.Map,
},
Kind: reflect.Map,
}
f.writeField(field, &buf)
exp := `Dish: map[Best dish ever: Kholodetsʹ]`
Expand All @@ -875,7 +848,6 @@ func TestFormatter_writeField(t *testing.T) {
},
Kind: reflect.Ptr,
},
Kind: reflect.Ptr,
}
f.writeField(field, &buf)
exp := `Dish: Kholodetsʹ`
Expand Down
28 changes: 14 additions & 14 deletions internal/formatter/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func TestFormatter_Struct(t *testing.T) {
v := models.Struct{
Name: "Foo",
Fields: []models.Field{
{Name: "Foo", Value: models.Value{Value: "foo", Kind: reflect.String}, Opts: options.FieldOptions{Display: true}, Kind: reflect.String},
{Name: "Bar", Value: models.Value{Value: 1, Kind: reflect.Int}, Opts: options.FieldOptions{Display: false}, Kind: reflect.Int},
{Name: "Foo", Value: models.Value{Value: "foo", Kind: reflect.String}, Opts: options.FieldOptions{Display: true}},
{Name: "Bar", Value: models.Value{Value: 1, Kind: reflect.Int}, Opts: options.FieldOptions{Display: false}},
},
}
got := f.Struct(v)
Expand All @@ -45,8 +45,8 @@ func TestFormatter_Struct(t *testing.T) {
v := models.Struct{
Name: "Foo",
Fields: []models.Field{
{Name: "Foo", Value: models.Value{Value: "foo", Kind: reflect.String}, Opts: options.FieldOptions{Display: true}, Kind: reflect.String},
{Name: "Bar", Value: models.Value{Value: 1, Kind: reflect.Int}, Opts: options.FieldOptions{Display: true}, Kind: reflect.Int},
{Name: "Foo", Value: models.Value{Value: "foo", Kind: reflect.String}, Opts: options.FieldOptions{Display: true}},
{Name: "Bar", Value: models.Value{Value: 1, Kind: reflect.Int}, Opts: options.FieldOptions{Display: true}},
},
}
got := f.Struct(v)
Expand All @@ -68,8 +68,8 @@ func TestFormatter_Struct(t *testing.T) {
v := models.Struct{
Name: "Foo",
Fields: []models.Field{
{Name: "Foo", Value: models.Value{Value: "testuser@exxxample.com", Kind: reflect.String}, Opts: options.FieldOptions{Display: true}, Kind: reflect.String},
{Name: "Bar", Value: models.Value{Value: 1, Kind: reflect.Int}, Opts: options.FieldOptions{Display: false}, Kind: reflect.Int},
{Name: "Foo", Value: models.Value{Value: "testuser@exxxample.com", Kind: reflect.String}, Opts: options.FieldOptions{Display: true}},
{Name: "Bar", Value: models.Value{Value: 1, Kind: reflect.Int}, Opts: options.FieldOptions{Display: false}},
},
}
got := f.Struct(v)
Expand All @@ -91,14 +91,14 @@ func TestFormatter_Struct(t *testing.T) {
v := models.Struct{
Name: "parser.structWithUnsupportedTypes",
Fields: []models.Field{
{Name: "ChanWithCensorTag", Value: models.Value{Value: "[Unsupported type: chan]", Kind: reflect.Chan}, Opts: options.FieldOptions{Display: true}, Kind: reflect.Chan},
{Name: "Chan", Value: models.Value{Value: "[Unsupported type: chan]", Kind: reflect.Chan}, Opts: options.FieldOptions{Display: false}, Kind: reflect.Chan},
{Name: "FuncWithCensorTag", Value: models.Value{Value: "[Unsupported type: func]", Kind: reflect.Func}, Opts: options.FieldOptions{Display: true}, Kind: reflect.Func},
{Name: "Func", Value: models.Value{Value: "[Unsupported type: func]", Kind: reflect.Func}, Opts: options.FieldOptions{Display: false}, Kind: reflect.Func},
{Name: "UnsafeWithCensorTag", Value: models.Value{Value: "[Unsupported type: unsafe.Pointer]", Kind: reflect.UnsafePointer}, Opts: options.FieldOptions{Display: true}, Kind: reflect.UnsafePointer},
{Name: "Unsafe", Value: models.Value{Value: "[Unsupported type: unsafe.Pointer]", Kind: reflect.UnsafePointer}, Opts: options.FieldOptions{Display: false}, Kind: reflect.UnsafePointer},
{Name: "UintPtrWithCensorTag", Value: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, Opts: options.FieldOptions{Display: true}, Kind: reflect.Uintptr},
{Name: "UintPtr", Value: models.Value{Value: "[Unsupported type: uintptr]", Kind: reflect.Uintptr}, Opts: options.FieldOptions{Display: false}, Kind: reflect.Uintptr},
{Name: "ChanWithCensorTag", Value: models.Value{Value: "[Unsupported type: chan]", Kind: reflect.Chan}, Opts: options.FieldOptions{Display: true}},
{Name: "Chan", Value: models.Value{Value: "[Unsupported type: chan]", Kind: reflect.Chan}, Opts: options.FieldOptions{Display: false}},
{Name: "FuncWithCensorTag", Value: models.Value{Value: "[Unsupported type: func]", Kind: reflect.Func}, Opts: options.FieldOptions{Display: true}},
{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)
Expand Down
3 changes: 0 additions & 3 deletions internal/models/field.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package models

import (
"reflect"

"github.com/vpakhuchyi/censor/internal/options"
)

Expand All @@ -11,5 +9,4 @@ type Field struct {
Name string
Value Value
Opts options.FieldOptions
Kind reflect.Kind
}
4 changes: 0 additions & 4 deletions internal/parser/const.go

This file was deleted.

3 changes: 2 additions & 1 deletion internal/parser/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

// Interface parses an interface and returns an Interface.
// In case of unsupported underlying type, UnsupportedValue const value is used instead of the real value.
// In case of a pointer to unsupported type of value, a string built from config.UnsupportedTypeTmpl
// is used instead of the real value. That string contains a type of the value.
//
//nolint:exhaustive,gocyclo
func (p *Parser) Interface(rv reflect.Value) models.Value {
Expand Down
Loading

0 comments on commit 167ae34

Please sign in to comment.