MCPcopy
hub / github.com/caddyserver/caddy / ExampleContext_LoadModule

Function ExampleContext_LoadModule

context_test.go:22–52  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

20)
21
22func 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
54func ExampleContext_LoadModule_array() {
55 // this whole first part is just setting up for the example;

Callers

nothing calls this directly

Calls 1

LoadModuleMethod · 0.95

Tested by

no test coverage detected