refactor: cleanup unused code (#7130)

* refactor: drop unused code

* refactor: drop unused code
This commit is contained in:
Silvan
2024-01-02 15:26:31 +01:00
committed by GitHub
parent 4e3936b5bf
commit 9892fd92b6
109 changed files with 0 additions and 6282 deletions

View File

@@ -1,223 +0,0 @@
package bigcache
import (
"reflect"
"testing"
a_cache "github.com/allegro/bigcache"
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
"github.com/zitadel/zitadel/internal/zerrors"
)
type TestStruct struct {
Test string
}
func getBigCacheMock() *Bigcache {
cache, _ := a_cache.NewBigCache(a_cache.DefaultConfig(2000))
return &Bigcache{cache: cache}
}
func TestSet(t *testing.T) {
type args struct {
cache *Bigcache
key string
value *TestStruct
}
type res struct {
result *TestStruct
errFunc func(err error) bool
}
tests := []struct {
name string
args args
res res
}{
{
name: "set cache no err",
args: args{
cache: getBigCacheMock(),
key: "KEY",
value: &TestStruct{Test: "Test"},
},
res: res{
result: &TestStruct{},
},
},
{
name: "key empty",
args: args{
cache: getBigCacheMock(),
key: "",
value: &TestStruct{Test: "Test"},
},
res: res{
errFunc: zerrors.IsErrorInvalidArgument,
},
},
{
name: "set cache nil value",
args: args{
cache: getBigCacheMock(),
key: "KEY",
},
res: res{
errFunc: zerrors.IsErrorInvalidArgument,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.args.cache.Set(tt.args.key, tt.args.value)
if tt.res.errFunc == nil && err != nil {
t.Errorf("got wrong result should not get err: %v ", err)
}
if tt.res.errFunc == nil {
tt.args.cache.Get(tt.args.key, tt.res.result)
if tt.res.result == nil {
t.Errorf("got wrong result should get result: %v ", err)
}
}
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
t.Errorf("got wrong err: %v ", err)
}
})
}
}
func TestGet(t *testing.T) {
type args struct {
event []*es_models.Event
cache *Bigcache
key string
setValue *TestStruct
getValue *TestStruct
}
type res struct {
result *TestStruct
errFunc func(err error) bool
}
tests := []struct {
name string
args args
res res
}{
{
name: "get cache no err",
args: args{
cache: getBigCacheMock(),
key: "KEY",
setValue: &TestStruct{Test: "Test"},
getValue: &TestStruct{Test: "Test"},
},
res: res{
result: &TestStruct{Test: "Test"},
},
},
{
name: "get cache no key",
args: args{
cache: getBigCacheMock(),
setValue: &TestStruct{Test: "Test"},
getValue: &TestStruct{Test: "Test"},
},
res: res{
errFunc: zerrors.IsErrorInvalidArgument,
},
},
{
name: "get cache no value",
args: args{
cache: getBigCacheMock(),
key: "KEY",
setValue: &TestStruct{Test: "Test"},
},
res: res{
errFunc: zerrors.IsErrorInvalidArgument,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.args.cache.Set("KEY", tt.args.setValue)
if err != nil {
t.Errorf("something went wrong")
}
err = tt.args.cache.Get(tt.args.key, tt.args.getValue)
if tt.res.errFunc == nil && err != nil {
t.Errorf("got wrong result should not get err: %v ", err)
}
if tt.res.errFunc == nil && !reflect.DeepEqual(tt.args.getValue, tt.res.result) {
t.Errorf("got wrong result expected: %v actual: %v", tt.res.result, tt.args.getValue)
}
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
t.Errorf("got wrong err: %v ", err)
}
})
}
}
func TestDelete(t *testing.T) {
type args struct {
event []*es_models.Event
cache *Bigcache
key string
setValue *TestStruct
getValue *TestStruct
}
type res struct {
result *TestStruct
errFunc func(err error) bool
}
tests := []struct {
name string
args args
res res
}{
{
name: "delete cache no err",
args: args{
cache: getBigCacheMock(),
key: "KEY",
setValue: &TestStruct{Test: "Test"},
},
res: res{},
},
{
name: "get cache no key",
args: args{
cache: getBigCacheMock(),
setValue: &TestStruct{Test: "Test"},
getValue: &TestStruct{Test: "Test"},
},
res: res{
errFunc: zerrors.IsErrorInvalidArgument,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.args.cache.Set("KEY", tt.args.setValue)
if err != nil {
t.Errorf("something went wrong")
}
err = tt.args.cache.Delete(tt.args.key)
if tt.res.errFunc == nil && err != nil {
t.Errorf("got wrong result should not get err: %v ", err)
}
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
t.Errorf("got wrong err: %v ", err)
}
})
}
}

