model

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: BSD-2-Clause Imports: 8 Imported by: 14

Documentation

Overview

Package model defines the request, response, and message payload types shared by Ranch transports and services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertValueToType

func ConvertValueToType(value any, targetType reflect.Type) (any, error)

ConvertValueToType converts value to targetType by round-tripping through JSON.

func Decode added in v0.9.0

func Decode[T any](msg *Message) (T, error)

Decode unwraps a Message payload from a Response envelope and decodes it into T.

Types

type BrokerDestinationConfig

type BrokerDestinationConfig struct {
	Destination  string
	ConnectionId string
}

BrokerDestinationConfig specifies the target user queue of a response.

type Direction

type Direction int

Direction int defining which way messages are travelling on a Channel.

const (
	// RequestDir marks a message as a service request.
	RequestDir Direction = 0
	// ResponseDir marks a message as a service response.
	ResponseDir Direction = 1
	// ErrorDir marks a message as an error response.
	ErrorDir Direction = 2
)

type Message

type Message struct {
	Id            *uuid.UUID      `json:"id"`            // message identifier
	DestinationId *uuid.UUID      `json:"destinationId"` // destinationId (targeted recipient)
	Channel       string          `json:"channel"`       // reference to channel message was sent on.
	Destination   string          `json:"destination"`   // destination message was sent to (if galactic)
	Payload       any             `json:"payload"`
	Error         error           `json:"error"`
	Direction     Direction       `json:"direction"`
	Headers       []MessageHeader `json:"headers"`
}

A Message is the encapsulation of the event sent on the bus. It holds a Direction, errors, a Payload and more.

func GenerateError

func GenerateError(msgConfig *MessageConfig) *Message

GenerateError creates an error message from msgConfig.

func GenerateRequest

func GenerateRequest(msgConfig *MessageConfig) *Message

GenerateRequest creates a request message from msgConfig.

func GenerateResponse

func GenerateResponse(msgConfig *MessageConfig) *Message

GenerateResponse creates a response message from msgConfig.

func (*Message) CastPayloadToType

func (m *Message) CastPayloadToType(typ any) error

CastPayloadToType converts the raw any typed Payload into the specified object passed as an argument.

type MessageConfig

type MessageConfig struct {
	Id            *uuid.UUID
	DestinationId *uuid.UUID
	Destination   string
	Channel       string
	Payload       any
	Headers       []MessageHeader
	Direction     Direction
	Err           error
}

MessageConfig contains the fields used by message factory helpers.

type MessageHeader

type MessageHeader struct {
	Label string
	Value string
}

MessageHeader carries message metadata as a label/value pair.

type Request

type Request struct {
	Id                 *uuid.UUID          `json:"id,omitempty"`
	Destination        string              `json:"channel,omitempty"`
	Payload            any                 `json:"payload,omitempty"`
	RequestCommand     string              `json:"request,omitempty" mapstructure:"request"`
	HttpRequest        *http.Request       `json:"-"`
	HttpResponseWriter http.ResponseWriter `json:"-"`
	// Populated if the request was sent on a "private" channel and
	// indicates where to send back the Response.
	// A service should check this field and if not null copy it to the
	// Response.BrokerDestination field to ensure that the response will be sent
	// back on the correct the "private" channel.
	BrokerDestination *BrokerDestinationConfig `json:"-"`
}

Request is the service-level payload delivered to Fabric services.

func CreateServiceRequest

func CreateServiceRequest(requestType string, body []byte) Request

CreateServiceRequest is a small utility function that takes request type and payload and returns a new model.Request instance populated with them

func CreateServiceRequestWithHttpRequest

func CreateServiceRequestWithHttpRequest(requestType string, r *http.Request) Request

CreateServiceRequestWithHttpRequest does the same as CreateServiceRequest, except the payload is a pointer to the Incoming http.Request, so you can essentially extract what ever you want from the incoming request within your service.

func CreateServiceRequestWithValues

func CreateServiceRequestWithValues(requestType string, vals url.Values) Request

CreateServiceRequestWithValues does the same as CreateServiceRequest, except the payload is url.Values and not A byte[] array

type Response

type Response struct {
	Id             *uuid.UUID `json:"id,omitempty"`
	Destination    string     `json:"channel,omitempty"`
	Payload        any        `json:"payload,omitempty"`
	HttpStatusCode int        `json:"httpStatusCode,omitempty"`
	Error          bool       `json:"error,omitempty"`
	ErrorCode      int        `json:"errorCode,omitempty"`
	ErrorMessage   string     `json:"errorMessage,omitempty"`
	ErrorObject    any        `json:"errorObject,omitempty"`
	// If populated the response will be sent to a single client
	// on the specified destination topic.
	BrokerDestination *BrokerDestinationConfig `json:"-"`
	Headers           map[string]any           `json:"-"` // passthrough any http headers
	Marshal           bool                     `json:"-"` // if true, the payload be marshalled into JSON.
}

Response represents a payload sent by a Fabric application.

type ResponseHandlerFunction

type ResponseHandlerFunction func(*Response)

ResponseHandlerFunction handles a service response.

Jump to

Keyboard shortcuts

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