models

package
v1.0.0-beta.191 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const ProblemContentType = "application/problem+json"

ProblemContentType is the default content type for a Problem response

View Source
const (
	// ProblemTypeDefault is the default problem type.
	ProblemTypeDefault = ProblemType("about:blank")
)

Variables

This section is empty.

Functions

func RespondProblem

func RespondProblem(problem Problem, w http.ResponseWriter)

Respond will render the problem as JSON to the provided ResponseWriter.

Types

type Address

type Address struct {
	Country     *CountryCode `json:"country,omitempty"`
	PostalCode  *string      `json:"postalCode,omitempty"`
	State       *string      `json:"state,omitempty"`
	City        *string      `json:"city,omitempty"`
	Line1       *string      `json:"line1,omitempty"`
	Line2       *string      `json:"line2,omitempty"`
	PhoneNumber *string      `json:"phoneNumber,omitempty"`
}

type Annotated

type Annotated interface {
	// contains filtered or unexported methods
}

type AnnotatedModel

type AnnotatedModel struct {
	Metadata map[string]string `json:"metadata,omitempty"`
}

type CadenceList

type CadenceList[T Cadenced] []T

CadenceList is a simple abstraction for a list of cadenced models. It is useful to validate the relationship between the cadences of the models, like their ordering, overlaps, continuity, etc.

func NewSortedCadenceList

func NewSortedCadenceList[T Cadenced](cadences []T) CadenceList[T]

func (CadenceList[T]) Cadences

func (t CadenceList[T]) Cadences() []T

Cadences returns the cadences in the timeline

func (CadenceList[T]) GetOverlaps

func (t CadenceList[T]) GetOverlaps() [][2]int

GetOverlaps returns true if there is any overlap between the cadences in the timeline

func (CadenceList[T]) IsSorted

func (t CadenceList[T]) IsSorted() bool

type Cadenced

type Cadenced interface {
	// contains filtered or unexported methods
}

Cadenced represents a model with active from and to dates. The interval described is inclusive on the from side and exclusive on the to side.

type CadencedModel

type CadencedModel struct {
	ActiveFrom time.Time `json:"activeFrom"`
	// ActiveTo CANNOT be BEFORE ActiveFrom (it can be the same, which would mean the entity is never active)
	ActiveTo *time.Time `json:"activeTo"`
}

func (CadencedModel) Equal

func (c CadencedModel) Equal(other CadencedModel) bool

func (CadencedModel) IsActiveAt

func (c CadencedModel) IsActiveAt(t time.Time) bool

func (CadencedModel) IsZero

func (c CadencedModel) IsZero() bool

type CadencedResourceRepo

type CadencedResourceRepo[T Cadenced] interface {
	SetEndOfCadence(ctx context.Context, id NamespacedID, at *time.Time) (*T, error)
}

type CountryCode

type CountryCode string