View File

@@ -1,67 +0,0 @@
package bigcache
import (
"bytes"
"encoding/gob"
"reflect"
a_cache "github.com/allegro/bigcache"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/zerrors"
)
type Bigcache struct {
cache *a_cache.BigCache
}
func NewBigcache(c *Config) (*Bigcache, error) {
cacheConfig := a_cache.DefaultConfig(c.CacheLifetime)
cacheConfig.HardMaxCacheSize = c.MaxCacheSizeInMB
cache, err := a_cache.NewBigCache(cacheConfig)
if err != nil {
return nil, err
}
return &Bigcache{
cache: cache,
}, nil
}
func (c *Bigcache) Set(key string, object interface{}) error {
if key == "" || reflect.ValueOf(object).IsNil() {
return zerrors.ThrowInvalidArgument(nil, "BIGCA-du73s", "key or value should not be empty")
}
var b bytes.Buffer
enc := gob.NewEncoder(&b)
if err := enc.Encode(object); err != nil {
return zerrors.ThrowInvalidArgument(err, "BIGCA-RUyxI", "unable to encode object")
}
return c.cache.Set(key, b.Bytes())
}
func (c *Bigcache) Get(key string, ptrToObject interface{}) error {
if key == "" || reflect.ValueOf(ptrToObject).IsNil() {
return zerrors.ThrowInvalidArgument(nil, "BIGCA-dksoe", "key or value should not be empty")
}
value, err := c.cache.Get(key)
if err == a_cache.ErrEntryNotFound {
return zerrors.ThrowNotFound(err, "BIGCA-we32s", "not in cache")
}
if err != nil {
logging.Log("BIGCA-ftofbc").WithError(err).Info("read from cache failed")
return zerrors.ThrowInvalidArgument(err, "BIGCA-3idls", "error in reading from cache")
}
b := bytes.NewBuffer(value)
dec := gob.NewDecoder(b)
return dec.Decode(ptrToObject)
}
func (c *Bigcache) Delete(key string) error {
if key == "" {
return zerrors.ThrowInvalidArgument(nil, "BIGCA-clsi2", "key should not be empty")
}
return c.cache.Delete(key)
}

View File

@@ -1,17 +0,0 @@
package bigcache
import (
"time"
"github.com/zitadel/zitadel/internal/cache"
)
type Config struct {
MaxCacheSizeInMB int
//CacheLifetime if set, entries older than the lifetime will be deleted on cleanup (every minute)
CacheLifetime time.Duration
}
func (c *Config) NewCache() (cache.Cache, error) {
return NewBigcache(c)
}

View File

@@ -1,7 +0,0 @@
package cache
type Cache interface {
Set(key string, object interface{}) error
Get(key string, ptrToObject interface{}) error
Delete(key string) error
}

View File

@@ -1,5 +0,0 @@
package cache
type Config interface {
NewCache() (Cache, error)
}

View File

@@ -1,59 +0,0 @@
package config
import (
"encoding/json"
"github.com/zitadel/zitadel/internal/cache"
"github.com/zitadel/zitadel/internal/cache/bigcache"
"github.com/zitadel/zitadel/internal/cache/fastcache"
"github.com/zitadel/zitadel/internal/zerrors"
)
type CacheConfig struct {
Type string
Config cache.Config
}
var caches = map[string]func() cache.Config{
"bigcache": func() cache.Config { return &bigcache.Config{} },
"fastcache": func() cache.Config { return &fastcache.Config{} },
}
func (c *CacheConfig) UnmarshalJSON(data []byte) error {
var rc struct {
Type string
Config json.RawMessage
}
if err := json.Unmarshal(data, &rc); err != nil {
return zerrors.ThrowInternal(err, "CONFI-98ejs", "unable to unmarshal config")
}
c.Type = rc.Type
var err error
c.Config, err = newCacheConfig(c.Type, rc.Config)
if err != nil {
return zerrors.ThrowInternal(err, "CONFI-do9es", "unable create config")
}
return nil
}
func newCacheConfig(cacheType string, configData []byte) (cache.Config, error) {
t, ok := caches[cacheType]
if !ok {
return nil, zerrors.ThrowInternal(nil, "CONFI-di328s", "no config")
}
cacheConfig := t()
if len(configData) == 0 {
return cacheConfig, nil
}
if err := json.Unmarshal(configData, cacheConfig); err != nil {
return nil, zerrors.ThrowInternal(nil, "CONFI-skei3", "could not read config")
}
return cacheConfig, nil
}

