From 2d5edf47d2d42eb8130fcbd435485a4c2baf746c Mon Sep 17 00:00:00 2001 From: Samuel Attwood Date: Thu, 13 Jun 2024 09:38:24 -0400 Subject: [PATCH] Adding Equal methods --- surveyor/jetstream_advisories.go | 50 ++++++++++++++++++++++++++++++++ surveyor/observation.go | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/surveyor/jetstream_advisories.go b/surveyor/jetstream_advisories.go index add9252..d5dab77 100644 --- a/surveyor/jetstream_advisories.go +++ b/surveyor/jetstream_advisories.go @@ -323,6 +323,56 @@ func (o *JSAdvisoryConfig) Validate() error { return fmt.Errorf(strings.Join(errs, ", ")) } +func (o *JSAdvisoryConfig) Equal(c *JSAdvisoryConfig) bool { + if o == nil || c == nil { + return o == c + } + + oBytes, err := json.Marshal(o) + if err != nil { + return false + } + + cBytes, err := json.Marshal(c) + if err != nil { + return false + } + + var oMap, cMap map[string]interface{} + + if err := json.Unmarshal([]byte(oBytes), &oMap); err != nil { + return false + } + + if err := json.Unmarshal([]byte(cBytes), &cMap); err != nil { + return false + } + + if !reflect.DeepEqual(oMap, cMap) { + return false + } + + if o.ExternalAccountConfig == nil || c.ExternalAccountConfig == nil { + if o.ExternalAccountConfig != c.ExternalAccountConfig { + return false + } + } + + if o.ExternalAccountConfig != nil { + if *o.ExternalAccountConfig != *c.ExternalAccountConfig { + return false + } + } + + for i, opt := range o.NatsOpts { + if fmt.Sprintf("%p", opt) != fmt.Sprintf("%p", c.NatsOpts[i]) { + return false + } + } + + return true +} + func (o *JSAdvisoryConfig) copy() *JSAdvisoryConfig { if o == nil { return nil diff --git a/surveyor/observation.go b/surveyor/observation.go index 157c67f..de3da07 100644 --- a/surveyor/observation.go +++ b/surveyor/observation.go @@ -199,6 +199,56 @@ func (o *ServiceObsConfig) Validate() error { return errors.New(strings.Join(errs, ", ")) } +func (o *ServiceObsConfig) Equal(c *ServiceObsConfig) bool { + if o == nil || c == nil { + return o == c + } + + oBytes, err := json.Marshal(o) + if err != nil { + return false + } + + cBytes, err := json.Marshal(c) + if err != nil { + return false + } + + var oMap, cMap map[string]interface{} + + if err := json.Unmarshal([]byte(oBytes), &oMap); err != nil { + return false + } + + if err := json.Unmarshal([]byte(cBytes), &cMap); err != nil { + return false + } + + if !reflect.DeepEqual(oMap, cMap) { + return false + } + + if o.ExternalAccountConfig == nil || c.ExternalAccountConfig == nil { + if o.ExternalAccountConfig != c.ExternalAccountConfig { + return false + } + } + + if o.ExternalAccountConfig != nil { + if *o.ExternalAccountConfig != *c.ExternalAccountConfig { + return false + } + } + + for i, opt := range o.NatsOpts { + if fmt.Sprintf("%p", opt) != fmt.Sprintf("%p", c.NatsOpts[i]) { + return false + } + } + + return true +} + func (o *ServiceObsConfig) copy() *ServiceObsConfig { if o == nil { return nil