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

Function JSONModuleObject

caddyconfig/configadapters.go:69–106  ·  view source on GitHub ↗

JSONModuleObject is like JSON(), except it marshals val into a JSON object with an added key named fieldName with the value fieldVal. This is useful for encoding module values where the module name has to be described within the object by a certain key; for example, `"handler": "file_server"` for a

(val any, fieldName, fieldVal string, warnings *[]Warning)

Source from the content-addressed store, hash-verified

67// The val parameter must encode into a map[string]any (i.e. it must be
68// a struct or map). Any errors are converted into warnings.
69func JSONModuleObject(val any, fieldName, fieldVal string, warnings *[]Warning) json.RawMessage {
70 // encode to a JSON object first
71 enc, err := json.Marshal(val)
72 if err != nil {
73 if warnings != nil {
74 *warnings = append(*warnings, Warning{Message: err.Error()})
75 }
76 return nil
77 }
78
79 // then decode the object
80 var tmp map[string]any
81 err = json.Unmarshal(enc, &tmp)
82 if err != nil {
83 if warnings != nil {
84 message := err.Error()
85 if jsonErr, ok := err.(*json.SyntaxError); ok {
86 message = fmt.Sprintf("%v, at offset %d", jsonErr.Error(), jsonErr.Offset)
87 }
88 *warnings = append(*warnings, Warning{Message: message})
89 }
90 return nil
91 }
92
93 // so we can easily add the module's field with its appointed value
94 tmp[fieldName] = fieldVal
95
96 // then re-marshal as JSON
97 result, err := json.Marshal(tmp)
98 if err != nil {
99 if warnings != nil {
100 *warnings = append(*warnings, Warning{Message: err.Error()})
101 }
102 return nil
103 }
104
105 return result
106}
107
108// RegisterAdapter registers a config adapter with the given name.
109// This should usually be done at init-time. It panics if the

Callers 15

UnmarshalCaddyfileMethod · 0.92
unmarshalCaddyfileMethod · 0.92
UnmarshalCaddyfileMethod · 0.92
UnmarshalCaddyfileMethod · 0.92
UnmarshalCaddyfileMethod · 0.92
UnmarshalCaddyfileMethod · 0.92
UnmarshalCaddyfileMethod · 0.92
UnmarshalCaddyfileMethod · 0.92
buildHTTPServerFunction · 0.92
parseCaddyfileHandlePathFunction · 0.92
NewTransportMethod · 0.92

Calls 1

ErrorMethod · 0.45

Tested by 1