View File

@@ -1,11 +0,0 @@
package fastcache
import "github.com/zitadel/zitadel/internal/cache"
type Config struct {
MaxCacheSizeInByte int
}
func (c *Config) NewCache() (cache.Cache, error) {
return NewFastcache(c)
}

View File

@@ -1,57 +0,0 @@
package fastcache
import (
"bytes"
"encoding/gob"
"reflect"
"github.com/VictoriaMetrics/fastcache"
"github.com/zitadel/zitadel/internal/zerrors"
)
type Fastcache struct {
cache *fastcache.Cache
}
func NewFastcache(config *Config) (*Fastcache, error) {
return &Fastcache{
cache: fastcache.New(config.MaxCacheSizeInByte),
}, nil
}
func (fc *Fastcache) Set(key string, object interface{}) error {
if key == "" || reflect.ValueOf(object).IsNil() {
return zerrors.ThrowInvalidArgument(nil, "FASTC-87dj3", "key or value should not be empty")
}
var b bytes.Buffer
enc := gob.NewEncoder(&b)
if err := enc.Encode(object); err != nil {
return zerrors.ThrowInvalidArgument(err, "FASTC-RUyxI", "unable to encode object")
}
fc.cache.Set([]byte(key), b.Bytes())
return nil
}
func (fc *Fastcache) Get(key string, ptrToObject interface{}) error {
if key == "" || reflect.ValueOf(ptrToObject).IsNil() {
return zerrors.ThrowInvalidArgument(nil, "FASTC-di8es", "key or value should not be empty")
}
data := fc.cache.Get(nil, []byte(key))
if len(data) == 0 {
return zerrors.ThrowNotFound(nil, "FASTC-xYzSm", "key not found")
}
b := bytes.NewBuffer(data)
dec := gob.NewDecoder(b)
return dec.Decode(ptrToObject)
}
func (fc *Fastcache) Delete(key string) error {
if key == "" {
return zerrors.ThrowInvalidArgument(nil, "FASTC-lod92", "key should not be empty")
}
fc.cache.Del([]byte(key))
return nil
}

View File