[ISO 3166-1](https://www.iso.org/iso-3166-country-codes.html) alpha-2 country code.

type Equaler

type Equaler[T any] interface {
	// Equal returns true in case all attributes of T are strictly equal.
	Equal(T) bool
}

type GenericConflictError

type GenericConflictError struct {
	Inner error
}

func (*GenericConflictError) Error

func (e *GenericConflictError) Error() string

func (*GenericConflictError) Unwrap

func (e *GenericConflictError) Unwrap() error

type GenericForbiddenError

type GenericForbiddenError struct {
	Inner error
}

func (*GenericForbiddenError) Error

func (e *GenericForbiddenError) Error() string

func (*GenericForbiddenError) Unwrap

func (e *GenericForbiddenError) Unwrap() error

type GenericUserError

type GenericUserError struct {
	Inner error
}

TODO: these should be picked up in a general server error handler

func (*GenericUserError) Error

func (e *GenericUserError) Error() string

func (*GenericUserError) Unwrap

func (e *GenericUserError) Unwrap() error

type ManagedModel

type ManagedModel struct {
	CreatedAt time.Time `json:"createdAt"`
	// After creation the entity is considered updated.
	UpdatedAt time.Time `json:"updatedAt"`
	// Time of soft delete. If not null, the entity is considered deleted.
	DeletedAt *time.Time `json:"deletedAt,omitempty"`
}

func (ManagedModel) IsDeleted

func (m ManagedModel) IsDeleted() bool

func (ManagedModel) IsDeletedAt

func (m ManagedModel) IsDeletedAt(t time.Time) bool

func (ManagedModel) Validate

func (m ManagedModel) Validate() error

type ManagedResource

type ManagedResource struct {
	NamespacedModel `json:",inline"`
	ManagedModel    `json:",inline"`

	// ID is the unique identifier for Resource.
	ID          string  `json:"id"`
	Description *string `json:"description,omitempty"`
	Name        string  `json:"name"`
}

func NewManagedResource

func NewManagedResource(input ManagedResourceInput) ManagedResource

func (ManagedResource) Validate

func (r ManagedResource) Validate() error

type ManagedResourceInput

type ManagedResourceInput struct {
	ID          string
	Namespace   string
	CreatedAt   time.Time
	UpdatedAt   time.Time
	DeletedAt   *time.Time
	Name        string
	Description *string
}

func (ManagedResourceInput) Validate

func (r ManagedResourceInput) Validate() error

type ManagedUniqueResource

type ManagedUniqueResource struct {
	NamespacedModel
	ManagedModel

	// ID is the unique identifier for Resource.
	ID string `json:"id"`

	// Key is the unique key for Resource.
	Key string `json:"key"`
}

type Metadata

type Metadata map[string]string

func NewMetadata

func NewMetadata[T ~map[string]string](m T) Metadata

func (Metadata) Equal

func (m Metadata) Equal(v Metadata) bool

type Meter

type Meter struct {
	// We don't accept namespace via config, it's set by the `namespace.default`.`
	Namespace     string            `json:"-" yaml:"-"`
	ID            string            `json:"id,omitempty" yaml:"id,omitempty"`
	Slug          string            `json:"slug" yaml:"slug"`
	Description   string            `json:"description,omitempty" yaml:"description,omitempty"`
	Aggregation   MeterAggregation  `json:"aggregation" yaml:"aggregation"`
	EventType     string            `json:"eventType" yaml:"eventType"`
	ValueProperty string            `json:"valueProperty,omitempty" yaml:"valueProperty,omitempty"`
	GroupBy       map[string]string `json:"groupBy,omitempty" yaml:"groupBy,omitempty"`
	WindowSize    WindowSize        `json:"windowSize,omitempty" yaml:"windowSize,omitempty"`
}

func NewMeter

func NewMeter(
	slug string,
	aggregatation MeterAggregation,
	eventType string,
	valueProperty string,
	options *MeterOptions,
) (*Meter, error)

func (*Meter) Render

func (m *Meter) Render(w http.ResponseWriter, r *http.Request) error

func (*Meter) SupportsWindowSize

func (m *Meter) SupportsWindowSize(w *WindowSize) error

func (*Meter) Validate

func (m *Meter) Validate() error

type MeterAggregation

type MeterAggregation string
const (
	MeterAggregationSum         MeterAggregation = "SUM"
	MeterAggregationCount       MeterAggregation = "COUNT"
	MeterAggregationAvg         MeterAggregation = "AVG"
	MeterAggregationMin         MeterAggregation = "MIN"
	MeterAggregationMax         MeterAggregation = "MAX"
	MeterAggregationUniqueCount MeterAggregation = "UNIQUE_COUNT"
)

Note: keep values up to date in the meter package

func (MeterAggregation) IsValid

func (MeterAggregation) IsValid(input string) bool

func (MeterAggregation) Values

func (MeterAggregation) Values() (kinds []string)

Values provides list valid values for Enum

type MeterNotFoundError

type MeterNotFoundError struct {
	MeterSlug string
}

func (*MeterNotFoundError) Error

func (e *MeterNotFoundError) Error() string

type MeterOptions

type MeterOptions struct {
	ID          string
	Description string
	GroupBy     map[string]string
	WindowSize  *WindowSize
}

type MeterQueryRow

type MeterQueryRow struct {
	Value       float64            `json:"value"`
	WindowStart time.Time          `json:"windowStart"`
	WindowEnd   time.Time          `json:"windowEnd"`
	Subject     *string            `json:"subject"`
	GroupBy     map[string]*string `json:"groupBy"`
}

MeterQueryRow returns a single row from the meter dataset.

func (*MeterQueryRow) Render

Render implements the chi renderer interface.

type NamespaceNotFoundError

type NamespaceNotFoundError struct {
	Namespace string
}

func (*NamespaceNotFoundError) Error

func (e *NamespaceNotFoundError) Error() string

type NamespacedID

type NamespacedID struct {
	Namespace string `json:"namespace"`
	ID        string `json:"id"`
}

func (NamespacedID) Validate

func (i NamespacedID) Validate() error

type NamespacedModel

type NamespacedModel struct {
	Namespace string `json:"-" yaml:"-"`
}

func (NamespacedModel) Validate

func (m NamespacedModel) Validate() error

type Problem

type Problem interface {
	Respond(w http.ResponseWriter)
	Error() string
	ProblemType() ProblemType
	ProblemTitle() string
	ProblemStatus() int
}

Problem is the RFC 7807 response body.

type ProblemType

type ProblemType string

ProblemType contains a URI that identifies the problem type. This URI will, ideally, contain human-readable documentation for the problem when de-referenced.

type StatusProblem

type StatusProblem struct {
	Err error `json:"-"` // low-level runtime error

	// Type is a URI reference that identifies the problem type.
	Type ProblemType `json:"type"`
	// Title is a short, human-readable summary of the problem type.
	Title string `json:"title"`
	// Status is the HTTP status code generated by the origin server for this occurrence of the problem.
	Status int `json:"status"`
	// Detail is a human-readable explanation specific to this occurrence of the problem.
	Detail string `json:"detail,omitempty"`
	// Instance is a URI reference that identifies the specific occurrence of the problem.
	Instance string `json:"instance,omitempty"`

	// Extension fields are allowed by spec, see https://datatracker.ietf.org/doc/html/rfc7807#section-3.2
	Extensions map[string]interface{} `json:"extensions,omitempty"`
}

StatusProblem is the RFC 7807 response body without additional fields.

func NewStatusProblem

func NewStatusProblem(ctx context.Context, err error, status int) *StatusProblem

NewStatusProblem will generate a problem for the provided HTTP status code. The Problem's Status field will be set to match the status argument, and the Title will be set to the default Go status text for that code.

func (*StatusProblem) Error

func (p *StatusProblem) Error() string

func (*StatusProblem) ProblemStatus

func (p *StatusProblem) ProblemStatus() int

func (*StatusProblem) ProblemTitle

func (p *StatusProblem) ProblemTitle() string

func (*StatusProblem) ProblemType

func (p *StatusProblem) ProblemType() ProblemType

func (*StatusProblem) RawError

func (p *StatusProblem) RawError() error

func (*StatusProblem) Respond

func (p *StatusProblem) Respond(w http.ResponseWriter)

Respond will render the problem as JSON to the provided ResponseWriter.

type Validator

type Validator interface {
	// Validate returns an error if the instance of the Validator is invalid.
	Validate() error
}

type VersionedModel

type VersionedModel struct {
	// Key is the unique identifier of the entity across its versions.
	// Might be generated by the system or provided by the user.
	Key string `json:"key,omitempty"`
	// Version is the integer sequential version of the entity, starting from 1.
	Version int `json:"version,omitempty"`
}

VersionedModel represents a versionable entity. With each new version the sequence is incremented while the key remains the same. Key + Version uniquely identifies an entity.

type WindowSize

type WindowSize string
const (
	WindowSizeMinute WindowSize = "MINUTE"
	WindowSizeHour   WindowSize = "HOUR"
	WindowSizeDay    WindowSize = "DAY"
)

Note: keep values up to date in the meter package

func WindowSizeFromDuration

func WindowSizeFromDuration(duration time.Duration) (WindowSize, error)

func (WindowSize) Duration

func (w WindowSize) Duration() time.Duration

Duration returns the duration of the window size

func (WindowSize) Values

func (WindowSize) Values() (kinds []string)

Values provides list valid values for Enum

Jump to

Keyboard shortcuts

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