@marcel91200 / caddy Mirror · read-only / commits / fab5e437
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

core: Add godoc examples for LoadModule

Matthew Holt committed Dec 10, 2019 at 14:06 UTC fab5e4372a941ee4259adea9295f49dcadca26a4
2 files changed +121 -1
context.go
+3 -1
@@ -124,7 +124,9 @@ func (ctx *Context) OnCancel(f func()) {
124 // and store them on the same struct. Storing them on the same struct makes for
125 // easy garbage collection when your host module is no longer needed.
126 //
127 -// Loaded modules have already been provisioned and validated.
127 +// Loaded modules have already been provisioned and validated. Upon returning
128 +// successfully, this method clears the json.RawMessage(s) in the field since
129 +// the raw JSON is no longer needed, and this allows the GC to free up memory.
130 func (ctx Context) LoadModule(structPointer interface{}, fieldName string) (interface{}, error) {
131 val := reflect.ValueOf(structPointer).Elem().FieldByName(fieldName)
132 typ := val.Type()
context_test.go new
+118
@@ -0,0 +1,118 @@
1 +// Copyright 2015 Matthew Holt and The Caddy Authors
2 +//
3 +// Licensed under the Apache License, Version 2.0 (the "License");
4 +// you may not use this file except in compliance with the License.
5 +// You may obtain a copy of the License at
6 +//
7 +// http://www.apache.org/licenses/LICENSE-2.0
8 +//
9 +// Unless required by applicable law or agreed to in writing, software
10 +// distributed under the License is distributed on an "AS IS" BASIS,
11 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 +// See the License for the specific language governing permissions and
13 +// limitations under the License.
14 +
15 +package caddy
16 +
17 +import (
18 + "encoding/json"
19 + "io"
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;
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 []interface{}
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.([]interface{}) {
81 + myStruct.guestModules = append(myStruct.guestModules, mod.(io.Writer))
82 + }
83 +
84 + // use myStruct.guestModules from now on
85 +}
86 +
87 +func 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]interface{}
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]interface{}) {
114 + myStruct.guestModules[modName] = mod.(io.Writer)
115 + }
116 +
117 + // use myStruct.guestModules from now on
118 +}