From 67465fbc57a5bbfd03c69f44555f68b14bdfbb2b Mon Sep 17 00:00:00 2001 From: Antoine Grondin Date: Thu, 1 Sep 2016 09:58:51 -0400 Subject: [PATCH] fix keep flag to keep unchanged keys --- handler.go | 32 +++++++++++++++++++------------- json_handler.go | 2 +- logrus_handler.go | 2 +- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/handler.go b/handler.go index bc77a8ee..ad30c01f 100644 --- a/handler.go +++ b/handler.go @@ -35,26 +35,32 @@ type HandlerOptions struct { ValRGB RGB } -func (h *HandlerOptions) setup() { - h.Skip = make(map[string]struct{}) - h.Keep = make(map[string]struct{}) -} - func (h *HandlerOptions) shouldShowKey(key string) bool { if len(h.Keep) != 0 { - _, keep := h.Keep[key] - return keep + if _, keep := h.Keep[key]; keep { + return true + } } if len(h.Skip) != 0 { - _, skip := h.Skip[key] - return !skip + if _, skip := h.Skip[key]; skip { + return false + } } return true } +func (h *HandlerOptions) shouldShowUnchanged(key string) bool { + if len(h.Keep) != 0 { + if _, keep := h.Keep[key]; keep { + return true + } + } + return false +} + func (h *HandlerOptions) SetSkip(skip []string) { - if len(h.Keep) != 0 || h.Skip == nil { - h.setup() + if h.Skip == nil { + h.Skip = make(map[string]struct{}) } for _, key := range skip { h.Skip[key] = struct{}{} @@ -62,8 +68,8 @@ func (h *HandlerOptions) SetSkip(skip []string) { } func (h *HandlerOptions) SetKeep(keep []string) { - if len(h.Skip) != 0 || h.Keep == nil { - h.setup() + if h.Keep == nil { + h.Keep = make(map[string]struct{}) } for _, key := range keep { h.Keep[key] = struct{}{} diff --git a/json_handler.go b/json_handler.go index 6dd2b27e..5f5f56e3 100644 --- a/json_handler.go +++ b/json_handler.go @@ -162,7 +162,7 @@ func (h *JSONHandler) joinKVs(skipUnchanged bool, sep string) []string { } if skipUnchanged { - if lastV, ok := h.last[k]; ok && lastV == v { + if lastV, ok := h.last[k]; ok && lastV == v && !h.Opts.shouldShowUnchanged(k) { continue } } diff --git a/logrus_handler.go b/logrus_handler.go index edf824ae..c7f67cf1 100644 --- a/logrus_handler.go +++ b/logrus_handler.go @@ -135,7 +135,7 @@ func (h *LogrusHandler) joinKVs(skipUnchanged bool, sep string) []string { } if skipUnchanged { - if lastV, ok := h.last[k]; ok && lastV == v { + if lastV, ok := h.last[k]; ok && lastV == v && !h.Opts.shouldShowUnchanged(k) { continue } }