docs(backpack,beachcomber,boardwalk,bonfire,bouncer): compile README Go examples via src= (quick-261118-qsd)
- backpack: Greeter and ExampleApp_Publish replace the whole-file fence - beachcomber: Post's Searchable methods plus a new SearchIndexSchema, a Plugin.Boot that installs the gate, and the existing gate/search regions - boardwalk: new example_test.go; the mount region runs in ExampleHandler - bonfire: importPostsCommand and acmeMain (the binary's main as a function returning the exit code), run by ExampleExecute; ExampleCatalog_Has - bouncer: users/FindByID and ExampleNewJWTGuard; RefreshRoute run by TestDocsRefreshRoute on a production compass config
This commit is contained in:
@@ -30,45 +30,36 @@ Use cases:
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
package blog
|
||||
```go src=modules/backpack/example_test.go#Greeter
|
||||
// Greeter is a service the acme.greeter plugin offers to other plugins.
|
||||
type Greeter interface {
|
||||
Greet(name string) string
|
||||
}
|
||||
```
|
||||
|
||||
import (
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/compass"
|
||||
)
|
||||
```go src=modules/backpack/example_test.go#ExampleApp_Publish
|
||||
app := backpack.New(&compass.Config{})
|
||||
app.SetPlugins([]string{"acme.greeter", "acme.blog"})
|
||||
|
||||
// Feed is a service the blog plugin offers to other plugins.
|
||||
type Feed interface {
|
||||
Latest(n int) []string
|
||||
// acme.greeter, in its Register step: publish under the interface type.
|
||||
var greeter Greeter = englishGreeter{}
|
||||
if err := app.Publish(greeter); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
type staticFeed struct{}
|
||||
|
||||
func (staticFeed) Latest(n int) []string { return []string{"hello-world"} }
|
||||
|
||||
func setup() error {
|
||||
cfg, err := compass.Load("config")
|
||||
if err != nil {
|
||||
return err
|
||||
// acme.blog, in its Boot step: look the service up by the same type.
|
||||
if app.HasPlugin("acme.greeter") {
|
||||
if found, ok := app.Lookup[Greeter](); ok {
|
||||
fmt.Println(found.Greet("blog"))
|
||||
}
|
||||
app := backpack.New(cfg)
|
||||
app.SetPlugins([]string{"acme.blog", "acme.search"})
|
||||
|
||||
// Provider side, usually in the plugin's Register step.
|
||||
var feed Feed = staticFeed{}
|
||||
if err := app.Publish(feed); err != nil { // published under Feed, not staticFeed
|
||||
return err
|
||||
}
|
||||
|
||||
// Consumer side, usually in another plugin's Boot step.
|
||||
if app.HasPlugin("acme.blog") {
|
||||
if found, ok := app.Lookup[Feed](); ok {
|
||||
_ = found.Latest(5)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// A second Publish under the same type is refused.
|
||||
fmt.Println(app.Publish(greeter) != nil)
|
||||
// Output:
|
||||
// Hello, blog
|
||||
// true
|
||||
```
|
||||
|
||||
Publish under an interface type when consumers should not depend on the concrete implementation; the lookup must use exactly the same type argument.
|
||||
|
||||
Reference in New Issue
Block a user