diff options
author | 2021-09-10 14:42:14 +0200 | |
---|---|---|
committer | 2021-09-10 14:42:14 +0200 | |
commit | f2e5bedea6fb93fbbf68ed8f7153c353cc57a9f0 (patch) | |
tree | 475ae9e7470d0df670ab2a59dce351cd1d07498a /vendor/github.com/goccy/go-json/encode.go | |
parent | fixes + db changes (#204) (diff) | |
download | gotosocial-f2e5bedea6fb93fbbf68ed8f7153c353cc57a9f0.tar.xz |
migrate go version to 1.17 (#203)
* migrate go version to 1.17
* update contributing
Diffstat (limited to 'vendor/github.com/goccy/go-json/encode.go')
-rw-r--r-- | vendor/github.com/goccy/go-json/encode.go | 159 |
1 files changed, 99 insertions, 60 deletions
diff --git a/vendor/github.com/goccy/go-json/encode.go b/vendor/github.com/goccy/go-json/encode.go index c574be79c..7f198bdca 100644 --- a/vendor/github.com/goccy/go-json/encode.go +++ b/vendor/github.com/goccy/go-json/encode.go @@ -1,13 +1,14 @@ package json import ( + "context" "io" "unsafe" "github.com/goccy/go-json/internal/encoder" "github.com/goccy/go-json/internal/encoder/vm" - "github.com/goccy/go-json/internal/encoder/vm_escaped" - "github.com/goccy/go-json/internal/encoder/vm_escaped_indent" + "github.com/goccy/go-json/internal/encoder/vm_color" + "github.com/goccy/go-json/internal/encoder/vm_color_indent" "github.com/goccy/go-json/internal/encoder/vm_indent" ) @@ -20,15 +21,6 @@ type Encoder struct { indentStr string } -type EncodeOption int - -const ( - EncodeOptionHTMLEscape EncodeOption = 1 << iota - EncodeOptionIndent - EncodeOptionUnorderedMap - EncodeOptionDebug -) - // NewEncoder returns a new encoder that writes to w. func NewEncoder(w io.Writer) *Encoder { return &Encoder{w: w, enabledHTMLEscape: true} @@ -44,6 +36,7 @@ func (e *Encoder) Encode(v interface{}) error { // EncodeWithOption call Encode with EncodeOption. func (e *Encoder) EncodeWithOption(v interface{}, optFuncs ...EncodeOptionFunc) error { ctx := encoder.TakeRuntimeContext() + ctx.Option.Flag = 0 err := e.encodeWithOption(ctx, v, optFuncs...) @@ -51,22 +44,34 @@ func (e *Encoder) EncodeWithOption(v interface{}, optFuncs ...EncodeOptionFunc) return err } +// EncodeContext call Encode with context.Context and EncodeOption. +func (e *Encoder) EncodeContext(ctx context.Context, v interface{}, optFuncs ...EncodeOptionFunc) error { + rctx := encoder.TakeRuntimeContext() + rctx.Option.Flag = 0 + rctx.Option.Flag |= encoder.ContextOption + rctx.Option.Context = ctx + + err := e.encodeWithOption(rctx, v, optFuncs...) + + encoder.ReleaseRuntimeContext(rctx) + return err +} + func (e *Encoder) encodeWithOption(ctx *encoder.RuntimeContext, v interface{}, optFuncs ...EncodeOptionFunc) error { - var opt EncodeOption if e.enabledHTMLEscape { - opt |= EncodeOptionHTMLEscape + ctx.Option.Flag |= encoder.HTMLEscapeOption } for _, optFunc := range optFuncs { - opt = optFunc(opt) + optFunc(ctx.Option) } var ( buf []byte err error ) if e.enabledIndent { - buf, err = encodeIndent(ctx, v, e.prefix, e.indentStr, opt) + buf, err = encodeIndent(ctx, v, e.prefix, e.indentStr) } else { - buf, err = encode(ctx, v, opt) + buf, err = encode(ctx, v) } if err != nil { return err @@ -103,10 +108,43 @@ func (e *Encoder) SetIndent(prefix, indent string) { e.enabledIndent = true } -func marshal(v interface{}, opt EncodeOption) ([]byte, error) { +func marshalContext(ctx context.Context, v interface{}, optFuncs ...EncodeOptionFunc) ([]byte, error) { + rctx := encoder.TakeRuntimeContext() + rctx.Option.Flag = 0 + rctx.Option.Flag = encoder.HTMLEscapeOption | encoder.ContextOption + rctx.Option.Context = ctx + for _, optFunc := range optFuncs { + optFunc(rctx.Option) + } + + buf, err := encode(rctx, v) + if err != nil { + encoder.ReleaseRuntimeContext(rctx) + return nil, err + } + + // this line exists to escape call of `runtime.makeslicecopy` . + // if use `make([]byte, len(buf)-1)` and `copy(copied, buf)`, + // dst buffer size and src buffer size are differrent. + // in this case, compiler uses `runtime.makeslicecopy`, but it is slow. + buf = buf[:len(buf)-1] + copied := make([]byte, len(buf)) + copy(copied, buf) + + encoder.ReleaseRuntimeContext(rctx) + return copied, nil +} + +func marshal(v interface{}, optFuncs ...EncodeOptionFunc) ([]byte, error) { ctx := encoder.TakeRuntimeContext() - buf, err := encode(ctx, v, opt|EncodeOptionHTMLEscape) + ctx.Option.Flag = 0 + ctx.Option.Flag |= encoder.HTMLEscapeOption + for _, optFunc := range optFuncs { + optFunc(ctx.Option) + } + + buf, err := encode(ctx, v) if err != nil { encoder.ReleaseRuntimeContext(ctx) return nil, err @@ -124,10 +162,13 @@ func marshal(v interface{}, opt EncodeOption) ([]byte, error) { return copied, nil } -func marshalNoEscape(v interface{}, opt EncodeOption) ([]byte, error) { +func marshalNoEscape(v interface{}) ([]byte, error) { ctx := encoder.TakeRuntimeContext() - buf, err := encodeNoEscape(ctx, v, opt|EncodeOptionHTMLEscape) + ctx.Option.Flag = 0 + ctx.Option.Flag |= encoder.HTMLEscapeOption + + buf, err := encodeNoEscape(ctx, v) if err != nil { encoder.ReleaseRuntimeContext(ctx) return nil, err @@ -145,10 +186,16 @@ func marshalNoEscape(v interface{}, opt EncodeOption) ([]byte, error) { return copied, nil } -func marshalIndent(v interface{}, prefix, indent string, opt EncodeOption) ([]byte, error) { +func marshalIndent(v interface{}, prefix, indent string, optFuncs ...EncodeOptionFunc) ([]byte, error) { ctx := encoder.TakeRuntimeContext() - buf, err := encodeIndent(ctx, v, prefix, indent, opt|EncodeOptionHTMLEscape) + ctx.Option.Flag = 0 + ctx.Option.Flag |= (encoder.HTMLEscapeOption | encoder.IndentOption) + for _, optFunc := range optFuncs { + optFunc(ctx.Option) + } + + buf, err := encodeIndent(ctx, v, prefix, indent) if err != nil { encoder.ReleaseRuntimeContext(ctx) return nil, err @@ -162,11 +209,11 @@ func marshalIndent(v interface{}, prefix, indent string, opt EncodeOption) ([]by return copied, nil } -func encode(ctx *encoder.RuntimeContext, v interface{}, opt EncodeOption) ([]byte, error) { +func encode(ctx *encoder.RuntimeContext, v interface{}) ([]byte, error) { b := ctx.Buf[:0] if v == nil { - b = encoder.AppendNull(b) - b = encoder.AppendComma(b) + b = encoder.AppendNull(ctx, b) + b = encoder.AppendComma(ctx, b) return b, nil } header := (*emptyInterface)(unsafe.Pointer(&v)) @@ -182,7 +229,7 @@ func encode(ctx *encoder.RuntimeContext, v interface{}, opt EncodeOption) ([]byt ctx.Init(p, codeSet.CodeLength) ctx.KeepRefs = append(ctx.KeepRefs, header.ptr) - buf, err := encodeRunCode(ctx, b, codeSet, opt) + buf, err := encodeRunCode(ctx, b, codeSet) if err != nil { return nil, err } @@ -190,11 +237,11 @@ func encode(ctx *encoder.RuntimeContext, v interface{}, opt EncodeOption) ([]byt return buf, nil } -func encodeNoEscape(ctx *encoder.RuntimeContext, v interface{}, opt EncodeOption) ([]byte, error) { +func encodeNoEscape(ctx *encoder.RuntimeContext, v interface{}) ([]byte, error) { b := ctx.Buf[:0] if v == nil { - b = encoder.AppendNull(b) - b = encoder.AppendComma(b) + b = encoder.AppendNull(ctx, b) + b = encoder.AppendComma(ctx, b) return b, nil } header := (*emptyInterface)(unsafe.Pointer(&v)) @@ -208,7 +255,7 @@ func encodeNoEscape(ctx *encoder.RuntimeContext, v interface{}, opt EncodeOption p := uintptr(header.ptr) ctx.Init(p, codeSet.CodeLength) - buf, err := encodeRunCode(ctx, b, codeSet, opt) + buf, err := encodeRunCode(ctx, b, codeSet) if err != nil { return nil, err } @@ -217,11 +264,11 @@ func encodeNoEscape(ctx *encoder.RuntimeContext, v interface{}, opt EncodeOption return buf, nil } -func encodeIndent(ctx *encoder.RuntimeContext, v interface{}, prefix, indent string, opt EncodeOption) ([]byte, error) { +func encodeIndent(ctx *encoder.RuntimeContext, v interface{}, prefix, indent string) ([]byte, error) { b := ctx.Buf[:0] if v == nil { - b = encoder.AppendNull(b) - b = encoder.AppendCommaIndent(b) + b = encoder.AppendNull(ctx, b) + b = encoder.AppendCommaIndent(ctx, b) return b, nil } header := (*emptyInterface)(unsafe.Pointer(&v)) @@ -235,7 +282,7 @@ func encodeIndent(ctx *encoder.RuntimeContext, v interface{}, prefix, indent str p := uintptr(header.ptr) ctx.Init(p, codeSet.CodeLength) - buf, err := encodeRunIndentCode(ctx, b, codeSet, prefix, indent, opt) + buf, err := encodeRunIndentCode(ctx, b, codeSet, prefix, indent) ctx.KeepRefs = append(ctx.KeepRefs, header.ptr) @@ -247,38 +294,30 @@ func encodeIndent(ctx *encoder.RuntimeContext, v interface{}, prefix, indent str return buf, nil } -func encodeRunCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt EncodeOption) ([]byte, error) { - if (opt & EncodeOptionDebug) != 0 { - return encodeDebugRunCode(ctx, b, codeSet, opt) +func encodeRunCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) { + if (ctx.Option.Flag & encoder.DebugOption) != 0 { + if (ctx.Option.Flag & encoder.ColorizeOption) != 0 { + return vm_color.DebugRun(ctx, b, codeSet) + } + return vm.DebugRun(ctx, b, codeSet) } - if (opt & EncodeOptionHTMLEscape) != 0 { - return vm_escaped.Run(ctx, b, codeSet, encoder.Option(opt)) + if (ctx.Option.Flag & encoder.ColorizeOption) != 0 { + return vm_color.Run(ctx, b, codeSet) } - return vm.Run(ctx, b, codeSet, encoder.Option(opt)) + return vm.Run(ctx, b, codeSet) } -func encodeDebugRunCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt EncodeOption) ([]byte, error) { - if (opt & EncodeOptionHTMLEscape) != 0 { - return vm_escaped.DebugRun(ctx, b, codeSet, encoder.Option(opt)) - } - return vm.DebugRun(ctx, b, codeSet, encoder.Option(opt)) -} - -func encodeRunIndentCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, prefix, indent string, opt EncodeOption) ([]byte, error) { +func encodeRunIndentCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, prefix, indent string) ([]byte, error) { ctx.Prefix = []byte(prefix) ctx.IndentStr = []byte(indent) - if (opt & EncodeOptionDebug) != 0 { - return encodeDebugRunIndentCode(ctx, b, codeSet, opt) - } - if (opt & EncodeOptionHTMLEscape) != 0 { - return vm_escaped_indent.Run(ctx, b, codeSet, encoder.Option(opt)) + if (ctx.Option.Flag & encoder.DebugOption) != 0 { + if (ctx.Option.Flag & encoder.ColorizeOption) != 0 { + return vm_color_indent.DebugRun(ctx, b, codeSet) + } + return vm_indent.DebugRun(ctx, b, codeSet) } - return vm_indent.Run(ctx, b, codeSet, encoder.Option(opt)) -} - -func encodeDebugRunIndentCode(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt EncodeOption) ([]byte, error) { - if (opt & EncodeOptionHTMLEscape) != 0 { - return vm_escaped_indent.DebugRun(ctx, b, codeSet, encoder.Option(opt)) + if (ctx.Option.Flag & encoder.ColorizeOption) != 0 { + return vm_color_indent.Run(ctx, b, codeSet) } - return vm_indent.DebugRun(ctx, b, codeSet, encoder.Option(opt)) + return vm_indent.Run(ctx, b, codeSet) } |