-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Update vaultFS struct and methods structure
Update the vaultFS struct fields and methods to use auth package. Add necessary imports and update the GetInfoFile method.
- Loading branch information
Showing
13 changed files
with
200 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cache | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/patrickmn/go-cache" | ||
) | ||
|
||
type stringConstraint interface { | ||
~string | ||
} | ||
|
||
type Cache[K stringConstraint, T any] struct { | ||
cache *cache.Cache | ||
} | ||
|
||
func New[K stringConstraint, T any](defaultExpiration time.Duration, cleanupInterval time.Duration) *Cache[K, T] { | ||
c := cache.New(defaultExpiration, cleanupInterval) | ||
|
||
return &Cache[K, T]{ | ||
cache: c, | ||
} | ||
} | ||
|
||
func (c *Cache[K, T]) Set(key K, val any) { | ||
c.cache.Set(string(key), val, cache.DefaultExpiration) | ||
} | ||
|
||
func (c *Cache[K, T]) Get(key K) (*T, bool) { | ||
v, ok := c.cache.Get(string(key)) | ||
if !ok { | ||
return nil, false | ||
} | ||
|
||
vTyped, ok := v.(T) | ||
if !ok { | ||
return nil, false | ||
} | ||
|
||
return &vTyped, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package cache_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/di-dao/sonr/pkg/cache" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCache_SetGet(t *testing.T) { | ||
type MyString string | ||
type Data struct { | ||
Value int | ||
} | ||
|
||
cache := cache.New[MyString, Data](5*time.Minute, 10*time.Minute) | ||
|
||
key := MyString("testKey") | ||
expectedValue := Data{Value: 42} | ||
|
||
cache.Set(key, expectedValue) | ||
retrievedValue, ok := cache.Get(key) | ||
|
||
require.True(t, ok, "Expected to retrieve a value from the cache") | ||
require.NotNil(t, retrievedValue, "Expected a non-nil value") | ||
assert.Equal(t, expectedValue, *retrievedValue, "Retrieved value does not match the expected value") | ||
} | ||
|
||
func TestCache_Get_NonExistentKey(t *testing.T) { | ||
type MyString string | ||
type Data struct { | ||
Value int | ||
} | ||
|
||
cache := cache.New[MyString, Data](5*time.Minute, 10*time.Minute) | ||
|
||
key := MyString("nonExistentKey") | ||
retrievedValue, ok := cache.Get(key) | ||
|
||
require.False(t, ok, "Expected not to retrieve a value from the cache for a non-existent key") | ||
require.Nil(t, retrievedValue, "Expected a nil value for a non-existent key") | ||
} | ||
|
||
func TestCache_Set_Overwrite(t *testing.T) { | ||
type MyString string | ||
type Data struct { | ||
Value int | ||
} | ||
|
||
cache := cache.New[MyString, Data](5*time.Minute, 10*time.Minute) | ||
|
||
key := MyString("testKey") | ||
initialValue := Data{Value: 42} | ||
updatedValue := Data{Value: 100} | ||
|
||
cache.Set(key, initialValue) | ||
cache.Set(key, updatedValue) | ||
|
||
retrievedValue, ok := cache.Get(key) | ||
|
||
require.True(t, ok, "Expected to retrieve a value from the cache") | ||
require.NotNil(t, retrievedValue, "Expected a non-nil value") | ||
assert.Equal(t, updatedValue, *retrievedValue, "Retrieved value does not match the updated value") | ||
} | ||
|
||
func TestCache_Set_Get_OtherType(t *testing.T) { | ||
type MyString string | ||
type Data struct { | ||
Value int | ||
} | ||
|
||
cache := cache.New[MyString, Data](5*time.Minute, 10*time.Minute) | ||
|
||
key := MyString("testKey") | ||
intVal := 42 | ||
|
||
cache.Set(key, intVal) | ||
|
||
retrievedValue, ok := cache.Get(key) | ||
|
||
require.False(t, ok, "Expected not to retrieve a value from the cache for a key with a wrong type") | ||
require.Nil(t, retrievedValue, "Expected a nil value for a key with a wrong type") | ||
} | ||
|
||
func TestCache_Set_WithExpiration(t *testing.T) { | ||
type MyString string | ||
type Data struct { | ||
Value int | ||
} | ||
|
||
expiration := 1 * time.Second | ||
cache := cache.New[MyString, Data](expiration, 10*time.Minute) | ||
|
||
key := MyString("testKey") | ||
expectedValue := Data{Value: 42} | ||
|
||
cache.Set(key, expectedValue) | ||
time.Sleep(2 * time.Second) | ||
|
||
retrievedValue, ok := cache.Get(key) | ||
|
||
require.False(t, ok, "Expected not to retrieve a value from the cache after expiration") | ||
require.Nil(t, retrievedValue, "Expected a nil value after expiration") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package auth | ||
|
||
import "encoding/json" | ||
|
||
type InfoFile struct { | ||
Creds Credentials `json:"credentials"` | ||
Properties Properties `json:"properties"` | ||
} | ||
|
||
func (i *InfoFile) Marshal() ([]byte, error) { | ||
return json.Marshal(i) | ||
} | ||
|
||
func (i *InfoFile) Unmarshal(data []byte) error { | ||
return json.Unmarshal(data, i) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package props | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package props | ||
package auth | ||
|
||
import ( | ||
"github.com/di-dao/sonr/crypto/accumulator" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
package vault | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/di-dao/sonr/pkg/cache" | ||
) | ||
|
||
var vaultCache *cache.Cache[contextKey, vaultFS] | ||
|
||
type contextKey string | ||
|
||
func (c contextKey) String() string { | ||
return "vault context key " + string(c) | ||
} | ||
|
||
var clientCtxKey = contextKey("vault-client-id") | ||
|
||
func init() { | ||
// This is a placeholder | ||
vaultCache = cache.New[contextKey, vaultFS](time.Minute*5, time.Minute) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters