transport

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2023 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ContactMapping = MessageContentMapping{
	Marshal: func(con known.KnownMessageContent) ([]byte, error) {
		msg := con.(known.Contact)

		t := transportContact{
			MessageContentType: NewMessageContentType(msg),
			Contact:            msg.Contact().String(),
		}

		err := marshalContactActions(msg.Actions(), &t)
		if err != nil {
			return nil, errors.Wrap(err, "could not marshal contact action")
		}

		return jsoniter.Marshal(t)
	},
	Unmarshal: func(b []byte) (known.KnownMessageContent, error) {
		var t transportContact

		if err := jsoniter.Unmarshal(b, &t); err != nil {
			return nil, errors.Wrap(err, "json unmarshal failed")
		}

		contact, err := refs.NewIdentity(t.Contact)
		if err != nil {
			return nil, errors.Wrap(err, "could not create a feed ref")
		}

		action, err := unmarshalContactAction(t)
		if err != nil {
			return nil, errors.Wrap(err, "could not unmarshal contact action")
		}

		return known.NewContact(contact, action)
	},
}
View Source
var PubMapping = MessageContentMapping{
	Marshal: func(con known.KnownMessageContent) ([]byte, error) {
		msg := con.(known.Pub)

		t := transportPub{
			MessageContentType: NewMessageContentType(msg),
			Address: transportPubAddress{
				Key:  msg.Key().String(),
				Host: msg.Host(),
				Port: msg.Port(),
			},
		}

		return jsoniter.Marshal(t)
	},
	Unmarshal: func(b []byte) (known.KnownMessageContent, error) {
		var t transportPub

		if err := jsoniter.Unmarshal(b, &t); err != nil {
			return nil, errors.Wrap(err, "json unmarshal failed")
		}

		key, err := refs.NewIdentity(t.Address.Key)
		if err != nil {
			return nil, errors.Wrap(err, "could not create an identity ref")
		}

		return known.NewPub(key, t.Address.Host, t.Address.Port)
	},
}

Functions

This section is empty.

Types

type Marshaler

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

func NewMarshaler

func NewMarshaler(mappings MessageContentMappings, logger logging.Logger) (*Marshaler, error)

func (*Marshaler) Marshal

func (m *Marshaler) Marshal(content known.KnownMessageContent) (message.RawContent, error)

func (*Marshaler) Unmarshal

type MessageContentMapping

type MessageContentMapping struct {
	Marshal   func(con known.KnownMessageContent) ([]byte, error)
	Unmarshal func(b []byte) (known.KnownMessageContent, error)
}

type MessageContentMappings

type MessageContentMappings map[known.MessageContentType]MessageContentMapping

func DefaultMappings

func DefaultMappings() MessageContentMappings

type MessageContentType

type MessageContentType struct {
	MessageContentType string `json:"type"`
}

func NewMessageContentType

func NewMessageContentType(messageContent known.KnownMessageContent) MessageContentType

Jump to

Keyboard shortcuts

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