mirror of https://github.com/goccy/go-json.git
feat: add DebugWith option
This commit is contained in:
parent
03950e7b0b
commit
321fe31260
|
@ -3,6 +3,7 @@ package json
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/goccy/go-json/internal/encoder"
|
"github.com/goccy/go-json/internal/encoder"
|
||||||
|
@ -62,6 +63,7 @@ func (e *Encoder) encodeWithOption(ctx *encoder.RuntimeContext, v interface{}, o
|
||||||
ctx.Option.Flag |= encoder.HTMLEscapeOption
|
ctx.Option.Flag |= encoder.HTMLEscapeOption
|
||||||
}
|
}
|
||||||
ctx.Option.Flag |= encoder.NormalizeUTF8Option
|
ctx.Option.Flag |= encoder.NormalizeUTF8Option
|
||||||
|
ctx.Option.DebugOut = os.Stdout
|
||||||
for _, optFunc := range optFuncs {
|
for _, optFunc := range optFuncs {
|
||||||
optFunc(ctx.Option)
|
optFunc(ctx.Option)
|
||||||
}
|
}
|
||||||
|
|
|
@ -477,7 +477,8 @@ func TestDebugMode(t *testing.T) {
|
||||||
t.Fatal("expected error")
|
t.Fatal("expected error")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
json.MarshalWithOption(mustErrTypeForDebug{}, json.Debug())
|
var buf bytes.Buffer
|
||||||
|
json.MarshalWithOption(mustErrTypeForDebug{}, json.Debug(), json.DebugWith(&buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIssue116(t *testing.T) {
|
func TestIssue116(t *testing.T) {
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package encoder
|
package encoder
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
type OptionFlag uint8
|
type OptionFlag uint8
|
||||||
|
|
||||||
|
@ -19,6 +22,7 @@ type Option struct {
|
||||||
Flag OptionFlag
|
Flag OptionFlag
|
||||||
ColorScheme *ColorScheme
|
ColorScheme *ColorScheme
|
||||||
Context context.Context
|
Context context.Context
|
||||||
|
DebugOut io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
type EncodeFormat struct {
|
type EncodeFormat struct {
|
||||||
|
|
|
@ -16,16 +16,17 @@ func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
fmt.Println("=============[DEBUG]===============")
|
w := ctx.Option.DebugOut
|
||||||
fmt.Println("* [TYPE]")
|
fmt.Fprintln(w, "=============[DEBUG]===============")
|
||||||
fmt.Println(codeSet.Type)
|
fmt.Fprintln(w, "* [TYPE]")
|
||||||
fmt.Printf("\n")
|
fmt.Fprintln(w, codeSet.Type)
|
||||||
fmt.Println("* [ALL OPCODE]")
|
fmt.Fprintf(w, "\n")
|
||||||
fmt.Println(code.Dump())
|
fmt.Fprintln(w, "* [ALL OPCODE]")
|
||||||
fmt.Printf("\n")
|
fmt.Fprintln(w, code.Dump())
|
||||||
fmt.Println("* [CONTEXT]")
|
fmt.Fprintf(w, "\n")
|
||||||
fmt.Printf("%+v\n", ctx)
|
fmt.Fprintln(w, "* [CONTEXT]")
|
||||||
fmt.Println("===================================")
|
fmt.Fprintf(w, "%+v\n", ctx)
|
||||||
|
fmt.Fprintln(w, "===================================")
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
@ -16,16 +16,17 @@ func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
fmt.Println("=============[DEBUG]===============")
|
w := ctx.Option.DebugOut
|
||||||
fmt.Println("* [TYPE]")
|
fmt.Fprintln(w, "=============[DEBUG]===============")
|
||||||
fmt.Println(codeSet.Type)
|
fmt.Fprintln(w, "* [TYPE]")
|
||||||
fmt.Printf("\n")
|
fmt.Fprintln(w, codeSet.Type)
|
||||||
fmt.Println("* [ALL OPCODE]")
|
fmt.Fprintf(w, "\n")
|
||||||
fmt.Println(code.Dump())
|
fmt.Fprintln(w, "* [ALL OPCODE]")
|
||||||
fmt.Printf("\n")
|
fmt.Fprintln(w, code.Dump())
|
||||||
fmt.Println("* [CONTEXT]")
|
fmt.Fprintf(w, "\n")
|
||||||
fmt.Printf("%+v\n", ctx)
|
fmt.Fprintln(w, "* [CONTEXT]")
|
||||||
fmt.Println("===================================")
|
fmt.Fprintf(w, "%+v\n", ctx)
|
||||||
|
fmt.Fprintln(w, "===================================")
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
@ -16,16 +16,17 @@ func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
fmt.Println("=============[DEBUG]===============")
|
w := ctx.Option.DebugOut
|
||||||
fmt.Println("* [TYPE]")
|
fmt.Fprintln(w, "=============[DEBUG]===============")
|
||||||
fmt.Println(codeSet.Type)
|
fmt.Fprintln(w, "* [TYPE]")
|
||||||
fmt.Printf("\n")
|
fmt.Fprintln(w, codeSet.Type)
|
||||||
fmt.Println("* [ALL OPCODE]")
|
fmt.Fprintf(w, "\n")
|
||||||
fmt.Println(code.Dump())
|
fmt.Fprintln(w, "* [ALL OPCODE]")
|
||||||
fmt.Printf("\n")
|
fmt.Fprintln(w, code.Dump())
|
||||||
fmt.Println("* [CONTEXT]")
|
fmt.Fprintf(w, "\n")
|
||||||
fmt.Printf("%+v\n", ctx)
|
fmt.Fprintln(w, "* [CONTEXT]")
|
||||||
fmt.Println("===================================")
|
fmt.Fprintf(w, "%+v\n", ctx)
|
||||||
|
fmt.Fprintln(w, "===================================")
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
@ -16,16 +16,17 @@ func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
fmt.Println("=============[DEBUG]===============")
|
w := ctx.Option.DebugOut
|
||||||
fmt.Println("* [TYPE]")
|
fmt.Fprintln(w, "=============[DEBUG]===============")
|
||||||
fmt.Println(codeSet.Type)
|
fmt.Fprintln(w, "* [TYPE]")
|
||||||
fmt.Printf("\n")
|
fmt.Fprintln(w, codeSet.Type)
|
||||||
fmt.Println("* [ALL OPCODE]")
|
fmt.Fprintf(w, "\n")
|
||||||
fmt.Println(code.Dump())
|
fmt.Fprintln(w, "* [ALL OPCODE]")
|
||||||
fmt.Printf("\n")
|
fmt.Fprintln(w, code.Dump())
|
||||||
fmt.Println("* [CONTEXT]")
|
fmt.Fprintf(w, "\n")
|
||||||
fmt.Printf("%+v\n", ctx)
|
fmt.Fprintln(w, "* [CONTEXT]")
|
||||||
fmt.Println("===================================")
|
fmt.Fprintf(w, "%+v\n", ctx)
|
||||||
|
fmt.Fprintln(w, "===================================")
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/goccy/go-json/internal/decoder"
|
"github.com/goccy/go-json/internal/decoder"
|
||||||
"github.com/goccy/go-json/internal/encoder"
|
"github.com/goccy/go-json/internal/encoder"
|
||||||
)
|
)
|
||||||
|
@ -39,6 +41,13 @@ func Debug() EncodeOptionFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DebugWith sets the destination to write debug messages.
|
||||||
|
func DebugWith(w io.Writer) EncodeOptionFunc {
|
||||||
|
return func(opt *EncodeOption) {
|
||||||
|
opt.DebugOut = w
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Colorize add an identifier for coloring to the string of the encoded result.
|
// Colorize add an identifier for coloring to the string of the encoded result.
|
||||||
func Colorize(scheme *ColorScheme) EncodeOptionFunc {
|
func Colorize(scheme *ColorScheme) EncodeOptionFunc {
|
||||||
return func(opt *EncodeOption) {
|
return func(opt *EncodeOption) {
|
||||||
|
|
Loading…
Reference in New Issue