Bumps the minor-and-patch group with 18 updates: | Package | From | To | | --- | --- | --- | | [github.com/blevesearch/bleve/v2](https://github.com/blevesearch/bleve) | `2.5.7` | `2.6.0` | | [github.com/davidbyttow/govips/v2](https://github.com/davidbyttow/govips) | `2.16.0` | `2.18.0` | | [github.com/go-chi/chi/v5](https://github.com/go-chi/chi) | `5.2.5` | `5.3.0` | | [github.com/gookit/config/v2](https://github.com/gookit/config) | `2.2.7` | `2.2.8` | | [github.com/grpc-ecosystem/grpc-gateway/v2](https://github.com/grpc-ecosystem/grpc-gateway) | `2.28.0` | `2.29.0` | | [github.com/kovidgoyal/imaging](https://github.com/kovidgoyal/imaging) | `1.8.19` | `1.8.21` | | [github.com/libregraph/lico](https://github.com/libregraph/lico) | `0.66.0` | `0.67.0` | | [github.com/nats-io/nats-server/v2](https://github.com/nats-io/nats-server) | `2.14.0` | `2.14.1` | | [github.com/nats-io/nats.go](https://github.com/nats-io/nats.go) | `1.51.0` | `1.52.0` | | [github.com/olekukonko/tablewriter](https://github.com/olekukonko/tablewriter) | `1.1.0` | `1.1.4` | | [github.com/onsi/gomega](https://github.com/onsi/gomega) | `1.40.0` | `1.41.0` | | [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) | `1.12.3` | `1.16.2` | | [github.com/tidwall/gjson](https://github.com/tidwall/gjson) | `1.18.0` | `1.19.0` | | [go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp](https://github.com/open-telemetry/opentelemetry-go-contrib) | `0.66.0` | `0.68.0` | | [go.opentelemetry.io/contrib/zpages](https://github.com/open-telemetry/opentelemetry-go-contrib) | `0.64.0` | `0.68.0` | | [golang.org/x/crypto](https://github.com/golang/crypto) | `0.51.0` | `0.52.0` | | [golang.org/x/image](https://github.com/golang/image) | `0.39.0` | `0.41.0` | | [google.golang.org/genproto/googleapis/api](https://github.com/googleapis/go-genproto) | `0.0.0-20260401024825-9d38bb4040a9` | `0.0.0-20260414002931-afd174a4e478` | Updates `github.com/blevesearch/bleve/v2` from 2.5.7 to 2.6.0 - [Release notes](https://github.com/blevesearch/bleve/releases) - [Commits](https://github.com/blevesearch/bleve/compare/v2.5.7...v2.6.0) Updates `github.com/davidbyttow/govips/v2` from 2.16.0 to 2.18.0 - [Release notes](https://github.com/davidbyttow/govips/releases) - [Commits](https://github.com/davidbyttow/govips/compare/v2.16.0...v2.18.0) Updates `github.com/go-chi/chi/v5` from 5.2.5 to 5.3.0 - [Release notes](https://github.com/go-chi/chi/releases) - [Changelog](https://github.com/go-chi/chi/blob/master/CHANGELOG.md) - [Commits](https://github.com/go-chi/chi/compare/v5.2.5...v5.3.0) Updates `github.com/gookit/config/v2` from 2.2.7 to 2.2.8 - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.7...v2.2.8) Updates `github.com/grpc-ecosystem/grpc-gateway/v2` from 2.28.0 to 2.29.0 - [Release notes](https://github.com/grpc-ecosystem/grpc-gateway/releases) - [Commits](https://github.com/grpc-ecosystem/grpc-gateway/compare/v2.28.0...v2.29.0) Updates `github.com/kovidgoyal/imaging` from 1.8.19 to 1.8.21 - [Release notes](https://github.com/kovidgoyal/imaging/releases) - [Commits](https://github.com/kovidgoyal/imaging/compare/v1.8.19...v1.8.21) Updates `github.com/libregraph/lico` from 0.66.0 to 0.67.0 - [Changelog](https://github.com/libregraph/lico/blob/master/CHANGELOG.md) - [Commits](https://github.com/libregraph/lico/compare/v0.66.0...v0.67.0) Updates `github.com/nats-io/nats-server/v2` from 2.14.0 to 2.14.1 - [Release notes](https://github.com/nats-io/nats-server/releases) - [Changelog](https://github.com/nats-io/nats-server/blob/main/RELEASES.md) - [Commits](https://github.com/nats-io/nats-server/compare/v2.14.0...v2.14.1) Updates `github.com/nats-io/nats.go` from 1.51.0 to 1.52.0 - [Release notes](https://github.com/nats-io/nats.go/releases) - [Commits](https://github.com/nats-io/nats.go/compare/v1.51.0...v1.52.0) Updates `github.com/olekukonko/tablewriter` from 1.1.0 to 1.1.4 - [Release notes](https://github.com/olekukonko/tablewriter/releases) - [Commits](https://github.com/olekukonko/tablewriter/compare/v1.1.0...v1.1.4) Updates `github.com/onsi/gomega` from 1.40.0 to 1.41.0 - [Release notes](https://github.com/onsi/gomega/releases) - [Changelog](https://github.com/onsi/gomega/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/gomega/compare/v1.40.0...v1.41.0) Updates `github.com/open-policy-agent/opa` from 1.12.3 to 1.16.2 - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v1.12.3...v1.16.2) Updates `github.com/tidwall/gjson` from 1.18.0 to 1.19.0 - [Commits](https://github.com/tidwall/gjson/compare/v1.18.0...v1.19.0) Updates `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` from 0.66.0 to 0.68.0 - [Release notes](https://github.com/open-telemetry/opentelemetry-go-contrib/releases) - [Changelog](https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-telemetry/opentelemetry-go-contrib/compare/zpages/v0.66.0...zpages/v0.68.0) Updates `go.opentelemetry.io/contrib/zpages` from 0.64.0 to 0.68.0 - [Release notes](https://github.com/open-telemetry/opentelemetry-go-contrib/releases) - [Changelog](https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-telemetry/opentelemetry-go-contrib/compare/zpages/v0.64.0...zpages/v0.68.0) Updates `golang.org/x/crypto` from 0.51.0 to 0.52.0 - [Commits](https://github.com/golang/crypto/compare/v0.51.0...v0.52.0) Updates `golang.org/x/image` from 0.39.0 to 0.41.0 - [Commits](https://github.com/golang/image/compare/v0.39.0...v0.41.0) Updates `google.golang.org/genproto/googleapis/api` from 0.0.0-20260401024825-9d38bb4040a9 to 0.0.0-20260414002931-afd174a4e478 - [Commits](https://github.com/googleapis/go-genproto/commits) --- updated-dependencies: - dependency-name: github.com/blevesearch/bleve/v2 dependency-version: 2.6.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/davidbyttow/govips/v2 dependency-version: 2.18.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/go-chi/chi/v5 dependency-version: 5.3.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/gookit/config/v2 dependency-version: 2.2.8 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/grpc-ecosystem/grpc-gateway/v2 dependency-version: 2.29.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/kovidgoyal/imaging dependency-version: 1.8.21 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/libregraph/lico dependency-version: 0.67.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats-server/v2 dependency-version: 2.14.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats.go dependency-version: 1.52.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/olekukonko/tablewriter dependency-version: 1.1.4 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/onsi/gomega dependency-version: 1.41.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/open-policy-agent/opa dependency-version: 1.16.2 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/tidwall/gjson dependency-version: 1.19.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp dependency-version: 0.68.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: go.opentelemetry.io/contrib/zpages dependency-version: 0.68.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: golang.org/x/crypto dependency-version: 0.52.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: golang.org/x/image dependency-version: 0.41.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: google.golang.org/genproto/googleapis/api dependency-version: 0.0.0-20260414002931-afd174a4e478 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
512 lines
15 KiB
Go
512 lines
15 KiB
Go
/*
|
|
Gomega's format package pretty-prints objects. It explores input objects recursively and generates formatted, indented output with type information.
|
|
*/
|
|
|
|
// untested sections: 4
|
|
|
|
package format
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Use MaxDepth to set the maximum recursion depth when printing deeply nested objects
|
|
var MaxDepth = uint(10)
|
|
|
|
// MaxLength of the string representation of an object.
|
|
// If MaxLength is set to 0, the Object will not be truncated.
|
|
var MaxLength = 4000
|
|
|
|
/*
|
|
By default, all objects (even those that implement fmt.Stringer and fmt.GoStringer) are recursively inspected to generate output.
|
|
|
|
Set UseStringerRepresentation = true to use GoString (for fmt.GoStringers) or String (for fmt.Stringer) instead.
|
|
|
|
Note that GoString and String don't always have all the information you need to understand why a test failed!
|
|
*/
|
|
var UseStringerRepresentation = false
|
|
|
|
/*
|
|
Print the content of context objects. By default it will be suppressed.
|
|
|
|
Set PrintContextObjects = true to enable printing of the context internals.
|
|
*/
|
|
var PrintContextObjects = false
|
|
|
|
// TruncatedDiff choose if we should display a truncated pretty diff or not
|
|
var TruncatedDiff = true
|
|
|
|
// TruncateThreshold (default 50) specifies the maximum length string to print in string comparison assertion error
|
|
// messages.
|
|
var TruncateThreshold uint = 50
|
|
|
|
// CharactersAroundMismatchToInclude (default 5) specifies how many contextual characters should be printed before and
|
|
// after the first diff location in a truncated string assertion error message.
|
|
var CharactersAroundMismatchToInclude uint = 5
|
|
|
|
var contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
|
|
var timeType = reflect.TypeOf(time.Time{})
|
|
|
|
// The default indentation string emitted by the format package
|
|
var Indent = " "
|
|
|
|
var longFormThreshold = 20
|
|
|
|
// GomegaStringer allows for custom formatting of objects for gomega.
|
|
type GomegaStringer interface {
|
|
// GomegaString will be used to custom format an object.
|
|
// It does not follow UseStringerRepresentation value and will always be called regardless.
|
|
// It also ignores the MaxLength value.
|
|
GomegaString() string
|
|
}
|
|
|
|
/*
|
|
CustomFormatters can be registered with Gomega via RegisterCustomFormatter()
|
|
Any value to be rendered by Gomega is passed to each registered CustomFormatters.
|
|
The CustomFormatter signals that it will handle formatting the value by returning (formatted-string, true)
|
|
If the CustomFormatter does not want to handle the object it should return ("", false)
|
|
|
|
Strings returned by CustomFormatters are not truncated
|
|
*/
|
|
type CustomFormatter func(value any) (string, bool)
|
|
type CustomFormatterKey uint
|
|
|
|
var customFormatterKey CustomFormatterKey = 1
|
|
|
|
type customFormatterKeyPair struct {
|
|
CustomFormatter
|
|
CustomFormatterKey
|
|
}
|
|
|
|
/*
|
|
RegisterCustomFormatter registers a CustomFormatter and returns a CustomFormatterKey
|
|
|
|
You can call UnregisterCustomFormatter with the returned key to unregister the associated CustomFormatter
|
|
*/
|
|
func RegisterCustomFormatter(customFormatter CustomFormatter) CustomFormatterKey {
|
|
key := customFormatterKey
|
|
customFormatterKey += 1
|
|
customFormatters = append(customFormatters, customFormatterKeyPair{customFormatter, key})
|
|
return key
|
|
}
|
|
|
|
/*
|
|
UnregisterCustomFormatter unregisters a previously registered CustomFormatter. You should pass in the key returned by RegisterCustomFormatter
|
|
*/
|
|
func UnregisterCustomFormatter(key CustomFormatterKey) {
|
|
formatters := []customFormatterKeyPair{}
|
|
for _, f := range customFormatters {
|
|
if f.CustomFormatterKey == key {
|
|
continue
|
|
}
|
|
formatters = append(formatters, f)
|
|
}
|
|
customFormatters = formatters
|
|
}
|
|
|
|
var customFormatters = []customFormatterKeyPair{}
|
|
|
|
/*
|
|
Generates a formatted matcher success/failure message of the form:
|
|
|
|
Expected
|
|
<pretty printed actual>
|
|
<message>
|
|
<pretty printed expected>
|
|
|
|
If expected is omitted, then the message looks like:
|
|
|
|
Expected
|
|
<pretty printed actual>
|
|
<message>
|
|
*/
|
|
func Message(actual any, message string, expected ...any) string {
|
|
if len(expected) == 0 {
|
|
return fmt.Sprintf("Expected\n%s\n%s", Object(actual, 1), message)
|
|
}
|
|
return fmt.Sprintf("Expected\n%s\n%s\n%s", Object(actual, 1), message, Object(expected[0], 1))
|
|
}
|
|
|
|
/*
|
|
|
|
Generates a nicely formatted matcher success / failure message
|
|
|
|
Much like Message(...), but it attempts to pretty print diffs in strings
|
|
|
|
Expected
|
|
<string>: "...aaaaabaaaaa..."
|
|
to equal |
|
|
<string>: "...aaaaazaaaaa..."
|
|
|
|
*/
|
|
|
|
func MessageWithDiff(actual, message, expected string) string {
|
|
if TruncatedDiff && len(actual) >= int(TruncateThreshold) && len(expected) >= int(TruncateThreshold) {
|
|
diffPoint := findFirstMismatch(actual, expected)
|
|
formattedActual := truncateAndFormat(actual, diffPoint)
|
|
formattedExpected := truncateAndFormat(expected, diffPoint)
|
|
|
|
spacesBeforeFormattedMismatch := findFirstMismatch(formattedActual, formattedExpected)
|
|
|
|
tabLength := 4
|
|
spaceFromMessageToActual := tabLength + len("<string>: ") - len(message)
|
|
|
|
paddingCount := spaceFromMessageToActual + spacesBeforeFormattedMismatch
|
|
if paddingCount < 0 {
|
|
return Message(formattedActual, message, formattedExpected)
|
|
}
|
|
|
|
padding := strings.Repeat(" ", paddingCount) + "|"
|
|
return Message(formattedActual, message+padding, formattedExpected)
|
|
}
|
|
|
|
actual = escapedWithGoSyntax(actual)
|
|
expected = escapedWithGoSyntax(expected)
|
|
|
|
return Message(actual, message, expected)
|
|
}
|
|
|
|
func escapedWithGoSyntax(str string) string {
|
|
withQuotes := fmt.Sprintf("%q", str)
|
|
return withQuotes[1 : len(withQuotes)-1]
|
|
}
|
|
|
|
func truncateAndFormat(str string, index int) string {
|
|
leftPadding := `...`
|
|
rightPadding := `...`
|
|
|
|
start := index - int(CharactersAroundMismatchToInclude)
|
|
if start < 0 {
|
|
start = 0
|
|
leftPadding = ""
|
|
}
|
|
|
|
// slice index must include the mis-matched character
|
|
lengthOfMismatchedCharacter := 1
|
|
end := index + int(CharactersAroundMismatchToInclude) + lengthOfMismatchedCharacter
|
|
if end > len(str) {
|
|
end = len(str)
|
|
rightPadding = ""
|
|
|
|
}
|
|
return fmt.Sprintf("\"%s\"", leftPadding+str[start:end]+rightPadding)
|
|
}
|
|
|
|
func findFirstMismatch(a, b string) int {
|
|
aSlice := strings.Split(a, "")
|
|
bSlice := strings.Split(b, "")
|
|
|
|
for index, str := range aSlice {
|
|
if index > len(bSlice)-1 {
|
|
return index
|
|
}
|
|
if str != bSlice[index] {
|
|
return index
|
|
}
|
|
}
|
|
|
|
if len(b) > len(a) {
|
|
return len(a) + 1
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
const truncateHelpText = `
|
|
Gomega truncated this representation as it exceeds 'format.MaxLength'.
|
|
Consider having the object provide a custom 'GomegaStringer' representation
|
|
or adjust the parameters in Gomega's 'format' package.
|
|
|
|
Learn more here: https://onsi.github.io/gomega/#adjusting-output
|
|
`
|
|
|
|
func truncateLongStrings(s string) string {
|
|
if MaxLength > 0 && len(s) > MaxLength {
|
|
var sb strings.Builder
|
|
for i, r := range s {
|
|
if i < MaxLength {
|
|
sb.WriteRune(r)
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
|
|
sb.WriteString("...\n")
|
|
sb.WriteString(truncateHelpText)
|
|
|
|
return sb.String()
|
|
}
|
|
return s
|
|
}
|
|
|
|
/*
|
|
Pretty prints the passed in object at the passed in indentation level.
|
|
|
|
Object recurses into deeply nested objects emitting pretty-printed representations of their components.
|
|
|
|
Modify format.MaxDepth to control how deep the recursion is allowed to go
|
|
Set format.UseStringerRepresentation to true to return object.GoString() or object.String() when available instead of
|
|
recursing into the object.
|
|
|
|
Set PrintContextObjects to true to print the content of objects implementing context.Context
|
|
*/
|
|
func Object(object any, indentation uint) string {
|
|
indent := strings.Repeat(Indent, int(indentation))
|
|
value := reflect.ValueOf(object)
|
|
commonRepresentation := ""
|
|
if err, ok := object.(error); ok && !isNilValue(value) { // isNilValue check needed here to avoid nil deref due to boxed nil
|
|
commonRepresentation += "\n" + IndentString(err.Error(), indentation) + "\n" + indent
|
|
}
|
|
return fmt.Sprintf("%s<%s>: %s%s", indent, formatType(value), commonRepresentation, formatValue(value, indentation, true, map[uintptr]struct{}{}))
|
|
}
|
|
|
|
/*
|
|
IndentString takes a string and indents each line by the specified amount.
|
|
*/
|
|
func IndentString(s string, indentation uint) string {
|
|
return indentString(s, indentation, true)
|
|
}
|
|
|
|
func indentString(s string, indentation uint, indentFirstLine bool) string {
|
|
result := &strings.Builder{}
|
|
components := strings.Split(s, "\n")
|
|
indent := strings.Repeat(Indent, int(indentation))
|
|
for i, component := range components {
|
|
if i > 0 || indentFirstLine {
|
|
result.WriteString(indent)
|
|
}
|
|
result.WriteString(component)
|
|
if i < len(components)-1 {
|
|
result.WriteString("\n")
|
|
}
|
|
}
|
|
|
|
return result.String()
|
|
}
|
|
|
|
func formatType(v reflect.Value) string {
|
|
switch v.Kind() {
|
|
case reflect.Invalid:
|
|
return "nil"
|
|
case reflect.Chan:
|
|
return fmt.Sprintf("%s | len:%d, cap:%d", v.Type(), v.Len(), v.Cap())
|
|
case reflect.Ptr:
|
|
return fmt.Sprintf("%s | 0x%x", v.Type(), v.Pointer())
|
|
case reflect.Slice:
|
|
return fmt.Sprintf("%s | len:%d, cap:%d", v.Type(), v.Len(), v.Cap())
|
|
case reflect.Map:
|
|
return fmt.Sprintf("%s | len:%d", v.Type(), v.Len())
|
|
default:
|
|
return v.Type().String()
|
|
}
|
|
}
|
|
|
|
func formatValue(value reflect.Value, indentation uint, isTopLevel bool, visited map[uintptr]struct{}) string {
|
|
if indentation > MaxDepth {
|
|
return "..."
|
|
}
|
|
|
|
if isNilValue(value) {
|
|
return "nil"
|
|
}
|
|
|
|
if value.CanInterface() {
|
|
obj := value.Interface()
|
|
|
|
// if a CustomFormatter handles this values, we'll go with that
|
|
for _, customFormatter := range customFormatters {
|
|
formatted, handled := customFormatter.CustomFormatter(obj)
|
|
// do not truncate a user-provided CustomFormatter()
|
|
if handled {
|
|
return indentString(formatted, indentation+1, false)
|
|
}
|
|
}
|
|
|
|
// GomegaStringer will take precedence to other representations and disregards UseStringerRepresentation
|
|
if x, ok := obj.(GomegaStringer); ok {
|
|
// do not truncate a user-defined GomegaString() value
|
|
return indentString(x.GomegaString(), indentation+1, false)
|
|
}
|
|
|
|
if UseStringerRepresentation {
|
|
switch x := obj.(type) {
|
|
case fmt.GoStringer:
|
|
return indentString(truncateLongStrings(x.GoString()), indentation+1, false)
|
|
case fmt.Stringer:
|
|
return indentString(truncateLongStrings(x.String()), indentation+1, false)
|
|
}
|
|
}
|
|
}
|
|
|
|
if !PrintContextObjects {
|
|
if value.Type().Implements(contextType) && indentation > 1 {
|
|
return "<suppressed context>"
|
|
}
|
|
}
|
|
|
|
switch value.Kind() {
|
|
case reflect.Bool:
|
|
return fmt.Sprintf("%v", value.Bool())
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return fmt.Sprintf("%v", value.Int())
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
return fmt.Sprintf("%v", value.Uint())
|
|
case reflect.Uintptr:
|
|
return fmt.Sprintf("0x%x", value.Uint())
|
|
case reflect.Float32, reflect.Float64:
|
|
return fmt.Sprintf("%v", value.Float())
|
|
case reflect.Complex64, reflect.Complex128:
|
|
return fmt.Sprintf("%v", value.Complex())
|
|
case reflect.Chan:
|
|
return fmt.Sprintf("0x%x", value.Pointer())
|
|
case reflect.Func:
|
|
return fmt.Sprintf("0x%x", value.Pointer())
|
|
case reflect.Ptr:
|
|
ptr := value.Pointer()
|
|
if _, ok := visited[ptr]; ok {
|
|
return fmt.Sprintf("0x%x (cyclic reference)", ptr)
|
|
}
|
|
visited[ptr] = struct{}{}
|
|
return formatValue(value.Elem(), indentation, isTopLevel, visited)
|
|
case reflect.Slice:
|
|
return truncateLongStrings(formatSlice(value, indentation, visited))
|
|
case reflect.String:
|
|
return truncateLongStrings(formatString(value.String(), indentation, isTopLevel))
|
|
case reflect.Array:
|
|
return truncateLongStrings(formatSlice(value, indentation, visited))
|
|
case reflect.Map:
|
|
return truncateLongStrings(formatMap(value, indentation, visited))
|
|
case reflect.Struct:
|
|
if value.Type() == timeType && value.CanInterface() {
|
|
t, _ := value.Interface().(time.Time)
|
|
return t.Format(time.RFC3339Nano)
|
|
}
|
|
return truncateLongStrings(formatStruct(value, indentation, visited))
|
|
case reflect.Interface:
|
|
return formatInterface(value, indentation, visited)
|
|
default:
|
|
if value.CanInterface() {
|
|
return truncateLongStrings(fmt.Sprintf("%#v", value.Interface()))
|
|
}
|
|
return truncateLongStrings(fmt.Sprintf("%#v", value))
|
|
}
|
|
}
|
|
|
|
func formatString(object any, indentation uint, isTopLevel bool) string {
|
|
if isTopLevel {
|
|
s := fmt.Sprintf("%s", object)
|
|
components := strings.Split(s, "\n")
|
|
result := ""
|
|
for i, component := range components {
|
|
if i == 0 {
|
|
result += component
|
|
} else {
|
|
result += Indent + component
|
|
}
|
|
if i < len(components)-1 {
|
|
result += "\n"
|
|
}
|
|
}
|
|
|
|
return result
|
|
} else {
|
|
return fmt.Sprintf("%q", object)
|
|
}
|
|
}
|
|
|
|
func formatSlice(v reflect.Value, indentation uint, visited map[uintptr]struct{}) string {
|
|
if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 && isPrintableString(string(v.Bytes())) {
|
|
return formatString(v.Bytes(), indentation, false)
|
|
}
|
|
|
|
l := v.Len()
|
|
result := make([]string, l)
|
|
longest := 0
|
|
for i := range l {
|
|
result[i] = formatValue(v.Index(i), indentation+1, false, visited)
|
|
if len(result[i]) > longest {
|
|
longest = len(result[i])
|
|
}
|
|
}
|
|
|
|
if longest > longFormThreshold {
|
|
indenter := strings.Repeat(Indent, int(indentation))
|
|
return fmt.Sprintf("[\n%s%s,\n%s]", indenter+Indent, strings.Join(result, ",\n"+indenter+Indent), indenter)
|
|
}
|
|
return fmt.Sprintf("[%s]", strings.Join(result, ", "))
|
|
}
|
|
|
|
func formatMap(v reflect.Value, indentation uint, visited map[uintptr]struct{}) string {
|
|
l := v.Len()
|
|
result := make([]string, l)
|
|
|
|
longest := 0
|
|
for i, key := range v.MapKeys() {
|
|
value := v.MapIndex(key)
|
|
result[i] = fmt.Sprintf("%s: %s", formatValue(key, indentation+1, false, visited), formatValue(value, indentation+1, false, visited))
|
|
if len(result[i]) > longest {
|
|
longest = len(result[i])
|
|
}
|
|
}
|
|
|
|
if longest > longFormThreshold {
|
|
indenter := strings.Repeat(Indent, int(indentation))
|
|
return fmt.Sprintf("{\n%s%s,\n%s}", indenter+Indent, strings.Join(result, ",\n"+indenter+Indent), indenter)
|
|
}
|
|
return fmt.Sprintf("{%s}", strings.Join(result, ", "))
|
|
}
|
|
|
|
func formatStruct(v reflect.Value, indentation uint, visited map[uintptr]struct{}) string {
|
|
t := v.Type()
|
|
|
|
l := v.NumField()
|
|
result := []string{}
|
|
longest := 0
|
|
for i := range l {
|
|
structField := t.Field(i)
|
|
fieldEntry := v.Field(i)
|
|
representation := fmt.Sprintf("%s: %s", structField.Name, formatValue(fieldEntry, indentation+1, false, visited))
|
|
result = append(result, representation)
|
|
if len(representation) > longest {
|
|
longest = len(representation)
|
|
}
|
|
}
|
|
if longest > longFormThreshold {
|
|
indenter := strings.Repeat(Indent, int(indentation))
|
|
return fmt.Sprintf("{\n%s%s,\n%s}", indenter+Indent, strings.Join(result, ",\n"+indenter+Indent), indenter)
|
|
}
|
|
return fmt.Sprintf("{%s}", strings.Join(result, ", "))
|
|
}
|
|
|
|
func formatInterface(v reflect.Value, indentation uint, visited map[uintptr]struct{}) string {
|
|
return fmt.Sprintf("<%s>%s", formatType(v.Elem()), formatValue(v.Elem(), indentation, false, visited))
|
|
}
|
|
|
|
func isNilValue(a reflect.Value) bool {
|
|
switch a.Kind() {
|
|
case reflect.Invalid:
|
|
return true
|
|
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
|
return a.IsNil()
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
/*
|
|
Returns true when the string is entirely made of printable runes, false otherwise.
|
|
*/
|
|
func isPrintableString(str string) bool {
|
|
for _, runeValue := range str {
|
|
if !strconv.IsPrint(runeValue) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|