parent
003f87b4d7
commit
6137f53c38
@ -1,5 +1,5 @@
|
|||||||
package go_library
|
package go_library
|
||||||
|
|
||||||
func Version() string {
|
func Version() string {
|
||||||
return "1.0.147"
|
return "1.0.148"
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package goip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/dtapps/go-library/utils/goip/geoip"
|
||||||
|
"github.com/dtapps/go-library/utils/goip/ip2region"
|
||||||
|
"github.com/dtapps/go-library/utils/goip/ip2region_v2"
|
||||||
|
"github.com/dtapps/go-library/utils/goip/ipv6wry"
|
||||||
|
"github.com/dtapps/go-library/utils/goip/qqwry"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestV4OnlineDownload(t *testing.T) {
|
||||||
|
qqwry.OnlineDownload()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestV6OnlineDownload(t *testing.T) {
|
||||||
|
ipv6wry.OnlineDownload()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIp2regionOnlineDownload(t *testing.T) {
|
||||||
|
ip2region.OnlineDownload()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIp2regionV2OnlineDownload(t *testing.T) {
|
||||||
|
ip2region_v2.OnlineDownload()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGeoIpOnlineDownload(t *testing.T) {
|
||||||
|
geoip.OnlineDownload(geoip.GetGeoLite2CountryDownloadUrl("bb26plSFSVqDCJen"), "GeoLite2-Country_20230602.tar.gz")
|
||||||
|
geoip.OnlineDownload(geoip.GetGeoLite2CityDownloadUrl("bb26plSFSVqDCJen"), "GeoLite2-City_20230602.tar.gz")
|
||||||
|
geoip.OnlineDownload(geoip.GetGeoLite2AsnDownloadUrl("bb26plSFSVqDCJen"), "GeoLite2-ASN_20230602.tar.gz")
|
||||||
|
}
|
Binary file not shown.
Before Width: | Height: | Size: 69 MiB After Width: | Height: | Size: 68 MiB |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,66 @@
|
|||||||
|
// +build amd64,go1.15,!go1.21
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2023 ByteDance Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package decoder
|
||||||
|
|
||||||
|
import (
|
||||||
|
`github.com/bytedance/sonic/internal/decoder`
|
||||||
|
)
|
||||||
|
|
||||||
|
// Decoder is the decoder context object
|
||||||
|
type Decoder = decoder.Decoder
|
||||||
|
|
||||||
|
type MismatchTypeError = decoder.MismatchTypeError
|
||||||
|
|
||||||
|
// Options for decode.
|
||||||
|
type Options = decoder.Options
|
||||||
|
|
||||||
|
const (
|
||||||
|
OptionUseInt64 Options = decoder.OptionUseInt64
|
||||||
|
OptionUseNumber Options = decoder.OptionUseNumber
|
||||||
|
OptionUseUnicodeErrors Options = decoder.OptionUseUnicodeErrors
|
||||||
|
OptionDisableUnknown Options = decoder.OptionDisableUnknown
|
||||||
|
OptionCopyString Options = decoder.OptionCopyString
|
||||||
|
OptionValidateString Options = decoder.OptionValidateString
|
||||||
|
)
|
||||||
|
|
||||||
|
// StreamDecoder is the decoder context object for streaming input.
|
||||||
|
type StreamDecoder = decoder.StreamDecoder
|
||||||
|
|
||||||
|
type SyntaxError = decoder.SyntaxError
|
||||||
|
|
||||||
|
var (
|
||||||
|
// NewDecoder creates a new decoder instance.
|
||||||
|
NewDecoder = decoder.NewDecoder
|
||||||
|
|
||||||
|
// NewStreamDecoder adapts to encoding/json.NewDecoder API.
|
||||||
|
//
|
||||||
|
// NewStreamDecoder returns a new decoder that reads from r.
|
||||||
|
NewStreamDecoder = decoder.NewStreamDecoder
|
||||||
|
|
||||||
|
// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
|
||||||
|
// order to reduce the first-hit latency.
|
||||||
|
//
|
||||||
|
// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
|
||||||
|
// a compile option to set the depth of recursive compile for the nested struct type.
|
||||||
|
Pretouch = decoder.Pretouch
|
||||||
|
|
||||||
|
// Skip skips only one json value, and returns first non-blank character position and its ending position if it is valid.
|
||||||
|
// Otherwise, returns negative error code using start and invalid character position using end
|
||||||
|
Skip = decoder.Skip
|
||||||
|
)
|
@ -0,0 +1,196 @@
|
|||||||
|
// +build !amd64 go1.21
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2023 ByteDance Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package decoder
|
||||||
|
|
||||||
|
import (
|
||||||
|
`encoding/json`
|
||||||
|
`bytes`
|
||||||
|
`reflect`
|
||||||
|
`github.com/bytedance/sonic/internal/native/types`
|
||||||
|
`github.com/bytedance/sonic/option`
|
||||||
|
`io`
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
_F_use_int64 = iota
|
||||||
|
_F_use_number
|
||||||
|
_F_disable_urc
|
||||||
|
_F_disable_unknown
|
||||||
|
_F_copy_string
|
||||||
|
_F_validate_string
|
||||||
|
|
||||||
|
_F_allow_control = 31
|
||||||
|
)
|
||||||
|
|
||||||
|
type Options uint64
|
||||||
|
|
||||||
|
const (
|
||||||
|
OptionUseInt64 Options = 1 << _F_use_int64
|
||||||
|
OptionUseNumber Options = 1 << _F_use_number
|
||||||
|
OptionUseUnicodeErrors Options = 1 << _F_disable_urc
|
||||||
|
OptionDisableUnknown Options = 1 << _F_disable_unknown
|
||||||
|
OptionCopyString Options = 1 << _F_copy_string
|
||||||
|
OptionValidateString Options = 1 << _F_validate_string
|
||||||
|
)
|
||||||
|
|
||||||
|
func (self *Decoder) SetOptions(opts Options) {
|
||||||
|
if (opts & OptionUseNumber != 0) && (opts & OptionUseInt64 != 0) {
|
||||||
|
panic("can't set OptionUseInt64 and OptionUseNumber both!")
|
||||||
|
}
|
||||||
|
self.f = uint64(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Decoder is the decoder context object
|
||||||
|
type Decoder struct {
|
||||||
|
i int
|
||||||
|
f uint64
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDecoder creates a new decoder instance.
|
||||||
|
func NewDecoder(s string) *Decoder {
|
||||||
|
return &Decoder{s: s}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pos returns the current decoding position.
|
||||||
|
func (self *Decoder) Pos() int {
|
||||||
|
return self.i
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Decoder) Reset(s string) {
|
||||||
|
self.s = s
|
||||||
|
self.i = 0
|
||||||
|
// self.f = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: api fallback do nothing
|
||||||
|
func (self *Decoder) CheckTrailings() error {
|
||||||
|
pos := self.i
|
||||||
|
buf := self.s
|
||||||
|
/* skip all the trailing spaces */
|
||||||
|
if pos != len(buf) {
|
||||||
|
for pos < len(buf) && (types.SPACE_MASK & (1 << buf[pos])) != 0 {
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* then it must be at EOF */
|
||||||
|
if pos == len(buf) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/* junk after JSON value */
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Decode parses the JSON-encoded data from current position and stores the result
|
||||||
|
// in the value pointed to by val.
|
||||||
|
func (self *Decoder) Decode(val interface{}) error {
|
||||||
|
r := bytes.NewBufferString(self.s)
|
||||||
|
dec := json.NewDecoder(r)
|
||||||
|
if (self.f | uint64(OptionUseNumber)) != 0 {
|
||||||
|
dec.UseNumber()
|
||||||
|
}
|
||||||
|
if (self.f | uint64(OptionDisableUnknown)) != 0 {
|
||||||
|
dec.DisallowUnknownFields()
|
||||||
|
}
|
||||||
|
return dec.Decode(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UseInt64 indicates the Decoder to unmarshal an integer into an interface{} as an
|
||||||
|
// int64 instead of as a float64.
|
||||||
|
func (self *Decoder) UseInt64() {
|
||||||
|
self.f |= 1 << _F_use_int64
|
||||||
|
self.f &^= 1 << _F_use_number
|
||||||
|
}
|
||||||
|
|
||||||
|
// UseNumber indicates the Decoder to unmarshal a number into an interface{} as a
|
||||||
|
// json.Number instead of as a float64.
|
||||||
|
func (self *Decoder) UseNumber() {
|
||||||
|
self.f &^= 1 << _F_use_int64
|
||||||
|
self.f |= 1 << _F_use_number
|
||||||
|
}
|
||||||
|
|
||||||
|
// UseUnicodeErrors indicates the Decoder to return an error when encounter invalid
|
||||||
|
// UTF-8 escape sequences.
|
||||||
|
func (self *Decoder) UseUnicodeErrors() {
|
||||||
|
self.f |= 1 << _F_disable_urc
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisallowUnknownFields indicates the Decoder to return an error when the destination
|
||||||
|
// is a struct and the input contains object keys which do not match any
|
||||||
|
// non-ignored, exported fields in the destination.
|
||||||
|
func (self *Decoder) DisallowUnknownFields() {
|
||||||
|
self.f |= 1 << _F_disable_unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
// CopyString indicates the Decoder to decode string values by copying instead of referring.
|
||||||
|
func (self *Decoder) CopyString() {
|
||||||
|
self.f |= 1 << _F_copy_string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateString causes the Decoder to validate string values when decoding string value
|
||||||
|
// in JSON. Validation is that, returning error when unescaped control chars(0x00-0x1f) or
|
||||||
|
// invalid UTF-8 chars in the string value of JSON.
|
||||||
|
func (self *Decoder) ValidateString() {
|
||||||
|
self.f |= 1 << _F_validate_string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
|
||||||
|
// order to reduce the first-hit latency.
|
||||||
|
//
|
||||||
|
// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
|
||||||
|
// a compile option to set the depth of recursive compile for the nested struct type.
|
||||||
|
func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type StreamDecoder struct {
|
||||||
|
r io.Reader
|
||||||
|
buf []byte
|
||||||
|
scanp int
|
||||||
|
scanned int64
|
||||||
|
err error
|
||||||
|
Decoder
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStreamDecoder adapts to encoding/json.NewDecoder API.
|
||||||
|
//
|
||||||
|
// NewStreamDecoder returns a new decoder that reads from r.
|
||||||
|
func NewStreamDecoder(r io.Reader) *StreamDecoder {
|
||||||
|
return &StreamDecoder{r : r}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode decodes input stream into val with corresponding data.
|
||||||
|
// Redundantly bytes may be read and left in its buffer, and can be used at next call.
|
||||||
|
// Either io error from underlying io.Reader (except io.EOF)
|
||||||
|
// or syntax error from data will be recorded and stop subsequently decoding.
|
||||||
|
func (self *StreamDecoder) Decode(val interface{}) (err error) {
|
||||||
|
dec := json.NewDecoder(self.r)
|
||||||
|
if (self.f | uint64(OptionUseNumber)) != 0 {
|
||||||
|
dec.UseNumber()
|
||||||
|
}
|
||||||
|
if (self.f | uint64(OptionDisableUnknown)) != 0 {
|
||||||
|
dec.DisallowUnknownFields()
|
||||||
|
}
|
||||||
|
return dec.Decode(val)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,108 @@
|
|||||||
|
// +build amd64,go1.15,!go1.21
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2023 ByteDance Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package encoder
|
||||||
|
|
||||||
|
import (
|
||||||
|
`github.com/bytedance/sonic/internal/encoder`
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
// Encoder represents a specific set of encoder configurations.
|
||||||
|
type Encoder = encoder.Encoder
|
||||||
|
|
||||||
|
// StreamEncoder uses io.Writer as input.
|
||||||
|
type StreamEncoder = encoder.StreamEncoder
|
||||||
|
|
||||||
|
// Options is a set of encoding options.
|
||||||
|
type Options = encoder.Options
|
||||||
|
|
||||||
|
const (
|
||||||
|
// SortMapKeys indicates that the keys of a map needs to be sorted
|
||||||
|
// before serializing into JSON.
|
||||||
|
// WARNING: This hurts performance A LOT, USE WITH CARE.
|
||||||
|
SortMapKeys Options = encoder.SortMapKeys
|
||||||
|
|
||||||
|
// EscapeHTML indicates encoder to escape all HTML characters
|
||||||
|
// after serializing into JSON (see https://pkg.go.dev/encoding/json#HTMLEscape).
|
||||||
|
// WARNING: This hurts performance A LOT, USE WITH CARE.
|
||||||
|
EscapeHTML Options = encoder.EscapeHTML
|
||||||
|
|
||||||
|
// CompactMarshaler indicates that the output JSON from json.Marshaler
|
||||||
|
// is always compact and needs no validation
|
||||||
|
CompactMarshaler Options = encoder.CompactMarshaler
|
||||||
|
|
||||||
|
// NoQuoteTextMarshaler indicates that the output text from encoding.TextMarshaler
|
||||||
|
// is always escaped string and needs no quoting
|
||||||
|
NoQuoteTextMarshaler Options = encoder.NoQuoteTextMarshaler
|
||||||
|
|
||||||
|
// NoNullSliceOrMap indicates all empty Array or Object are encoded as '[]' or '{}',
|
||||||
|
// instead of 'null'
|
||||||
|
NoNullSliceOrMap Options = encoder.NoNullSliceOrMap
|
||||||
|
|
||||||
|
// ValidateString indicates that encoder should validate the input string
|
||||||
|
// before encoding it into JSON.
|
||||||
|
ValidateString Options = encoder.ValidateString
|
||||||
|
|
||||||
|
// CompatibleWithStd is used to be compatible with std encoder.
|
||||||
|
CompatibleWithStd Options = encoder.CompatibleWithStd
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Encode returns the JSON encoding of val, encoded with opts.
|
||||||
|
Encode = encoder.Encode
|
||||||
|
|
||||||
|
// EncodeInto is like Encode but uses a user-supplied buffer instead of allocating a new one.
|
||||||
|
EncodeIndented = encoder.EncodeIndented
|
||||||
|
|
||||||
|
// EncodeIndented is like Encode but applies Indent to format the output.
|
||||||
|
// Each JSON element in the output will begin on a new line beginning with prefix
|
||||||
|
// followed by one or more copies of indent according to the indentation nesting.
|
||||||
|
EncodeInto = encoder.EncodeInto
|
||||||
|
|
||||||
|
// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
|
||||||
|
// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
|
||||||
|
// so that the JSON will be safe to embed inside HTML <script> tags.
|
||||||
|
// For historical reasons, web browsers don't honor standard HTML
|
||||||
|
// escaping within <script> tags, so an alternative JSON encoding must
|
||||||
|
// be used.
|
||||||
|
HTMLEscape = encoder.HTMLEscape
|
||||||
|
|
||||||
|
// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
|
||||||
|
// order to reduce the first-hit latency.
|
||||||
|
//
|
||||||
|
// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
|
||||||
|
// a compile option to set the depth of recursive compile for the nested struct type.
|
||||||
|
Pretouch = encoder.Pretouch
|
||||||
|
|
||||||
|
// Quote returns the JSON-quoted version of s.
|
||||||
|
Quote = encoder.Quote
|
||||||
|
|
||||||
|
// Valid validates json and returns first non-blank character position,
|
||||||
|
// if it is only one valid json value.
|
||||||
|
// Otherwise returns invalid character position using start.
|
||||||
|
//
|
||||||
|
// Note: it does not check for the invalid UTF-8 characters.
|
||||||
|
Valid = encoder.Valid
|
||||||
|
|
||||||
|
// NewStreamEncoder adapts to encoding/json.NewDecoder API.
|
||||||
|
//
|
||||||
|
// NewStreamEncoder returns a new encoder that write to w.
|
||||||
|
NewStreamEncoder = encoder.NewStreamEncoder
|
||||||
|
)
|
@ -0,0 +1,234 @@
|
|||||||
|
// +build !amd64 go1.21
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2023 ByteDance Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package encoder
|
||||||
|
|
||||||
|
import (
|
||||||
|
`io`
|
||||||
|
`bytes`
|
||||||
|
`encoding/json`
|
||||||
|
`reflect`
|
||||||
|
|
||||||
|
`github.com/bytedance/sonic/option`
|
||||||
|
)
|
||||||
|
|
||||||
|
// Options is a set of encoding options.
|
||||||
|
type Options uint64
|
||||||
|
|
||||||
|
const (
|
||||||
|
bitSortMapKeys = iota
|
||||||
|
bitEscapeHTML
|
||||||
|
bitCompactMarshaler
|
||||||
|
bitNoQuoteTextMarshaler
|
||||||
|
bitNoNullSliceOrMap
|
||||||
|
bitValidateString
|
||||||
|
|
||||||
|
// used for recursive compile
|
||||||
|
bitPointerValue = 63
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// SortMapKeys indicates that the keys of a map needs to be sorted
|
||||||
|
// before serializing into JSON.
|
||||||
|
// WARNING: This hurts performance A LOT, USE WITH CARE.
|
||||||
|
SortMapKeys Options = 1 << bitSortMapKeys
|
||||||
|
|
||||||
|
// EscapeHTML indicates encoder to escape all HTML characters
|
||||||
|
// after serializing into JSON (see https://pkg.go.dev/encoding/json#HTMLEscape).
|
||||||
|
// WARNING: This hurts performance A LOT, USE WITH CARE.
|
||||||
|
EscapeHTML Options = 1 << bitEscapeHTML
|
||||||
|
|
||||||
|
// CompactMarshaler indicates that the output JSON from json.Marshaler
|
||||||
|
// is always compact and needs no validation
|
||||||
|
CompactMarshaler Options = 1 << bitCompactMarshaler
|
||||||
|
|
||||||
|
// NoQuoteTextMarshaler indicates that the output text from encoding.TextMarshaler
|
||||||
|
// is always escaped string and needs no quoting
|
||||||
|
NoQuoteTextMarshaler Options = 1 << bitNoQuoteTextMarshaler
|
||||||
|
|
||||||
|
// NoNullSliceOrMap indicates all empty Array or Object are encoded as '[]' or '{}',
|
||||||
|
// instead of 'null'
|
||||||
|
NoNullSliceOrMap Options = 1 << bitNoNullSliceOrMap
|
||||||
|
|
||||||
|
// ValidateString indicates that encoder should validate the input string
|
||||||
|
// before encoding it into JSON.
|
||||||
|
ValidateString Options = 1 << bitValidateString
|
||||||
|
|
||||||
|
// CompatibleWithStd is used to be compatible with std encoder.
|
||||||
|
CompatibleWithStd Options = SortMapKeys | EscapeHTML | CompactMarshaler
|
||||||
|
)
|
||||||
|
|
||||||
|
// Encoder represents a specific set of encoder configurations.
|
||||||
|
type Encoder struct {
|
||||||
|
Opts Options
|
||||||
|
prefix string
|
||||||
|
indent string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode returns the JSON encoding of v.
|
||||||
|
func (self *Encoder) Encode(v interface{}) ([]byte, error) {
|
||||||
|
if self.indent != "" || self.prefix != "" {
|
||||||
|
return EncodeIndented(v, self.prefix, self.indent, self.Opts)
|
||||||
|
}
|
||||||
|
return Encode(v, self.Opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SortKeys enables the SortMapKeys option.
|
||||||
|
func (self *Encoder) SortKeys() *Encoder {
|
||||||
|
self.Opts |= SortMapKeys
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetEscapeHTML specifies if option EscapeHTML opens
|
||||||
|
func (self *Encoder) SetEscapeHTML(f bool) {
|
||||||
|
if f {
|
||||||
|
self.Opts |= EscapeHTML
|
||||||
|
} else {
|
||||||
|
self.Opts &= ^EscapeHTML
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetValidateString specifies if option ValidateString opens
|
||||||
|
func (self *Encoder) SetValidateString(f bool) {
|
||||||
|
if f {
|
||||||
|
self.Opts |= ValidateString
|
||||||
|
} else {
|
||||||
|
self.Opts &= ^ValidateString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCompactMarshaler specifies if option CompactMarshaler opens
|
||||||
|
func (self *Encoder) SetCompactMarshaler(f bool) {
|
||||||
|
if f {
|
||||||
|
self.Opts |= CompactMarshaler
|
||||||
|
} else {
|
||||||
|
self.Opts &= ^CompactMarshaler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetNoQuoteTextMarshaler specifies if option NoQuoteTextMarshaler opens
|
||||||
|
func (self *Encoder) SetNoQuoteTextMarshaler(f bool) {
|
||||||
|
if f {
|
||||||
|
self.Opts |= NoQuoteTextMarshaler
|
||||||
|
} else {
|
||||||
|
self.Opts &= ^NoQuoteTextMarshaler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetIndent instructs the encoder to format each subsequent encoded
|
||||||
|
// value as if indented by the package-level function EncodeIndent().
|
||||||
|
// Calling SetIndent("", "") disables indentation.
|
||||||
|
func (enc *Encoder) SetIndent(prefix, indent string) {
|
||||||
|
enc.prefix = prefix
|
||||||
|
enc.indent = indent
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quote returns the JSON-quoted version of s.
|
||||||
|
func Quote(s string) string {
|
||||||
|
/* check for empty string */
|
||||||
|
if s == "" {
|
||||||
|
return `""`
|
||||||
|
}
|
||||||
|
|
||||||
|
out, _ := json.Marshal(s)
|
||||||
|
return string(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode returns the JSON encoding of val, encoded with opts.
|
||||||
|
func Encode(val interface{}, opts Options) ([]byte, error) {
|
||||||
|
return json.Marshal(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncodeInto is like Encode but uses a user-supplied buffer instead of allocating
|
||||||
|
// a new one.
|
||||||
|
func EncodeInto(buf *[]byte, val interface{}, opts Options) error {
|
||||||
|
if buf == nil {
|
||||||
|
panic("user-supplied buffer buf is nil")
|
||||||
|
}
|
||||||
|
w := bytes.NewBuffer(*buf)
|
||||||
|
enc := json.NewEncoder(w)
|
||||||
|
enc.SetEscapeHTML((opts & EscapeHTML) != 0)
|
||||||
|
err := enc.Encode(val)
|
||||||
|
*buf = w.Bytes()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
|
||||||
|
// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
|
||||||
|
// so that the JSON will be safe to embed inside HTML <script> tags.
|
||||||
|
// For historical reasons, web browsers don't honor standard HTML
|
||||||
|
// escaping within <script> tags, so an alternative JSON encoding must
|
||||||
|
// be used.
|
||||||
|
func HTMLEscape(dst []byte, src []byte) []byte {
|
||||||
|
d := bytes.NewBuffer(dst)
|
||||||
|
json.HTMLEscape(d, src)
|
||||||
|
return d.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncodeIndented is like Encode but applies Indent to format the output.
|
||||||
|
// Each JSON element in the output will begin on a new line beginning with prefix
|
||||||
|
// followed by one or more copies of indent according to the indentation nesting.
|
||||||
|
func EncodeIndented(val interface{}, prefix string, indent string, opts Options) ([]byte, error) {
|
||||||
|
w := bytes.NewBuffer([]byte{})
|
||||||
|
enc := json.NewEncoder(w)
|
||||||
|
enc.SetEscapeHTML((opts & EscapeHTML) != 0)
|
||||||
|
enc.SetIndent(prefix, indent)
|
||||||
|
err := enc.Encode(val)
|
||||||
|
out := w.Bytes()
|
||||||
|
return out, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
|
||||||
|
// order to reduce the first-hit latency.
|
||||||
|
//
|
||||||
|
// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
|
||||||
|
// a compile option to set the depth of recursive compile for the nested struct type.
|
||||||
|
func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Valid validates json and returns first non-blank character position,
|
||||||
|
// if it is only one valid json value.
|
||||||
|
// Otherwise returns invalid character position using start.
|
||||||
|
//
|
||||||
|
// Note: it does not check for the invalid UTF-8 characters.
|
||||||
|
func Valid(data []byte) (ok bool, start int) {
|
||||||
|
return json.Valid(data), 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// StreamEncoder uses io.Writer as
|
||||||
|
type StreamEncoder struct {
|
||||||
|
w io.Writer
|
||||||
|
Encoder
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStreamEncoder adapts to encoding/json.NewDecoder API.
|
||||||
|
//
|
||||||
|
// NewStreamEncoder returns a new encoder that write to w.
|
||||||
|
func NewStreamEncoder(w io.Writer) *StreamEncoder {
|
||||||
|
return &StreamEncoder{w: w}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode encodes interface{} as JSON to io.Writer
|
||||||
|
func (enc *StreamEncoder) Encode(val interface{}) (err error) {
|
||||||
|
jenc := json.NewEncoder(enc.w)
|
||||||
|
jenc.SetEscapeHTML((enc.Opts & EscapeHTML) != 0)
|
||||||
|
jenc.SetIndent(enc.prefix, enc.indent)
|
||||||
|
err = jenc.Encode(val)
|
||||||
|
return err
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
// Copyright 2022 Gin Core Team. All rights reserved.
|
|
||||||
// Use of this source code is governed by a MIT style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
//go:build !go1.18
|
|
||||||
// +build !go1.18
|
|
||||||
|
|
||||||
package gin
|
|
||||||
|
|
||||||
type any = interface{}
|
|
@ -1,10 +0,0 @@
|
|||||||
// Copyright 2022 Gin Core Team. All rights reserved.
|
|
||||||
// Use of this source code is governed by a MIT style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
//go:build !go1.18
|
|
||||||
// +build !go1.18
|
|
||||||
|
|
||||||
package binding
|
|
||||||
|
|
||||||
type any = interface{}
|
|
@ -1,5 +0,0 @@
|
|||||||
coverage:
|
|
||||||
notify:
|
|
||||||
gitter:
|
|
||||||
default:
|
|
||||||
url: https://webhooks.gitter.im/e/d90dcdeeab2f1e357165
|
|
@ -0,0 +1,23 @@
|
|||||||
|
// Copyright 2023 Gin Core Team. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
//go:build go1.20
|
||||||
|
|
||||||
|
package bytesconv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StringToBytes converts string to byte slice without a memory allocation.
|
||||||
|
// For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.
|
||||||
|
func StringToBytes(s string) []byte {
|
||||||
|
return unsafe.Slice(unsafe.StringData(s), len(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
// BytesToString converts byte slice to string without a memory allocation.
|
||||||
|
// For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.
|
||||||
|
func BytesToString(b []byte) string {
|
||||||
|
return unsafe.String(unsafe.SliceData(b), len(b))
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
// Copyright 2021 Gin Core Team. All rights reserved.
|
|
||||||
// Use of this source code is governed by a MIT style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
//go:build !go1.18
|
|
||||||
// +build !go1.18
|
|
||||||
|
|
||||||
package render
|
|
||||||
|
|
||||||
type any = interface{}
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue