Debian Codegraph @

internal/golang/encoding/json/kubernetes_patch.go

raw symbols SCIP
1 /*
2 Copyright 2021 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package json
18
19 import (
20 gojson "encoding/json"
21 "strconv"
22 "strings"
23 )
24
25 // Type-alias error and data types returned from decoding
26
27 type UnmarshalTypeError = gojson.UnmarshalTypeError
28 type UnmarshalFieldError = gojson.UnmarshalFieldError
29 type InvalidUnmarshalError = gojson.InvalidUnmarshalError
30 type Number = gojson.Number
31 type RawMessage = gojson.RawMessage
32 type Token = gojson.Token
33 type Delim = gojson.Delim
34
35 type UnmarshalOpt func(*decodeState)
36
37 func UseNumber(d *decodeState) {
38 d.useNumber = true
39 }
40 func DisallowUnknownFields(d *decodeState) {
41 d.disallowUnknownFields = true
42 }
43
44 // CaseSensitive requires json keys to exactly match specified json tags (for tagged struct fields)
45 // or struct field names (for untagged struct fields), or be treated as an unknown field.
46 func CaseSensitive(d *decodeState) {
47 d.caseSensitive = true
48 }
49 func (d *Decoder) CaseSensitive() {
50 d.d.caseSensitive = true
51 }
52
53 // PreserveInts decodes numbers as int64 when decoding to untyped fields,
54 // if the JSON data does not contain a "." character, parses as an integer successfully,
55 // and does not overflow int64. Otherwise, it falls back to default float64 decoding behavior.
56 //
57 // If UseNumber is also set, it takes precedence over PreserveInts.
58 func PreserveInts(d *decodeState) {
59 d.preserveInts = true
60 }
61 func (d *Decoder) PreserveInts() {
62 d.d.preserveInts = true
63 }
64
65 // DisallowDuplicateFields treats duplicate fields encountered while decoding as an error.
66 func DisallowDuplicateFields(d *decodeState) {
67 d.disallowDuplicateFields = true
68 }
69 func (d *Decoder) DisallowDuplicateFields() {
70 d.d.disallowDuplicateFields = true
71 }
72
73 func (d *decodeState) newFieldError(errType strictErrType, field string) *strictError {
74 if len(d.strictFieldStack) > 0 {
75 return &strictError{
76 ErrType: errType,
77 Path: strings.Join(d.strictFieldStack, "") + "." + field,
78 }
79 } else {
80 return &strictError{
81 ErrType: errType,
82 Path: field,
83 }
84 }
85 }
86
87 // saveStrictError saves a strict decoding error,
88 // for reporting at the end of the unmarshal if no other errors occurred.
89 func (d *decodeState) saveStrictError(err *strictError) {
90 // prevent excessive numbers of accumulated errors
91 if len(d.savedStrictErrors) >= 100 {
92 return
93 }
94 // dedupe accumulated strict errors
95 if d.seenStrictErrors == nil {
96 d.seenStrictErrors = map[strictError]struct{}{}
97 }
98 if _, seen := d.seenStrictErrors[*err]; seen {
99 return
100 }
101
102 // accumulate the error
103 d.seenStrictErrors[*err] = struct{}{}
104 d.savedStrictErrors = append(d.savedStrictErrors, err)
105 }
106
107 func (d *decodeState) appendStrictFieldStackKey(key string) {
108 if !d.disallowDuplicateFields && !d.disallowUnknownFields {
109 return
110 }
111 if len(d.strictFieldStack) > 0 {
112 d.strictFieldStack = append(d.strictFieldStack, ".", key)
113 } else {
114 d.strictFieldStack = append(d.strictFieldStack, key)
115 }
116 }
117
118 func (d *decodeState) appendStrictFieldStackIndex(i int) {
119 if !d.disallowDuplicateFields && !d.disallowUnknownFields {
120 return
121 }
122 d.strictFieldStack = append(d.strictFieldStack, "[", strconv.Itoa(i), "]")
123 }
124
125 type strictErrType string
126
127 const (
128 unknownStrictErrType strictErrType = "unknown field"
129 duplicateStrictErrType strictErrType = "duplicate field"
130 )
131
132 // strictError is a strict decoding error
133 // It has an ErrType (either unknown or duplicate)
134 // and a path to the erroneous field
135 type strictError struct {
136 ErrType strictErrType
137 Path string
138 }
139
140 func (e *strictError) Error() string {
141 return string(e.ErrType) + " " + strconv.Quote(e.Path)
142 }
143
144 func (e *strictError) FieldPath() string {
145 return e.Path
146 }
147
148 func (e *strictError) SetFieldPath(path string) {
149 e.Path = path
150 }
151
152 // UnmarshalStrictError holds errors resulting from use of strict disallow___ decoder directives.
153 // If this is returned from Unmarshal(), it means the decoding was successful in all other respects.
154 type UnmarshalStrictError struct {
155 Errors []error
156 }
157
158 func (e *UnmarshalStrictError) Error() string {
159 var b strings.Builder
160 b.WriteString("json: ")
161 for i, err := range e.Errors {
162 if i > 0 {
163 b.WriteString(", ")
164 }
165 b.WriteString(err.Error())
166 }
167 return b.String()
168 }