json

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

type Store[T any] struct {
	// contains filtered or unexported fields
}

Store provides locked read/modify/write access to a JSON file. T is the top-level structure stored in the file (must have exported fields with json tags). If *T implements storage.Initer, Init() is called automatically after loading. The caller provides the Locker, decoupling storage from any specific lock implementation.

func New

func New[T any](filePath string, locker lock.Locker) *Store[T]

New creates a Store backed by filePath, using locker for mutual exclusion.

func (*Store[T]) ReadRaw

func (s *Store[T]) ReadRaw(fn func(*T) error) error

ReadRaw deserializes the JSON file and passes the data to fn. The caller must already hold the lock (via TryLock).

func (*Store[T]) TryLock

func (s *Store[T]) TryLock(ctx context.Context) (bool, error)

TryLock delegates to the underlying locker.

func (*Store[T]) Unlock

func (s *Store[T]) Unlock(ctx context.Context) error

Unlock delegates to the underlying locker.

func (*Store[T]) Update

func (s *Store[T]) Update(ctx context.Context, fn func(*T) error) error

Update acquires the lock (blocking), calls Write under lock, then releases. If fn returns nil the data is atomically persisted.

func (*Store[T]) With

func (s *Store[T]) With(ctx context.Context, fn func(*T) error) error

With acquires the lock (blocking), calls Read under lock, then releases.

func (*Store[T]) WriteRaw

func (s *Store[T]) WriteRaw(fn func(*T) error) error

WriteRaw deserializes the JSON file, passes the data to fn, and atomically persists the result if fn returns nil. The caller must already hold the lock (via TryLock).

Jump to

Keyboard shortcuts

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