thing

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package thing provides an example demonstrating a way of using the versionary package. It includes a simple struct, Thing, and a few methods that operate on it. It also includes a Table and a Service that can be used to manage Things.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMemTable

func NewMemTable(table v.Table[Thing]) v.MemTable[Thing]

NewMemTable creates an in-memory Thing table for testing purposes.

func NewTable

func NewTable(dbClient *dynamodb.Client, env string) v.Table[Thing]

NewTable returns a new Table for Thing objects

Types

type Service

type Service struct {
	EntityType string
	Table      v.TableReadWriter[Thing]
}

Service is used to manage a Thing database.

func NewMemService

func NewMemService(env string) Service

NewMemService returns a new Thing Service backed by an in-memory table for testing.

func NewService

func NewService(dbClient *dynamodb.Client, env string) Service

NewService returns a new Thing Service for the specified operating environment.

func (Service) CountThings

func (s Service) CountThings(ctx context.Context) (int64, error)

CountThings returns the number of Things in the Thing table.

func (Service) Create

func (s Service) Create(ctx context.Context, t Thing) (Thing, []string, error)

Create a new Thing in the Thing table.

func (Service) Delete

func (s Service) Delete(ctx context.Context, id string) (Thing, error)

Delete a Thing from the Thing table. The deleted Thing is returned.

func (Service) DeleteVersion

func (s Service) DeleteVersion(ctx context.Context, id, versionID string) (Thing, error)

Delete a Thing version from the Thing table. The deleted Thing is returned.

func (Service) Exists

func (s Service) Exists(ctx context.Context, id string) bool

Exists checks if a Thing exists in the Thing table.

func (Service) FilterMessagesByTag

func (s Service) FilterMessagesByTag(ctx context.Context, tag string, contains string, anyMatch bool) ([]v.TextValue, error)

FilterMessagesByTag returns a filtered list of Thing IDs and messages for a specified tag. The case-insensitive contains query is split into words, and the words are compared with the value in the TextValue. If anyMatch is true, then a TextValue is included in the results if any of the words are found (OR filter). If anyMatch is false, then the TextValue must contain all the words in the query string (AND filter). The filtered results are sorted alphabetically by value, not by ID.

func (Service) FilterThingMessages

func (s Service) FilterThingMessages(ctx context.Context, contains string, anyMatch bool) ([]v.TextValue, error)

FilterThingMessages returns a filtered list of Thing IDs and messages. The case-insensitive contains query is split into words, and the words are compared with the message. If anyMatch is true, then a TextValue is included in the results if any of the words are found (OR filter). If anyMatch is false, then the TextValue must contain all the words in the query string (AND filter). The filtered results are sorted alphabetically by message, not by ID.

func (Service) Read

func (s Service) Read(ctx context.Context, id string) (Thing, error)

Read a specified Thing from the Thing table.

func (Service) ReadAllCountsByTag

func (s Service) ReadAllCountsByTag(ctx context.Context, tag string, sortByValue bool) ([]v.NumValue, error)

ReadAllCountsByTag returns a complete list of Thing IDs and counts for a specified tag, optionally sorted by ascending count. The default order is chronological (sorted by ID).

func (Service) ReadAllDates

func (s Service) ReadAllDates(ctx context.Context) ([]string, error)

ReadAllDates returns a complete chronological list of all days for which there are Things in the table.

func (Service) ReadAllMessagesByTag

func (s Service) ReadAllMessagesByTag(ctx context.Context, tag string, sortByValue bool) ([]v.TextValue, error)

ReadAllMessagesByTag returns a complete list of Thing IDs and messages for a specified tag, optionally sorted by message. The default order is chronological (sorted by ID).

func (Service) ReadAllTags

func (s Service) ReadAllTags(ctx context.Context) ([]string, error)

ReadAllTags returns a complete alphabetical list of all tags for which there are Things in the table.

func (Service) ReadAllThingIDs

func (s Service) ReadAllThingIDs(ctx context.Context) ([]string, error)

ReadAllThingIDs returns a complete chronological list of all Thing IDs in the Thing table.

func (Service) ReadAllThingMessages

func (s Service) ReadAllThingMessages(ctx context.Context, sortByValue bool) ([]v.TextValue, error)

ReadAllThingMessages returns all Thing IDs and messages in the Thing table, optionally sorted by message (the default is sorted by ID). Caution: this may be a LOT of data!

func (Service) ReadAllThingsByDate

func (s Service) ReadAllThingsByDate(ctx context.Context, day string) ([]Thing, error)

ReadAllThingsByDate returns a complete chronological list of all Things for a specified day.

func (Service) ReadAllThingsByDateAsJSON

func (s Service) ReadAllThingsByDateAsJSON(ctx context.Context, day string) ([]byte, error)

ReadAllThingsByDateAsJSON returns a complete chronological list of all Things for a specified day, serialized as JSON.

func (Service) ReadAllThingsByTag

func (s Service) ReadAllThingsByTag(ctx context.Context, tag string) ([]Thing, error)

ReadAllThingsByTag returns a complete chronological list of all Things for a specified tag.

func (Service) ReadAllThingsByTagAsJSON

func (s Service) ReadAllThingsByTagAsJSON(ctx context.Context, tag string) ([]byte, error)

ReadAllThingsByTagAsJSON returns a complete chronological list of all Things for a specified tag, serialized as JSON.

func (Service) ReadAllVersions

func (s Service) ReadAllVersions(ctx context.Context, id string) ([]Thing, error)

ReadAllVersions returns all versions of the specified Thing in chronological order. Caution: this may be a LOT of data!

func (Service) ReadAllVersionsAsJSON

func (s Service) ReadAllVersionsAsJSON(ctx context.Context, id string) ([]byte, error)

ReadAllVersionsAsJSON returns all versions of the specified Thing, serialized as JSON. Caution: this may be a LOT of data!

func (Service) ReadAsJSON

func (s Service) ReadAsJSON(ctx context.Context, id string) ([]byte, error)

ReadAsJSON gets a specified Thing from the Thing table, serialized as JSON.

func (Service) ReadCountsByTag

func (s Service) ReadCountsByTag(ctx context.Context, tag string, reverse bool, limit int, offset string) ([]v.NumValue, error)

ReadCountsByTag returns a paginated list of Thing IDs and counts for a specified tag. Sorting is chronological (or reverse). The offset is the last ID returned in a previous request.

func (Service) ReadDates

func (s Service) ReadDates(ctx context.Context, reverse bool, limit int, offset string) ([]string, error)

ReadDates returns a paginated list of days for which there are Things in the table. Sorting is chronological (or reverse). The offset is the last day returned in a previous request.

func (Service) ReadMessageRangeByTag

func (s Service) ReadMessageRangeByTag(ctx context.Context, tag string, from, to string, reverse bool) ([]v.TextValue, error)

ReadMessageRangeByTag returns a range of Thing IDs with associated messages for a specified tag. The 'from' and 'to' parameters are inclusive. If omitted, they're replaced with sentinel values.

func (Service) ReadMessagesByTag

func (s Service) ReadMessagesByTag(ctx context.Context, tag string, reverse bool, limit int, offset string) ([]v.TextValue, error)

ReadMessagesByTag returns a paginated list of Thing IDs and messages for a specified tag. Sorting is chronological (or reverse). The offset is the last ID returned in a previous request.

func (Service) ReadTags

func (s Service) ReadTags(ctx context.Context, reverse bool, limit int, offset string) ([]string, error)

ReadTags returns a paginated list of tags for which there are Things in the table. Sorting is alphabetical (or reverse). The offset is the last tag returned in a previous request.

func (Service) ReadThingIDs

func (s Service) ReadThingIDs(ctx context.Context, reverse bool, limit int, offset string) ([]string, error)

ReadThingIDs returns a paginated list of Thing IDs in the Thing table. Sorting is chronological (or reverse). The offset is the last ID returned in a previous request.

func (Service) ReadThingMessageRange

func (s Service) ReadThingMessageRange(ctx context.Context, from, to string, reverse bool) ([]v.TextValue, error)

ReadThingMessageRange returns a range of Thing IDs with associated messages. The 'from' and 'to' parameters are inclusive. If omitted, they're replaced with sentinel values.

func (Service) ReadThingMessages

