Browse Source

move kv to label

pull/2/head
andrey1s 4 years ago
parent
commit
4f91a55f2f
  1. 3
      output/formatter.go
  2. 2
      output/label/key.go
  3. 2
      output/label/kv.go
  4. 2
      output/label/value.go
  5. 11
      output/output.go
  6. 3
      output/quiet.go
  7. 3
      output/verbosity.go
  8. 9
      output/writer.go
  9. 15
      output/writer_test.go

3
output/formatter.go

@ -4,11 +4,12 @@ import (
"context" "context"
"gitoa.ru/go-4devs/console/output/formatter" "gitoa.ru/go-4devs/console/output/formatter"
"gitoa.ru/go-4devs/console/output/label"
"gitoa.ru/go-4devs/console/output/verbosity" "gitoa.ru/go-4devs/console/output/verbosity"
) )
func Format(out Output, format *formatter.Formatter) Output { func Format(out Output, format *formatter.Formatter) Output {
return func(ctx context.Context, v verbosity.Verbosity, msg string, kv ...KeyValue) (int, error) { return func(ctx context.Context, v verbosity.Verbosity, msg string, kv ...label.KeyValue) (int, error) {
return out(ctx, v, format.Format(ctx, msg), kv...) return out(ctx, v, format.Format(ctx, msg), kv...)
} }
} }

2
output/key.go → output/label/key.go

@ -1,4 +1,4 @@
package output package label
type Key string type Key string

2
output/kv.go → output/label/kv.go

@ -1,4 +1,4 @@
package output package label
import ( import (
"fmt" "fmt"

2
output/value.go → output/label/value.go

@ -1,4 +1,4 @@
package output package label
import "fmt" import "fmt"

11
output/output.go

@ -6,6 +6,7 @@ import (
"io" "io"
"os" "os"
"gitoa.ru/go-4devs/console/output/label"
"gitoa.ru/go-4devs/console/output/verbosity" "gitoa.ru/go-4devs/console/output/verbosity"
) )
@ -15,13 +16,13 @@ func writeError(_ int, err error) {
} }
} }
type Output func(ctx context.Context, verb verbosity.Verbosity, msg string, args ...KeyValue) (int, error) type Output func(ctx context.Context, verb verbosity.Verbosity, msg string, args ...label.KeyValue) (int, error)
func (o Output) Print(ctx context.Context, args ...interface{}) { func (o Output) Print(ctx context.Context, args ...interface{}) {
writeError(o(ctx, verbosity.Norm, fmt.Sprint(args...))) writeError(o(ctx, verbosity.Norm, fmt.Sprint(args...)))
} }
func (o Output) PrintKV(ctx context.Context, msg string, kv ...KeyValue) { func (o Output) PrintKV(ctx context.Context, msg string, kv ...label.KeyValue) {
writeError(o(ctx, verbosity.Norm, msg, kv...)) writeError(o(ctx, verbosity.Norm, msg, kv...))
} }
@ -37,7 +38,7 @@ func (o Output) Info(ctx context.Context, args ...interface{}) {
writeError(o(ctx, verbosity.Info, fmt.Sprint(args...))) writeError(o(ctx, verbosity.Info, fmt.Sprint(args...)))
} }
func (o Output) InfoKV(ctx context.Context, msg string, kv ...KeyValue) { func (o Output) InfoKV(ctx context.Context, msg string, kv ...label.KeyValue) {
writeError(o(ctx, verbosity.Info, msg, kv...)) writeError(o(ctx, verbosity.Info, msg, kv...))
} }
@ -45,7 +46,7 @@ func (o Output) Debug(ctx context.Context, args ...interface{}) {
writeError(o(ctx, verbosity.Debug, fmt.Sprint(args...))) writeError(o(ctx, verbosity.Debug, fmt.Sprint(args...)))
} }
func (o Output) DebugKV(ctx context.Context, msg string, kv ...KeyValue) { func (o Output) DebugKV(ctx context.Context, msg string, kv ...label.KeyValue) {
writeError(o(ctx, verbosity.Debug, msg, kv...)) writeError(o(ctx, verbosity.Debug, msg, kv...))
} }
@ -53,7 +54,7 @@ func (o Output) Trace(ctx context.Context, args ...interface{}) {
writeError(o(ctx, verbosity.Trace, fmt.Sprint(args...))) writeError(o(ctx, verbosity.Trace, fmt.Sprint(args...)))
} }
func (o Output) TraceKV(ctx context.Context, msg string, kv ...KeyValue) { func (o Output) TraceKV(ctx context.Context, msg string, kv ...label.KeyValue) {
writeError(o(ctx, verbosity.Trace, msg, kv...)) writeError(o(ctx, verbosity.Trace, msg, kv...))
} }

