diff options
author | Matthew Holt <[email protected]> | 2021-01-05 14:39:30 -0700 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2021-01-05 14:39:30 -0700 |
commit | f0216967dca12831b1aac351fc8c4bfcea148697 (patch) | |
tree | 7e444eec3fd296dd45d27a3e36897b37d2ae1b6d /modules/logging | |
parent | b1bec8c8992424b423db3d92101bd5b4989dcf65 (diff) | |
download | caddy-f0216967dca12831b1aac351fc8c4bfcea148697.tar.gz caddy-f0216967dca12831b1aac351fc8c4bfcea148697.zip |
caddyfile: Refactor unmarshaling of module tokens
Eliminates a fair amount of repeated code
Diffstat (limited to 'modules/logging')
-rw-r--r-- | modules/logging/filterencoder.go | 30 |
1 files changed, 8 insertions, 22 deletions
diff --git a/modules/logging/filterencoder.go b/modules/logging/filterencoder.go index d1c335f46..cdb552d8e 100644 --- a/modules/logging/filterencoder.go +++ b/modules/logging/filterencoder.go @@ -115,21 +115,14 @@ func (fe *FilterEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { return d.ArgErr() } moduleName := d.Val() - mod, err := caddy.GetModule("caddy.logging.encoders." + moduleName) - if err != nil { - return d.Errf("getting log encoder module named '%s': %v", moduleName, err) - } - unm, ok := mod.New().(caddyfile.Unmarshaler) - if !ok { - return d.Errf("log encoder module '%s' is not a Caddyfile unmarshaler", mod) - } - err = unm.UnmarshalCaddyfile(d.NewFromNextSegment()) + moduleID := "caddy.logging.encoders." + moduleName + unm, err := caddyfile.UnmarshalModule(d, moduleID) if err != nil { return err } enc, ok := unm.(zapcore.Encoder) if !ok { - return d.Errf("module %s is not a zapcore.Encoder", mod) + return d.Errf("module %s (%T) is not a zapcore.Encoder", moduleID, unm) } fe.WrappedRaw = caddyconfig.JSONModuleObject(enc, "format", moduleName, nil) @@ -140,26 +133,19 @@ func (fe *FilterEncoder) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { return d.ArgErr() } filterName := d.Val() - mod, err := caddy.GetModule("caddy.logging.encoders.filter." + filterName) - if err != nil { - return d.Errf("getting log filter module named '%s': %v", filterName, err) - } - unm, ok := mod.New().(caddyfile.Unmarshaler) - if !ok { - return d.Errf("log encoder module '%s' is not a Caddyfile unmarshaler", mod) - } - err = unm.UnmarshalCaddyfile(d.NewFromNextSegment()) + moduleID := "caddy.logging.encoders.filter." + filterName + unm, err := caddyfile.UnmarshalModule(d, moduleID) if err != nil { return err } - f, ok := unm.(LogFieldFilter) + filter, ok := unm.(LogFieldFilter) if !ok { - return d.Errf("module %s is not a LogFieldFilter", mod) + return d.Errf("module %s (%T) is not a logging.LogFieldFilter", moduleID, unm) } if fe.FieldsRaw == nil { fe.FieldsRaw = make(map[string]json.RawMessage) } - fe.FieldsRaw[field] = caddyconfig.JSONModuleObject(f, "filter", filterName, nil) + fe.FieldsRaw[field] = caddyconfig.JSONModuleObject(filter, "filter", filterName, nil) } default: |