Added type inference
This commit is contained in:
parent
af35cde2d8
commit
11b4ae49ca
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -26,6 +27,7 @@ type collection[T any] struct {
|
|||||||
|
|
||||||
// New creates a new collection with the given name.
|
// New creates a new collection with the given name.
|
||||||
func New[T any](directories ...string) (*collection[T], error) {
|
func New[T any](directories ...string) (*collection[T], error) {
|
||||||
|
name := reflect.TypeOf(*new(T)).Name()
|
||||||
home, err := os.UserHomeDir()
|
home, err := os.UserHomeDir()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -33,6 +35,7 @@ func New[T any](directories ...string) (*collection[T], error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
directories = append([]string{home, ".ocean"}, directories...)
|
directories = append([]string{home, ".ocean"}, directories...)
|
||||||
|
directories = append(directories, name)
|
||||||
directory := filepath.Join(directories...)
|
directory := filepath.Join(directories...)
|
||||||
err = os.MkdirAll(directory, 0700)
|
err = os.MkdirAll(directory, 0700)
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ type User struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCollectionGet(t *testing.T) {
|
func TestCollectionGet(t *testing.T) {
|
||||||
users, err := ocean.New[User]("test", "User")
|
users, err := ocean.New[User]("test")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
defer users.Clear()
|
defer users.Clear()
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ func TestCollectionGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInteraction(t *testing.T) {
|
func TestInteraction(t *testing.T) {
|
||||||
users, err := ocean.New[User]("test", "User")
|
users, err := ocean.New[User]("test")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
defer users.Clear()
|
defer users.Clear()
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ func TestInteraction(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCollectionAll(t *testing.T) {
|
func TestCollectionAll(t *testing.T) {
|
||||||
users, err := ocean.New[User]("test", "User")
|
users, err := ocean.New[User]("test")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
defer users.Clear()
|
defer users.Clear()
|
||||||
|
|
||||||
|
10
README.md
10
README.md
@ -20,13 +20,13 @@ type User struct {
|
|||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data saved to ~/.ocean/User/
|
// Load existing data from ~/.ocean/User/
|
||||||
users := ocean.New[User]("User")
|
users := ocean.New[User]()
|
||||||
|
|
||||||
// Store key and value in memory and write ~/.ocean/User/1.json
|
// Store in memory and also store in ~/.ocean/User/1.json
|
||||||
users.Set("1", &User{Name: "User 1"})
|
users.Set("1", &User{Name: "User 1"})
|
||||||
|
|
||||||
// Read value from memory
|
// Read from memory
|
||||||
user, err := users.Get("1")
|
user, err := users.Get("1")
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ In a real project you would usually prefix your collections with a project or co
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
// Data saved to ~/.ocean/google/User/
|
// Data saved to ~/.ocean/google/User/
|
||||||
users := ocean.New[User]("google", "User")
|
users := ocean.New[User]("google")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Recommended structure
|
## Recommended structure
|
||||||
|
Loading…
Reference in New Issue
Block a user