caller

package
v0.0.0-...-e05c840 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2016 License: MIT Imports: 3 Imported by: 0

README

Caller

Package caller is used to dynamically call functions with data unmarshalled into the functions' first argument. Its main purpose is to hide common unmarshalling code from each function implementation thus reducing boilerplate and making package interaction code sexier.

Documentation

Example
package main

import (
    "log"
    "github.com/localhots/shezmu/caller"
    "github.com/path/to/package/messenger"
)

type PriceUpdate struct {
    Product string  `json:"product"`
    Amount  float32 `json:"amount"`
}

func main() {
    messenger.Subscribe("ProductPriceUpdates", func(p PriceUpdate) {
        log.Printf("Price for %q is now $%.2f", p.Product, p.Amount)
    })
    messenger.Deliver()
}

Support code:

package messenger

import (
    "github.com/localhots/shezmu/caller"
)

type item struct {
    topic   string
    payload []byte
}

var queue <-chan item
var subscriptions = make(map[string][]*caller.Caller)

func Subscribe(topic string, callback interface{}) {
    c, err := caller.New(processMessage)
    if err != nil {
        panic(err)
    }
    subcriptions[topic] = append(subcriptions[topic], c)
}

func Deliver() {
    for itm := range queue {
        for _, c := range subscriptions[itm.topic] {
            // Payload example:
            // {"product": "Paperclip", "amount": 0.01}
            c.Call(itm.payload)
        }
    }
}

Documentation

Overview

Package caller is used to dynamically call functions with data unmarshalled into the functions' first argument. Its main purpose is to hide common unmarshalling code from each function implementation thus reducing boilerplate and making package interaction code sexier.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidFunctionType is an error that is returned by the New function
	// when its argument is not a function.
	ErrInvalidFunctionType = errors.New("argument must be function")
	// ErrInvalidFunctionInArguments is an error that is returned by the New
	// function when its argument-function has a number of input arguments other
	// than 1.
	ErrInvalidFunctionInArguments = errors.New("function must have only one input argument")
	// ErrInvalidFunctionOutArguments is an error that is returned by the New
	// function when its argument-function returs any values.
	ErrInvalidFunctionOutArguments = errors.New("function must not have output arguments")
)

Functions

This section is empty.

Types

type Caller

type Caller struct {
	// Unmarshaller is a BYOB unmarshaller function. By default it uses JSON.
	Unmarshaller func(data []byte, v interface{}) error
	// contains filtered or unexported fields
}

Caller wraps a function and makes it ready to be dynamically called.

func New

func New(fun interface{}) (c *Caller, err error)

New creates a new Caller instance using the function given as an argument. It returns the Caller instance and an error if something is wrong with the argument-function.

func (*Caller) Call

func (c *Caller) Call(data []byte) error

Call creates an instance of the Caller function's argument type, unmarshalls the payload into it and dynamically calls the Caller function with this instance.

Jump to

Keyboard shortcuts

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