Skip to content

Commit

Permalink
Merge pull request #31 rename params to cachekey (internals)
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby authored May 11, 2023
2 parents b7baacf + 5c9176c commit eeeb87e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (e *EnvT) T() T {
// opt - fixture options, nil for default options.
// f - callback - fixture body.
// Cache guarantee for call f exactly once for same Cache called and params combination.
func (e *EnvT) Cache(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
return e.cache(params, opt, f)
func (e *EnvT) Cache(cacheKey interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
return e.cache(cacheKey, opt, f)
}

// CacheWithCleanup call from fixture and manage call f and cache it.
Expand All @@ -99,7 +99,7 @@ func (e *EnvT) Cache(params interface{}, opt *FixtureOptions, f FixtureCallbackF
// f - callback - fixture body.
// cleanup, returned from f called while fixture cleanup
// Cache guarantee for call f exactly once for same Cache called and params combination.
func (e *EnvT) CacheWithCleanup(params interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{} {
func (e *EnvT) CacheWithCleanup(cacheKey interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{} {
if opt == nil {
opt = &FixtureOptions{}
}
Expand All @@ -116,16 +116,16 @@ func (e *EnvT) CacheWithCleanup(params interface{}, opt *FixtureOptions, f Fixtu
}
}

return e.cache(params, opt, fWithoutCleanup)
return e.cache(cacheKey, opt, fWithoutCleanup)
}

// cache must be call from first-level public function
// UserFunction->EnvFunction->cache for good determine caller name
func (e *EnvT) cache(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
func (e *EnvT) cache(cacheKey interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
if opt == nil {
opt = globalEmptyFixtureOptions
}
key, err := makeCacheKey(e.t.Name(), params, opt, false)
key, err := makeCacheKey(e.t.Name(), cacheKey, opt, false)
if err != nil {
e.t.Fatalf("failed to create cache key: %v", err)
// return not reacheble after Fatalf
Expand Down
8 changes: 4 additions & 4 deletions env_generic_sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package fixenv

func Cache[TRes any](env Env, params any, opt *FixtureOptions, f func() (TRes, error)) TRes {
func Cache[TRes any](env Env, cacheKey any, opt *FixtureOptions, f func() (TRes, error)) TRes {
addSkipLevel(&opt)
callbackResult := env.Cache(params, opt, func() (res interface{}, err error) {
callbackResult := env.Cache(cacheKey, opt, func() (res interface{}, err error) {
return f()
})

Expand All @@ -16,9 +16,9 @@ func Cache[TRes any](env Env, params any, opt *FixtureOptions, f func() (TRes, e
return res
}

func CacheWithCleanup[TRes any](env Env, params any, opt *FixtureOptions, f func() (TRes, FixtureCleanupFunc, error)) TRes {
func CacheWithCleanup[TRes any](env Env, cacheKey any, opt *FixtureOptions, f func() (TRes, FixtureCleanupFunc, error)) TRes {
addSkipLevel(&opt)
callbackResult := env.CacheWithCleanup(params, opt, func() (res interface{}, cleanup FixtureCleanupFunc, err error) {
callbackResult := env.CacheWithCleanup(cacheKey, opt, func() (res interface{}, cleanup FixtureCleanupFunc, err error) {
return f()
})

Expand Down
4 changes: 2 additions & 2 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ type Env interface {
// Cache cache result of f calls
// f call exactly once for every combination of scope and params
// params must be json serializable (deserialize not need)
Cache(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{}
Cache(cacheKey interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{}

// CacheWithCleanup cache result of f calls
// f call exactly once for every combination of scope and params
// params must be json serializable (deserialize not need)
CacheWithCleanup(params interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{}
CacheWithCleanup(cacheKey interface{}, opt *FixtureOptions, f FixtureCallbackWithCleanupFunc) interface{}
}

var (
Expand Down

0 comments on commit eeeb87e

Please sign in to comment.