Simplified constructor name

This commit is contained in:
Eduard Urbach 2023-07-06 14:27:48 +02:00
parent edb369fe0a
commit c62306bdf7
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
5 changed files with 19 additions and 44 deletions

View File

@ -3,7 +3,6 @@ package ocean
import (
"encoding/json"
"os"
"path"
"path/filepath"
"strings"
"sync"
@ -25,15 +24,16 @@ type collection[T any] struct {
directory string
}
// NewCollection creates a new collection with the given name.
func NewCollection[T any](namespace string, name string) (*collection[T], error) {
// New creates a new collection with the given name.
func New[T any](directories ...string) (*collection[T], error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
directory := path.Join(home, ".ocean", namespace, name)
directories = append([]string{home, ".ocean"}, directories...)
directory := filepath.Join(directories...)
err = os.MkdirAll(directory, 0700)
if err != nil {
@ -41,7 +41,7 @@ func NewCollection[T any](namespace string, name string) (*collection[T], error)
}
c := &collection[T]{
name: name,
name: directories[len(directories)-1],
directory: directory,
}

View File

@ -12,7 +12,7 @@ type User struct {
}
func TestCollectionGet(t *testing.T) {
users, err := ocean.NewCollection[User]("test", "User")
users, err := ocean.New[User]("test", "User")
assert.Nil(t, err)
defer users.Clear()
@ -24,7 +24,7 @@ func TestCollectionGet(t *testing.T) {
}
func TestInteraction(t *testing.T) {
users, err := ocean.NewCollection[User]("test", "User")
users, err := ocean.New[User]("test", "User")
assert.Nil(t, err)
defer users.Clear()
@ -53,7 +53,7 @@ func TestInteraction(t *testing.T) {
}
func TestCollectionAll(t *testing.T) {
users, err := ocean.NewCollection[User]("test", "User")
users, err := ocean.New[User]("test", "User")
assert.Nil(t, err)
defer users.Clear()

View File

@ -1,22 +0,0 @@
package ocean
type Namespace interface {
}
// Force interface implementation
var _ Namespace = (*namespace)(nil)
// namespaces groups multiple collections under a common name.
type namespace struct {
name string
}
// NewNamespace creates a new namespace to group multiple collections under a common name.
func NewNamespace(name string) *namespace {
return &namespace{name: name}
}
// Count counts the number of collections in the namespace.
func (ns *namespace) Count() int {
return 0
}

View File

@ -1,13 +0,0 @@
package ocean_test
import (
"testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/ocean"
)
func TestNewNamespace(t *testing.T) {
ns := ocean.NewNamespace("test")
assert.Equal(t, ns.Count(), 0)
}

View File

@ -5,6 +5,16 @@ In-memory key value store that saves your data to plain old JSON files.
If you like, you can operate on your entire data with classic UNIX tools.
```go
users := ocean.NewCollection("namespace", "User")
// Data saved to ~/.ocean/User/
users := ocean.New("User")
users.Set("1", &User{Name: "User 1"})
```
In a real project you would usually prefix your user collection with a project or company name, serving as a namespace:
```go
// Data saved to ~/.ocean/google/User/
users := ocean.New("google", "User")
```
You can add as many hierarchies as you need but I recommend using a simple `/namespace/collection/` structure.