Update caller readme
This commit is contained in:
parent
e05c84027c
commit
98b341ec02
|
@ -7,62 +7,21 @@ boilerplate and making package interaction code sexier.
|
||||||
|
|
||||||
[Documentation](https://godoc.org/github.com/localhots/shezmu/caller)
|
[Documentation](https://godoc.org/github.com/localhots/shezmu/caller)
|
||||||
|
|
||||||
### Example
|
Caller abstracts away the process of unmarshaling data before processing.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"github.com/localhots/shezmu/caller"
|
|
||||||
"github.com/path/to/package/messenger"
|
|
||||||
)
|
|
||||||
|
|
||||||
type PriceUpdate struct {
|
type PriceUpdate struct {
|
||||||
Product string `json:"product"`
|
Product string `json:"product"`
|
||||||
Amount float32 `json:"amount"`
|
Amount float32 `json:"amount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func PriceUpdatePrinter(p PriceUpdate) {
|
||||||
messenger.Subscribe("ProductPriceUpdates", func(p PriceUpdate) {
|
|
||||||
log.Printf("Price for %q is now $%.2f", p.Product, p.Amount)
|
log.Printf("Price for %q is now $%.2f", p.Product, p.Amount)
|
||||||
})
|
}
|
||||||
messenger.Deliver()
|
|
||||||
}
|
// Error handling is skipped for clarity
|
||||||
```
|
func main() {
|
||||||
|
printer, _ := caller.New(PriceUpdatePrinter)
|
||||||
Support code:
|
_ = printer.Call([]byte(`{"product": "Paperclip", "amount": 0.01}`))
|
||||||
|
|
||||||
```go
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue