Skip to content

Commit

Permalink
Document map generators
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingmutant committed Jan 8, 2023
1 parent 95a77e4 commit 29bc927
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ func (g *sliceGen[E, K]) value(t *T) []E {
return sl
}

// MapOf creates a map[K]V generator. MapOf(key, val) is equivalent to MapOfN(key, val, -1, -1).
func MapOf[K comparable, V any](key *Generator[K], val *Generator[V]) *Generator[map[K]V] {
return MapOfN(key, val, -1, -1)
}

// MapOfN creates a map[K]V generator. If minLen >= 0, generated maps have minimum length of minLen.
// If maxLen >= 0, generated maps have maximum length of maxLen. MapOfN panics if maxLen >= 0
// and minLen > maxLen.
func MapOfN[K comparable, V any](key *Generator[K], val *Generator[V], minLen int, maxLen int) *Generator[map[K]V] {
assertValidRange(minLen, maxLen)

Expand All @@ -118,10 +122,15 @@ func MapOfN[K comparable, V any](key *Generator[K], val *Generator[V], minLen in
})
}

// MapOfValues creates a map[K]V generator, where keys are generated by applying keyFn to values.
// MapOfValues(val, keyFn) is equivalent to MapOfNValues(val, -1, -1, keyFn).
func MapOfValues[K comparable, V any](val *Generator[V], keyFn func(V) K) *Generator[map[K]V] {
return MapOfNValues(val, -1, -1, keyFn)
}

// MapOfNValues creates a map[K]V generator, where keys are generated by applying keyFn to values.
// If minLen >= 0, generated maps have minimum length of minLen. If maxLen >= 0, generated maps
// have maximum length of maxLen. MapOfNValues panics if maxLen >= 0 and minLen > maxLen.
func MapOfNValues[K comparable, V any](val *Generator[V], minLen int, maxLen int, keyFn func(V) K) *Generator[map[K]V] {
assertValidRange(minLen, maxLen)

Expand Down

0 comments on commit 29bc927

Please sign in to comment.