Fix color format

This commit is contained in:
Masaaki Goshima 2021-06-01 15:10:28 +09:00
parent 4e2a9c06ef
commit d495f67045
5 changed files with 72 additions and 142 deletions

View File

@ -62,12 +62,7 @@ var (
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),
}
)

View File

@ -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,
},
}
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))
})
}

View File

@ -27,13 +27,7 @@ type EncodeFormatScheme struct {
Bool EncodeFormat
String EncodeFormat
Binary EncodeFormat
ObjectStart EncodeFormat
ObjectKey EncodeFormat
ObjectEnd EncodeFormat
ArrayStart EncodeFormat
ArrayEnd EncodeFormat
Colon EncodeFormat
Comma EncodeFormat
Null EncodeFormat
}

View File

@ -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)
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,27 +257,14 @@ 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] = '}'
@ -346,14 +273,6 @@ func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode,
return appendStructEnd(ctx, code, b)
}
last := len(b) - lastCharIdx
if b[last] == ',' {
b[last] = '}'
return appendComma(ctx, b)
}
return appendStructEnd(ctx, code, b)
}
func restoreIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, _ uintptr) {}
func storeIndent(_ uintptr, _ *encoder.Opcode, _ uintptr) {}
func appendMapKeyIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { return b }

View File

@ -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 {