func (s Service) ReadThingMessages(ctx context.Context, reverse bool, limit int, offset string) ([]v.TextValue, error)

ReadThingMessages returns a paginated list of Thing IDs and messages in the Thing table. Sorting is chronological (or reverse). The offset is the last ID returned in a previous request.

func (Service) ReadThingsByDate

func (s Service) ReadThingsByDate(ctx context.Context, day string, reverse bool, limit int, offset string) ([]Thing, error)

ReadThingsByDate returns a paginated list of Things for a specified day. Sorting is chronological (or reverse). The offset is the last ID returned in a previous request.

func (Service) ReadThingsByDateAsJSON

func (s Service) ReadThingsByDateAsJSON(ctx context.Context, day string, reverse bool, limit int, offset string) ([]byte, error)

ReadThingsByDateAsJSON returns a paginated list of Things for a specified day, serialized as JSON. Sorting is chronological (or reverse). The offset is the last ID returned in a previous request.

func (Service) ReadThingsByTag

func (s Service) ReadThingsByTag(ctx context.Context, tag string, reverse bool, limit int, offset string) ([]Thing, error)

ReadThingsByTag returns a paginated list of Things for a specified tag. Sorting is chronological (or reverse). The offset is the last ID returned in a previous request.

func (Service) ReadThingsByTagAsJSON

func (s Service) ReadThingsByTagAsJSON(ctx context.Context, tag string, reverse bool, limit int, offset string) ([]byte, error)

ReadThingsByTagAsJSON returns a paginated list of Things for a specified tag, serialized as JSON. Sorting is chronological (or reverse). The offset is the last ID returned in a previous request.

func (Service) ReadVersion

func (s Service) ReadVersion(ctx context.Context, id, versionID string) (Thing, error)

ReadVersion gets a specified Thing version from the Thing table.

func (Service) ReadVersionAsJSON

func (s Service) ReadVersionAsJSON(ctx context.Context, id, versionID string) ([]byte, error)

ReadVersionAsJSON gets a specified Thing version from the Thing table, serialized as JSON.

func (Service) ReadVersions

func (s Service) ReadVersions(ctx context.Context, id string, reverse bool, limit int, offset string) ([]Thing, error)

ReadVersions returns paginated versions of the specified Thing. Sorting is chronological (or reverse). The offset is the last ID returned in a previous request.

func (Service) ReadVersionsAsJSON

func (s Service) ReadVersionsAsJSON(ctx context.Context, id string, reverse bool, limit int, offset string) ([]byte, error)

ReadVersionsAsJSON returns paginated versions of the specified Thing, serialized as JSON. Sorting is chronological (or reverse). The offset is the last ID returned in a previous request.

func (Service) SumCountsByTag

func (s Service) SumCountsByTag(ctx context.Context, tag string) (float64, error)

SumCountsByTag returns the sum of all counts for a specified tag.

func (Service) Update

func (s Service) Update(ctx context.Context, t Thing) (Thing, []string, error)

Update a Thing in the Thing table, creating a new version and ensuring that the index rows are consistent.

func (Service) VersionExists

func (s Service) VersionExists(ctx context.Context, id, versionID string) bool

VersionExists checks if a specified Thing version exists in the Thing table.

type Thing

type Thing struct {
	ID        string    `json:"id"`
	VersionID string    `json:"versionId"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
	ExpiresAt time.Time `json:"expiresAt"`
	Tags      []string  `json:"tags"`
	Count     int       `json:"count"`
	Message   string    `json:"message"`
}

Thing is a simple example struct that demonstrates how to use the versionary package.

func (Thing) CompressedJSON

func (t Thing) CompressedJSON() []byte

CompressedJSON returns a compressed JSON representation of the Thing. It fails silently, returning nil, if the JSON cannot be generated.

func (Thing) CreatedOn

func (t Thing) CreatedOn() string

CreatedOn returns an ISO-8601 formatted string date from the CreatedAt timestamp. It is useful for grouping things by date.

func (Thing) Validate

func (t Thing) Validate() []string

Validate checks whether the Thing has all required fields and whether the supplied fields are valid, returning a list of problems. If the list is empty, the Thing is valid.

Jump to

Keyboard shortcuts

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