...

Package datacapsule

import "github.com/Diarkis/diarkis/datacapsule"
Overview
Index

Overview ▾

Package datacapsule ▷

The major flaw of using interface{} is breaking the strong type of golang.

datacapsule aims to help with this by using a data structure called Capsule.

Capsule encapsulates the data map and enforces the data type of each value in the map.

Packing the datacapsule to encode for network transport via mesh package of Diarkis Example of using datacapsule to create a return value for mesh HandleCommand and SendRequest:

// A mesh command handler with a callback for mesh.SendRequest
mesh.HandleCommand(someCmdID, func(requestData map[string]interface{}) ([]byte, error) {
  cp := datacapsule.NewCapsule()
  cp.SetAsString("name", "Bob")
  cp.SetAsBytes("byteMessate", []byte("Hello"))

  resp := make(map[string]interface{})

  packed, err := cp.Pack()
  if err != nil {
    // handle error here...
  }

  resp["packed"] = packed

  return mesh.CreateReturnBytes(resp)
})

Unpacking the encoded datacapsule data Example of datacapsule usage with mesh HandleCommand and SendRequest

// The callback that handles data returned by mesh.HandleCommand
mesh.SendRequest(someCmdID, targetNodeAddress, func(err error, resp map[string]interface{}) {
  packed := mesh.GetBytes(resp, "packed")
  cp, err := datacapsule.Unpack(packed)

  if err != nil {
    // handle error...
  }

  name, err := cp.GetAsString("name")

  if err != nil {
    // handle error...
  }

  byteMessage, err := cp.GetAsBytes("byteMessage")

  if err != nil {
    // handle error...
  }

})

Index ▾

func Pack(c *Capsule) ([]byte, error)
type Capsule
    func NewCapsule() *Capsule
    func Unpack(src []byte) (*Capsule, error)
    func (c *Capsule) Export() map[string]interface{}
    func (c *Capsule) GetAsArray(name string) ([]*Capsule, error)
    func (c *Capsule) GetAsBool(name string) (bool, error)
    func (c *Capsule) GetAsBytes(name string) ([]byte, error)
    func (c *Capsule) GetAsCapsule(name string) (*Capsule, error)
    func (c *Capsule) GetAsFloat64(name string) (float64, error)
    func (c *Capsule) GetAsInt16(name string) (int16, error)
    func (c *Capsule) GetAsInt32(name string) (int32, error)
    func (c *Capsule) GetAsInt64(name string) (int64, error)
    func (c *Capsule) GetAsInt8(name string) (int8, error)
    func (c *Capsule) GetAsMap(name string) (map[string]*Capsule, error)
    func (c *Capsule) GetAsString(name string) (string, error)
    func (c *Capsule) GetAsUint16(name string) (uint16, error)
    func (c *Capsule) GetAsUint32(name string) (uint32, error)
    func (c *Capsule) GetAsUint64(name string) (uint64, error)
    func (c *Capsule) GetAsUint8(name string) (uint8, error)
    func (c *Capsule) Import(src map[string]interface{}) error
    func (c *Capsule) SetAsArray(name string, val []*Capsule)
    func (c *Capsule) SetAsBool(name string, val bool)
    func (c *Capsule) SetAsBytes(name string, val []byte)
    func (c *Capsule) SetAsCapsule(name string, val *Capsule)
    func (c *Capsule) SetAsFloat64(name string, val float64)
    func (c *Capsule) SetAsInt16(name string, val int16)
    func (c *Capsule) SetAsInt32(name string, val int32)
    func (c *Capsule) SetAsInt64(name string, val int64)
    func (c *Capsule) SetAsInt8(name string, val int8)
    func (c *Capsule) SetAsMap(name string, val map[string]*Capsule)
    func (c *Capsule) SetAsString(name string, val string)
    func (c *Capsule) SetAsUint16(name string, val uint16)
    func (c *Capsule) SetAsUint32(name string, val uint32)
    func (c *Capsule) SetAsUint64(name string, val uint64)
    func (c *Capsule) SetAsUint8(name string, val uint8)

func Pack

func Pack(c *Capsule) ([]byte, error)

Pack converts Capsule into a byte array to be transported etc.

Example of using datacapsule to create a return value for mesh HandleCommand and SendRequest:

// A mesh command handler with a callback for mesh.SendRequest
mesh.HandleCommand(someCmdID, func(requestData map[string]interface{}) ([]byte, error) {
  cp := datacapsule.NewCapsule()
  cp.SetAsString("name", "Bob")
  cp.SetAsBytes("byteMessate", []byte("Hello"))

  resp := make(map[string]interface{})

  packed, err := cp.Pack()
  if err != nil {
    // handle error here...
  }

  resp["packed"] = packed

  return mesh.CreateReturnBytes(resp)
})

type Capsule

Capsule represents data map of interface{} and it enforces its properties' data type despite the properties being interface{} preventing unexpected errors and behaviors because of the interface{} properties.

For example Capsule can be used as a property value of Room. When you set Capsule as Room property,
use capsule.Export() to convert it into map[string]interface{}
And when you get Capsule from Room property, use capsule.Import(roomPropertyCapsuleMap).
type Capsule struct {
    // contains filtered or unexported fields
}

func NewCapsule

func NewCapsule() *Capsule

NewCapsule returns a new empty Capsule instance.

func Unpack

func Unpack(src []byte) (*Capsule, error)

Unpack converts packed Capsule back to Capsule.

Example of datacapsule usage with mesh HandleCommand and SendRequest

