From f5cd0c1140e4a613cee9ca52b6f47701dfd24df0 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 6 Nov 2023 18:35:11 -0300 Subject: [PATCH] Filter.Clone() --- filter.go | 33 +++++++++++++++++++++++++++++++-- filter_test.go | 38 ++++++++++++++++++++++++++++++++++++++ utils.go | 10 ++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/filter.go b/filter.go index 32eb41c..c2c07db 100644 --- a/filter.go +++ b/filter.go @@ -101,11 +101,11 @@ func FilterEqual(a Filter, b Filter) bool { } } - if a.Since != b.Since { + if !arePointerValuesEqual(a.Since, b.Since) { return false } - if a.Until != b.Until { + if !arePointerValuesEqual(a.Until, b.Until) { return false } @@ -115,3 +115,32 @@ func FilterEqual(a Filter, b Filter) bool { return true } + +func (ef Filter) Clone() Filter { + clone := Filter{ + IDs: slices.Clone(ef.IDs), + Authors: slices.Clone(ef.Authors), + Kinds: slices.Clone(ef.Kinds), + Limit: ef.Limit, + Search: ef.Search, + } + + if ef.Tags != nil { + clone.Tags = make(TagMap, len(ef.Tags)) + for k, v := range ef.Tags { + clone.Tags[k] = slices.Clone(v) + } + } + + if ef.Since != nil { + since := *ef.Since + clone.Since = &since + } + + if ef.Until != nil { + until := *ef.Until + clone.Until = &until + } + + return clone +} diff --git a/filter_test.go b/filter_test.go index e0b0b9e..b73d024 100644 --- a/filter_test.go +++ b/filter_test.go @@ -91,3 +91,41 @@ func TestFilterEquality(t *testing.T) { t.Error("kinds filters shouldn't be equal") } } + +func TestFilterClone(t *testing.T) { + ts := Now() - 60*60 + flt := Filter{ + Kinds: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}}, + Since: &ts, + IDs: []string{"9894b4b5cb5166d23ee8899a4151cf0c66aec00bde101982a13b8e8ceb972df9"}, + } + clone := flt.Clone() + if !FilterEqual(flt, clone) { + t.Errorf("clone is not equal:\n %v !=\n %v", flt, clone) + } + + clone1 := flt.Clone() + clone1.IDs = append(clone1.IDs, "88f0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d") + if FilterEqual(flt, clone1) { + t.Error("modifying the clone ids should cause it to not be equal anymore") + } + + clone2 := flt.Clone() + clone2.Tags["letter"] = append(clone2.Tags["letter"], "c") + if FilterEqual(flt, clone2) { + t.Error("modifying the clone tag items should cause it to not be equal anymore") + } + + clone3 := flt.Clone() + clone3.Tags["g"] = []string{"drt"} + if FilterEqual(flt, clone3) { + t.Error("modifying the clone tag map should cause it to not be equal anymore") + } + + clone4 := flt.Clone() + *clone4.Since++ + if FilterEqual(flt, clone4) { + t.Error("modifying the clone since should cause it to not be equal anymore") + } +} diff --git a/utils.go b/utils.go index 724bcaf..fcd07d9 100644 --- a/utils.go +++ b/utils.go @@ -93,3 +93,13 @@ func IsValidRelayURL(u string) bool { } return true } + +func arePointerValuesEqual[V comparable](a *V, b *V) bool { + if a == nil && b == nil { + return true + } + if a != nil && b != nil { + return *a == *b + } + return false +}