shapes

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package shapes is generated from shapes.yaml, which covers the schema forms the chat example does not: unions without a discriminator keyword, primitive variants, typed maps, nullable fields, recursive schemas and numeric enums.

Index

Constants

View Source
const (
	// Payload: Node.
	MessageTree = "Tree"
	// An OpenAPI 3.0 schema, which brings nullable, boolean exclusive bounds and discriminator mappings.
	// Payload: SensorReading.
	MessageReading = "Reading"
	// Socket frames tagged by a const type, with no discriminator keyword.
	// Payload: FramePayload.
	MessageFrame = "Frame"
)

Message names, as the spec gives them.

View Source
const FeedAddressTemplate = "feed/{type}/{userId}"

FeedAddressTemplate is the address of the Feed channel with its parameters unfilled.

Variables

This section is empty.

Functions

func Dispatch

func Dispatch(ctx context.Context, handler Handler, message ReceivedMessage) error

Dispatch passes message to the handler method for its name.

func FeedAddress

func FeedAddress(typeValue, userID string) string

FeedAddress returns the address of the Feed channel with its parameters filled in.

Types

type Batch

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

Batch holds one of several shapes. Set it with the From methods and read it with Value or the As methods.

func (Batch) AsArrayOfString

func (v Batch) AsArrayOfString() ([]string, error)

AsArrayOfString decodes the held value as a []string.

func (Batch) AsMapOfInt64

func (v Batch) AsMapOfInt64() (map[string]int64, error)

AsMapOfInt64 decodes the held value as a map[string]int64.

func (*Batch) FromArrayOfString

func (v *Batch) FromArrayOfString(value []string) error

FromArrayOfString sets the held value to a []string.

func (*Batch) FromMapOfInt64

func (v *Batch) FromMapOfInt64(value map[string]int64) error

FromMapOfInt64 sets the held value to a map[string]int64.

func (Batch) MarshalJSON

func (v Batch) MarshalJSON() ([]byte, error)

MarshalJSON encodes the held value.

func (*Batch) UnmarshalJSON

func (v *Batch) UnmarshalJSON(data []byte) error

UnmarshalJSON stores data after checking that it is exactly one variant.

func (Batch) Validate

func (v Batch) Validate() error

Validate checks that the held value is a valid variant.

func (Batch) Value

func (v Batch) Value() (any, error)

Value decodes the held value as the one variant it matches. The union has no discriminator, so every variant is tried and exactly one must decode.

type Client

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

Client sends and receives this API's messages over a transport.

func NewClient

func NewClient(transport asyncapirt.Transport) *Client

NewClient returns a client that uses transport.

func (*Client) Close

func (c *Client) Close() error

Close closes the transport.

func (*Client) PublishTree

func (c *Client) PublishTree(ctx context.Context, payload Node) error

PublishTree validates payload and sends it as a MessageTree message.

func (*Client) Run

func (c *Client) Run(ctx context.Context, handler Handler) error

Run reads messages until ctx ends or the transport fails and passes each one to handler. Messages that match nothing go to HandleUnknownMessage. Run stops at the first error a handler returns.

type FramePayload

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

FramePayload holds one of several shapes. Set it with the From methods and read it with Value or the As methods.

func (FramePayload) AsStartedFrame

func (v FramePayload) AsStartedFrame() (StartedFrame, error)

AsStartedFrame decodes the held value as a StartedFrame.

func (FramePayload) AsStoppedFrame

func (v FramePayload) AsStoppedFrame() (StoppedFrame, error)

AsStoppedFrame decodes the held value as a StoppedFrame.

func (FramePayload) Discriminator

func (v FramePayload) Discriminator() (string, error)

Discriminator returns the "type" property that selects the variant.

func (*FramePayload) FromStartedFrame

func (v *FramePayload) FromStartedFrame(value StartedFrame) error

FromStartedFrame sets the held value to a StartedFrame.

func (*FramePayload) FromStoppedFrame

func (v *FramePayload) FromStoppedFrame(value StoppedFrame) error

FromStoppedFrame sets the held value to a StoppedFrame.

func (FramePayload) MarshalJSON

func (v FramePayload) MarshalJSON() ([]byte, error)

MarshalJSON encodes the held value.

func (*FramePayload) UnmarshalJSON

func (v *FramePayload) UnmarshalJSON(data []byte) error

UnmarshalJSON stores data after checking that it is exactly one variant.

func (FramePayload) Validate

func (v FramePayload) Validate() error

Validate checks that the held value is a valid variant.

func (FramePayload) Value

func (v FramePayload) Value() (any, error)

Value decodes the held value as the variant its discriminator selects.

type Handler

