()
| 20 | ) |
| 21 | |
| 22 | func ExampleContext_LoadModule() { |
| 23 | // this whole first part is just setting up for the example; |
| 24 | // note the struct tags - very important; we specify inline_key |
| 25 | // because that is the only way to know the module name |
| 26 | var ctx Context |
| 27 | myStruct := &struct { |
| 28 | // This godoc comment will appear in module documentation. |
| 29 | GuestModuleRaw json.RawMessage `json:"guest_module,omitempty" caddy:"namespace=example inline_key=name"` |
| 30 | |
| 31 | // this is where the decoded module will be stored; in this |
| 32 | // example, we pretend we need an io.Writer but it can be |
| 33 | // any interface type that is useful to you |
| 34 | guestModule io.Writer |
| 35 | }{ |
| 36 | GuestModuleRaw: json.RawMessage(`{"name":"module_name","foo":"bar"}`), |
| 37 | } |
| 38 | |
| 39 | // if a guest module is provided, we can load it easily |
| 40 | if myStruct.GuestModuleRaw != nil { |
| 41 | mod, err := ctx.LoadModule(myStruct, "GuestModuleRaw") |
| 42 | if err != nil { |
| 43 | // you'd want to actually handle the error here |
| 44 | // return fmt.Errorf("loading guest module: %v", err) |
| 45 | } |
| 46 | // mod contains the loaded and provisioned module, |
| 47 | // it is now ready for us to use |
| 48 | myStruct.guestModule = mod.(io.Writer) |
| 49 | } |
| 50 | |
| 51 | // use myStruct.guestModule from now on |
| 52 | } |
| 53 | |
| 54 | func ExampleContext_LoadModule_array() { |
| 55 | // this whole first part is just setting up for the example; |
nothing calls this directly
no test coverage detected