feat: Add database data generator to make fakedbs easier to populate (#5922)

* feat: Add database data generator to make fakedbs easier to populate
This commit is contained in:
Steven Masley
2023-01-31 15:10:03 -06:00
committed by GitHub
parent c162c0f284
commit 4a6fc40949
10 changed files with 645 additions and 477 deletions

View File

@ -0,0 +1,29 @@
package dbgen
// takeFirstBytes implements takeFirst for []byte.
// []byte is not a comparable type.
func takeFirstBytes(values ...[]byte) []byte {
return takeFirstF(values, func(v []byte) bool {
return len(v) != 0
})
}
// takeFirstF takes the first value that returns true
func takeFirstF[Value any](values []Value, take func(v Value) bool) Value {
var empty Value
for _, v := range values {
if take(v) {
return v
}
}
// If all empty, return empty
return empty
}
// takeFirst will take the first non-empty value.
func takeFirst[Value comparable](values ...Value) Value {
var empty Value
return takeFirstF(values, func(v Value) bool {
return v != empty
})
}