()
| 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 []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 | |
| 87 | func ExampleContext_LoadModule_map() { |
| 88 | // this whole first part is just setting up for the example; |
nothing calls this directly
no test coverage detected