podman
164 строки · 4.6 Кб
1// +build amd64,go1.16,!go1.22
2
3/*
4* Copyright 2021 ByteDance Inc.
5*
6* Licensed under the Apache License, Version 2.0 (the "License");
7* you may not use this file except in compliance with the License.
8* You may obtain a copy of the License at
9*
10* http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing, software
13* distributed under the License is distributed on an "AS IS" BASIS,
14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15* See the License for the specific language governing permissions and
16* limitations under the License.
17*/
18
19//go:generate make
20package sonic21
22import (23`io`24`reflect`25
26`github.com/bytedance/sonic/decoder`27`github.com/bytedance/sonic/encoder`28`github.com/bytedance/sonic/option`29`github.com/bytedance/sonic/internal/rt`30)
31
32type frozenConfig struct {33Config
34encoderOpts encoder.Options35decoderOpts decoder.Options36}
37
38// Froze convert the Config to API
39func (cfg Config) Froze() API {40api := &frozenConfig{Config: cfg}41
42// configure encoder options:43if cfg.EscapeHTML {44api.encoderOpts |= encoder.EscapeHTML45}46if cfg.SortMapKeys {47api.encoderOpts |= encoder.SortMapKeys48}49if cfg.CompactMarshaler {50api.encoderOpts |= encoder.CompactMarshaler51}52if cfg.NoQuoteTextMarshaler {53api.encoderOpts |= encoder.NoQuoteTextMarshaler54}55if cfg.NoNullSliceOrMap {56api.encoderOpts |= encoder.NoNullSliceOrMap57}58if cfg.ValidateString {59api.encoderOpts |= encoder.ValidateString60}61if cfg.NoValidateJSONMarshaler {62api.encoderOpts |= encoder.NoValidateJSONMarshaler63}64
65// configure decoder options:66if cfg.UseInt64 {67api.decoderOpts |= decoder.OptionUseInt6468}69if cfg.UseNumber {70api.decoderOpts |= decoder.OptionUseNumber71}72if cfg.DisallowUnknownFields {73api.decoderOpts |= decoder.OptionDisableUnknown74}75if cfg.CopyString {76api.decoderOpts |= decoder.OptionCopyString77}78if cfg.ValidateString {79api.decoderOpts |= decoder.OptionValidateString80}81return api82}
83
84// Marshal is implemented by sonic
85func (cfg frozenConfig) Marshal(val interface{}) ([]byte, error) {86return encoder.Encode(val, cfg.encoderOpts)87}
88
89// MarshalToString is implemented by sonic
90func (cfg frozenConfig) MarshalToString(val interface{}) (string, error) {91buf, err := encoder.Encode(val, cfg.encoderOpts)92return rt.Mem2Str(buf), err93}
94
95// MarshalIndent is implemented by sonic
96func (cfg frozenConfig) MarshalIndent(val interface{}, prefix, indent string) ([]byte, error) {97return encoder.EncodeIndented(val, prefix, indent, cfg.encoderOpts)98}
99
100// UnmarshalFromString is implemented by sonic
101func (cfg frozenConfig) UnmarshalFromString(buf string, val interface{}) error {102dec := decoder.NewDecoder(buf)103dec.SetOptions(cfg.decoderOpts)104err := dec.Decode(val)105
106/* check for errors */107if err != nil {108return err109}110
111return dec.CheckTrailings()112}
113
114// Unmarshal is implemented by sonic
115func (cfg frozenConfig) Unmarshal(buf []byte, val interface{}) error {116return cfg.UnmarshalFromString(string(buf), val)117}
118
119// NewEncoder is implemented by sonic
120func (cfg frozenConfig) NewEncoder(writer io.Writer) Encoder {121enc := encoder.NewStreamEncoder(writer)122enc.Opts = cfg.encoderOpts123return enc124}
125
126// NewDecoder is implemented by sonic
127func (cfg frozenConfig) NewDecoder(reader io.Reader) Decoder {128dec := decoder.NewStreamDecoder(reader)129dec.SetOptions(cfg.decoderOpts)130return dec131}
132
133// Valid is implemented by sonic
134func (cfg frozenConfig) Valid(data []byte) bool {135ok, _ := encoder.Valid(data)136return ok137}
138
139// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
140// order to reduce the first-hit latency.
141//
142// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
143// a compile option to set the depth of recursive compile for the nested struct type.
144func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {145if err := encoder.Pretouch(vt, opts...); err != nil {146return err147}148if err := decoder.Pretouch(vt, opts...); err != nil {149return err150}151// to pretouch the corresponding pointer type as well152if vt.Kind() == reflect.Ptr {153vt = vt.Elem()154} else {155vt = reflect.PtrTo(vt)156}157if err := encoder.Pretouch(vt, opts...); err != nil {158return err159}160if err := decoder.Pretouch(vt, opts...); err != nil {161return err162}163return nil164}
165