From d495f67045e2e7dfb4b0630485e3d6d952fcfa05 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 1 Jun 2021 15:10:28 +0900 Subject: [PATCH] Fix color format --- color.go | 21 ++-- color_test.go | 27 +++++- internal/encoder/option.go | 22 ++--- internal/encoder/vm_color/util.go | 117 ++++------------------- internal/encoder/vm_color_indent/util.go | 27 +++--- 5 files changed, 72 insertions(+), 142 deletions(-) diff --git a/color.go b/color.go index 9fa4545..e80b22b 100644 --- a/color.go +++ b/color.go @@ -56,18 +56,13 @@ func resetColor() string { var ( DefaultColorScheme = &ColorScheme{ - Int: createColorFormat(fgHiMagentaColor), - Uint: createColorFormat(fgHiMagentaColor), - Float: createColorFormat(fgHiMagentaColor), - Bool: createColorFormat(fgHiYellowColor), - String: createColorFormat(fgHiGreenColor), - Binary: createColorFormat(fgHiRedColor), - ObjectStart: createColorFormat(fgWhiteColor), - ObjectKey: createColorFormat(fgHiCyanColor), - ObjectEnd: createColorFormat(fgWhiteColor), - ArrayStart: createColorFormat(fgWhiteColor), - ArrayEnd: createColorFormat(fgWhiteColor), - Colon: createColorFormat(fgWhiteColor), - Null: createColorFormat(fgBlueColor), + Int: createColorFormat(fgHiMagentaColor), + Uint: createColorFormat(fgHiMagentaColor), + Float: createColorFormat(fgHiMagentaColor), + Bool: createColorFormat(fgHiYellowColor), + String: createColorFormat(fgHiGreenColor), + Binary: createColorFormat(fgHiRedColor), + ObjectKey: createColorFormat(fgHiCyanColor), + Null: createColorFormat(fgBlueColor), } ) diff --git a/color_test.go b/color_test.go index f1f3878..c21819d 100644 --- a/color_test.go +++ b/color_test.go @@ -15,6 +15,8 @@ func TestColorize(t *testing.T) { E bool F []byte G []int + H *struct{} + I map[string]interface{} }{ A: 123, B: 456, @@ -23,10 +25,25 @@ func TestColorize(t *testing.T) { E: true, F: []byte("binary"), G: []int{1, 2, 3, 4}, + H: nil, + I: map[string]interface{}{ + "mapA": -10, + "mapB": 10, + "mapC": nil, + }, } - b, err := json.MarshalWithOption(v, json.Colorize(json.DefaultColorScheme)) - if err != nil { - t.Fatal(err) - } - t.Log(string(b)) + t.Run("marshal with color", func(t *testing.T) { + b, err := json.MarshalWithOption(v, json.Colorize(json.DefaultColorScheme)) + if err != nil { + t.Fatal(err) + } + t.Log(string(b)) + }) + t.Run("marshal indent with color", func(t *testing.T) { + b, err := json.MarshalIndentWithOption(v, "", "\t", json.Colorize(json.DefaultColorScheme)) + if err != nil { + t.Fatal(err) + } + t.Log("\n" + string(b)) + }) } diff --git a/internal/encoder/option.go b/internal/encoder/option.go index 3cc3502..76a43ae 100644 --- a/internal/encoder/option.go +++ b/internal/encoder/option.go @@ -21,20 +21,14 @@ type EncodeFormat struct { } type EncodeFormatScheme struct { - Int EncodeFormat - Uint EncodeFormat - Float EncodeFormat - Bool EncodeFormat - String EncodeFormat - Binary EncodeFormat - ObjectStart EncodeFormat - ObjectKey EncodeFormat - ObjectEnd EncodeFormat - ArrayStart EncodeFormat - ArrayEnd EncodeFormat - Colon EncodeFormat - Comma EncodeFormat - Null EncodeFormat + Int EncodeFormat + Uint EncodeFormat + Float EncodeFormat + Bool EncodeFormat + String EncodeFormat + Binary EncodeFormat + ObjectKey EncodeFormat + Null EncodeFormat } type ( diff --git a/internal/encoder/vm_color/util.go b/internal/encoder/vm_color/util.go index 615956d..6331b14 100644 --- a/internal/encoder/vm_color/util.go +++ b/internal/encoder/vm_color/util.go @@ -154,47 +154,23 @@ func appendNull(ctx *encoder.RuntimeContext, b []byte) []byte { } func appendComma(ctx *encoder.RuntimeContext, b []byte) []byte { - format := ctx.Option.ColorScheme.Comma - b = append(b, format.Header...) - b = append(b, ',') - return append(b, format.Footer...) + return append(b, ',') } -func appendColon(ctx *encoder.RuntimeContext, b []byte) []byte { - format := ctx.Option.ColorScheme.Colon +func appendColon(_ *encoder.RuntimeContext, b []byte) []byte { last := len(b) - 1 - if len(format.Header) > 0 { - b[last] = format.Header[0] - b = append(b, format.Header[1:]...) - b = append(b, ':') - return append(b, format.Footer...) - } b[last] = ':' return b } func appendMapKeyValue(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b, key, value []byte) []byte { - keyFormat := ctx.Option.ColorScheme.String - b = append(b, keyFormat.Header...) - b = append(b, key...) - b = append(b, keyFormat.Footer...) - - b = appendColon(ctx, b) - + b = append(b, key[:len(key)-1]...) + b = append(b, ':') return append(b, value...) } func appendMapEnd(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { - format := ctx.Option.ColorScheme.ObjectEnd last := len(b) - 1 - if len(format.Header) > 0 { - b[last] = format.Header[0] - b = append(b, format.Header[1:]...) - b = append(b, '}') - b = append(b, format.Footer...) - - return append(b, ',') - } b[last] = '}' b = append(b, ',') return b @@ -248,67 +224,31 @@ func appendMarshalText(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []by } func appendArrayHead(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { - format := ctx.Option.ColorScheme.ArrayStart - b = append(b, format.Header...) - b = append(b, '[') - return append(b, format.Footer...) + return append(b, '[') } -func appendArrayEnd(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { +func appendArrayEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { last := len(b) - 1 - if b[last] == ',' { - b[last] = ']' - return appendComma(ctx, b) - } - format := ctx.Option.ColorScheme.ArrayEnd - b = append(b, format.Header...) - b = append(b, ']') - b = append(b, format.Footer...) - return appendComma(ctx, b) + b[last] = ']' + return append(b, ',') } -func appendEmptyArray(ctx *encoder.RuntimeContext, b []byte) []byte { - b = appendArrayHead(ctx, nil, b) - - format := ctx.Option.ColorScheme.ArrayEnd - b = append(b, format.Header...) - b = append(b, ']') - b = append(b, format.Footer...) - - return appendComma(ctx, b) +func appendEmptyArray(_ *encoder.RuntimeContext, b []byte) []byte { + return append(b, '[', ']', ',') } -func appendEmptyObject(ctx *encoder.RuntimeContext, b []byte) []byte { - b = appendStructHead(ctx, b) - - format := ctx.Option.ColorScheme.ObjectEnd - b = append(b, format.Header...) - b = append(b, '}') - b = append(b, format.Footer...) - - return appendComma(ctx, b) +func appendEmptyObject(_ *encoder.RuntimeContext, b []byte) []byte { + return append(b, '{', '}', ',') } -func appendObjectEnd(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { - format := ctx.Option.ColorScheme.ObjectEnd - +func appendObjectEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { last := len(b) - 1 - if len(format.Header) > 0 { - b[last] = format.Header[0] - b = append(b, format.Header[1:]...) - b = append(b, '}') - b = append(b, format.Footer...) - return appendComma(ctx, b) - } b[last] = '}' return append(b, ',') } func appendStructHead(ctx *encoder.RuntimeContext, b []byte) []byte { - format := ctx.Option.ColorScheme.ObjectStart - b = append(b, format.Header...) - b = append(b, '{') - return append(b, format.Footer...) + return append(b, '{') } func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { @@ -317,36 +257,15 @@ func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte b = append(b, code.Key[:len(code.Key)-1]...) b = append(b, format.Footer...) - colonFormat := ctx.Option.ColorScheme.Colon - b = append(b, colonFormat.Header...) - b = append(b, ':') - return append(b, colonFormat.Footer...) + return append(b, ':') } -func appendStructEnd(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { - format := ctx.Option.ColorScheme.ObjectEnd - - b = append(b, format.Header...) - b = append(b, '}') - b = append(b, format.Footer...) - - return appendComma(ctx, b) +func appendStructEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { + return append(b, '}', ',') } func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { - footerLen := len(ctx.Option.ColorScheme.Comma.Footer) - lastCharIdx := footerLen + 1 - - if len(b) < lastCharIdx { - last := len(b) - 1 - if b[last] == ',' { - b[last] = '}' - return appendComma(ctx, b) - } - return appendStructEnd(ctx, code, b) - } - - last := len(b) - lastCharIdx + last := len(b) - 1 if b[last] == ',' { b[last] = '}' return appendComma(ctx, b) diff --git a/internal/encoder/vm_color_indent/util.go b/internal/encoder/vm_color_indent/util.go index 0b231d6..8fc2b62 100644 --- a/internal/encoder/vm_color_indent/util.go +++ b/internal/encoder/vm_color_indent/util.go @@ -156,17 +156,11 @@ func appendNull(ctx *encoder.RuntimeContext, b []byte) []byte { } func appendComma(ctx *encoder.RuntimeContext, b []byte) []byte { - format := ctx.Option.ColorScheme.Comma - b = append(b, format.Header...) - b = append(b, ',', '\n') - return append(b, format.Footer...) + return append(b, ',', '\n') } func appendColon(ctx *encoder.RuntimeContext, b []byte) []byte { - format := ctx.Option.ColorScheme.Colon - b = append(b, format.Header...) - b = append(b, ':', ' ') - return append(b, format.Footer...) + return append(b, ':', ' ') } func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, code *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) { @@ -253,7 +247,13 @@ func appendMarshalJSON(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []by } func appendMarshalText(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte, v interface{}) ([]byte, error) { - return encoder.AppendMarshalTextIndent(ctx, code, b, v) + format := ctx.Option.ColorScheme.String + b = append(b, format.Header...) + bb, err := encoder.AppendMarshalTextIndent(ctx, code, b, v) + if err != nil { + return nil, err + } + return append(bb, format.Footer...), nil } func appendStructHead(_ *encoder.RuntimeContext, b []byte) []byte { @@ -262,8 +262,13 @@ func appendStructHead(_ *encoder.RuntimeContext, b []byte) []byte { func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { b = appendIndent(ctx, b, code.Indent) - b = append(b, code.Key...) - return append(b, ' ') + + format := ctx.Option.ColorScheme.ObjectKey + b = append(b, format.Header...) + b = append(b, code.Key[:len(code.Key)-1]...) + b = append(b, format.Footer...) + + return append(b, ':', ' ') } func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {