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

Add a function for selecting a random member of an enum #4

Merged
merged 9 commits into from
Aug 31, 2023

Conversation

xyproto
Copy link
Contributor

@xyproto xyproto commented Aug 30, 2023

This is potentially useful if there is a large selection of possible settings, as an enum.

Mildly related: https://stackoverflow.com/questions/48490049/how-do-i-choose-a-random-value-from-an-enum

@xyproto
Copy link
Contributor Author

xyproto commented Aug 30, 2023

Note that if there are no members, it will panic with panic: invalid argument to Intn.

I have an alternative implementation where it first checks if len(e.members) == 0 and then panics, but it did not feel like an improvement, since both implementations panics if there are no members.

Returning a slice of either 0 or 1 members would also be a possibility.

@MaxBreida
Copy link

I don't think that this should ever panic, instead there could be an error returned if it's already known what would cause this panic.

@xyproto
Copy link
Contributor Author

xyproto commented Aug 30, 2023

Imagine if the function could return an Optional[M] instead, and Optional was also provided by this or a similar package. Would something along those lines be feasible?

enum.go Outdated Show resolved Hide resolved
@orsinium
Copy link
Member

Imagine if the function could return an Optional[M] instead

I think it's a good idea to return a pointer to a member and return a nil pointer for an empty enum. Then it will be consistent with the Enum.Parse behavior.

@orsinium
Copy link
Member

As a side note, I also thought about adding a method like this and a few similar ones but I'm careful not to reinvent genesis. If you want to have some specific operations on the collection of enum members, you can always do something like genesis.Choice(colors.Members()).

On the other hand, I don't mind any contributions for which you have a use case. After all, "A little copying is better than a little dependency", I wouldn't want people to bring yet another library to just pick a random enum member or something similar. So, it's a good idea to provide some convenient functions that many different people might need.

@orsinium
Copy link
Member

golangci-lint complains about using rand. Feel free to suppress the error, I don't think the function needs to be cryptographically secure or something.

enum.go Outdated Show resolved Hide resolved
Copy link
Member

@orsinium orsinium left a comment

Choose a reason for hiding this comment

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

.

@orsinium
Copy link
Member

orsinium commented Aug 31, 2023

Add // nolint: gosec in the place where golangci-lint reports, and that's it, I'll be happy to merge it :) I can't merge PRs with failing CI.

UPD: run golangci-lint run locally to check.

@orsinium orsinium merged commit 527edcb into orsinium-labs:master Aug 31, 2023
6 checks passed
@orsinium
Copy link
Member

Perfect! Thank you for the contribution and for sticking with me through all review comments :)

@xyproto
Copy link
Contributor Author

xyproto commented Aug 31, 2023

Thanks for reviewing and merging. Open source software has the potential to benefit us all. 🙂

@xyproto xyproto deleted the random branch August 31, 2023 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants