2022-10-11 00:30:42 +03:00
|
|
|
package unwrap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sf = "0-+# "
|
|
|
|
|
|
|
|
var hexDigits = "0123456789abcdef"
|
|
|
|
|
|
|
|
var (
|
|
|
|
panicBytes = []byte("(PANIC=")
|
|
|
|
plusBytes = []byte("+")
|
|
|
|
iBytes = []byte("i")
|
|
|
|
trueBytes = []byte("true")
|
|
|
|
falseBytes = []byte("false")
|
|
|
|
interfaceBytes = []byte("(interface {})")
|
|
|
|
openBraceBytes = []byte("{")
|
|
|
|
closeBraceBytes = []byte("}")
|
|
|
|
asteriskBytes = []byte("*")
|
|
|
|
ampBytes = []byte("&")
|
|
|
|
colonBytes = []byte(":")
|
|
|
|
openParenBytes = []byte("(")
|
|
|
|
closeParenBytes = []byte(")")
|
|
|
|
spaceBytes = []byte(" ")
|
|
|
|
commaBytes = []byte(",")
|
|
|
|
pointerChainBytes = []byte("->")
|
|
|
|
nilAngleBytes = []byte("<nil>")
|
|
|
|
circularShortBytes = []byte("<shown>")
|
|
|
|
invalidAngleBytes = []byte("<invalid>")
|
2022-12-29 23:16:58 +03:00
|
|
|
filteredBytes = []byte("<filtered>")
|
2024-12-06 19:05:27 +03:00
|
|
|
// openBracketBytes = []byte("[")
|
|
|
|
// closeBracketBytes = []byte("]")
|
|
|
|
percentBytes = []byte("%")
|
|
|
|
precisionBytes = []byte(".")
|
|
|
|
openAngleBytes = []byte("<")
|
|
|
|
closeAngleBytes = []byte(">")
|
|
|
|
openMapBytes = []byte("{")
|
|
|
|
closeMapBytes = []byte("}")
|
2022-10-11 00:30:42 +03:00
|
|
|
)
|
|
|
|
|
2023-02-08 14:54:37 +03:00
|
|
|
type protoMessage interface {
|
|
|
|
Reset()
|
|
|
|
ProtoMessage()
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
type Wrapper struct {
|
2024-12-09 13:06:43 +03:00
|
|
|
pointers map[uintptr]int
|
|
|
|
takeMap map[int]bool
|
|
|
|
|
|
|
|
val interface{}
|
|
|
|
s fmt.State
|
|
|
|
opts *Options
|
|
|
|
|
2023-02-06 18:50:00 +03:00
|
|
|
depth int
|
|
|
|
ignoreNextType bool
|
|
|
|
protoWrapperType bool
|
|
|
|
sqlWrapperType bool
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
|
2022-12-29 23:16:58 +03:00
|
|
|
// Options struct
|
2022-10-11 00:30:42 +03:00
|
|
|
type Options struct {
|
2022-11-19 15:20:42 +03:00
|
|
|
Codec codec.Codec
|
|
|
|
Indent string
|
|
|
|
Methods bool
|
|
|
|
Tagged bool
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
|
2022-12-29 23:16:58 +03:00
|
|
|
// NewOptions creates new Options struct via provided args
|
2022-10-11 00:30:42 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2022-11-19 15:20:42 +03:00
|
|
|
Indent: " ",
|
|
|
|
Methods: false,
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:16:58 +03:00
|
|
|
// Option func signature
|
2022-10-11 00:30:42 +03:00
|
|
|
type Option func(*Options)
|
|
|
|
|
2022-12-29 23:16:58 +03:00
|
|
|
// Indent option specify indent level
|
2022-11-19 15:20:42 +03:00
|
|
|
func Indent(f string) Option {
|
2022-10-11 00:30:42 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Indent = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:16:58 +03:00
|
|
|
// Methods option toggles fmt.Stringer methods
|
2022-11-19 15:20:42 +03:00
|
|
|
func Methods(b bool) Option {
|
2022-10-11 00:30:42 +03:00
|
|
|
return func(o *Options) {
|
2022-11-19 15:20:42 +03:00
|
|
|
o.Methods = b
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:16:58 +03:00
|
|
|
// Codec option automatic marshal arg via specified codec and write it to log
|
2022-11-19 15:20:42 +03:00
|
|
|
func Codec(c codec.Codec) Option {
|
2022-10-11 00:30:42 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Codec = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:16:58 +03:00
|
|
|
// Tagged option toggles output only logger:"take" fields
|
2022-11-19 15:20:42 +03:00
|
|
|
func Tagged(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tagged = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
func Unwrap(val interface{}, opts ...Option) *Wrapper {
|
2022-10-11 00:30:42 +03:00
|
|
|
options := NewOptions(opts...)
|
2023-02-08 14:54:37 +03:00
|
|
|
return &Wrapper{val: val, opts: &options, pointers: make(map[uintptr]int), takeMap: make(map[int]bool)}
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
func (w *Wrapper) unpackValue(v reflect.Value) reflect.Value {
|
2022-10-11 00:30:42 +03:00
|
|
|
if v.Kind() == reflect.Interface {
|
2023-02-06 22:36:24 +03:00
|
|
|
w.ignoreNextType = false
|
2022-10-11 00:30:42 +03:00
|
|
|
if !v.IsNil() {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatPtr handles formatting of pointers by indirecting them as necessary.
|
2023-02-06 22:36:24 +03:00
|
|
|
func (w *Wrapper) formatPtr(v reflect.Value) {
|
2022-10-11 00:30:42 +03:00
|
|
|
// Display nil if top level pointer is nil.
|
2023-02-06 22:36:24 +03:00
|
|
|
showTypes := w.s.Flag('#')
|
|
|
|
if v.IsNil() && (!showTypes || w.ignoreNextType) {
|
|
|
|
_, _ = w.s.Write(nilAngleBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove pointers at or below the current depth from map used to detect
|
|
|
|
// circular refs.
|
2023-02-06 22:36:24 +03:00
|
|
|
for k, depth := range w.pointers {
|
|
|
|
if depth >= w.depth {
|
|
|
|
delete(w.pointers, k)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep list of all dereferenced pointers to possibly show later.
|
|
|
|
pointerChain := make([]uintptr, 0)
|
|
|
|
|
|
|
|
// Figure out how many levels of indirection there are by derferencing
|
|
|
|
// pointers and unpacking interfaces down the chain while detecting circular
|
|
|
|
// references.
|
|
|
|
nilFound := false
|
|
|
|
cycleFound := false
|
|
|
|
indirects := 0
|
|
|
|
ve := v
|
|
|
|
for ve.Kind() == reflect.Ptr {
|
|
|
|
if ve.IsNil() {
|
|
|
|
nilFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
indirects++
|
|
|
|
addr := ve.Pointer()
|
|
|
|
pointerChain = append(pointerChain, addr)
|
2023-02-06 22:36:24 +03:00
|
|
|
if pd, ok := w.pointers[addr]; ok && pd < w.depth {
|
2022-10-11 00:30:42 +03:00
|
|
|
cycleFound = true
|
|
|
|
indirects--
|
|
|
|
break
|
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
w.pointers[addr] = w.depth
|
2022-10-11 00:30:42 +03:00
|
|
|
|
|
|
|
ve = ve.Elem()
|
|
|
|
if ve.Kind() == reflect.Interface {
|
|
|
|
if ve.IsNil() {
|
|
|
|
nilFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ve = ve.Elem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display type or indirection level depending on flags.
|
2023-02-06 22:36:24 +03:00
|
|
|
if showTypes && !w.ignoreNextType {
|
|
|
|
if w.depth > 0 {
|
|
|
|
_, _ = w.s.Write(openParenBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
if w.depth > 0 {
|
|
|
|
_, _ = w.s.Write(bytes.Repeat(asteriskBytes, indirects))
|
2022-10-11 00:30:42 +03:00
|
|
|
} else {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(bytes.Repeat(ampBytes, indirects))
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write([]byte(ve.Type().String()))
|
|
|
|
if w.depth > 0 {
|
|
|
|
_, _ = w.s.Write(closeParenBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if nilFound || cycleFound {
|
|
|
|
indirects += strings.Count(ve.Type().String(), "*")
|
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(openAngleBytes)
|
|
|
|
_, _ = w.s.Write([]byte(strings.Repeat("*", indirects)))
|
|
|
|
_, _ = w.s.Write(closeAngleBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display pointer information depending on flags.
|
2023-02-06 22:36:24 +03:00
|
|
|
if w.s.Flag('+') && (len(pointerChain) > 0) {
|
|
|
|
_, _ = w.s.Write(openParenBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
for i, addr := range pointerChain {
|
|
|
|
if i > 0 {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(pointerChainBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
getHexPtr(w.s, addr)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(closeParenBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display dereferenced value.
|
|
|
|
switch {
|
|
|
|
case nilFound:
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(nilAngleBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
case cycleFound:
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(circularShortBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
default:
|
2023-02-06 22:36:24 +03:00
|
|
|
w.ignoreNextType = true
|
|
|
|
w.format(ve)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// format is the main workhorse for providing the Formatter interface. It
|
|
|
|
// uses the passed reflect value to figure out what kind of object we are
|
|
|
|
// dealing with and formats it appropriately. It is a recursive function,
|
|
|
|
// however circular data structures are detected and handled properly.
|
2023-02-06 22:36:24 +03:00
|
|
|
func (w *Wrapper) format(v reflect.Value) {
|
|
|
|
if w.opts.Codec != nil {
|
|
|
|
buf, err := w.opts.Codec.Marshal(v.Interface())
|
2022-10-11 00:30:42 +03:00
|
|
|
if err != nil {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(invalidAngleBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
return
|
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(buf)
|
2022-10-11 00:30:42 +03:00
|
|
|
return
|
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
|
2022-10-11 00:30:42 +03:00
|
|
|
// Handle invalid reflect values immediately.
|
|
|
|
kind := v.Kind()
|
|
|
|
if kind == reflect.Invalid {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(invalidAngleBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle pointers specially.
|
2023-02-06 18:50:00 +03:00
|
|
|
switch kind {
|
|
|
|
case reflect.Ptr:
|
|
|
|
if !v.IsZero() {
|
|
|
|
if strings.HasPrefix(reflect.Indirect(v).Type().String(), "wrapperspb.") {
|
2023-02-06 22:36:24 +03:00
|
|
|
w.protoWrapperType = true
|
2023-02-06 18:50:00 +03:00
|
|
|
} else if strings.HasPrefix(reflect.Indirect(v).Type().String(), "sql.Null") {
|
2023-02-06 22:36:24 +03:00
|
|
|
w.sqlWrapperType = true
|
2023-02-08 14:54:37 +03:00
|
|
|
} else if v.CanInterface() {
|
|
|
|
if _, ok := v.Interface().(protoMessage); ok {
|
|
|
|
w.protoWrapperType = true
|
|
|
|
}
|
2023-02-06 18:50:00 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
w.formatPtr(v)
|
2022-10-11 00:30:42 +03:00
|
|
|
return
|
2023-02-06 18:50:00 +03:00
|
|
|
case reflect.Struct:
|
|
|
|
if !v.IsZero() {
|
|
|
|
if strings.HasPrefix(reflect.Indirect(v).Type().String(), "sql.Null") {
|
2023-02-06 22:36:24 +03:00
|
|
|
w.sqlWrapperType = true
|
2023-02-06 18:50:00 +03:00
|
|
|
}
|
|
|
|
}
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get type information unless already handled elsewhere.
|
2023-02-06 22:36:24 +03:00
|
|
|
if !w.ignoreNextType && w.s.Flag('#') {
|
2022-11-07 15:56:09 +03:00
|
|
|
if v.Type().Kind() != reflect.Map &&
|
|
|
|
v.Type().Kind() != reflect.String &&
|
|
|
|
v.Type().Kind() != reflect.Array &&
|
|
|
|
v.Type().Kind() != reflect.Slice {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(openParenBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2022-11-07 15:56:09 +03:00
|
|
|
if v.Kind() != reflect.String {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write([]byte(v.Type().String()))
|
2022-11-07 15:56:09 +03:00
|
|
|
}
|
|
|
|
if v.Type().Kind() != reflect.Map &&
|
|
|
|
v.Type().Kind() != reflect.String &&
|
|
|
|
v.Type().Kind() != reflect.Array &&
|
|
|
|
v.Type().Kind() != reflect.Slice {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(closeParenBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
w.ignoreNextType = false
|
2022-10-11 00:30:42 +03:00
|
|
|
|
|
|
|
// Call Stringer/error interfaces if they exist and the handle methods
|
|
|
|
// flag is enabled.
|
2023-02-06 22:36:24 +03:00
|
|
|
if w.opts.Methods {
|
2022-10-11 00:30:42 +03:00
|
|
|
if (kind != reflect.Invalid) && (kind != reflect.Interface) {
|
2023-02-06 22:36:24 +03:00
|
|
|
if handled := handleMethods(w.opts, w.s, v); handled {
|
2022-10-11 00:30:42 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch kind {
|
|
|
|
case reflect.Invalid:
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(invalidAngleBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Bool:
|
2023-02-06 22:36:24 +03:00
|
|
|
getBool(w.s, v.Bool())
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
2023-02-06 22:36:24 +03:00
|
|
|
getInt(w.s, v.Int(), 10)
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
2023-02-06 22:36:24 +03:00
|
|
|
getUint(w.s, v.Uint(), 10)
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Float32:
|
2023-02-06 22:36:24 +03:00
|
|
|
getFloat(w.s, v.Float(), 32)
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Float64:
|
2023-02-06 22:36:24 +03:00
|
|
|
getFloat(w.s, v.Float(), 64)
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Complex64:
|
2023-02-06 22:36:24 +03:00
|
|
|
getComplex(w.s, v.Complex(), 32)
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Complex128:
|
2023-02-06 22:36:24 +03:00
|
|
|
getComplex(w.s, v.Complex(), 64)
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Slice:
|
|
|
|
if v.IsNil() {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(nilAngleBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case reflect.Array:
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(openBraceBytes)
|
|
|
|
w.depth++
|
2022-10-11 00:30:42 +03:00
|
|
|
numEntries := v.Len()
|
|
|
|
for i := 0; i < numEntries; i++ {
|
|
|
|
if i > 0 {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(commaBytes)
|
|
|
|
_, _ = w.s.Write(spaceBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
w.ignoreNextType = true
|
|
|
|
w.format(w.unpackValue(v.Index(i)))
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
w.depth--
|
|
|
|
_, _ = w.s.Write(closeBraceBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.String:
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write([]byte(`"` + v.String() + `"`))
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Interface:
|
|
|
|
// The only time we should get here is for nil interfaces due to
|
|
|
|
// unpackValue calls.
|
|
|
|
if v.IsNil() {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(nilAngleBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
|
|
|
// Do nothing. We should never get here since pointers have already
|
|
|
|
// been handled above.
|
|
|
|
case reflect.Map:
|
|
|
|
// nil maps should be indicated as different than empty maps
|
|
|
|
if v.IsNil() {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(nilAngleBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
break
|
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(openMapBytes)
|
|
|
|
w.depth++
|
2022-10-11 00:30:42 +03:00
|
|
|
keys := v.MapKeys()
|
|
|
|
for i, key := range keys {
|
|
|
|
if i > 0 {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(spaceBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
w.ignoreNextType = true
|
|
|
|
w.format(w.unpackValue(key))
|
|
|
|
_, _ = w.s.Write(colonBytes)
|
|
|
|
w.ignoreNextType = true
|
|
|
|
w.format(w.unpackValue(v.MapIndex(key)))
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
w.depth--
|
|
|
|
_, _ = w.s.Write(closeMapBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Struct:
|
2023-02-06 22:36:24 +03:00
|
|
|
|
2022-10-11 00:30:42 +03:00
|
|
|
numFields := v.NumField()
|
2022-12-29 23:16:58 +03:00
|
|
|
numWritten := 0
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(openBraceBytes)
|
|
|
|
w.depth++
|
|
|
|
|
2022-10-11 00:30:42 +03:00
|
|
|
vt := v.Type()
|
2022-11-07 15:56:09 +03:00
|
|
|
prevSkip := false
|
2023-02-03 23:33:24 +03:00
|
|
|
|
2022-10-11 00:30:42 +03:00
|
|
|
for i := 0; i < numFields; i++ {
|
2023-02-08 14:54:37 +03:00
|
|
|
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
|
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
if w.protoWrapperType && !vt.Field(i).IsExported() {
|
2023-02-06 18:50:00 +03:00
|
|
|
prevSkip = true
|
|
|
|
continue
|
2023-02-06 22:36:24 +03:00
|
|
|
} else if w.sqlWrapperType && vt.Field(i).Name == "Valid" {
|
2023-02-06 18:50:00 +03:00
|
|
|
prevSkip = true
|
|
|
|
continue
|
|
|
|
}
|
2023-02-08 14:54:37 +03:00
|
|
|
if _, ok := vt.Field(i).Tag.Lookup("protobuf"); ok && !w.protoWrapperType {
|
|
|
|
w.protoWrapperType = true
|
|
|
|
}
|
2022-11-07 15:56:09 +03:00
|
|
|
sv, ok := vt.Field(i).Tag.Lookup("logger")
|
2023-02-06 18:50:00 +03:00
|
|
|
switch {
|
|
|
|
case ok:
|
2022-12-29 23:16:58 +03:00
|
|
|
switch sv {
|
|
|
|
case "omit":
|
2022-11-19 15:20:42 +03:00
|
|
|
prevSkip = true
|
|
|
|
continue
|
2022-12-29 23:16:58 +03:00
|
|
|
case "take":
|
|
|
|
break
|
2022-11-19 15:20:42 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
case !ok && w.opts.Tagged:
|
2023-02-08 14:54:37 +03:00
|
|
|
// skip top level untagged
|
|
|
|
if w.depth == 1 {
|
|
|
|
prevSkip = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tv, ok := w.takeMap[w.depth]; ok && !tv {
|
|
|
|
prevSkip = true
|
|
|
|
continue
|
|
|
|
}
|
2022-11-07 15:56:09 +03:00
|
|
|
}
|
2023-02-03 23:33:24 +03:00
|
|
|
|
2022-11-07 15:56:09 +03:00
|
|
|
if prevSkip {
|
|
|
|
prevSkip = false
|
|
|
|
}
|
2023-02-03 23:33:24 +03:00
|
|
|
|
|
|
|
if numWritten > 0 {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(commaBytes)
|
|
|
|
_, _ = w.s.Write(spaceBytes)
|
2023-02-03 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
vt := vt.Field(i)
|
|
|
|
if w.s.Flag('+') || w.s.Flag('#') {
|
|
|
|
_, _ = w.s.Write([]byte(vt.Name))
|
|
|
|
_, _ = w.s.Write(colonBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-08 14:54:37 +03:00
|
|
|
w.format(w.unpackValue(v.Field(i)))
|
2022-12-29 23:16:58 +03:00
|
|
|
numWritten++
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
w.depth--
|
|
|
|
|
|
|
|
if numWritten == 0 && w.depth < 0 {
|
|
|
|
_, _ = w.s.Write(filteredBytes)
|
2022-12-29 23:16:58 +03:00
|
|
|
}
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = w.s.Write(closeBraceBytes)
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.Uintptr:
|
2023-02-06 22:36:24 +03:00
|
|
|
getHexPtr(w.s, uintptr(v.Uint()))
|
2022-10-11 00:30:42 +03:00
|
|
|
case reflect.UnsafePointer, reflect.Chan, reflect.Func:
|
2023-02-06 22:36:24 +03:00
|
|
|
getHexPtr(w.s, v.Pointer())
|
2022-10-11 00:30:42 +03:00
|
|
|
// There were not any other types at the time this code was written, but
|
|
|
|
// fall back to letting the default fmt package handle it if any get added.
|
|
|
|
default:
|
2023-02-06 22:36:24 +03:00
|
|
|
format := w.buildDefaultFormat()
|
2022-10-11 00:30:42 +03:00
|
|
|
if v.CanInterface() {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = fmt.Fprintf(w.s, format, v.Interface())
|
2022-10-11 00:30:42 +03:00
|
|
|
} else {
|
2023-02-06 22:36:24 +03:00
|
|
|
_, _ = fmt.Fprintf(w.s, format, v.String())
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
func (w *Wrapper) Format(s fmt.State, verb rune) {
|
|
|
|
w.s = s
|
2022-10-11 00:30:42 +03:00
|
|
|
|
|
|
|
// Use standard formatting for verbs that are not v.
|
|
|
|
if verb != 'v' {
|
2023-02-06 22:36:24 +03:00
|
|
|
format := w.constructOrigFormat(verb)
|
|
|
|
_, _ = fmt.Fprintf(s, format, w.val)
|
2022-10-11 00:30:42 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
if w.val == nil {
|
2022-10-11 00:30:42 +03:00
|
|
|
if s.Flag('#') {
|
|
|
|
_, _ = s.Write(interfaceBytes)
|
|
|
|
}
|
|
|
|
_, _ = s.Write(nilAngleBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-08 14:54:37 +03:00
|
|
|
if w.opts.Tagged {
|
|
|
|
w.buildTakeMap(reflect.ValueOf(w.val), 1)
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
w.format(reflect.ValueOf(w.val))
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle special methods like error.Error() or fmt.Stringer interface
|
|
|
|
func handleMethods(_ *Options, w io.Writer, v reflect.Value) (handled bool) {
|
|
|
|
if !v.CanInterface() {
|
|
|
|
// not our case
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !v.CanAddr() {
|
|
|
|
// not our case
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.CanAddr() {
|
|
|
|
v = v.Addr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is it an error or Stringer?
|
|
|
|
switch iface := v.Interface().(type) {
|
|
|
|
case error:
|
|
|
|
defer catchPanic(w, v)
|
|
|
|
_, _ = w.Write([]byte(iface.Error()))
|
|
|
|
return true
|
|
|
|
case fmt.Stringer:
|
|
|
|
defer catchPanic(w, v)
|
|
|
|
_, _ = w.Write([]byte(iface.String()))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBool outputs a boolean value as true or false to Writer w.
|
|
|
|
func getBool(w io.Writer, val bool) {
|
|
|
|
if val {
|
|
|
|
_, _ = w.Write(trueBytes)
|
|
|
|
} else {
|
|
|
|
_, _ = w.Write(falseBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getInt outputs a signed integer value to Writer w.
|
|
|
|
func getInt(w io.Writer, val int64, base int) {
|
|
|
|
_, _ = w.Write([]byte(strconv.FormatInt(val, base)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// getUint outputs an unsigned integer value to Writer w.
|
|
|
|
func getUint(w io.Writer, val uint64, base int) {
|
|
|
|
_, _ = w.Write([]byte(strconv.FormatUint(val, base)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFloat outputs a floating point value using the specified precision,
|
|
|
|
// which is expected to be 32 or 64bit, to Writer w.
|
|
|
|
func getFloat(w io.Writer, val float64, precision int) {
|
|
|
|
_, _ = w.Write([]byte(strconv.FormatFloat(val, 'g', -1, precision)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// getComplex outputs a complex value using the specified float precision
|
|
|
|
// for the real and imaginary parts to Writer w.
|
|
|
|
func getComplex(w io.Writer, c complex128, floatPrecision int) {
|
|
|
|
r := real(c)
|
|
|
|
_, _ = w.Write(openParenBytes)
|
|
|
|
_, _ = w.Write([]byte(strconv.FormatFloat(r, 'g', -1, floatPrecision)))
|
|
|
|
i := imag(c)
|
|
|
|
if i >= 0 {
|
|
|
|
_, _ = w.Write(plusBytes)
|
|
|
|
}
|
|
|
|
_, _ = w.Write([]byte(strconv.FormatFloat(i, 'g', -1, floatPrecision)))
|
|
|
|
_, _ = w.Write(iBytes)
|
|
|
|
_, _ = w.Write(closeParenBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getHexPtr outputs a uintptr formatted as hexadecimal with a leading '0x'
|
|
|
|
// prefix to Writer w.
|
|
|
|
func getHexPtr(w io.Writer, p uintptr) {
|
|
|
|
// Null pointer.
|
|
|
|
num := uint64(p)
|
|
|
|
if num == 0 {
|
|
|
|
_, _ = w.Write(nilAngleBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Max uint64 is 16 bytes in hex + 2 bytes for '0x' prefix
|
|
|
|
buf := make([]byte, 18)
|
|
|
|
|
|
|
|
// It's simpler to construct the hex string right to left.
|
|
|
|
base := uint64(16)
|
|
|
|
i := len(buf) - 1
|
|
|
|
for num >= base {
|
|
|
|
buf[i] = hexDigits[num%base]
|
|
|
|
num /= base
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
buf[i] = hexDigits[num]
|
|
|
|
|
|
|
|
// Add '0x' prefix.
|
|
|
|
i--
|
|
|
|
buf[i] = 'x'
|
|
|
|
i--
|
|
|
|
buf[i] = '0'
|
|
|
|
|
|
|
|
// Strip unused leading bytes.
|
|
|
|
buf = buf[i:]
|
|
|
|
_, _ = w.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func catchPanic(w io.Writer, _ reflect.Value) {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
_, _ = w.Write(panicBytes)
|
|
|
|
_, _ = fmt.Fprintf(w, "%v", err)
|
|
|
|
_, _ = w.Write(closeParenBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
func (w *Wrapper) buildDefaultFormat() (format string) {
|
2022-10-11 00:30:42 +03:00
|
|
|
buf := bytes.NewBuffer(percentBytes)
|
|
|
|
|
|
|
|
for _, flag := range sf {
|
2023-02-06 22:36:24 +03:00
|
|
|
if w.s.Flag(int(flag)) {
|
2022-10-11 00:30:42 +03:00
|
|
|
_, _ = buf.WriteRune(flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = buf.WriteRune('v')
|
|
|
|
|
|
|
|
format = buf.String()
|
|
|
|
return format
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
func (w *Wrapper) constructOrigFormat(verb rune) string {
|
2022-10-11 00:30:42 +03:00
|
|
|
buf := bytes.NewBuffer(percentBytes)
|
|
|
|
|
|
|
|
for _, flag := range sf {
|
2023-02-06 22:36:24 +03:00
|
|
|
if w.s.Flag(int(flag)) {
|
2022-10-11 00:30:42 +03:00
|
|
|
_, _ = buf.WriteRune(flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
if width, ok := w.s.Width(); ok {
|
2022-10-11 00:30:42 +03:00
|
|
|
_, _ = buf.WriteString(strconv.Itoa(width))
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
if precision, ok := w.s.Precision(); ok {
|
2022-10-11 00:30:42 +03:00
|
|
|
_, _ = buf.Write(precisionBytes)
|
|
|
|
_, _ = buf.WriteString(strconv.Itoa(precision))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = buf.WriteRune(verb)
|
|
|
|
|
2023-02-06 22:36:24 +03:00
|
|
|
return buf.String()
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2023-02-06 18:50:00 +03:00
|
|
|
|
2023-02-08 14:54:37 +03:00
|
|
|
func (w *Wrapper) buildTakeMap(v reflect.Value, depth int) {
|
2023-02-06 22:36:24 +03:00
|
|
|
if !v.IsValid() || v.IsZero() {
|
|
|
|
return
|
|
|
|
}
|
2023-02-08 14:54:37 +03:00
|
|
|
|
2023-02-06 18:50:00 +03:00
|
|
|
switch v.Kind() {
|
2023-02-08 14:54:37 +03:00
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
w.buildTakeMap(v.Index(i), depth+1)
|
|
|
|
}
|
|
|
|
w.takeMap[depth] = true
|
|
|
|
return
|
2023-02-06 18:50:00 +03:00
|
|
|
case reflect.Struct:
|
|
|
|
break
|
2023-02-06 18:53:27 +03:00
|
|
|
case reflect.Ptr:
|
2023-02-06 18:50:00 +03:00
|
|
|
v = v.Elem()
|
|
|
|
if v.Kind() != reflect.Struct {
|
2023-02-08 14:54:37 +03:00
|
|
|
w.takeMap[depth] = true
|
2023-02-06 22:36:24 +03:00
|
|
|
return
|
2023-02-06 18:50:00 +03:00
|
|
|
}
|
|
|
|
default:
|
2023-02-08 14:54:37 +03:00
|
|
|
w.takeMap[depth] = true
|
2023-02-06 22:36:24 +03:00
|
|
|
return
|
2023-02-06 18:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
vt := v.Type()
|
|
|
|
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
sv, ok := vt.Field(i).Tag.Lookup("logger")
|
|
|
|
if ok && sv == "take" {
|
2023-02-08 14:54:37 +03:00
|
|
|
w.takeMap[depth] = false
|
2023-02-06 18:50:00 +03:00
|
|
|
}
|
2023-02-08 14:54:37 +03:00
|
|
|
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
|
2023-02-06 18:50:00 +03:00
|
|
|
}
|
|
|
|
}
|