Categorygithub.com/thingsdb/go-timod
modulepackage
0.0.3
Repository: https://github.com/thingsdb/go-timod.git
Documentation: pkg.go.dev

# README

timod

Library for creating ThingsDB modules using the Go language

Installation

Simple install the package to your $GOPATH with the go tool from shell:

$ go get github.com/thingsdb/go-timod

Make sure Git is installed on your machine and in your system's PATH.

Usage

Modules for ThingsDB are simple binary files which should read from stdin and write a response back to stdout. Work by the module must be non-blocking. When a request is received from ThingsDB, the data contains a package id (Pid) which should be used for the response. This is used by ThingsDB to map the response to the correct request since responses do not have to be written back to ThingsDB in order.

If the module requires configuration data, for example a connection string, then this configuration will be send immediately after start-up but may be received again when the module configuration is changed in ThingsDB.

Do not use functions like Println and Printf since these function will write to stdout and this is reserved for ThingsDB. Instead, use log.Print.. to write to stderr instead.

The following code may be used as a template: (see: https://github.com/thingsdb/ThingsDB/tree/master/modules/go/demo)

package main

import (
    "fmt"
    "log"

    timod "github.com/thingsdb/go-timod"

    "github.com/vmihailenco/msgpack"
)

func handler(buf *timod.Buffer, quit chan bool) {
    for {
        select {
        case pkg := <-buf.PkgCh:
            switch timod.Proto(pkg.Tp) {
            case timod.ProtoModuleConf:
                // Configuration data for this module is received from ThingsDB.
                //
                // The module should respond with:
                //
                // - timod.WriteConfOk(): if successful
                // - timod.WriteConfErr(): in case the configuration has failed
                log.Println("No configuration data is required for this module")
                timod.WriteConfOk() // Just write OK

            case timod.ProtoModuleReq:
                // A request from ThingsDB may be unpacked to a struct or to
                // an map[string]interface{}.
                //
                // The module should respond with:
                //
                // - timod.WriteResponse(pid, value): if successful
                // - timod.WriteEx(pid, err_code, err_msg): in case of an error
                //
                // (alternative to WriteResponse, WriteResponseRaw is available
                //  for a pre-packed, raw response)
                type Demo struct {
                    Message string `msgpack:"message"`
                }
                var demo Demo

                // pkg.Data contains Message Packed data, most likely you want
                // to unpack the data into a struct.
                err := msgpack.Unmarshal(pkg.Data, &demo)
                if err == nil {
                    // In this demo a `message` property will be unpacked and
                    // used as a return value.
                    timod.WriteResponse(pkg.Pid, &demo.Message)
                } else {
                    // In case of an error, make sure to call `WriteEx(..)` so
                    // ThingsDB can finish the future request with an
                    // appropriate error. (see error codes below)
                    timod.WriteEx(
                        pkg.Pid,
                        timod.ExBadData,
                        fmt.Sprintf("failed to unpack request (%s)", err))
				}

            default:
                log.Printf("Error: Unexpected package type: %d", pkg.Tp)
			}
        case err := <-buf.ErrCh:
            // In case of an error you probably want to quit the module.
            // ThingsDB will try to restart the module a few times if this
            // happens.
            log.Printf("Error: %s", err)
            quit <- true
        }
    }
}

func main() {
    // Starts the module
    timod.StartModule("demo", handler)

    // It is possible to add some cleanup code here
}

Exceptions

These are the possible exceptions which may be used. Do not use any other exception code, as ThingsDB will only accept values withing the given range.

  • ExCancelled - operation is cancelled before completion
  • ExOperation - operation is not valid in the current context
  • ExNumArguments - wrong number of arguments
  • ExTypeError - object of inappropriate type
  • ExValueError - object has the right type but an inappropriate value
  • ExOverflow - integer overflow
  • ExZeroDiv - division or module by zero
  • ExMaxQuota - max quota is reached
  • ExAuthError - authentication error
  • ExForbidden - forbidden (access denied)
  • ExLookupError - requested resource not found
  • ExBadData - unable to handle request due to invalid data
  • ExSyntaxError - syntax error in query
  • ExNodeError - node is temporary unable to handle the request
  • ExAssertError - assertion statement has failed
  • ExCustom100..ExCustom127 - can be used as a custom errors

# Functions

NewBuffer retur a pointer to a new buffer.
PkgEmpty can be used to create an empty package.
PkgPack returns a byte array containing a header with serialized data.
PkgPackBin returns a byte array containing a header with serialized data.
StartModule can be used to start the module.
WriteConfErr should be used when module configuration has failed.
WriteConfOk should be used when the module is successfully configured.
WriteEx can be used to write an error response.
WriteResponse can be used to write a response.
WriteResponseRaw can be used to write a raw response.

# Constants

ExAssertError - assertion statement has failed.
ExAuthError - authentication error.
ExBadData - unable to handle request due to invalid data.
ExCancelled - operation is cancelled before completion.
ExCustom100 can be used as a custom error.
ExCustom101 can be used as a custom error.
ExCustom102 can be used as a custom error.
ExCustom103 can be used as a custom error.
ExCustom104 can be used as a custom error.
ExCustom105 can be used as a custom error.
ExCustom106 can be used as a custom error.
ExCustom107 can be used as a custom error.
ExCustom108 can be used as a custom error.
ExCustom109 can be used as a custom error.
ExCustom110 can be used as a custom error.
ExCustom111 can be used as a custom error.
ExCustom112 can be used as a custom error.
ExCustom113 can be used as a custom error.
ExCustom114 can be used as a custom error.
ExCustom115 can be used as a custom error.
ExCustom116 can be used as a custom error.
ExCustom117 can be used as a custom error.
ExCustom118 can be used as a custom error.
ExCustom119 can be used as a custom error.
ExCustom120 can be used as a custom error.
ExCustom121 can be used as a custom error.
ExCustom122 can be used as a custom error.
ExCustom123 can be used as a custom error.
ExCustom124 can be used as a custom error.
ExCustom125 can be used as a custom error.
ExCustom126 can be used as a custom error.
ExCustom127 can be used as a custom error.
ExForbidden - forbidden (access denied).
ExLookupError - requested resource not found.
ExMaxQuota - max quota is reached.
ExNodeError - node is temporary unable to handle the request.
ExNumArguments - wrong number of arguments.
ExOperation - operation is not valid in the current context.
ExOverflow - integer overflow.
ExSyntaxError - syntax error in query.
ExTypeError - object of inappropriate type.
ExValueError - object has the right type but an inappropriate value.
ExZeroDiv - division or module by zero.
ProtoModuleConf - when configuration data for the module is received.
ProtoModuleConfErr - respond with a configuration error.
ProtoModuleConfOk - respond after successfully configuring the module.
ProtoModuleErr is used to respond to a ProtoModuleReq with an error.
ProtoModuleReq - when a request is received.
ProtoModuleRes is used to respond to a ProtoModuleReq package.
Version exposes the version of this library.

# Structs

Buffer is used to read data from a connection.
Pkg contains of a header and data.

# Type aliases

Ex is used as protocol type used by ThingsDB.
Proto is used as protocol type used by ThingsDB.