Skip to content

reddec/librarian

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Librarian

Generates in-memory, type-safe, thread-safe index of data stored from an arbitrary backend, keeping in memory only required meta-data (indexed fields).

Go generate capable. Go modules capable.

Install

go get -u -v github.com/reddec/librarian/cmd/...

Usage

librarian -out some/storage.go some/package/types.go

Configuration

For base type, exported field in struct use: index:"[NAME][,unique]":

  • NAME - index name (also getter name). If not defined - field name prefixed by By will be used.
  • unique - mark index as unique, by default index is not unique.

Example

For file

types.go

type User struct {
    Name string `index:",unique"`
    Role string `index:""`
    Year int
}

will generate (implementation omitted)

generated.go

// constructors

// NewUserStorage
// NewUserStorageJSON
// NewUserStorageFilesJSON

// storage
type UserStorage struct {}
func (*UserStorage) Synchronize(context.Context) error {}
func (*UserStorage) Add(ctx context.Context, user User) error {}
func (*UserStorage) ByName(ctx context.Context, name string) (User, error) {}
func (*UserStorage) ByRole(ctx context.Context, role string) ([]User, error) {}
func (*UserStorage) RemoveByName(ctx context.Context, name string) error {}
func (*UserStorage) RemoveByRole(ctx context.Context, role string) error {}
func (*UserStorage) UpdateByName(ctx context.Context, user User) error {}
func (*UserStorage) UpsertByName(ctx context.Context, user User) error {}

checkout examples

Basic usage in code:

func main() {
    directory := "."
    ctx := context.Background()
    store := NewUserStorageFilesJSON(directory)
    if err := store.Synchronize(ctx); err != nil { // synchronize internal state
        panic(err)
    }
    // store.ByName
    // store.Add
    // store.UpdateByName
    // ...
}

About

Generates in-memory, type-safe, thread-safe index of data stored from an arbitrary backend, keeping in memory only required meta-data (indexed fields)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages