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

Function ExampleContext_LoadModule_array

context_test.go:54–85  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

52}
53
54func ExampleContext_LoadModule_array() {
55 // this whole first part is just setting up for the example;
56 // note the struct tags - very important; we specify inline_key
57 // because that is the only way to know the module name
58 var ctx Context
59 myStruct := &struct {
60 // This godoc comment will appear in module documentation.
61 GuestModulesRaw []json.RawMessage `json:"guest_modules,omitempty" caddy:"namespace=example inline_key=name"`
62
63 // this is where the decoded module will be stored; in this
64 // example, we pretend we need an io.Writer but it can be
65 // any interface type that is useful to you
66 guestModules []io.Writer
67 }{
68 GuestModulesRaw: []json.RawMessage{
69 json.RawMessage(`{"name":"module1_name","foo":"bar1"}`),
70 json.RawMessage(`{"name":"module2_name","foo":"bar2"}`),
71 },
72 }
73
74 // since our input is []json.RawMessage, the output will be []any
75 mods, err := ctx.LoadModule(myStruct, "GuestModulesRaw")
76 if err != nil {
77 // you'd want to actually handle the error here
78 // return fmt.Errorf("loading guest modules: %v", err)
79 }
80 for _, mod := range mods.([]any) {
81 myStruct.guestModules = append(myStruct.guestModules, mod.(io.Writer))
82 }
83
84 // use myStruct.guestModules from now on
85}
86
87func ExampleContext_LoadModule_map() {
88 // 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