Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/maps: add generics sets implementations #70

Merged
merged 1 commit into from
Dec 6, 2024
Merged

Conversation

Sypheos
Copy link
Member

@Sypheos Sypheos commented Dec 3, 2024

What does this PR do?

Add generics set type:

  • Set as replacement for stringutil.Set and int64util.Set
  • IndexedSet to create set where the key is not the object itself

What are the observable changes?

New types

Good PR checklist

  • Title makes sense
  • Is against the correct branch
  • Only addresses one issue
  • Properly assigned
  • Added/updated tests
  • Added/updated documentation
  • Properly labeled

maps/sets.go Outdated
// IndexedSet is a Set where the key of the map is extracted from
// Fn.
// Adding values with the same key will replace the previous value
type IndexedSet[K comparable, T any] struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what kind of usecase would you use this in?

All the usecases i can think about are taken into account by https://github.com/upfluence/pkg/blob/master/slices/index_by.go#L4

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where you will have used index_by several times in a row while scrolling.
Let's take entities A and B, where A has B as a foreign key. You want to list everything a subset of A based an a value of B and return the related B.
Without the possibility of a join, you will have to fetch both A and B. Filter A by B. Repeat until you have everything you need.

  1. Fetch A
  2. Fetch related B -> read in Set first
  3. Filter A with B filter
  4. Index B in Set to avoid refetch.
  5. Return A and B (Set.Values)

It would be lot simpler if we could fetch all of A in one shot but that's not always the case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure to understand your example, lets chat about this tomorrow

Copy link
Member

@AlexisMontagne AlexisMontagne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we split this one in two:

1 - With your Set and the helper Keys and Values. (keep it here)
2- A second one to discuss IndexedSet

Comment on lines 141 to 153
func TestIndexedSet_Has(t *testing.T) {
var s Set[string]

assert.False(t, s.Has("foo"))

s.Add("foo")

assert.True(t, s.Has("foo"))
assert.False(t, s.Has("bar"))
}

func TestIndexedSet_Delete(t *testing.T) {
var s Set[string]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are not even using an IndexedSet 🤔

@Sypheos Sypheos merged commit e213adc into master Dec 6, 2024
2 checks passed
@Sypheos Sypheos deleted the sp/add-sets branch December 6, 2024 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants