diff --git a/CHANGELOG.md b/CHANGELOG.md index 0153ee6..4d02fdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## v0.0.34 Ease testing of PubSub +- Add function `gcp.pubsubcmp.DiffMessages`. + ## v0.0.5 - Migrate `dockerimage` to _sherlock-mavrodi_ - Add function `dockerimage.BuildFromContainerfile` for building Container Image. diff --git a/pkg/gcp/pubsubcmp/message.go b/pkg/gcp/pubsubcmp/message.go new file mode 100644 index 0000000..416e146 --- /dev/null +++ b/pkg/gcp/pubsubcmp/message.go @@ -0,0 +1,42 @@ +package pubsubcmp + +import ( + "time" + + "cloud.google.com/go/pubsub" + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" +) + +// DiffMessages compares two Messages for equality using all exported Fields. +// Ignores differences in: +// - Attributes.ce-id +// - ID +// - PublishTime +func DiffMessages(lhs pubsub.Message, rhs pubsub.Message) string { + lhs.ID = "" + rhs.ID = "" + lhsAttrs := make(map[string]string, len(lhs.Attributes)) + for k, v := range lhs.Attributes { + if k == "ce-id" { + continue + } + lhsAttrs[k] = v + } + lhs.Attributes = lhsAttrs + + rhsAttrs := make(map[string]string, len(rhs.Attributes)) + for k, v := range rhs.Attributes { + if k == "ce-id" { + continue + } + rhsAttrs[k] = v + } + rhs.Attributes = rhsAttrs + + pt := time.Now() + lhs.PublishTime = pt + rhs.PublishTime = pt + + return cmp.Diff(lhs, rhs, cmpopts.IgnoreUnexported(pubsub.Message{})) +}