common/helpers: opt-in for custom formatters for diff

Many stuff has a `String()` method that would hide details.
This commit is contained in:
Vincent Bernat
2022-08-16 21:21:22 +02:00
parent f9b507ff35
commit c41fa8cb55
2 changed files with 18 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ import (
"net"
"net/http"
"os"
"reflect"
"testing"
"time"
@@ -28,9 +29,14 @@ import (
var prettyC = pretty.Config{
Diffable: true,
PrintStringers: true,
PrintStringers: false,
SkipZeroFields: true,
IncludeUnexported: false,
Formatter: map[reflect.Type]interface{}{
reflect.TypeOf(net.IP{}): fmt.Sprint,
reflect.TypeOf(time.Time{}): fmt.Sprint,
reflect.TypeOf(SubnetMap[string]{}): fmt.Sprint,
},
}
// Diff return a diff of two objects. If no diff, an empty string is
@@ -39,6 +45,11 @@ func Diff(a, b interface{}) string {
return prettyC.Compare(a, b)
}
// RegisterDiffFormatter add an additional formatter for pretty diff.
func RegisterDiffFormatter(t reflect.Type, fn interface{}) {
prettyC.Formatter[t] = fn
}
// HTTPEndpointCases describes case for TestHTTPEndpoints
type HTTPEndpointCases []struct {
Description string

View File

@@ -4,6 +4,8 @@
package clickhouse
import (
"fmt"
"reflect"
"testing"
"github.com/gin-gonic/gin"
@@ -91,3 +93,7 @@ func TestDefaultConfiguration(t *testing.T) {
t.Fatalf("validate.Struct() error:\n%+v", err)
}
}
func init() {
helpers.RegisterDiffFormatter(reflect.TypeOf(helpers.SubnetMap[NetworkAttributes]{}), fmt.Sprint)
}