Skip to content

Commit

Permalink
Make stacktrace colorization configurable (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
komuw authored Mar 2, 2024
1 parent 675d15d commit e0d993a
Show file tree
Hide file tree
Showing 85 changed files with 380 additions and 279 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Most recent version is listed first.

# v0.0.23
- Make stacktrace colorization configurable: https://github.com/komuw/kama/pull/76

# v0.0.22
- Bugfix

Expand Down
5 changes: 0 additions & 5 deletions diff.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package diff provides basic text comparison (like Unix's diff(1)).
package kama

import (
Expand Down
66 changes: 33 additions & 33 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"golang.org/x/exp/slices"
)

func dump(val reflect.Value, hideZeroValues bool, indentLevel int) string {
func (va vari) dump(val reflect.Value, hideZeroValues bool, indentLevel int) string {
/*
`compact` indicates whether the struct should be laid in one line or not
`hideZeroValues` indicates whether to show zeroValued vars
Expand Down Expand Up @@ -76,7 +76,7 @@ func dump(val reflect.Value, hideZeroValues bool, indentLevel int) string {
case reflect.Invalid:
return "<invalid>"
case reflect.String:
return dumpString(val)
return va.dumpString(val)
case reflect.Int,
reflect.Int8,
reflect.Int16,
Expand All @@ -90,7 +90,7 @@ func dump(val reflect.Value, hideZeroValues bool, indentLevel int) string {
reflect.Float32,
reflect.Float64,
reflect.Uintptr:
return dumpNumbers(val)
return va.dumpNumbers(val)
case reflect.Struct:
// We used to use `sanity-io/litter` to do dumping.
// We however, decided to implement our own dump functionality.
Expand All @@ -100,49 +100,49 @@ func dump(val reflect.Value, hideZeroValues bool, indentLevel int) string {
// This logic can be discarded if sanity-io/litter implements similar.
// see: https://github.com/sanity-io/litter/pull/43
fromPtr := false
return dumpStruct(val, fromPtr, hideZeroValues, indentLevel)
return va.dumpStruct(val, fromPtr, hideZeroValues, indentLevel)
case reflect.Ptr:
v := val.Elem()
if v.IsValid() {
if v.Type().Kind() == reflect.Struct {
fromPtr := true
// TODO: should we pass in `val`, itself instead of `v`
// that way `val.Pointer()` would happen inside `dumpStruct`
return dumpStruct(v, fromPtr, hideZeroValues, indentLevel)
return va.dumpStruct(v, fromPtr, hideZeroValues, indentLevel)
} else {
return dumpNonStructPointer(v, hideZeroValues, indentLevel)
return va.dumpNonStructPointer(v, hideZeroValues, indentLevel)
}
} else {
// `v.IsValid()` returns false if v is the zero Value.
// If `IsValid` returns false, all other methods except String panic.
return val.Type().String() + "(nil)"
}
case reflect.Array, reflect.Slice:
return dumpSlice(val, hideZeroValues, indentLevel)
return va.dumpSlice(val, hideZeroValues, indentLevel)
case reflect.Chan:
return dumpChan(val)
return va.dumpChan(val)
case reflect.Map:
return dumpMap(val, hideZeroValues, indentLevel)
return va.dumpMap(val, hideZeroValues, indentLevel)
case reflect.Bool:
return fmt.Sprint(val)
case reflect.Func:
return dumpFunc(val)
return va.dumpFunc(val)
case reflect.Complex64, reflect.Complex128:
return dumpComplexNum(val)
return va.dumpComplexNum(val)
case reflect.UnsafePointer:
// It is not generally safe to do anything with an unsafe.Pointer
// see: https://golang.org/pkg/unsafe/#Pointer
// so we probably want to leave it as is.
// do note that if we wanted we could get a uintptr via `val.Pointer()`
return "unsafe.Pointer"
case reflect.Interface:
return dumpInterface(val)
return va.dumpInterface(val)
default:
return fmt.Sprintf("%v NotImplemented", iTypeKind)
}
}

func dumpString(v reflect.Value) string {
func (va vari) dumpString(v reflect.Value) string {
// dumps strings

adder := 1 // this is a custom string type.
Expand All @@ -155,7 +155,7 @@ func dumpString(v reflect.Value) string {
}
newLineCount := strings.Count(fmt.Sprintf("%s", v), "\n")

constraint := int(math.Min(float64(numEntries), float64(cfg.MaxLength+50))) + adder + newLineCount
constraint := int(math.Min(float64(numEntries), float64(va.cfg.MaxLength+50))) + adder + newLineCount

s := fmt.Sprintf("%#v", v)[:constraint]

Expand All @@ -170,7 +170,7 @@ func dumpString(v reflect.Value) string {
return s
}

func dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int) string {
func (va vari) dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int) string {
/*
`fromPtr` indicates whether the struct is a value or a pointer; `T{}` vs `&T{}`
`compact` indicates whether the struct should be laid in one line or not
Expand All @@ -182,8 +182,8 @@ func dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int)
typeName = "&" + typeName
}

if indentLevel > cfg.MaxIndentLevel {
return fmt.Sprintf("%v: kama warning(indentation `%d` exceeds max of `%d`. Possible circular reference)", typeName, indentLevel, cfg.MaxIndentLevel)
if indentLevel > va.cfg.MaxIndentLevel {
return fmt.Sprintf("%v: kama warning(indentation `%d` exceeds max of `%d`. Possible circular reference)", typeName, indentLevel, va.cfg.MaxIndentLevel)
}

sep := "\n"
Expand All @@ -197,7 +197,7 @@ func dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int)
for i := 0; i < numFields; i++ {
vtf := vt.Field(i)
fieldd := v.Field(i)
if unicode.IsUpper(rune(vtf.Name[0])) || cfg.ShowPrivateFields {
if unicode.IsUpper(rune(vtf.Name[0])) || va.cfg.ShowPrivateFields {
// Only dump public fields, unless the config option for private is turned on.

if hideZeroValues && isZeroValue(fieldd) {
Expand All @@ -207,7 +207,7 @@ func dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int)
cpt := true
_ = cpt
hzv := true
val := dump(fieldd, hzv, indentLevel)
val := va.dump(fieldd, hzv, indentLevel)
s = s + fieldNameSep + vtf.Name + ": " + val + fmt.Sprintf(",%s", sep)
}
}
Expand All @@ -223,7 +223,7 @@ func dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int)
return s
}

func dumpSlice(v reflect.Value, hideZeroValues bool, indentLevel int) string {
func (va vari) dumpSlice(v reflect.Value, hideZeroValues bool, indentLevel int) string {
// dumps slices & arrays

// In future(if we ever add compation) we could restrict compaction only to arrays/slices/maps that are of primitive(basic) types
Expand All @@ -232,7 +232,7 @@ func dumpSlice(v reflect.Value, hideZeroValues bool, indentLevel int) string {
// 2. https://github.com/komuw/kama/pull/28

numEntries := v.Len()
constraint := int(math.Min(float64(numEntries), float64(cfg.MaxLength)))
constraint := int(math.Min(float64(numEntries), float64(va.cfg.MaxLength)))
typeName := v.Type().String()

newline := "\n"
Expand All @@ -245,7 +245,7 @@ func dumpSlice(v reflect.Value, hideZeroValues bool, indentLevel int) string {
s := typeName + "{" + newline
for i := 0; i < constraint; i++ {
elm := v.Index(i)
s = s + leftSep + dump(elm, hideZeroValues, indentLevel) + "," + newline
s = s + leftSep + va.dump(elm, hideZeroValues, indentLevel) + "," + newline
}
if numEntries > constraint {
remainder := numEntries - constraint
Expand All @@ -264,7 +264,7 @@ func dumpSlice(v reflect.Value, hideZeroValues bool, indentLevel int) string {
return s
}

func dumpMap(v reflect.Value, hideZeroValues bool, indentLevel int) string {
func (va vari) dumpMap(v reflect.Value, hideZeroValues bool, indentLevel int) string {
// dumps maps

// In future(if we ever add compation) we could restrict compaction only to arrays/slices/maps that are of primitive(basic) types
Expand All @@ -273,7 +273,7 @@ func dumpMap(v reflect.Value, hideZeroValues bool, indentLevel int) string {
// 2. https://github.com/komuw/kama/pull/28

numEntries := v.Len()
constraint := int(math.Min(float64(numEntries), float64(cfg.MaxLength)))
constraint := int(math.Min(float64(numEntries), float64(va.cfg.MaxLength)))
typeName := v.Type().String()

newline := "\n"
Expand All @@ -287,13 +287,13 @@ func dumpMap(v reflect.Value, hideZeroValues bool, indentLevel int) string {
sort.Slice(keys,
func(i, j int) bool {
// it's unfortunate that we have to dump twice. In this func and in the `for range` below.
return dump(keys[i], hideZeroValues, indentLevel) < dump(keys[j], hideZeroValues, indentLevel)
return va.dump(keys[i], hideZeroValues, indentLevel) < va.dump(keys[j], hideZeroValues, indentLevel)
},
)
for count, key := range keys {
mapKey := key
mapVal := v.MapIndex(key)
s = s + leftSep + dump(mapKey, hideZeroValues, indentLevel) + ":" + colonSep + dump(mapVal, hideZeroValues, indentLevel) + ", " + newline
s = s + leftSep + va.dump(mapKey, hideZeroValues, indentLevel) + ":" + colonSep + va.dump(mapVal, hideZeroValues, indentLevel) + ", " + newline
if count > constraint {
remainder := numEntries - constraint
s = s + fmt.Sprintf("%s...<%d more redacted>..", leftSep, remainder)
Expand All @@ -313,7 +313,7 @@ func dumpMap(v reflect.Value, hideZeroValues bool, indentLevel int) string {
return s
}

func dumpChan(v reflect.Value) string {
func (va vari) dumpChan(v reflect.Value) string {
// dumps channels
cap := v.Cap()
len := v.Len()
Expand All @@ -322,7 +322,7 @@ func dumpChan(v reflect.Value) string {
return fmt.Sprintf("%v %v (len=%d, cap=%d)", direction, element, len, cap)
}

func dumpFunc(v reflect.Value) string {
func (va vari) dumpFunc(v reflect.Value) string {
// dumps functions

vType := v.Type()
Expand Down Expand Up @@ -357,7 +357,7 @@ func dumpFunc(v reflect.Value) string {
return typeName
}

func dumpComplexNum(v reflect.Value) string {
func (va vari) dumpComplexNum(v reflect.Value) string {
// dumps complex64 and complex128 numbers
bits := v.Type().Bits()
cmp := v.Complex() // returns complex128 even for `reflect.Complex64`
Expand All @@ -367,21 +367,21 @@ func dumpComplexNum(v reflect.Value) string {
return fmt.Sprintf("complex128%v", cmp)
}

func dumpNonStructPointer(v reflect.Value, hideZeroValues bool, indentLevel int) string {
func (va vari) dumpNonStructPointer(v reflect.Value, hideZeroValues bool, indentLevel int) string {
// dumps pointer types other than struct.
// ie; someIntEight := int8(14); kama.Dirp(&someIntEight)
// dumping for struct pointers is handled in `dumpStruct()`

pref := "&"
s := dump(v, hideZeroValues, indentLevel)
s := va.dump(v, hideZeroValues, indentLevel)

if strings.HasPrefix(s, pref) {
return s
}
return pref + s
}

func dumpNumbers(v reflect.Value) string {
func (va vari) dumpNumbers(v reflect.Value) string {
// dumps numbers.

iType := v.Type()
Expand Down Expand Up @@ -412,7 +412,7 @@ func dumpNumbers(v reflect.Value) string {
}
}

func dumpInterface(v reflect.Value) string {
func (va vari) dumpInterface(v reflect.Value) string {
// dump interface

name := v.Type().String() // eg; `io.Reader` or `error`
Expand Down
8 changes: 5 additions & 3 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ func dealWithTestData(t *testing.T, path, gotContent string) {

writeData := os.Getenv(kamaWriteDataForTests) != ""
if writeData {
attest.Ok(t,
os.WriteFile(path, []byte(gotContent), 0o644),
)
errM := os.MkdirAll(filepath.Dir(p), 0o755)
attest.Ok(t, errM)

err := os.WriteFile(path, []byte(gotContent), 0o644)
attest.Ok(t, err)
t.Logf("\n\t written testdata to %s\n", path)
return
}
Expand Down
Loading

0 comments on commit e0d993a

Please sign in to comment.