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
- func Dispatch(ctx context.Context, handler Handler, message ReceivedMessage) error
- func RoomAddress(roomID string) string
- type Ack
- type AlertEnvelope
- type Attachment
- type AttachmentKind
- type AttachmentMeta
- type Client
- type EventBase
- type Handler
- type JoinRequest
- type Member
- type MemberJoined
- type MemberRole
- type MessagePosted
- type NoticeEnvelope
- type Ping
- type PushItem
- type ReceivedMessage
- type SayRequest
- type ServerEvent
- func (v ServerEvent) AsMemberJoined() (MemberJoined, error)
- func (v ServerEvent) AsMessagePosted() (MessagePosted, error)
- func (v ServerEvent) AsPing() (Ping, error)
- func (v ServerEvent) Discriminator() (string, error)
- func (v *ServerEvent) FromMemberJoined(value MemberJoined) error
- func (v *ServerEvent) FromMessagePosted(value MessagePosted) error
- func (v *ServerEvent) FromPing(value Ping) error
- func (v ServerEvent) MarshalJSON() ([]byte, error)
- func (v *ServerEvent) UnmarshalJSON(data []byte) error
- func (v ServerEvent) Validate() error
- func (v ServerEvent) Value() (any, error)
- type UnimplementedHandler
- func (UnimplementedHandler) HandleAck(context.Context, Ack) error
- func (UnimplementedHandler) HandleAlert(context.Context, AlertEnvelope) error
- func (UnimplementedHandler) HandleEvent(context.Context, ServerEvent) error
- func (UnimplementedHandler) HandleNotice(context.Context, NoticeEnvelope) error
- func (UnimplementedHandler) HandleUnknownMessage(ctx context.Context, data []byte, err error) error
Constants ¶
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.
const RoomAddressTemplate = "rooms/{roomId}"
RoomAddressTemplate is the address of the Room channel with its parameters unfilled.
One chat room.
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 ¶
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 ¶
MarshalJSON encodes Ack with its const properties filled in.
func (*Ack) UnmarshalJSON ¶
UnmarshalJSON decodes Ack strictly. Required properties must be present and the decoded value must pass Validate.
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 ¶
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) JoinRoom ¶
func (c *Client) JoinRoom(ctx context.Context, payload JoinRequest) error
JoinRoom validates payload and sends it as a MessageJoin message. Join a room.
type EventBase ¶
func (*EventBase) UnmarshalJSON ¶
UnmarshalJSON decodes EventBase strictly. Required properties must be present and the decoded value must pass Validate.
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 ¶
UnmarshalJSON decodes Member strictly. Required properties must be present and the decoded value must pass Validate.
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 ¶
MarshalJSON encodes Ping with its const properties filled in.
func (*Ping) UnmarshalJSON ¶
UnmarshalJSON decodes Ping strictly. Required properties must be present and the decoded value must pass Validate.
type PushItem ¶
func (*PushItem) UnmarshalJSON ¶
UnmarshalJSON decodes PushItem strictly. Required properties must be present and the decoded value must pass Validate.
type ReceivedMessage ¶
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 ¶
func (UnimplementedHandler) HandleAck(context.Context, Ack) error
HandleAck ignores the message.
func (UnimplementedHandler) HandleAlert ¶
func (UnimplementedHandler) HandleAlert(context.Context, AlertEnvelope) error
HandleAlert ignores the message.
func (UnimplementedHandler) HandleEvent ¶
func (UnimplementedHandler) HandleEvent(context.Context, ServerEvent) error
HandleEvent ignores the message.
func (UnimplementedHandler) HandleNotice ¶
func (UnimplementedHandler) HandleNotice(context.Context, NoticeEnvelope) error
HandleNotice ignores the message.
func (UnimplementedHandler) HandleUnknownMessage ¶
HandleUnknownMessage logs the message size and the reason, then continues.