Merge "Change type of Once keys to OnceKey"
This commit is contained in:
commit
450bde14b8
|
@ -26,7 +26,7 @@ type OncePer struct {
|
|||
|
||||
// Once computes a value the first time it is called with a given key per OncePer, and returns the
|
||||
// value without recomputing when called with the same key. key must be hashable.
|
||||
func (once *OncePer) Once(key interface{}, value func() interface{}) interface{} {
|
||||
func (once *OncePer) Once(key OnceKey, value func() interface{}) interface{} {
|
||||
// Fast path: check if the key is already in the map
|
||||
if v, ok := once.values.Load(key); ok {
|
||||
return v
|
||||
|
@ -50,7 +50,7 @@ func (once *OncePer) Once(key interface{}, value func() interface{}) interface{}
|
|||
|
||||
// Get returns the value previously computed with Once for a given key. If Once has not been called for the given
|
||||
// key Get will panic.
|
||||
func (once *OncePer) Get(key interface{}) interface{} {
|
||||
func (once *OncePer) Get(key OnceKey) interface{} {
|
||||
v, ok := once.values.Load(key)
|
||||
if !ok {
|
||||
panic(fmt.Errorf("Get() called before Once()"))
|
||||
|
@ -60,12 +60,12 @@ func (once *OncePer) Get(key interface{}) interface{} {
|
|||
}
|
||||
|
||||
// OnceStringSlice is the same as Once, but returns the value cast to a []string
|
||||
func (once *OncePer) OnceStringSlice(key interface{}, value func() []string) []string {
|
||||
func (once *OncePer) OnceStringSlice(key OnceKey, value func() []string) []string {
|
||||
return once.Once(key, func() interface{} { return value() }).([]string)
|
||||
}
|
||||
|
||||
// OnceStringSlice is the same as Once, but returns two values cast to []string
|
||||
func (once *OncePer) Once2StringSlice(key interface{}, value func() ([]string, []string)) ([]string, []string) {
|
||||
func (once *OncePer) Once2StringSlice(key OnceKey, value func() ([]string, []string)) ([]string, []string) {
|
||||
type twoStringSlice [2][]string
|
||||
s := once.Once(key, func() interface{} {
|
||||
var s twoStringSlice
|
||||
|
|
Loading…
Reference in New Issue