ampmessage

package module
v0.0.0-...-f03ce9a Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2015 License: MIT Imports: 3 Imported by: 0

README

amp-message

High level AMP Message implementation for manipulating, encoding and decoding AMP messages in Go.

Godoc

Installation

$ go get github.com/nickpoorman/go-amp-message

Example

Encoding a message:

msg := ampmessage.New(nil)

fmt.Printf("<Bytes: %x>\n", msg.ToBytes())
// => <Bytes: 10>

msg.Push(ampmessage.NewStringArg("foo"))
msg.PushString("bar") // convenience method for pushing a new StringArg
msg.PushString("baz")
fmt.Printf("<Bytes: %x>\n", msg.ToBytes())
// => <Bytes: 1300000005733a666f6f00000005733a62617200000005733a62617a>

// json
jsonObj, _ := ampmessage.NewJSONArg(map[string]string{"foo": "bar"})
msg.Push(jsonObj)
fmt.Printf("<Bytes: %x>\n", msg.ToBytes())
// => <Bytes: 1400000005733a666f6f00000005733a62617200000005733a62617a0000000f6a3a7b22666f6f223a22626172227d>

// convenience method for pushing a new JSONArg
msg.PushJSON(map[string]string{"ping": "pong"})
fmt.Printf("<Bytes: %x>\n", msg.ToBytes())
// => <Bytes: 1500000005733a666f6f00000005733a62617200000005733a62617a0000000f6a3a7b22666f6f223a22626172227d000000116a3a7b2270696e67223a22706f6e67227d>

blob := []byte("beep")
msg.Push(ampmessage.NewBlobArg(blob))
msg.PushBlob([]byte("boop")) // convenience method for pushing a new BlobArg
fmt.Printf("<Bytes: %x>\n", msg.ToBytes())
// => <Bytes: 1700000005733a666f6f00000005733a62617200000005733a62617a0000000f6a3a7b22666f6f223a22626172227d000000116a3a7b2270696e67223a22706f6e67 ... >

Decoding a message:

msg := ampmessage.New(nil)

msg.PushString("foo")
msg.PushJSON(map[string]string{"hello": "world"})
msg.PushBlob([]byte("hello"))

other, _ := ampmessage.NewFromBytes(msg.ToBytes())

fmt.Printf("%s\n", other.Shift())
// => &foo
fmt.Printf("%s\n", other.Shift())
// => &{"hello":"world"}
fmt.Printf("%s\n", other.Shift())
// => &hello

API

Godoc

ampmessage.New(nil)

Initialize an empty message.

ampmessage.NewFromBytes(bytes)

Decode the buffer AMP message to populate the Message.

ampmessage.New(args)

Initialize a message populated with args.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arg

type Arg interface {
	Bytes() []byte
}

Arg is an abstract type that knows what type it is

type BlobArg

type BlobArg []byte

BlobArg is a Blob Arg

func NewBlobArg

func NewBlobArg(buf []byte) *BlobArg

NewBlobArg simply returns the same byte array given to it

func (*BlobArg) Bytes

func (a *BlobArg) Bytes() []byte

Bytes returns the underlying bytes for this Arg

type JSONArg

type JSONArg []byte

JSONArg is a JSON Arg

func NewJSONArg

func NewJSONArg(v interface{}) (*JSONArg, error)

NewJSONArg will return a new instance of JSONArg from marshalled json

func (*JSONArg) Bytes

func (a *JSONArg) Bytes() []byte

Bytes returns the underlying bytes for this Arg

type Message

type Message struct {
	Args []Arg
}

Message is a high level AMP message

func New

func New(args []Arg) *Message

New will return a AMP Message from the provided args as bytes

func NewFromBytes

func NewFromBytes(msg []byte) (*Message, error)

NewFromBytes will return a AMP Message from the provided args as encoded bytes

func (*Message) Inspect

func (m *Message) Inspect() string

Inspect the Message

func (*Message) Pop

func (m *Message) Pop() Arg

Pop an arg off the tail of a Message

func (*Message) Push

func (m *Message) Push(arg Arg) []Arg

Push an arg onto the tail of a Message

func (*Message) PushBlob

func (m *Message) PushBlob(b []byte) []Arg

PushBlob will push a BlobArg given a byte array onto the tail of a Message

func (*Message) PushJSON

func (m *Message) PushJSON(v interface{}) ([]Arg, error)

PushJSON will push a JSONArg given a string onto the tail of a Message

func (*Message) PushString

func (m *Message) PushString(s string) []Arg

PushString will push a StringArg given a string onto the tail of a Message

func (*Message) Shift

func (m *Message) Shift() Arg

Shift an arg off the head of a Message

func (*Message) ToBytes

func (m *Message) ToBytes() []byte

ToBytes return an encoded AMP Message

func (*Message) Unshift

func (m *Message) Unshift(arg Arg) []Arg

Unshift an arg onto the head of a Message

func (*Message) UnshiftBlob

func (m *Message) UnshiftBlob(b []byte) []Arg

UnshiftBlob will unshift a BlobArg given a byte array onto the head of a Message

func (*Message) UnshiftJSON

func (m *Message) UnshiftJSON(v interface{}) ([]Arg, error)

UnshiftJSON will unshift a JSONArg given a string onto the head of a Message

func (*Message) UnshiftString

func (m *Message) UnshiftString(s string) []Arg

UnshiftString will unshift a StringArg given a string onto the head of a Message

type StringArg

type StringArg []byte

StringArg is a String Arg

func NewStringArg

func NewStringArg(str string) *StringArg

NewStringArg will return a new instance of StringArg from a string

func (*StringArg) Bytes

func (a *StringArg) Bytes() []byte

Bytes returns the underlying bytes for this Arg

Jump to

Keyboard shortcuts

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