librarian

package module
v0.0.0-...-dcb9718 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2020 License: MIT Imports: 5 Imported by: 0

README

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
    // ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Storage

type Storage interface {
	// Get data by ID.
	Get(ctx context.Context, id string) ([]byte, error)
	// Iterate over all items.
	Iterate(ctx context.Context, iterator func(id string, data []byte) error) error
	// Delete item by id.
	Delete(ctx context.Context, id string) error
	// Update or replace item by id.
	Update(ctx context.Context, id string, data []byte) error
	// Create record and issue id.
	Create(ctx context.Context, data []byte) (string, error)
}

Storage for indexed data.

func Directory

func Directory(dir string) Storage

Directory based storage, where ID is generated by UUIDv4 algorithm.

Directories

Path Synopsis
cmd
Code generated by librarian.
Code generated by librarian.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL