podman

Форк
0
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
20
package sonic
21

22
import (
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

32
type frozenConfig struct {
33
    Config
34
    encoderOpts encoder.Options
35
    decoderOpts decoder.Options
36
}
37

38
// Froze convert the Config to API
39
func (cfg Config) Froze() API {
40
    api := &frozenConfig{Config: cfg}
41

42
    // configure encoder options:
43
    if cfg.EscapeHTML {
44
        api.encoderOpts |= encoder.EscapeHTML
45
    }
46
    if cfg.SortMapKeys {
47
        api.encoderOpts |= encoder.SortMapKeys
48
    }
49
    if cfg.CompactMarshaler {
50
        api.encoderOpts |= encoder.CompactMarshaler
51
    }
52
    if cfg.NoQuoteTextMarshaler {
53
        api.encoderOpts |= encoder.NoQuoteTextMarshaler
54
    }
55
    if cfg.NoNullSliceOrMap {
56
        api.encoderOpts |= encoder.NoNullSliceOrMap
57
    }
58
    if cfg.ValidateString {
59
        api.encoderOpts |= encoder.ValidateString
60
    }
61
    if cfg.NoValidateJSONMarshaler {
62
        api.encoderOpts |= encoder.NoValidateJSONMarshaler
63
    }
64

65
    // configure decoder options:
66
    if cfg.UseInt64 {
67
        api.decoderOpts |= decoder.OptionUseInt64
68
    }
69
    if cfg.UseNumber {
70
        api.decoderOpts |= decoder.OptionUseNumber
71
    }
72
    if cfg.DisallowUnknownFields {
73
        api.decoderOpts |= decoder.OptionDisableUnknown
74
    }
75
    if cfg.CopyString {
76
        api.decoderOpts |= decoder.OptionCopyString
77
    }
78
    if cfg.ValidateString {
79
        api.decoderOpts |= decoder.OptionValidateString
80
    }
81
    return api
82
}
83

84
// Marshal is implemented by sonic
85
func (cfg frozenConfig) Marshal(val interface{}) ([]byte, error) {
86
    return encoder.Encode(val, cfg.encoderOpts)
87
}
88

89
// MarshalToString is implemented by sonic
90
func (cfg frozenConfig) MarshalToString(val interface{}) (string, error) {
91
    buf, err := encoder.Encode(val, cfg.encoderOpts)
92
    return rt.Mem2Str(buf), err
93
}
94

95
// MarshalIndent is implemented by sonic
96
func (cfg frozenConfig) MarshalIndent(val interface{}, prefix, indent string) ([]byte, error) {
97
    return encoder.EncodeIndented(val, prefix, indent, cfg.encoderOpts)
98
}
99

100
// UnmarshalFromString is implemented by sonic
101
func (cfg frozenConfig) UnmarshalFromString(buf string, val interface{}) error {
102
    dec := decoder.NewDecoder(buf)
103
    dec.SetOptions(cfg.decoderOpts)
104
    err := dec.Decode(val)
105

106
    /* check for errors */
107
    if err != nil {
108
        return err
109
    }
110

111
    return dec.CheckTrailings()
112
}
113

114
// Unmarshal is implemented by sonic
115
func (cfg frozenConfig) Unmarshal(buf []byte, val interface{}) error {
116
    return cfg.UnmarshalFromString(string(buf), val)
117
}
118

119
// NewEncoder is implemented by sonic
120
func (cfg frozenConfig) NewEncoder(writer io.Writer) Encoder {
121
    enc := encoder.NewStreamEncoder(writer)
122
    enc.Opts = cfg.encoderOpts
123
    return enc
124
}
125

126
// NewDecoder is implemented by sonic
127
func (cfg frozenConfig) NewDecoder(reader io.Reader) Decoder {
128
    dec := decoder.NewStreamDecoder(reader)
129
    dec.SetOptions(cfg.decoderOpts)
130
    return dec
131
}
132

133
// Valid is implemented by sonic
134
func (cfg frozenConfig) Valid(data []byte) bool {
135
    ok, _ := encoder.Valid(data)
136
    return ok
137
}
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.
144
func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
145
    if err := encoder.Pretouch(vt, opts...); err != nil {
146
        return err
147
    } 
148
    if err := decoder.Pretouch(vt, opts...); err != nil {
149
        return err
150
    }
151
    // to pretouch the corresponding pointer type as well
152
    if vt.Kind() == reflect.Ptr {
153
        vt = vt.Elem()
154
    } else {
155
        vt = reflect.PtrTo(vt)
156
    }
157
    if err := encoder.Pretouch(vt, opts...); err != nil {
158
        return err
159
    } 
160
    if err := decoder.Pretouch(vt, opts...); err != nil {
161
        return err
162
    }
163
    return nil
164
}
165

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.