Skip to content

Commit

Permalink
Merge pull request #183 from unistack-org/logger/unwrap
Browse files Browse the repository at this point in the history
logger/unwrap: fix for nested tagged/untagged
  • Loading branch information
vtolstov authored Feb 8, 2023
2 parents 7c29afb + 0f343da commit f4d0237
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions logger/unwrap/unwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ var (
closeMapBytes = []byte("}")
)

type protoMessage interface {
Reset()
ProtoMessage()
}

type Wrapper struct {
val interface{}
s fmt.State
pointers map[uintptr]int
opts *Options
depth int
ignoreNextType bool
takeAll map[int]bool
takeMap map[int]bool
protoWrapperType bool
sqlWrapperType bool
}
Expand Down Expand Up @@ -111,7 +116,7 @@ func Tagged(b bool) Option {

func Unwrap(val interface{}, opts ...Option) *Wrapper {
options := NewOptions(opts...)
return &Wrapper{val: val, opts: &options, pointers: make(map[uintptr]int), takeAll: make(map[int]bool)}
return &Wrapper{val: val, opts: &options, pointers: make(map[uintptr]int), takeMap: make(map[int]bool)}
}

func (w *Wrapper) unpackValue(v reflect.Value) reflect.Value {
Expand Down Expand Up @@ -237,9 +242,6 @@ func (w *Wrapper) format(v reflect.Value) {
_, _ = w.s.Write(buf)
return
}
if w.opts.Tagged {
w.checkTakeAll(v, 1)
}

// Handle invalid reflect values immediately.
kind := v.Kind()
Expand All @@ -256,6 +258,10 @@ func (w *Wrapper) format(v reflect.Value) {
w.protoWrapperType = true
} else if strings.HasPrefix(reflect.Indirect(v).Type().String(), "sql.Null") {
w.sqlWrapperType = true
} else if v.CanInterface() {
if _, ok := v.Interface().(protoMessage); ok {
w.protoWrapperType = true
}
}
}
w.formatPtr(v)
Expand Down Expand Up @@ -378,13 +384,22 @@ func (w *Wrapper) format(v reflect.Value) {
prevSkip := false

for i := 0; i < numFields; i++ {
switch vt.Field(i).Type.PkgPath() {
case "google.golang.org/protobuf/internal/impl", "google.golang.org/protobuf/internal/pragma":
w.protoWrapperType = true
prevSkip = true
continue
}
if w.protoWrapperType && !vt.Field(i).IsExported() {
prevSkip = true
continue
} else if w.sqlWrapperType && vt.Field(i).Name == "Valid" {
prevSkip = true
continue
}
if _, ok := vt.Field(i).Tag.Lookup("protobuf"); ok && !w.protoWrapperType {
w.protoWrapperType = true
}
sv, ok := vt.Field(i).Tag.Lookup("logger")
switch {
case ok:
Expand All @@ -395,11 +410,16 @@ func (w *Wrapper) format(v reflect.Value) {
case "take":
break
}
case w.takeAll[w.depth]:
break
case !ok && w.opts.Tagged:
prevSkip = true
continue
// skip top level untagged
if w.depth == 1 {
prevSkip = true
continue
}
if tv, ok := w.takeMap[w.depth]; ok && !tv {
prevSkip = true
continue
}
}

if prevSkip {
Expand All @@ -416,9 +436,7 @@ func (w *Wrapper) format(v reflect.Value) {
_, _ = w.s.Write([]byte(vt.Name))
_, _ = w.s.Write(colonBytes)
}
unpackValue := w.unpackValue(v.Field(i))
w.checkTakeAll(unpackValue, w.depth)
w.format(unpackValue)
w.format(w.unpackValue(v.Field(i)))
numWritten++
}
w.depth--
Expand Down Expand Up @@ -461,6 +479,10 @@ func (w *Wrapper) Format(s fmt.State, verb rune) {
return
}

if w.opts.Tagged {
w.buildTakeMap(reflect.ValueOf(w.val), 1)
}

w.format(reflect.ValueOf(w.val))
}

Expand Down Expand Up @@ -615,24 +637,28 @@ func (w *Wrapper) constructOrigFormat(verb rune) string {
return buf.String()
}

func (w *Wrapper) checkTakeAll(v reflect.Value, depth int) {
if _, ok := w.takeAll[depth]; ok {
return
}
func (w *Wrapper) buildTakeMap(v reflect.Value, depth int) {
if !v.IsValid() || v.IsZero() {
return
}

switch v.Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
w.buildTakeMap(v.Index(i), depth+1)
}
w.takeMap[depth] = true
return
case reflect.Struct:
break
case reflect.Ptr:
v = v.Elem()
if v.Kind() != reflect.Struct {
w.takeAll[depth] = true
w.takeMap[depth] = true
return
}
default:
w.takeAll[depth] = true
w.takeMap[depth] = true
return
}

Expand All @@ -641,8 +667,15 @@ func (w *Wrapper) checkTakeAll(v reflect.Value, depth int) {
for i := 0; i < v.NumField(); i++ {
sv, ok := vt.Field(i).Tag.Lookup("logger")
if ok && sv == "take" {
w.takeAll[depth] = false
w.takeMap[depth] = false
}
w.checkTakeAll(v.Field(i), depth+1)
if v.Kind() == reflect.Struct ||
(v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct) {
w.buildTakeMap(v.Field(i), depth+1)
}
}

if _, ok := w.takeMap[depth]; !ok {
w.takeMap[depth] = true
}
}

0 comments on commit f4d0237

Please sign in to comment.