chat

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: 9 Imported by: 0

Documentation

Overview

Package chat is generated from chat.yaml, a spec that uses every feature gen-asyncapi supports. Its tests check the generated code end to end.

Index

Constants

View Source
const (
	// Request to join a room.
	// Payload: JoinRequest.
	MessageJoin = "join"
	// Payload: SayRequest.
	MessageSay = "say"
	// Server acknowledgement of a request.
	// Payload: Ack.
	MessageAck = "ack"
	// Something happened in the room.
	// Payload: ServerEvent.
	MessageEvent = "event"
	// Batched notices. Same shape as Alert; only the item type tells them apart.
	// Payload: NoticeEnvelope.
	MessageNotice = "notice"
	// Payload: AlertEnvelope.
	MessageAlert = "alert"
)

Message names, as the spec gives them.

View Source
const RoomAddressTemplate = "rooms/{roomId}"

RoomAddressTemplate is the address of the Room channel with its parameters unfilled.

One chat room.

View Source
const (
	// Public chat server.
	ServerProductionURL = "wss://chat.example.com/ws"
)

Server URLs, with any server variables at their defaults.

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 RoomAddress

func RoomAddress(roomID string) string

RoomAddress returns the address of the Room channel with its parameters filled in.

Types

type Ack

type Ack struct {
	Type     string  `json:"type"`
	ID       string  `json:"id"`
	Ok       bool    `json:"ok"`
	Error    *string `json:"error,omitempty"`
	Sequence *int64  `json:"sequence,omitempty"`
}

func (Ack) MarshalJSON

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

MarshalJSON encodes Ack with its const properties filled in.

func (*Ack) UnmarshalJSON

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

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

func (Ack) Validate

func (v Ack) Validate() error

Validate checks the constraints the schema declares for Ack.

type AlertEnvelope

type AlertEnvelope struct {
	Messages []PushItem `json:"messages"`
}

func (*AlertEnvelope) UnmarshalJSON

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

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

func (AlertEnvelope) Validate

func (v AlertEnvelope) Validate() error

Validate checks the constraints the schema declares for AlertEnvelope.

type Attachment

type Attachment struct {
	Kind AttachmentKind `json:"kind"`
	Meta string         `json:"meta"`
}

func (Attachment) MetaValue

func (v Attachment) MetaValue() (AttachmentMeta, error)

MetaValue decodes the JSON document carried as a string in Meta.

func (*Attachment) SetMetaValue

func (v *Attachment) SetMetaValue(value AttachmentMeta) error

SetMetaValue encodes value as JSON and stores it in Meta.

func (*Attachment) UnmarshalJSON

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

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

func (Attachment) Validate

func (v Attachment) Validate() error

Validate checks the constraints the schema declares for Attachment.

type AttachmentKind

type AttachmentKind string
const (
	AttachmentKindImage AttachmentKind = "image"
	AttachmentKindFile  AttachmentKind = "file"
)

Values of AttachmentKind.

func (AttachmentKind) Validate

func (v AttachmentKind) Validate() error

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

type AttachmentMeta

type AttachmentMeta struct {
	Size int64   `json:"size"`
	Mime *string `json:"mime,omitempty"`
}

func (*AttachmentMeta) UnmarshalJSON

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

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

func (AttachmentMeta) Validate

func (v AttachmentMeta) Validate() error

Validate checks the constraints the schema declares for AttachmentMeta.

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) JoinRoom

func (c *Client) JoinRoom(ctx context.Context, payload JoinRequest) error

JoinRoom validates payload and sends it as a MessageJoin message. Join a room.

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.

func (*Client) Say

func (c *Client) Say(ctx context.Context, payload SayRequest) error

Say validates payload and sends it as a MessageSay message. Post a message to the room.

type EventBase

type EventBase struct {
	At       time.Time `json:"at"`
	Sequence int64     `json:"sequence"`
}

func (*EventBase) UnmarshalJSON

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

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

func (EventBase) Validate

func (v EventBase) Validate() error

Validate checks the constraints the schema declares for EventBase.

type Handler