@@ -1,218 +0,0 @@
package fastcache
import (
"reflect"
"testing"
"github.com/VictoriaMetrics/fastcache"
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
"github.com/zitadel/zitadel/internal/zerrors"
)
type TestStruct struct {
Test string
}
func TestSet(t *testing.T) {
type args struct {
cache *Fastcache
key string
value *TestStruct
}
type res struct {
result *TestStruct
errFunc func(err error) bool
}
tests := []struct {
name string
args args
res res
}{
{
name: "set cache no err",
args: args{
cache: &Fastcache{cache: fastcache.New(2000)},
key: "KEY",
value: &TestStruct{Test: "Test"},
},
res: res{
result: &TestStruct{},
},
},
{
name: "key empty",
args: args{
cache: &Fastcache{cache: fastcache.New(2000)},
key: "",
value: &TestStruct{Test: "Test"},
},
res: res{
errFunc: zerrors.IsErrorInvalidArgument,
},
},
{
name: "set cache nil value",
args: args{
cache: &Fastcache{cache: fastcache.New(2000)},
key: "KEY",
},
res: res{
errFunc: zerrors.IsErrorInvalidArgument,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.args.cache.Set(tt.args.key, tt.args.value)
if tt.res.errFunc == nil && err != nil {
t.Errorf("got wrong result should not get err: %v ", err)
}
if tt.res.errFunc == nil {
tt.args.cache.Get(tt.args.key, tt.res.result)
if tt.res.result == nil {
t.Errorf("got wrong result should get result: %v ", err)
}
}
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
t.Errorf("got wrong err: %v ", err)
}
})
}
}
func TestGet(t *testing.T) {
type args struct {
event []*es_models.Event
cache *Fastcache
key string
setValue *TestStruct
getValue *TestStruct
}
type res struct {
result *TestStruct
errFunc func(err error) bool
}
tests := []struct {
name string
args args
res res
}{
{
name: "get cache no err",
args: args{
cache: &Fastcache{cache: fastcache.New(2000)},
key: "KEY",
setValue: &TestStruct{Test: "Test"},
getValue: &TestStruct{Test: "Test"},
},
res: res{
result: &TestStruct{Test: "Test"},
},
},
{
name: "get cache no key",
args: args{
cache: &Fastcache{cache: fastcache.New(2000)},
setValue: &TestStruct{Test: "Test"},
getValue: &TestStruct{Test: "Test"},
},
res: res{
errFunc: zerrors.IsErrorInvalidArgument,
},
},
{
name: "get cache no value",
args: args{
cache: &Fastcache{cache: fastcache.New(2000)},
key: "KEY",
setValue: &TestStruct{Test: "Test"},
},
res: res{
errFunc: zerrors.IsErrorInvalidArgument,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.args.cache.Set("KEY", tt.args.setValue)
if err != nil {
t.Errorf("something went wrong")
}
err = tt.args.cache.Get(tt.args.key, tt.args.getValue)
if tt.res.errFunc == nil && err != nil {
t.Errorf("got wrong result should not get err: %v ", err)
}
if tt.res.errFunc == nil && !reflect.DeepEqual(tt.args.getValue, tt.res.result) {
t.Errorf("got wrong result expected: %v actual: %v", tt.res.result, tt.args.getValue)
}
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
t.Errorf("got wrong err: %v ", err)
}
})
}
}
func TestDelete(t *testing.T) {
type args struct {
event []*es_models.Event
cache *Fastcache
key string
setValue *TestStruct
getValue *TestStruct
}
type res struct {
result *TestStruct
errFunc func(err error) bool
}
tests := []struct {
name string
args args
res res
}{
{
name: "delete cache no err",
args: args{
cache: &Fastcache{cache: fastcache.New(2000)},
key: "KEY",
setValue: &TestStruct{Test: "Test"},
},
res: res{},
},
{
name: "get cache no key",
args: args{
cache: &Fastcache{cache: fastcache.New(2000)},
setValue: &TestStruct{Test: "Test"},
getValue: &TestStruct{Test: "Test"},
},
res: res{
errFunc: zerrors.IsErrorInvalidArgument,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.args.cache.Set("KEY", tt.args.setValue)
if err != nil {
t.Errorf("something went wrong")
}
err = tt.args.cache.Delete(tt.args.key)
if tt.res.errFunc == nil && err != nil {
t.Errorf("got wrong result should not get err: %v ", err)
}
if tt.res.errFunc != nil && !tt.res.errFunc(err) {
t.Errorf("got wrong err: %v ", err)
}
})
}
}

View File

@@ -1,3 +0,0 @@
package cache
//go:generate mockgen -package mock -destination ./mock/cache.mock.go github.com/zitadel/zitadel/internal/cache Cache

View File

@@ -1,80 +0,0 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/zitadel/zitadel/internal/cache (interfaces: Cache)
//
// Generated by this command:
//
// mockgen -package mock -destination ./mock/cache.mock.go github.com/zitadel/zitadel/internal/cache Cache
//
// Package mock is a generated GoMock package.
package mock
import (
reflect "reflect"
gomock "go.uber.org/mock/gomock"
)
// MockCache is a mock of Cache interface.
type MockCache struct {
ctrl *gomock.Controller
recorder *MockCacheMockRecorder
}
// MockCacheMockRecorder is the mock recorder for MockCache.
type MockCacheMockRecorder struct {
mock *MockCache
}
// NewMockCache creates a new mock instance.
func NewMockCache(ctrl *gomock.Controller) *MockCache {
mock := &MockCache{ctrl: ctrl}
mock.recorder = &MockCacheMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockCache) EXPECT() *MockCacheMockRecorder {
return m.recorder
}
// Delete mocks base method.
func (m *MockCache) Delete(arg0 string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Delete", arg0)
ret0, _ := ret[0].(error)
return ret0
}
// Delete indicates an expected call of Delete.
func (mr *MockCacheMockRecorder) Delete(arg0 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockCache)(nil).Delete), arg0)
}
// Get mocks base method.
func (m *MockCache) Get(arg0 string, arg1 any) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Get", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
}
// Get indicates an expected call of Get.
func (mr *MockCacheMockRecorder) Get(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockCache)(nil).Get), arg0, arg1)
}
// Set mocks base method.
func (m *MockCache) Set(arg0 string, arg1 any) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Set", arg0, arg1)
ret0, _ := ret[0].(error)
return ret0
}
// Set indicates an expected call of Set.
func (mr *MockCacheMockRecorder) Set(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Set", reflect.TypeOf((*MockCache)(nil).Set), arg0, arg1)
}