diff --git a/env.go b/env.go index f3cef1c..37f71f1 100644 --- a/env.go +++ b/env.go @@ -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. @@ -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{} } @@ -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 diff --git a/env_generic_sugar.go b/env_generic_sugar.go index f5098de..09e0a0e 100644 --- a/env_generic_sugar.go +++ b/env_generic_sugar.go @@ -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() }) @@ -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() }) diff --git a/interface.go b/interface.go index 85eb82a..38f9b4d 100644 --- a/interface.go +++ b/interface.go @@ -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 (