package datacapsule

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

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...
  }

})

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

Index

Functions

func Pack deprecated

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)
})

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

Types

type Capsule deprecated

type Capsule struct {
}

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).

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func NewCapsule deprecated

func NewCapsule() *Capsule

NewCapsule returns a new empty Capsule instance.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func Unpack deprecated

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...
  }

})

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) Export deprecated

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

Export returns its internal map data.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsArray deprecated

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

GetAsArray returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsBool deprecated

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

GetAsBool returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsBytes deprecated

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

GetAsBytes returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsCapsule deprecated

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

GetAsCapsule returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsFloat64 deprecated

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

GetAsFloat64 returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsInt16 deprecated

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

GetAsInt16 returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsInt32 deprecated

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

GetAsInt32 returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsInt64 deprecated

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

GetAsInt64 returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsInt8 deprecated

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

GetAsInt8 returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsMap deprecated

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

GetAsMap returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsString deprecated

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

GetAsString returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsUint16 deprecated

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

GetAsUint16 returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsUint32 deprecated

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

GetAsUint32 returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsUint64 deprecated

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

GetAsUint64 returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) GetAsUint8 deprecated

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

GetAsUint8 returns the value of the name given.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) Import deprecated

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

Import replaces its internal map data with imported data map.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsArray deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsBool deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsBytes deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsCapsule deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsFloat64 deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsInt16 deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsInt32 deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsInt64 deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsInt8 deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsMap deprecated

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.

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsString deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsUint16 deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsUint32 deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsUint64 deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.

func (*Capsule) SetAsUint8 deprecated

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

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

Deprecated: Please use either Diarkis transport data, or Diarkis proto instead.