File: kubernetes_patch.go

package info (click to toggle)
golang-k8s-sigs-json 0.0~git20211208.9f7c6b3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 588 kB
  • sloc: sh: 86; makefile: 29
file content (137 lines) | stat: -rw-r--r-- 4,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Copyright 2021 The Kubernetes Authors.

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 json

import (
	gojson "encoding/json"
	"fmt"
	"strconv"
	"strings"
)

// Type-alias error and data types returned from decoding

type UnmarshalTypeError = gojson.UnmarshalTypeError
type UnmarshalFieldError = gojson.UnmarshalFieldError
type InvalidUnmarshalError = gojson.InvalidUnmarshalError
type Number = gojson.Number
type RawMessage = gojson.RawMessage
type Token = gojson.Token
type Delim = gojson.Delim

type UnmarshalOpt func(*decodeState)

func UseNumber(d *decodeState) {
	d.useNumber = true
}
func DisallowUnknownFields(d *decodeState) {
	d.disallowUnknownFields = true
}

// CaseSensitive requires json keys to exactly match specified json tags (for tagged struct fields)
// or struct field names (for untagged struct fields), or be treated as an unknown field.
func CaseSensitive(d *decodeState) {
	d.caseSensitive = true
}
func (d *Decoder) CaseSensitive() {
	d.d.caseSensitive = true
}

// PreserveInts decodes numbers as int64 when decoding to untyped fields,
// if the JSON data does not contain a "." character, parses as an integer successfully,
// and does not overflow int64. Otherwise, it falls back to default float64 decoding behavior.
//
// If UseNumber is also set, it takes precedence over PreserveInts.
func PreserveInts(d *decodeState) {
	d.preserveInts = true
}
func (d *Decoder) PreserveInts() {
	d.d.preserveInts = true
}

// DisallowDuplicateFields treats duplicate fields encountered while decoding as an error.
func DisallowDuplicateFields(d *decodeState) {
	d.disallowDuplicateFields = true
}
func (d *Decoder) DisallowDuplicateFields() {
	d.d.disallowDuplicateFields = true
}

func (d *decodeState) newFieldError(msg, field string) error {
	if len(d.strictFieldStack) > 0 {
		return fmt.Errorf("%s %q", msg, strings.Join(d.strictFieldStack, "")+"."+field)
	} else {
		return fmt.Errorf("%s %q", msg, field)
	}
}

// saveStrictError saves a strict decoding error,
// for reporting at the end of the unmarshal if no other errors occurred.
func (d *decodeState) saveStrictError(err error) {
	// prevent excessive numbers of accumulated errors
	if len(d.savedStrictErrors) >= 100 {
		return
	}
	// dedupe accumulated strict errors
	if d.seenStrictErrors == nil {
		d.seenStrictErrors = map[string]struct{}{}
	}
	msg := err.Error()
	if _, seen := d.seenStrictErrors[msg]; seen {
		return
	}

	// accumulate the error
	d.seenStrictErrors[msg] = struct{}{}
	d.savedStrictErrors = append(d.savedStrictErrors, err)
}

func (d *decodeState) appendStrictFieldStackKey(key string) {
	if !d.disallowDuplicateFields && !d.disallowUnknownFields {
		return
	}
	if len(d.strictFieldStack) > 0 {
		d.strictFieldStack = append(d.strictFieldStack, ".", key)
	} else {
		d.strictFieldStack = append(d.strictFieldStack, key)
	}
}

func (d *decodeState) appendStrictFieldStackIndex(i int) {
	if !d.disallowDuplicateFields && !d.disallowUnknownFields {
		return
	}
	d.strictFieldStack = append(d.strictFieldStack, "[", strconv.Itoa(i), "]")
}

// UnmarshalStrictError holds errors resulting from use of strict disallow___ decoder directives.
// If this is returned from Unmarshal(), it means the decoding was successful in all other respects.
type UnmarshalStrictError struct {
	Errors []error
}

func (e *UnmarshalStrictError) Error() string {
	var b strings.Builder
	b.WriteString("json: ")
	for i, err := range e.Errors {
		if i > 0 {
			b.WriteString(", ")
		}
		b.WriteString(err.Error())
	}
	return b.String()
}