// The callback that handles data returned by mesh.HandleCommand
mesh.SendRequest(someCmdID, targetNodeAddress, func(err error, resp map[string]interface{}) {
  packed := mesh.GetBytes(resp, "packed")
  cp, err := datacapsule.Unpack(packed)

  if err != nil {
    // handle error...
  }

  name, err := cp.GetAsString("name")

  if err != nil {
    // handle error...
  }

  byteMessage, err := cp.GetAsBytes("byteMessage")

  if err != nil {
    // handle error...
  }

})

func (*Capsule) Export

func (c *Capsule) Export() map[string]interface{}

Export returns its internal map data.

func (*Capsule) GetAsArray

func (c *Capsule) GetAsArray(name string) ([]*Capsule, error)

GetAsArray returns the value of the name given.

func (*Capsule) GetAsBool

func (c *Capsule) GetAsBool(name string) (bool, error)

GetAsBool returns the value of the name given.

func (*Capsule) GetAsBytes

func (c *Capsule) GetAsBytes(name string) ([]byte, error)

GetAsBytes returns the value of the name given.

func (*Capsule) GetAsCapsule

func (c *Capsule) GetAsCapsule(name string) (*Capsule, error)

GetAsCapsule returns the value of the name given.

func (*Capsule) GetAsFloat64

func (c *Capsule) GetAsFloat64(name string) (float64, error)

GetAsFloat64 returns the value of the name given.

func (*Capsule) GetAsInt16

func (c *Capsule) GetAsInt16(name string) (int16, error)

GetAsInt16 returns the value of the name given.

func (*Capsule) GetAsInt32

func (c *Capsule) GetAsInt32(name string) (int32, error)

GetAsInt32 returns the value of the name given.

func (*Capsule) GetAsInt64

func (c *Capsule) GetAsInt64(name string) (int64, error)

GetAsInt64 returns the value of the name given.

func (*Capsule) GetAsInt8

func (c *Capsule) GetAsInt8(name string) (int8, error)

GetAsInt8 returns the value of the name given.

func (*Capsule) GetAsMap

func (c *Capsule) GetAsMap(name string) (map[string]*Capsule, error)

GetAsMap returns the value of the name given.

func (*Capsule) GetAsString

func (c *Capsule) GetAsString(name string) (string, error)

GetAsString returns the value of the name given.

func (*Capsule) GetAsUint16

func (c *Capsule) GetAsUint16(name string) (uint16, error)

GetAsUint16 returns the value of the name given.

func (*Capsule) GetAsUint32

func (c *Capsule) GetAsUint32(name string) (uint32, error)

GetAsUint32 returns the value of the name given.

func (*Capsule) GetAsUint64

func (c *Capsule) GetAsUint64(name string) (uint64, error)

GetAsUint64 returns the value of the name given.

func (*Capsule) GetAsUint8

func (c *Capsule) GetAsUint8(name string) (uint8, error)

GetAsUint8 returns the value of the name given.

func (*Capsule) Import

func (c *Capsule) Import(src map[string]interface{}) error

Import replaces its internal map data with imported data map.

func (*Capsule) SetAsArray

func (c *Capsule) SetAsArray(name string, val []*Capsule)

SetAsArray sets an array of Capsules to the internal map with name.

func (*Capsule) SetAsBool

func (c *Capsule) SetAsBool(name string, val bool)

SetAsBool sets a bool value to the internal map with name.

func (*Capsule) SetAsBytes

func (c *Capsule) SetAsBytes(name string, val []byte)

SetAsBytes sets a byte array to the internal map with name.

func (*Capsule) SetAsCapsule

func (c *Capsule) SetAsCapsule(name string, val *Capsule)

SetAsCapsule sets a Capsule value to the internal map with name.

func (*Capsule) SetAsFloat64

func (c *Capsule) SetAsFloat64(name string, val float64)

SetAsFloat64 sets a float64 value to the internal map with name.

func (*Capsule) SetAsInt16

func (c *Capsule) SetAsInt16(name string, val int16)

SetAsInt16 sets an int16 value to the internal map with name.

func (*Capsule) SetAsInt32

func (c *Capsule) SetAsInt32(name string, val int32)

SetAsInt32 sets an int32 value to the internal map with name.

func (*Capsule) SetAsInt64

func (c *Capsule) SetAsInt64(name string, val int64)

SetAsInt64 sets an int64 value to the internal map with name.

func (*Capsule) SetAsInt8

func (c *Capsule) SetAsInt8(name string, val int8)

SetAsInt8 sets an int8 value to the internal map with name.

func (*Capsule) SetAsMap

func (c *Capsule) SetAsMap(name string, val map[string]*Capsule)

SetAsMap sets a map of Capsules with string keys to the internal map with name.

func (*Capsule) SetAsString

func (c *Capsule) SetAsString(name string, val string)

SetAsString sets a string value to the internal map with name.

func (*Capsule) SetAsUint16

func (c *Capsule) SetAsUint16(name string, val uint16)

SetAsUint16 sets a uint16 value to the internal map with name.

func (*Capsule) SetAsUint32

func (c *Capsule) SetAsUint32(name string, val uint32)

SetAsUint32 sets a uint32 value to the internal map with name.

func (*Capsule) SetAsUint64

func (c *Capsule) SetAsUint64(name string, val uint64)

SetAsUint64 sets a uint64 value to the internal map with name.

func (*Capsule) SetAsUint8

func (c *Capsule) SetAsUint8(name string, val uint8)

SetAsUint8 sets a uint8 value to the internal map with name.