type Handler interface {
	// Server acknowledgement of a request.
	// Payload: Ack.
	HandleAck(ctx context.Context, payload Ack) error
	// Something happened in the room.
	// Payload: ServerEvent.
	HandleEvent(ctx context.Context, payload ServerEvent) error
	// Batched notices. Same shape as Alert; only the item type tells them apart.
	// Payload: NoticeEnvelope.
	HandleNotice(ctx context.Context, payload NoticeEnvelope) error
	// Payload: AlertEnvelope.
	HandleAlert(ctx context.Context, payload AlertEnvelope) 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 JoinRequest

type JoinRequest struct {
	Type string `json:"type"`
	// Not enforced by the generated code: format uuid.
	ID   string `json:"id"`
	Room string `json:"room"`
	// Display name; the server picks one when absent.
	Nickname *string `json:"nickname,omitempty"`
}

func (JoinRequest) MarshalJSON

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

MarshalJSON encodes JoinRequest with its const properties filled in.

func (*JoinRequest) UnmarshalJSON

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

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

func (JoinRequest) Validate

func (v JoinRequest) Validate() error

Validate checks the constraints the schema declares for JoinRequest.

type Member

type Member struct {
	ID   string     `json:"id"`
	Role MemberRole `json:"role"`
}

func (*Member) UnmarshalJSON

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

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

func (Member) Validate

func (v Member) Validate() error

Validate checks the constraints the schema declares for Member.

type MemberJoined

type MemberJoined struct {
	At       time.Time `json:"at"`
	Sequence int64     `json:"sequence"`
	Type     string    `json:"type"`
	Member   Member    `json:"member"`
}

func (MemberJoined) MarshalJSON

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

MarshalJSON encodes MemberJoined with its const properties filled in.

func (*MemberJoined) UnmarshalJSON

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

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

func (MemberJoined) Validate

func (v MemberJoined) Validate() error

Validate checks the constraints the schema declares for MemberJoined.

type MemberRole

type MemberRole string
const (
	MemberRoleOwner  MemberRole = "owner"
	MemberRoleMember MemberRole = "member"
	MemberRoleGuest  MemberRole = "guest"
)

Values of MemberRole.

func (MemberRole) Validate

func (v MemberRole) Validate() error

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

type MessagePosted

type MessagePosted struct {
	At          time.Time    `json:"at"`
	Sequence    int64        `json:"sequence"`
	Type        string       `json:"type"`
	Text        string       `json:"text"`
	Author      Member       `json:"author"`
	Attachments []Attachment `json:"attachments,omitempty"`
}

func (MessagePosted) MarshalJSON

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

MarshalJSON encodes MessagePosted with its const properties filled in.

func (*MessagePosted) UnmarshalJSON

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

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

func (MessagePosted) Validate

func (v MessagePosted) Validate() error

Validate checks the constraints the schema declares for MessagePosted.

type NoticeEnvelope

type NoticeEnvelope struct {
	Messages []PushItem `json:"messages"`
}

func (*NoticeEnvelope) UnmarshalJSON

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

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

func (NoticeEnvelope) Validate

func (v NoticeEnvelope) Validate() error

Validate checks the constraints the schema declares for NoticeEnvelope.

type Ping

type Ping struct {
	Type string `json:"type"`
}

func (Ping) MarshalJSON

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

MarshalJSON encodes Ping with its const properties filled in.

func (*Ping) UnmarshalJSON

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

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

func (Ping) Validate

func (v Ping) Validate() error

Validate checks the constraints the schema declares for Ping.

type PushItem

type PushItem struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

func (*PushItem) UnmarshalJSON

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

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

func (PushItem) Validate

func (v PushItem) Validate() error

Validate checks the constraints the schema declares for PushItem.

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 SayRequest

type SayRequest struct {
	Type     string   `json:"type"`
	ID       string   `json:"id"`
	Text     string   `json:"text"`
	Mentions []string `json:"mentions,omitempty"`
}

func (SayRequest) MarshalJSON

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

MarshalJSON encodes SayRequest with its const properties filled in.

func (*SayRequest) UnmarshalJSON

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

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

func (SayRequest) Validate

func (v SayRequest) Validate() error

Validate checks the constraints the schema declares for SayRequest.

type ServerEvent

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

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

func (ServerEvent) AsMemberJoined

func (v ServerEvent) AsMemberJoined() (MemberJoined, error)

AsMemberJoined decodes the held value as a MemberJoined.

func (ServerEvent) AsMessagePosted

func (v ServerEvent) AsMessagePosted() (MessagePosted, error)

AsMessagePosted decodes the held value as a MessagePosted.

func (ServerEvent) AsPing

func (v ServerEvent) AsPing() (Ping, error)

AsPing decodes the held value as a Ping.

func (ServerEvent) Discriminator

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

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

func (*ServerEvent) FromMemberJoined

func (v *ServerEvent) FromMemberJoined(value MemberJoined) error

FromMemberJoined sets the held value to a MemberJoined.

func (*ServerEvent) FromMessagePosted

func (v *ServerEvent) FromMessagePosted(value MessagePosted) error

FromMessagePosted sets the held value to a MessagePosted.

func (*ServerEvent) FromPing

func (v *ServerEvent) FromPing(value Ping) error

FromPing sets the held value to a Ping.

func (ServerEvent) MarshalJSON

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

MarshalJSON encodes the held value.

func (*ServerEvent) UnmarshalJSON

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

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

func (ServerEvent) Validate

func (v ServerEvent) Validate() error

Validate checks that the held value is a valid variant.

func (ServerEvent) Value

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

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

type UnimplementedHandler

type UnimplementedHandler struct{}

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

func (UnimplementedHandler) HandleAck

HandleAck ignores the message.

func (UnimplementedHandler) HandleAlert

HandleAlert ignores the message.

func (UnimplementedHandler) HandleEvent

HandleEvent ignores the message.

func (UnimplementedHandler) HandleNotice

HandleNotice 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