1
0
Fork 0
shezmu/caller/README.md

69 lines
1.4 KiB
Markdown
Raw Normal View History

2015-10-13 21:08:45 +00:00
# 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.
2015-10-13 21:08:45 +00:00
2015-10-14 00:50:43 +00:00
[Documentation](https://godoc.org/github.com/localhots/satan/caller)
2015-10-13 21:08:45 +00:00
### Example
2015-10-13 21:08:45 +00:00
```go
package main
import (
"log"
2015-10-14 00:50:43 +00:00
"github.com/localhots/satan/caller"
"github.com/path/to/package/messenger"
2015-10-13 21:08:45 +00:00
)
type PriceUpdate struct {
Product string `json:"product"`
Amount float32 `json:"amount"`
2015-10-13 21:08:45 +00:00
}
func main() {
messenger.Subscribe("ProductPriceUpdates", func(p PriceUpdate) {
log.Printf("Price for %q is now $%.2f", p.Product, p.Amount)
})
messenger.Deliver()
2015-10-13 21:08:45 +00:00
}
```
2015-10-13 21:08:45 +00:00
Support code:
```go
package messenger
import (
2015-10-14 00:50:43 +00:00
"github.com/localhots/satan/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)
}
}
2015-10-13 21:08:45 +00:00
}
```