From 2eea5ab8c6818e6b9a269b1687e84f0e314ce9f2 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 21 Feb 2019 16:30:34 +0100 Subject: [PATCH] Add dump util --- go.mod | 1 + go.sum | 2 ++ internal/util/dump.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 internal/util/dump.go diff --git a/go.mod b/go.mod index 99630a2..819e27f 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/minio/minio-go v6.0.14+incompatible github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/pkg/errors v0.8.0 + github.com/sanity-io/litter v1.1.0 github.com/smartystreets/assertions v0.0.0-20190215210624-980c5ac6f3ac // indirect github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c // indirect go.etcd.io/etcd v0.0.0-20181128220305-dedae6eb7c25 diff --git a/go.sum b/go.sum index d67a1e7..99af4af 100644 --- a/go.sum +++ b/go.sum @@ -94,6 +94,8 @@ github.com/prometheus/common v0.0.0-20180518154759-7600349dcfe1 h1:osmNoEW2SCW3L github.com/prometheus/common v0.0.0-20180518154759-7600349dcfe1/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/procfs v0.0.0-20180612222113-7d6f385de8be h1:MoyXp/VjXUwM0GyDcdwT7Ubea2gxOSHpPaFo3qV+Y2A= github.com/prometheus/procfs v0.0.0-20180612222113-7d6f385de8be/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/sanity-io/litter v1.1.0 h1:BllcKWa3VbZmOZbDCoszYLk7zCsKHz5Beossi8SUcTc= +github.com/sanity-io/litter v1.1.0/go.mod h1:CJ0VCw2q4qKU7LaQr3n7UOSHzgEMgcGco7N/SkZQPjw= github.com/sirupsen/logrus v1.0.5 h1:8c8b5uO0zS4X6RPl/sd1ENwSkIc0/H2PaHxE3udaE8I= github.com/sirupsen/logrus v1.0.5/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= github.com/smartystreets/assertions v0.0.0-20190215210624-980c5ac6f3ac h1:wbW+Bybf9pXxnCFAOWZTqkRjAc7rAIwo2e1ArUhiHxg= diff --git a/internal/util/dump.go b/internal/util/dump.go new file mode 100644 index 0000000..0972359 --- /dev/null +++ b/internal/util/dump.go @@ -0,0 +1,40 @@ +// Copyright 2019 Sorint.lab +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied +// See the License for the specific language governing permissions and +// limitations under the License. + +package util + +import ( + "fmt" + + "github.com/sanity-io/litter" +) + +// dump implements the fmt.Formatter interface and can be used instead of a +// direct call to litter.Sdump. +// In this way litter.Sdump will be executed only when really needed without +// consuming CPU when not required. +// I.E. if logging with zap using log.Debugf("dumped value: ", util.Dump(value)), +// the formatting (and so the call to litter.Sdump) won't happen if the log +// level is less than debug. +type dump struct { + data interface{} +} + +func (d *dump) Format(f fmt.State, c rune) { + f.Write([]byte(litter.Sdump(d.data))) +} + +func Dump(data interface{}) *dump { + return &dump{data: data} +}