type Handler interface {
	// Payload: Node.
	HandleTree(ctx context.Context, payload Node) error
	// An OpenAPI 3.0 schema, which brings nullable, boolean exclusive bounds and discriminator mappings.
	// Payload: SensorReading.
	HandleReading(ctx context.Context, payload SensorReading) error
	// Socket frames tagged by a const type, with no discriminator keyword.
	// Payload: FramePayload.
	HandleFrame(ctx context.Context, payload FramePayload) error
	// HandleUnknownMessage gets every message that matched nothing, with the
	// reason. Returning an error stops Client.Run.
	HandleUnknownMessage(ctx context.Context, data []byte, err error) error
}

Handler receives incoming messages. Embed UnimplementedHandler to implement only the methods you need.

type Level

type Level int64
const (
	LevelMinus1 Level = -1
	Level0      Level = 0
	Level1      Level = 1
	Level2      Level = 2
)

Values of Level.

func (Level) Validate

func (v Level) Validate() error

Validate reports whether v is one of the values the schema lists.

type Measurement

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

Measurement holds one of several shapes. Set it with the From methods and read it with Value or the As methods.

func (Measurement) AsFloat64

func (v Measurement) AsFloat64() (float64, error)

AsFloat64 decodes the held value as a float64.

func (Measurement) AsPoint

func (v Measurement) AsPoint() (Point, error)

AsPoint decodes the held value as a Point.

func (Measurement) AsString

func (v Measurement) AsString() (string, error)

AsString decodes the held value as a string.

func (*Measurement) FromFloat64

func (v *Measurement) FromFloat64(value float64) error

FromFloat64 sets the held value to a float64.

func (*Measurement) FromPoint

func (v *Measurement) FromPoint(value Point) error

FromPoint sets the held value to a Point.

func (*Measurement) FromString

func (v *Measurement) FromString(value string) error

FromString sets the held value to a string.

func (Measurement) MarshalJSON

func (v Measurement) MarshalJSON() ([]byte, error)

MarshalJSON encodes the held value.

func (*Measurement) UnmarshalJSON

func (v *Measurement) UnmarshalJSON(data []byte) error

UnmarshalJSON stores data after checking that it is exactly one variant.

func (Measurement) Validate

func (v Measurement) Validate() error

Validate checks that the held value is a valid variant.

func (Measurement) Value

func (v Measurement) Value() (any, error)

Value decodes the held value as the one variant it matches. The union has no discriminator, so every variant is tried and exactly one must decode.

type Node

type Node struct {
	Name     string `json:"name"`
	Children []Node `json:"children,omitempty"`
}

func (*Node) UnmarshalJSON

func (v *Node) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes Node strictly. Required properties must be present and the decoded value must pass Validate.

func (Node) Validate

func (v Node) Validate() error

Validate checks the constraints the schema declares for Node.

type Point

type Point struct {
	X int64 `json:"x"`
	Y int64 `json:"y"`
}

func (*Point) UnmarshalJSON

func (v *Point) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes Point strictly. Required properties must be present and the decoded value must pass Validate. Properties the schema does not declare are rejected.

func (Point) Validate

func (v Point) Validate() error

Validate checks the constraints the schema declares for Point.

type ReceivedMessage

type ReceivedMessage struct {
	Name    string
	Payload any
}

ReceivedMessage is a decoded incoming message. Payload holds the payload type of the message Name names.

func DecodeReceivedMessage

func DecodeReceivedMessage(data []byte) (ReceivedMessage, error)

DecodeReceivedMessage works out which incoming message data is and decodes it. It fails with asyncapirt.ErrUnknownMessage when nothing matches and asyncapirt.ErrAmbiguousMessage when several messages match.

type SensorA

type SensorA struct {
	Kind  string `json:"kind"`
	Alpha int64  `json:"alpha"`
}

func (*SensorA) UnmarshalJSON

func (v *SensorA) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes SensorA strictly. Required properties must be present and the decoded value must pass Validate.

func (SensorA) Validate

func (v SensorA) Validate() error

Validate checks the constraints the schema declares for SensorA.

type SensorB

type SensorB struct {
	Kind string `json:"kind"`
	Beta string `json:"beta"`
}

func (*SensorB) UnmarshalJSON

func (v *SensorB) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes SensorB strictly. Required properties must be present and the decoded value must pass Validate.

func (SensorB) Validate

func (v SensorB) Validate() error

Validate checks the constraints the schema declares for SensorB.

type SensorC

type SensorC struct {
	Kind  string `json:"kind"`
	Gamma bool   `json:"gamma"`
}

Not in the mapping and no const, so its schema name selects it.

func (*SensorC) UnmarshalJSON

func (v *SensorC) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes SensorC strictly. Required properties must be present and the decoded value must pass Validate.

func (SensorC) Validate

func (v SensorC) Validate() error

Validate checks the constraints the schema declares for SensorC.

