Improve caller readme and package description
This commit is contained in:
		
							parent
							
								
									318d29bdd6
								
							
						
					
					
						commit
						e08841fdc7
					
				| @ -1,30 +1,68 @@ | ||||
| # Caller | ||||
| 
 | ||||
| Package caller is used to dynamicly call functions with data unmarshalled | ||||
| into the functions' first argument. It's main purpose is to hide common | ||||
| unmarshalling code from each function's implementation thus reducing | ||||
| boilerplate and making the code sexier. | ||||
| 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](https://godoc.org/github.com/localhots/uberdaemon/caller) | ||||
| 
 | ||||
| ### Example | ||||
| 
 | ||||
| ```go | ||||
| package main | ||||
| 
 | ||||
| import ( | ||||
|     "log" | ||||
|     "github.com/localhots/uberdaemon/caller" | ||||
|     "github.com/path/to/package/messenger" | ||||
| ) | ||||
| 
 | ||||
| type message struct { | ||||
|     Title string `json:"title"` | ||||
|     Body  string `json:"body"` | ||||
| } | ||||
| 
 | ||||
| func processMessage(m message) { | ||||
|     fmt.Printf("Title: %s\nBody: %s\n", m.Title, m.Body) | ||||
| type PriceUpdate struct { | ||||
|     Product string  `json:"product"` | ||||
|     Amount  float32 `json:"amount"` | ||||
| } | ||||
| 
 | ||||
| func main() { | ||||
|     c, _ := caller.New(processMessage) | ||||
|     c.Call(`{"title": "Hello", "body": "World"}`) | ||||
|     messenger.Subscribe("ProductPriceUpdates", func(p PriceUpdate) { | ||||
|         log.Printf("Price for %q is now $%.2f", p.Product, p.Amount) | ||||
|     }) | ||||
|     messenger.Deliver() | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| Support code: | ||||
| 
 | ||||
| ```go | ||||
| package messenger | ||||
| 
 | ||||
| import ( | ||||
|     "github.com/localhots/uberdaemon/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) | ||||
|         } | ||||
|     } | ||||
| } | ||||
| ``` | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| // Package caller is used to dynamicly call functions with data unmarshalled | ||||
| // into the functions' first argument. It's main purpose is to hide common | ||||
| // unmarshalling code from each function's implementation thus reducing | ||||
| // boilerplate and making the code sexier. | ||||
| // 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. | ||||
| package caller | ||||
| 
 | ||||
| import ( | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user