vending

package
v0.0.0-...-084e4ab Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultModelOptions []resource.Option

DefaultModelOptions holds the default options for the model.

Functions

func WithConsumablesOption

func WithConsumablesOption(opts ...resource.Option) resource.Option

WithConsumablesOption configures the consumables resource of the model.

func WithInitialConsumable

func WithInitialConsumable(consumables ...*traits.Consumable) resource.Option

WithInitialConsumable configures the model to initialise with the given consumable. Can be used multiple times with consumables being additive. Creating a model with duplicate consumable names will panic. Calling this function with an empty consumable name property will panic.

func WithInitialStock

func WithInitialStock(inventory ...*traits.Consumable_Stock) resource.Option

WithInitialStock configures the model to initialise with the given stock. Can be used multiple times with stock being additive. Creating a model with duplicate stock consumable names will panic. Calling this function with an empty stock consumable property will panic.

func WithInventoryOption

func WithInventoryOption(opts ...resource.Option) resource.Option

WithInventoryOption configures the inventory resource of the model.

func WithVendingApiClientFactory

func WithVendingApiClientFactory(f func(name string) (traits.VendingApiClient, error)) router.Option

WithVendingApiClientFactory instructs the router to create a new client the first time Get is called for that name.

func WithVendingInfoClientFactory

func WithVendingInfoClientFactory(f func(name string) (traits.VendingInfoClient, error)) router.Option

WithVendingInfoClientFactory instructs the router to create a new client the first time Get is called for that name.

func WrapApi

WrapApi adapts a traits.VendingApiServer and presents it as a traits.VendingApiClient

func WrapInfo

WrapInfo adapts a traits.VendingInfoServer and presents it as a traits.VendingInfoClient

Types

type ApiRouter

ApiRouter is a traits.VendingApiServer that allows routing named requests to specific traits.VendingApiClient

func NewApiRouter

func NewApiRouter(opts ...router.Option) *ApiRouter

func (*ApiRouter) Add

func (r *ApiRouter) Add(name string, client any) any

Add extends Router.Add to panic if client is not of type traits.VendingApiClient.

func (*ApiRouter) AddVendingApiClient

func (r *ApiRouter) AddVendingApiClient(name string, client traits.VendingApiClient) traits.VendingApiClient

func (*ApiRouter) Dispense

func (*ApiRouter) GetStock

func (*ApiRouter) GetVendingApiClient

func (r *ApiRouter) GetVendingApiClient(name string) (traits.VendingApiClient, error)

func (*ApiRouter) HoldsType

func (r *ApiRouter) HoldsType(client any) bool

func (*ApiRouter) ListConsumables

func (*ApiRouter) ListInventory

func (*ApiRouter) PullConsumables

func (*ApiRouter) PullInventory

func (*ApiRouter) PullStock

func (*ApiRouter) Register

func (r *ApiRouter) Register(server *grpc.Server)

func (*ApiRouter) RemoveVendingApiClient

func (r *ApiRouter) RemoveVendingApiClient(name string) traits.VendingApiClient

func (*ApiRouter) StopDispense

func (r *ApiRouter) StopDispense(ctx context.Context, request *traits.StopDispenseRequest) (*traits.Consumable_Stock, error)

func (*ApiRouter) UpdateStock

func (r *ApiRouter) UpdateStock(ctx context.Context, request *traits.UpdateStockRequest) (*traits.Consumable_Stock, error)

type ConsumableChange

type ConsumableChange struct {
	ChangeTime time.Time
	Value      *traits.Consumable
}

type ConsumablesChange

type ConsumablesChange struct {
	ID         string
	ChangeTime time.Time
	ChangeType types.ChangeType
	OldValue   *traits.Consumable
	NewValue   *traits.Consumable
}

type InfoRouter

type InfoRouter struct {
	traits.UnimplementedVendingInfoServer

	router.Router
}

InfoRouter is a traits.VendingInfoServer that allows routing named requests to specific traits.VendingInfoClient

func NewInfoRouter

func NewInfoRouter(opts ...router.Option) *InfoRouter

func (*InfoRouter) Add

func (r *InfoRouter) Add(name string, client any) any

Add extends Router.Add to panic if client is not of type traits.VendingInfoClient.

func (*InfoRouter) AddVendingInfoClient

func (r *InfoRouter) AddVendingInfoClient(name string, client traits.VendingInfoClient) traits.VendingInfoClient

func (*InfoRouter) GetVendingInfoClient

func (r *InfoRouter) GetVendingInfoClient(name string) (traits.VendingInfoClient, error)

func (*InfoRouter) HoldsType

func (r *InfoRouter) HoldsType(client any) bool

func (*InfoRouter) Register

func (r *InfoRouter) Register(server *grpc.Server)

func (*InfoRouter) RemoveVendingInfoClient

func (r *InfoRouter) RemoveVendingInfoClient(name string) traits.VendingInfoClient

type InventoryChange

type InventoryChange struct {
	ID         string
	ChangeTime time.Time
	ChangeType types.ChangeType
	OldValue   *traits.Consumable_Stock
	NewValue   *traits.Consumable_Stock
}

type Model

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

Model describes the data structure needed to implement the Vending trait.

func NewModel

func NewModel(opts ...resource.Option) *Model

NewModel creates a new Model with the given options. Options from the resource package are applied to all resources of this model. Use WithInventoryOption or WithConsumablesOption to target a specific resource. See WithInitialStock and WithInitialConsumable for simple ways to pre-populate this model.

func (*Model) CreateConsumable

func (m *Model) CreateConsumable(consumable *traits.Consumable) (*traits.Consumable, error)

CreateConsumable creates a new consumable record. If consumable.Name is specified it will be used as the key, it absent a new name will be invented. If the consumables name already exists, an error will be returned.

func (*Model) CreateStock

func (m *Model) CreateStock(stock *traits.Consumable_Stock) (*traits.Consumable_Stock, error)

CreateStock adds a stock record to this model. If stock.Consumable is not supplied, a new consumable name will be invented. Errors if the stock.Consumable already exists as a known stock entry.

func (*Model) DeleteConsumable

func (m *Model) DeleteConsumable(name string, opts ...resource.WriteOption) (*traits.Consumable, error)

func (*Model) DeleteStock

func (m *Model) DeleteStock(consumable string, opts ...resource.WriteOption) (*traits.Consumable_Stock, error)

func (*Model) DispenseInstantly

func (m *Model) DispenseInstantly(consumable string, quantity *traits.Consumable_Quantity) (*traits.Consumable_Stock, error)

DispenseInstantly removes quantity amount from the stock of the named consumable. This updates the stock LastDispensed, Used, and Remaining. Used and Remaining are only updated if they have a (possibly zero) value in the stock already. Stock Used and Remaining units are maintained, the quantity is converted to those units before modification.

func (*Model) GetConsumable

func (m *Model) GetConsumable(name string, opts ...resource.ReadOption) (*traits.Consumable, bool)

func (*Model) GetStock

func (m *Model) GetStock(consumable string, opts ...resource.ReadOption) (*traits.Consumable_Stock, bool)

func (*Model) ListConsumables

func (m *Model) ListConsumables(opts ...resource.ReadOption) []*traits.Consumable

func (*Model) ListInventory

func (m *Model) ListInventory(opts ...resource.ReadOption) []*traits.Consumable_Stock

ListInventory returns all known stock records.

func (*Model) PullConsumable

func (m *Model) PullConsumable(ctx context.Context, name string, opts ...resource.ReadOption) <-chan ConsumableChange

func (*Model) PullConsumables

func (m *Model) PullConsumables(ctx context.Context, opts ...resource.ReadOption) <-chan ConsumablesChange

func (*Model) PullInventory

func (m *Model) PullInventory(ctx context.Context, opts ...resource.ReadOption) <-chan InventoryChange

PullInventory subscribes to changes in the list of known stock records.

func (*Model) PullStock

func (m *Model) PullStock(ctx context.Context, consumable string, opts ...resource.ReadOption) <-chan StockChange

PullStock subscribes to changes in a single consumables stock. The returned channel will be closed if ctx is Done or the stock record identified by consumable is deleted.

func (*Model) UpdateConsumable

func (m *Model) UpdateConsumable(consumable *traits.Consumable, opts ...resource.WriteOption) (*traits.Consumable, error)

func (*Model) UpdateStock

func (m *Model) UpdateStock(stock *traits.Consumable_Stock, opts ...resource.WriteOption) (*traits.Consumable_Stock, error)

type ModelOption

type ModelOption interface {
	resource.Option
	// contains filtered or unexported methods
}

ModelOption defined the base type for all options that apply to this traits model.

type ModelServer

type ModelServer struct {
	traits.UnimplementedVendingApiServer
	// contains filtered or unexported fields
}

ModelServer adapts a Model to implement traits.VendingApiServer.

func NewModelServer

func NewModelServer(model *Model) *ModelServer

func (*ModelServer) Dispense

func (*ModelServer) GetStock

func (*ModelServer) ListConsumables

func (*ModelServer) ListInventory

func (*ModelServer) PullConsumables

func (*ModelServer) PullInventory

func (*ModelServer) PullStock

func (*ModelServer) Register

func (m *ModelServer) Register(server *grpc.Server)

func (*ModelServer) StopDispense

func (*ModelServer) Unwrap

func (m *ModelServer) Unwrap() any

func (*ModelServer) UpdateStock

type StockChange

type StockChange struct {
	ChangeTime time.Time
	Value      *traits.Consumable_Stock
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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