dongjiang, change go-cmp to reflect

Signed-off-by: dongjiang1989 <dongjiang1989@126.com>
This commit is contained in:
dongjiang1989 2024-04-18 20:19:10 +08:00
parent 7cebf0b3f1
commit bfbe25e926
No known key found for this signature in database
1 changed files with 2 additions and 28 deletions

View File

@ -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)