timshannon / badgerhold

BadgerHold is an embeddable NoSQL store for querying Go types built on Badger
MIT License
514 stars 52 forks source link

Added Empty() helper for creating empty queries. #65

Closed kajjagtenberg closed 2 years ago

kajjagtenberg commented 2 years ago

Added a quick helper function for creating empty queries which target every record. Makes it a little more clear how to select everything.

timshannon commented 2 years ago

Creating an empty struct for default behaviors is pretty idiomatic Go. See https://pkg.go.dev/net/http and a dozen other areas in the core libraries. I'm not sure adding another method is useful or clearer in this context. Does Empty create an empty query, or an empty store, or empty an existing store, or empty some cache the query engine uses? I think this is best fixed with documentation as to the behavior of an empty query, or a query with no criteria.

kajjagtenberg commented 2 years ago

I agree with the vagueness of the function name with regards to what it creates. Then again the function signature tells you that it returns a Query and not a Store. I also agree that the documentation could be improved, but I'm not sure whether or not an empty struct clarifies its associated behavior.

timshannon commented 2 years ago

Once again, idiomatic go as defined by the libraries in Go itself do this. It's expected behavior if you work with the core libraries.

https://golang.org/doc/effective_go#allocation_new

See http.Client and http.Server and sync.Mutex, etc. It's a default, zero value of the type you are explicitly creating. An empty / zero value query.