type SensorReading

type SensorReading struct {
	Value  Measurement          `json:"value"`
	Unit   *string              `json:"unit"`
	Tags   Tags                 `json:"tags"`
	Level  Level                `json:"level"`
	Ratio  float32              `json:"ratio"`
	Labels map[string]int32     `json:"labels"`
	Range  *string              `json:"range,omitempty"`
	Extra  json.RawMessage      `json:"extra,omitempty"`
	Func   *bool                `json:"func,omitempty"`
	Batch  *Batch               `json:"batch,omitempty"`
	Source *SensorReadingSource `json:"source,omitempty"`
}

func (*SensorReading) UnmarshalJSON

func (v *SensorReading) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes SensorReading strictly. Required properties must be present and the decoded value must pass Validate.

func (SensorReading) Validate

func (v SensorReading) Validate() error

Validate checks the constraints the schema declares for SensorReading.

type SensorReadingSource

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

SensorReadingSource holds one of several shapes. Set it with the From methods and read it with Value or the As methods.

func (SensorReadingSource) AsSensorA

func (v SensorReadingSource) AsSensorA() (SensorA, error)

AsSensorA decodes the held value as a SensorA.

func (SensorReadingSource) AsSensorB

func (v SensorReadingSource) AsSensorB() (SensorB, error)

AsSensorB decodes the held value as a SensorB.

func (SensorReadingSource) AsSensorC

func (v SensorReadingSource) AsSensorC() (SensorC, error)

AsSensorC decodes the held value as a SensorC.

func (SensorReadingSource) Discriminator

func (v SensorReadingSource) Discriminator() (string, error)

Discriminator returns the "kind" property that selects the variant.

func (*SensorReadingSource) FromSensorA

func (v *SensorReadingSource) FromSensorA(value SensorA) error

FromSensorA sets the held value to a SensorA.

func (*SensorReadingSource) FromSensorB

func (v *SensorReadingSource) FromSensorB(value SensorB) error

FromSensorB sets the held value to a SensorB.

func (*SensorReadingSource) FromSensorC

func (v *SensorReadingSource) FromSensorC(value SensorC) error

FromSensorC sets the held value to a SensorC.

func (SensorReadingSource) MarshalJSON

func (v SensorReadingSource) MarshalJSON() ([]byte, error)

MarshalJSON encodes the held value.

func (*SensorReadingSource) UnmarshalJSON

func (v *SensorReadingSource) UnmarshalJSON(data []byte) error

UnmarshalJSON stores data after checking that it is exactly one variant.

func (SensorReadingSource) Validate

func (v SensorReadingSource) Validate() error

Validate checks that the held value is a valid variant.

func (SensorReadingSource) Value

func (v SensorReadingSource) Value() (any, error)

Value decodes the held value as the variant its discriminator selects.

type StartedFrame

type StartedFrame struct {
	Type     string `json:"type"`
	Sequence int64  `json:"sequence"`
}

func (StartedFrame) MarshalJSON

func (v StartedFrame) MarshalJSON() ([]byte, error)

MarshalJSON encodes StartedFrame with its const properties filled in.

func (*StartedFrame) UnmarshalJSON

func (v *StartedFrame) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes StartedFrame strictly. Required properties must be present and the decoded value must pass Validate.

func (StartedFrame) Validate

func (v StartedFrame) Validate() error

Validate checks the constraints the schema declares for StartedFrame.

type StoppedFrame

type StoppedFrame struct {
	Type     string  `json:"type"`
	Sequence int64   `json:"sequence"`
	Reason   *string `json:"reason,omitempty"`
}

func (StoppedFrame) MarshalJSON

func (v StoppedFrame) MarshalJSON() ([]byte, error)

MarshalJSON encodes StoppedFrame with its const properties filled in.

func (*StoppedFrame) UnmarshalJSON

func (v *StoppedFrame) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes StoppedFrame strictly. Required properties must be present and the decoded value must pass Validate.

func (StoppedFrame) Validate

func (v StoppedFrame) Validate() error

Validate checks the constraints the schema declares for StoppedFrame.

type Tags

type Tags = []string

type UnimplementedHandler

type UnimplementedHandler struct{}

UnimplementedHandler ignores every message. Its HandleUnknownMessage logs a warning and keeps the receive loop running.

func (UnimplementedHandler) HandleFrame

HandleFrame ignores the message.

func (UnimplementedHandler) HandleReading

HandleReading ignores the message.

func (UnimplementedHandler) HandleTree

HandleTree ignores the message.

func (UnimplementedHandler) HandleUnknownMessage

func (UnimplementedHandler) HandleUnknownMessage(ctx context.Context, data []byte, err error) error

HandleUnknownMessage logs the message size and the reason, then continues.

Jump to

Keyboard shortcuts

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