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"
"gitoa.ru/go-4devs/console/output/formatter"
"gitoa.ru/go-4devs/console/output/label"
"gitoa.ru/go-4devs/console/output/verbosity"
)
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...)
}
}

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

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

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

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

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

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

11
output/output.go

@ -6,6 +6,7 @@ import (
"io"
"os"
"gitoa.ru/go-4devs/console/output/label"
"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{}) {
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...))
}
@ -37,7 +38,7 @@ func (o Output) Info(ctx context.Context, args ...interface{}) {
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...))
}
@ -45,7 +46,7 @@ func (o Output) Debug(ctx context.Context, args ...interface{}) {
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...))
}
@ -53,7 +54,7 @@ func (o Output) Trace(ctx context.Context, args ...interface{}) {
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...))
}

3
output/quiet.go

@ -3,11 +3,12 @@ package output
import (
"context"
"gitoa.ru/go-4devs/console/output/label"
"gitoa.ru/go-4devs/console/output/verbosity"
)
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
}
}

3
output/verbosity.go

@ -3,11 +3,12 @@ package output
import (
"context"
"gitoa.ru/go-4devs/console/output/label"
"gitoa.ru/go-4devs/console/output/verbosity"
)
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 {
return out(ctx, v, msg, kv...)
}

9
output/writer.go

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

15
output/writer_test.go

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

Loading…
Cancel
Save