This repository was archived by the owner on Jun 12, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathencode.go
More file actions
246 lines (213 loc) · 6.38 KB
/
Copy pathencode.go
File metadata and controls
246 lines (213 loc) · 6.38 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright 2025 The Go-Spring 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
*
* https://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 jsonflow
import (
"encoding/base64"
"reflect"
"sort"
"strconv"
"github.com/go-spring/stdlib/jsonflow/internal/json"
)
// NotForPublicUse is a private type used to prevent the use of
// the package outside of this module.
type NotForPublicUse struct{}
// MarshalOptions is an interface that defines options for encoding JSON.
type MarshalOptions interface {
JSONOptions(NotForPublicUse)
}
type (
Indent string
IndentPrefix string
NilSliceAsNull bool
NilMapAsNull bool
Deterministic bool
)
func (Indent) JSONOptions(NotForPublicUse) {}
func (IndentPrefix) JSONOptions(NotForPublicUse) {}
func (NilSliceAsNull) JSONOptions(NotForPublicUse) {}
func (NilMapAsNull) JSONOptions(NotForPublicUse) {}
func (Deterministic) JSONOptions(NotForPublicUse) {}
// Encoder is a streaming JSON encoder.
type Encoder = json.Encoder
// EncodeJSONer represents a JSON-mappable object that supports streaming encoding.
type EncodeJSONer interface {
// EncodeJSON writes this object as one JSON value to the Encoder.
EncodeJSON(e Encoder) error
}
// EncodeNull encodes a JSON null value.
func EncodeNull(e Encoder) error {
return e.WriteNull()
}
// EncodeBool encodes a boolean value to JSON.
func EncodeBool[T ~bool](e Encoder, v T) error {
return e.WriteBool(bool(v))
}
// EncodeBoolPtr encodes a pointer to boolean value to JSON.
func EncodeBoolPtr[T ~bool](e Encoder, v *T) error {
if v == nil {
return EncodeNull(e)
}
return EncodeBool(e, *v)
}
// EncodeInt encodes an integer value to JSON.
func EncodeInt[T ~int | ~int8 | ~int16 | ~int32 | ~int64](e Encoder, i T) error {
return e.WriteInt(int64(i))
}
// EncodeIntPtr encodes a pointer to integer value to JSON.
func EncodeIntPtr[T ~int | ~int8 | ~int16 | ~int32 | ~int64](e Encoder, i *T) error {
if i == nil {
return EncodeNull(e)
}
return EncodeInt(e, *i)
}
// EncodeUint encodes an unsigned integer value to JSON.
func EncodeUint[T ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64](e Encoder, u T) error {
return e.WriteUint(uint64(u))
}
// EncodeUintPtr encodes a pointer to unsigned integer value to JSON.
func EncodeUintPtr[T ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64](e Encoder, u *T) error {
if u == nil {
return EncodeNull(e)
}
return EncodeUint(e, *u)
}
// EncodeFloat encodes a floating-point value to JSON.
func EncodeFloat[T ~float32 | ~float64](e Encoder, f T) error {
return e.WriteFloat(float64(f))
}
// EncodeFloatPtr encodes a pointer to floating-point value to JSON.
func EncodeFloatPtr[T ~float32 | ~float64](e Encoder, f *T) error {
if f == nil {
return EncodeNull(e)
}
return EncodeFloat(e, *f)
}
// EncodeString encodes a string value to JSON.
func EncodeString[T ~string](e Encoder, s T) error {
return e.WriteString(string(s))
}
// EncodeStringPtr encodes a pointer to string value to JSON.
func EncodeStringPtr[T ~string](e Encoder, s *T) error {
if s == nil {
return EncodeNull(e)
}
return EncodeString(e, *s)
}
// EncodeBytes encodes bytes as a base64 JSON string.
func EncodeBytes(e Encoder, b []byte) error {
if b == nil {
return EncodeNull(e)
}
return e.WriteString(base64.StdEncoding.EncodeToString(b))
}
// EncodeAny marshals an arbitrary Go value and writes it as a raw JSON value.
func EncodeAny[T any](e Encoder, v T) error {
b, err := Marshal(v)
if err != nil {
return err
}
return e.WriteValue(b)
}
// EncodeObject encodes an object that implements EncodeJSONer.
func EncodeObject[T EncodeJSONer](e Encoder, v T) error {
if isNil(v) {
return EncodeNull(e)
}
return v.EncodeJSON(e)
}
// EncodeArray encodes a slice using the provided item encoder.
func EncodeArray[T any](
encodeItem func(Encoder, T) error,
) func(Encoder, []T) error {
return func(e Encoder, values []T) error {
if values == nil {
return EncodeNull(e)
}
if err := e.WriteArrayBegin(); err != nil {
return err
}
for _, v := range values {
if err := encodeItem(e, v); err != nil {
return err
}
}
return e.WriteArrayEnd()
}
}
// MapKeyEncoder encodes a Go map key as a JSON object member name.
type MapKeyEncoder[K comparable] func(K) (string, error)
// EncodeStringKey encodes a string-like map key as a JSON object member name.
func EncodeStringKey[T ~string](v T) (string, error) {
return string(v), nil
}
// EncodeIntKey encodes an integer-like map key as a JSON object member name.
func EncodeIntKey[T ~int | ~int8 | ~int16 | ~int32 | ~int64](v T) (string, error) {
return strconv.FormatInt(int64(v), 10), nil
}
// EncodeUintKey encodes an unsigned integer-like map key as a JSON object member name.
func EncodeUintKey[T ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64](v T) (string, error) {
return strconv.FormatUint(uint64(v), 10), nil
}
// EncodeMap encodes a map as a JSON object using the provided key and value encoders.
func EncodeMap[K comparable, V any](
encodeKey MapKeyEncoder[K],
encodeVal func(Encoder, V) error,
) func(Encoder, map[K]V) error {
return func(e Encoder, values map[K]V) error {
if values == nil {
return EncodeNull(e)
}
type entry struct {
key string
val V
}
entries := make([]entry, 0, len(values))
for k, v := range values {
key, err := encodeKey(k)
if err != nil {
return err
}
entries = append(entries, entry{key: key, val: v})
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].key < entries[j].key
})
if err := e.WriteObjectBegin(); err != nil {
return err
}
for _, ent := range entries {
if err := e.WriteString(ent.key); err != nil {
return err
}
if err := encodeVal(e, ent.val); err != nil {
return err
}
}
return e.WriteObjectEnd()
}
}
func isNil(v any) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
return rv.IsNil()
default:
return false
}
}