dongjiang, change go-cmp to reflect
Signed-off-by: dongjiang1989 <dongjiang1989@126.com>
This commit is contained in:
parent
7cebf0b3f1
commit
bfbe25e926
|
@ -44,7 +44,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
"github.com/prometheus/common/expfmt"
|
"github.com/prometheus/common/expfmt"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
|
@ -277,37 +276,12 @@ func compare(got, want []*dto.MetricFamily) error {
|
||||||
return fmt.Errorf("encoding expected metrics failed: %w", err)
|
return fmt.Errorf("encoding expected metrics failed: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if diffErr := diff(gotBuf.String(), wantBuf.String()); diffErr != "" {
|
if diffErr := reflect.DeepEqual(gotBuf, wantBuf); !diffErr {
|
||||||
return fmt.Errorf(diffErr)
|
return fmt.Errorf("metrics diff")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// diff returns a diff of both values as long as both are of the same type and
|
|
||||||
// are a struct, map, slice, array or string. Otherwise it returns an empty string.
|
|
||||||
func diff(expected, actual interface{}) string {
|
|
||||||
if expected == nil || actual == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
et, ek := typeAndKind(expected)
|
|
||||||
at, _ := typeAndKind(actual)
|
|
||||||
if et != at {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
diff := cmp.Diff(expected, actual)
|
|
||||||
if diff == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return "\n\nDiff:\n" + diff
|
|
||||||
}
|
|
||||||
|
|
||||||
// typeAndKind returns the type and kind of the given interface{}
|
// typeAndKind returns the type and kind of the given interface{}
|
||||||
func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
|
func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
|
||||||
t := reflect.TypeOf(v)
|
t := reflect.TypeOf(v)
|
||||||
|
|
Loading…
Reference in New Issue