mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
common/helpers: opt-in for custom formatters for diff
Many stuff has a `String()` method that would hide details.
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -28,9 +29,14 @@ import (
|
|||||||
|
|
||||||
var prettyC = pretty.Config{
|
var prettyC = pretty.Config{
|
||||||
Diffable: true,
|
Diffable: true,
|
||||||
PrintStringers: true,
|
PrintStringers: false,
|
||||||
SkipZeroFields: true,
|
SkipZeroFields: true,
|
||||||
IncludeUnexported: false,
|
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
|
// 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)
|
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
|
// HTTPEndpointCases describes case for TestHTTPEndpoints
|
||||||
type HTTPEndpointCases []struct {
|
type HTTPEndpointCases []struct {
|
||||||
Description string
|
Description string
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
package clickhouse
|
package clickhouse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -91,3 +93,7 @@ func TestDefaultConfiguration(t *testing.T) {
|
|||||||
t.Fatalf("validate.Struct() error:\n%+v", err)
|
t.Fatalf("validate.Struct() error:\n%+v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
helpers.RegisterDiffFormatter(reflect.TypeOf(helpers.SubnetMap[NetworkAttributes]{}), fmt.Sprint)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user