3
output/quiet.go

@ -3,11 +3,12 @@ package output
import ( import (
"context" "context"
"gitoa.ru/go-4devs/console/output/label"
"gitoa.ru/go-4devs/console/output/verbosity" "gitoa.ru/go-4devs/console/output/verbosity"
) )
func Quiet() Output { func Quiet() Output {
return func(context.Context, verbosity.Verbosity, string, ...KeyValue) (int, error) { return func(context.Context, verbosity.Verbosity, string, ...label.KeyValue) (int, error) {
return 0, nil return 0, nil
} }
} }

3
output/verbosity.go

@ -3,11 +3,12 @@ package output
import ( import (
"context" "context"
"gitoa.ru/go-4devs/console/output/label"
"gitoa.ru/go-4devs/console/output/verbosity" "gitoa.ru/go-4devs/console/output/verbosity"
) )
func Verbosity(out Output, verb verbosity.Verbosity) Output { func Verbosity(out Output, verb verbosity.Verbosity) Output {
return func(ctx context.Context, v verbosity.Verbosity, msg string, kv ...KeyValue) (int, error) { return func(ctx context.Context, v verbosity.Verbosity, msg string, kv ...label.KeyValue) (int, error) {
if verb >= v { if verb >= v {
return out(ctx, v, msg, kv...) return out(ctx, v, msg, kv...)
} }

9
output/writer.go

@ -8,6 +8,7 @@ import (
"os" "os"
"strings" "strings"
"gitoa.ru/go-4devs/console/output/label"
"gitoa.ru/go-4devs/console/output/verbosity" "gitoa.ru/go-4devs/console/output/verbosity"
) )
@ -25,21 +26,21 @@ func Buffer(buf *bytes.Buffer) Output {
return New(buf, FormatString) return New(buf, FormatString)
} }
func FormatString(_ verbosity.Verbosity, msg string, kv ...KeyValue) string { func FormatString(_ verbosity.Verbosity, msg string, kv ...label.KeyValue) string {
if len(kv) > 0 { if len(kv) > 0 {
nline := "" nline := ""
if msg[len(msg)-1:] == newline { if msg[len(msg)-1:] == newline {
nline = newline nline = newline
} }
return "msg=\"" + strings.TrimSpace(msg) + "\", " + KeyValues(kv).String() + nline return "msg=\"" + strings.TrimSpace(msg) + "\", " + label.KeyValues(kv).String() + nline
} }
return msg return msg
} }
func New(w io.Writer, format func(verb verbosity.Verbosity, msg string, kv ...KeyValue) string) Output { func New(w io.Writer, format func(verb verbosity.Verbosity, msg string, kv ...label.KeyValue) string) Output {
return func(ctx context.Context, verb verbosity.Verbosity, msg string, kv ...KeyValue) (int, error) { return func(ctx context.Context, verb verbosity.Verbosity, msg string, kv ...label.KeyValue) (int, error) {
return fmt.Fprint(w, format(verb, msg, kv...)) return fmt.Fprint(w, format(verb, msg, kv...))
} }
} }

15
output/writer_test.go

@ -6,6 +6,7 @@ import (
"testing" "testing"
"gitoa.ru/go-4devs/console/output" "gitoa.ru/go-4devs/console/output"
"gitoa.ru/go-4devs/console/output/label"
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
@ -15,23 +16,23 @@ func TestNew(t *testing.T) {
cases := map[string]struct { cases := map[string]struct {
ex string ex string
kv []output.KeyValue kv []label.KeyValue
}{ }{
"message": { "message": {
ex: "message", ex: "message",
}, },
"msg with kv": { "msg with kv": {
ex: "msg=\"msg with kv\", string key=\"string value\", bool key=\"false\", int key=\"42\"", ex: "msg=\"msg with kv\", string key=\"string value\", bool key=\"false\", int key=\"42\"",
kv: []output.KeyValue{ kv: []label.KeyValue{
output.String("string key", "string value"), label.String("string key", "string value"),
output.Bool("bool key", false), label.Bool("bool key", false),
output.Int("int key", 42), label.Int("int key", 42),
}, },
}, },
"msg with newline \n": { "msg with newline \n": {
ex: "msg=\"msg with newline\", int=\"42\"\n", ex: "msg=\"msg with newline\", int=\"42\"\n",
kv: []output.KeyValue{ kv: []label.KeyValue{
output.Int("int", 42), label.Int("int", 42),
}, },
}, },
} }

Loading…
Cancel
Save