sticky

package module
v0.0.0-...-833cde7 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 12 Imported by: 3

README

Sticky

Sticky is a file database for small projects.

It uses the idea of an event store.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event[Model any] interface {
	Validate(Model) error
	Execute(Model, time.Time) Model
	Name() string
}

Event is a set of functions that can modify a model.

type FileDB

type FileDB struct {
	File string
}

FileDB is a evet database based of one file.

func (FileDB) Append

func (db FileDB) Append(bs []byte) error

Append adds data to the file with a newline.

func (FileDB) Reader

func (db FileDB) Reader() (io.ReadCloser, error)

Reader opens the file and returns its reader.

type JSONDuration

type JSONDuration time.Duration

JSONDuration is a duration type with a consum marshaling to json.

func (JSONDuration) MarshalJSON

func (d JSONDuration) MarshalJSON() ([]byte, error)

MarshalJSON converts the value to json.

func (*JSONDuration) UnmarshalJSON

func (d *JSONDuration) UnmarshalJSON(bs []byte) error

UnmarshalJSON converts the time from json.

type JSONTime

type JSONTime time.Time

JSONTime is a time type that can be converted to json.

func (JSONTime) MarshalJSON

func (t JSONTime) MarshalJSON() ([]byte, error)

MarshalJSON converts the value to json.

func (*JSONTime) UnmarshalJSON

func (t *JSONTime) UnmarshalJSON(bs []byte) error

UnmarshalJSON converts the time from json.

type MemoryDB

type MemoryDB struct {
	Content string
}

MemoryDB stores Events in memory.

Usefull for testing.

func NewMemoryDB

func NewMemoryDB(content string) *MemoryDB

NewMemoryDB initializes a MemoryDB

func (*MemoryDB) Append

func (db *MemoryDB) Append(bs []byte) error

Append adds a new event.

func (*MemoryDB) Reader

func (db *MemoryDB) Reader() (io.ReadCloser, error)

Reader reads the content.

type Option

type Option[Model any] func(s *Sticky[Model])

Option is a option for sticky.New()

func WithNow

func WithNow[Model any](now func() time.Time) Option[Model]

WithNow uses a special now function. Default is time.Now()

type Sticky

type Sticky[Model any] struct {
	// contains filtered or unexported fields
}

Sticky is some sort of db that persists a model on disk in a event storage way.

func New

func New[Model any](db database, emptyModel Model, getEvent func(name string) Event[Model], os ...Option[Model]) (*Sticky[Model], error)

New initializes a new Sticky instance.

func (*Sticky[Model]) ForReading

func (s *Sticky[Model]) ForReading() (Model, func())

ForReading returns the model for reading.

Call the done function, when reading is finished.

m, done := model.ForReading() defer done()

m...

func (*Sticky[Model]) ForWriting

func (s *Sticky[Model]) ForWriting() (Model, func(...Event[Model]) error, func())

ForWriting returns the model for writing.

Call the write function with one or more events.

Call the done function, when you are done.

m, write, done := model.ForWriting() defer done()

event := ... write(event)

func (*Sticky[Model]) Listen

func (s *Sticky[Model]) Listen(ctx context.Context) func(yield func(val []string) bool)

func (*Sticky[Model]) Read

func (s *Sticky[Model]) Read(f func(Model) error) error

Read calls a function that has access to an instance of the model for reading.

func (*Sticky[Model]) Write

func (s *Sticky[Model]) Write(f func(Model) Event[Model]) error

Write calls a function that has access to an instance of the model for writing. It has to return an event.

Write can return a ValidationError or ExecutionError when the event can not be processed.

type ValidationError

type ValidationError struct {
	// contains filtered or unexported fields
}

ValidationError happens, when the event can not be validated.

func (ValidationError) Error

func (err ValidationError) Error() string

func (ValidationError) String

func (err ValidationError) String() string

func (ValidationError) Unwrap

func (err ValidationError) Unwrap() error

Jump to

Keyboard shortcuts

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