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

Function ExampleContext_LoadModule_map

context_test.go:87–118  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

85}
86
87func ExampleContext_LoadModule_map() {
88 // this whole first part is just setting up for the example;
89 // note the struct tags - very important; we don't specify
90 // inline_key because the map key is the module name
91 var ctx Context
92 myStruct := &struct {
93 // This godoc comment will appear in module documentation.
94 GuestModulesRaw ModuleMap `json:"guest_modules,omitempty" caddy:"namespace=example"`
95
96 // this is where the decoded module will be stored; in this
97 // example, we pretend we need an io.Writer but it can be
98 // any interface type that is useful to you
99 guestModules map[string]io.Writer
100 }{
101 GuestModulesRaw: ModuleMap{
102 "module1_name": json.RawMessage(`{"foo":"bar1"}`),
103 "module2_name": json.RawMessage(`{"foo":"bar2"}`),
104 },
105 }
106
107 // since our input is map[string]json.RawMessage, the output will be map[string]any
108 mods, err := ctx.LoadModule(myStruct, "GuestModulesRaw")
109 if err != nil {
110 // you'd want to actually handle the error here
111 // return fmt.Errorf("loading guest modules: %v", err)
112 }
113 for modName, mod := range mods.(map[string]any) {
114 myStruct.guestModules[modName] = mod.(io.Writer)
115 }
116
117 // use myStruct.guestModules from now on
118}

Callers

nothing calls this directly

Calls 1

LoadModuleMethod · 0.95

Tested by

no test coverage detected