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), Bool: createColorFormat(fgHiYellowColor),
String: createColorFormat(fgHiGreenColor), String: createColorFormat(fgHiGreenColor),
Binary: createColorFormat(fgHiRedColor), Binary: createColorFormat(fgHiRedColor),
ObjectStart: createColorFormat(fgWhiteColor),
ObjectKey: createColorFormat(fgHiCyanColor), ObjectKey: createColorFormat(fgHiCyanColor),
ObjectEnd: createColorFormat(fgWhiteColor),
ArrayStart: createColorFormat(fgWhiteColor),
ArrayEnd: createColorFormat(fgWhiteColor),
Colon: createColorFormat(fgWhiteColor),
Null: createColorFormat(fgBlueColor), Null: createColorFormat(fgBlueColor),
} }
) )

View File

@ -15,6 +15,8 @@ func TestColorize(t *testing.T) {
E bool E bool
F []byte F []byte
G []int G []int
H *struct{}
I map[string]interface{}
}{ }{
A: 123, A: 123,
B: 456, B: 456,
@ -23,10 +25,25 @@ func TestColorize(t *testing.T) {
E: true, E: true,
F: []byte("binary"), F: []byte("binary"),
G: []int{1, 2, 3, 4}, 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)) b, err := json.MarshalWithOption(v, json.Colorize(json.DefaultColorScheme))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Log(string(b)) 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 Bool EncodeFormat
String EncodeFormat String EncodeFormat
Binary EncodeFormat Binary EncodeFormat
ObjectStart EncodeFormat
ObjectKey EncodeFormat ObjectKey EncodeFormat
ObjectEnd EncodeFormat
ArrayStart EncodeFormat
ArrayEnd EncodeFormat
Colon EncodeFormat
Comma EncodeFormat
Null 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 { func appendComma(ctx *encoder.RuntimeContext, b []byte) []byte {
format := ctx.Option.ColorScheme.Comma return append(b, ',')
b = append(b, format.Header...)
b = append(b, ',')
return append(b, format.Footer...)
} }
func appendColon(ctx *encoder.RuntimeContext, b []byte) []byte { func appendColon(_ *encoder.RuntimeContext, b []byte) []byte {
format := ctx.Option.ColorScheme.Colon
last := len(b) - 1 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] = ':' b[last] = ':'
return b return b
} }
func appendMapKeyValue(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b, key, value []byte) []byte { func appendMapKeyValue(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b, key, value []byte) []byte {
keyFormat := ctx.Option.ColorScheme.String b = append(b, key[:len(key)-1]...)
b = append(b, keyFormat.Header...) b = append(b, ':')
b = append(b, key...)
b = append(b, keyFormat.Footer...)
b = appendColon(ctx, b)
return append(b, value...) return append(b, value...)
} }
func appendMapEnd(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { func appendMapEnd(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
format := ctx.Option.ColorScheme.ObjectEnd
last := len(b) - 1 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[last] = '}'
b = append(b, ',') b = append(b, ',')
return 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 { func appendArrayHead(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
format := ctx.Option.ColorScheme.ArrayStart return append(b, '[')
b = append(b, format.Header...)
b = append(b, '[')
return append(b, format.Footer...)
} }
func appendArrayEnd(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { func appendArrayEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
last := len(b) - 1 last := len(b) - 1
if b[last] == ',' {
b[last] = ']' b[last] = ']'
return appendComma(ctx, b) return append(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(ctx *encoder.RuntimeContext, b []byte) []byte { func appendEmptyArray(_ *encoder.RuntimeContext, b []byte) []byte {
b = appendArrayHead(ctx, nil, b) return append(b, '[', ']', ',')
format := ctx.Option.ColorScheme.ArrayEnd
b = append(b, format.Header...)
b = append(b, ']')
b = append(b, format.Footer...)
return appendComma(ctx, b)
} }
func appendEmptyObject(ctx *encoder.RuntimeContext, b []byte) []byte { func appendEmptyObject(_ *encoder.RuntimeContext, b []byte) []byte {
b = appendStructHead(ctx, b) return append(b, '{', '}', ',')
format := ctx.Option.ColorScheme.ObjectEnd
b = append(b, format.Header...)
b = append(b, '}')
b = append(b, format.Footer...)
return appendComma(ctx, b)
} }
func appendObjectEnd(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { func appendObjectEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
format := ctx.Option.ColorScheme.ObjectEnd
last := len(b) - 1 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] = '}' b[last] = '}'
return append(b, ',') return append(b, ',')
} }
func appendStructHead(ctx *encoder.RuntimeContext, b []byte) []byte { func appendStructHead(ctx *encoder.RuntimeContext, b []byte) []byte {
format := ctx.Option.ColorScheme.ObjectStart return append(b, '{')
b = append(b, format.Header...)
b = append(b, '{')
return append(b, format.Footer...)
} }
func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
@ -317,41 +257,20 @@ func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte
b = append(b, code.Key[:len(code.Key)-1]...) b = append(b, code.Key[:len(code.Key)-1]...)
b = append(b, format.Footer...) b = append(b, format.Footer...)
colonFormat := ctx.Option.ColorScheme.Colon return append(b, ':')
b = append(b, colonFormat.Header...)
b = append(b, ':')
return append(b, colonFormat.Footer...)
} }
func appendStructEnd(ctx *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte { func appendStructEnd(_ *encoder.RuntimeContext, _ *encoder.Opcode, b []byte) []byte {
format := ctx.Option.ColorScheme.ObjectEnd return append(b, '}', ',')
b = append(b, format.Header...)
b = append(b, '}')
b = append(b, format.Footer...)
return appendComma(ctx, b)
} }
func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte { 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 last := len(b) - 1
if b[last] == ',' { if b[last] == ',' {
b[last] = '}' b[last] = '}'
return appendComma(ctx, b) return appendComma(ctx, b)
} }
return appendStructEnd(ctx, code, b) 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 restoreIndent(_ *encoder.RuntimeContext, _ *encoder.Opcode, _ uintptr) {}

View File

@ -156,17 +156,11 @@ func appendNull(ctx *encoder.RuntimeContext, b []byte) []byte {
} }
func appendComma(ctx *encoder.RuntimeContext, b []byte) []byte { func appendComma(ctx *encoder.RuntimeContext, b []byte) []byte {
format := ctx.Option.ColorScheme.Comma return append(b, ',', '\n')
b = append(b, format.Header...)
b = append(b, ',', '\n')
return append(b, format.Footer...)
} }
func appendColon(ctx *encoder.RuntimeContext, b []byte) []byte { func appendColon(ctx *encoder.RuntimeContext, b []byte) []byte {
format := ctx.Option.ColorScheme.Colon return append(b, ':', ' ')
b = append(b, format.Header...)
b = append(b, ':', ' ')
return append(b, format.Footer...)
} }
func appendInterface(ctx *encoder.RuntimeContext, codeSet *encoder.OpcodeSet, code *encoder.Opcode, b []byte, iface *emptyInterface, ptrOffset uintptr) ([]byte, error) { 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) { 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 { 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 { func appendStructKey(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {
b = appendIndent(ctx, b, code.Indent) 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 { func appendStructEndSkipLast(ctx *encoder.RuntimeContext, code *encoder.Opcode, b []byte) []byte {