| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
package jsonld |
| 12 |
|
| 13 |
import ( |
| 14 |
"bytes" |
| 15 |
"encoding" |
| 16 |
"encoding/base64" |
| 17 |
"encoding/json" |
| 18 |
"fmt" |
| 19 |
"math" |
| 20 |
"reflect" |
| 21 |
"runtime" |
| 22 |
"sort" |
| 23 |
"strconv" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"sync/atomic" |
| 27 |
"unicode" |
| 28 |
"unicode/utf8" |
| 29 |
) |
| 30 |
|
| 31 |
const ( |
| 32 |
tagLabel = "jsonld" |
| 33 |
tagOmitEmpty = "omitempty" |
| 34 |
tagCollapsible = "collapsible" |
| 35 |
tagNoMarshal = "nomarshal" |
| 36 |
) |
| 37 |
|
| 38 |
type payloadWithContext struct { |
| 39 |
Context []Collapsible `jsonld:"@context,omitempty,collapsible"` |
| 40 |
ID interface{} `jsonld:"@id,omitempty,collapsible"` |
| 41 |
Type interface{} `jsonld:"@type,omitempty,collapsible"` |
| 42 |
Obj interface{} |
| 43 |
} |
| 44 |
|
| 45 |
func (p payloadWithContext) Collapse() interface{} { |
| 46 |
return p |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
func WithContext(c ...Collapsible) payloadWithContext { |
| 51 |
return payloadWithContext{ |
| 52 |
Context: c, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
var payloadType = reflect.TypeOf(new(payloadWithContext)).Elem() |
| 57 |
|
| 58 |
|
| 59 |
func (p payloadWithContext) Marshal(v interface{}) ([]byte, error) { |
| 60 |
p.Obj = v |
| 61 |
return Marshal(p) |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
type Tag struct { |
| 66 |
Name string |
| 67 |
Ignore bool |
| 68 |
OmitEmpty bool |
| 69 |
Collapsible bool |
| 70 |
NoMarshal bool |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func LoadTag(tag reflect.StructTag) (Tag, bool) { |
| 75 |
jlTag, ok := tag.Lookup(tagLabel) |
| 76 |
if !ok { |
| 77 |
return Tag{}, false |
| 78 |
} |
| 79 |
val := strings.Split(jlTag, ",") |
| 80 |
cont := func(arr []string, s string) bool { |
| 81 |
for _, v := range arr { |
| 82 |
if v == s { |
| 83 |
return true |
| 84 |
} |
| 85 |
} |
| 86 |
return false |
| 87 |
} |
| 88 |
t := Tag{ |
| 89 |
OmitEmpty: cont(val, tagOmitEmpty), |
| 90 |
Collapsible: cont(val, tagCollapsible), |
| 91 |
NoMarshal: cont(val, tagNoMarshal), |
| 92 |
} |
| 93 |
t.Name, t.Ignore = func(v string) (string, bool) { |
| 94 |
if len(v) > 0 && v != "_" { |
| 95 |
return v, false |
| 96 |
} |
| 97 |
return "", true |
| 98 |
}(val[0]) |
| 99 |
|
| 100 |
return t, true |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func TagName(n string, tag Tag) string { |
| 105 |
if len(tag.Name) > 0 { |
| 106 |
return tag.Name |
| 107 |
} |
| 108 |
return n |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
type UnsupportedTypeError struct { |
| 114 |
Type reflect.Type |
| 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 |
func Marshal(v interface{}) ([]byte, error) { |
| 247 |
e := &encodeState{} |
| 248 |
|
| 249 |
err := e.marshal(v, encOpts{escapeHTML: false}) |
| 250 |
if err != nil { |
| 251 |
return nil, err |
| 252 |
} |
| 253 |
output := e.Bytes() |
| 254 |
if v == nil { |
| 255 |
return output, nil |
| 256 |
} |
| 257 |
|
| 258 |
repl := func(dat *[]byte) { |
| 259 |
|
| 260 |
o := *dat |
| 261 |
if bytes.Contains(o, []byte(`"Obj":{`)) { |
| 262 |
o = bytes.Replace(o, []byte(`,"Obj":{`), []byte(","), 1) |
| 263 |
o = o[:len(o)-1] |
| 264 |
} else if bytes.Contains(o, []byte(`"Obj":null`)) { |
| 265 |
o = bytes.Replace(o, []byte(`,"Obj":null`), []byte(""), 1) |
| 266 |
} else { |
| 267 |
contextIndex := bytes.Index(o, []byte(`,"Obj":`)) + len([]byte(`,"Obj":`)) |
| 268 |
if contextIndex < len(o)-1 { |
| 269 |
o = o[contextIndex : len(o)-1] |
| 270 |
} else { |
| 271 |
o = nil |
| 272 |
} |
| 273 |
} |
| 274 |
*dat = o |
| 275 |
} |
| 276 |
|
| 277 |
switch v.(type) { |
| 278 |
case payloadWithContext: |
| 279 |
repl(&output) |
| 280 |
case *payloadWithContext: |
| 281 |
repl(&output) |
| 282 |
} |
| 283 |
return output, nil |
| 284 |
} |
| 285 |
|
| 286 |
func (e *UnsupportedTypeError) Error() string { |
| 287 |
return tagLabel + ": unsupported type: " + e.Type.String() |
| 288 |
} |
| 289 |
|
| 290 |
type UnsupportedValueError struct { |
| 291 |
Value reflect.Value |
| 292 |
Str string |
| 293 |
} |
| 294 |
|
| 295 |
func (e *UnsupportedValueError) Error() string { |
| 296 |
return tagLabel + ": unsupported value: " + e.Str |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
type InvalidUTF8Error struct { |
| 306 |
S string |
| 307 |
} |
| 308 |
|
| 309 |
func (e *InvalidUTF8Error) Error() string { |
| 310 |
return tagLabel + ": invalid UTF-8 in string: " + strconv.Quote(e.S) |
| 311 |
} |
| 312 |
|
| 313 |
type MarshalerError struct { |
| 314 |
Type reflect.Type |
| 315 |
Err error |
| 316 |
} |
| 317 |
|
| 318 |
func (e *MarshalerError) Error() string { |
| 319 |
return tagLabel + ": error calling MarshalJSON for type " + e.Type.String() + ": " + e.Err.Error() |
| 320 |
} |
| 321 |
|
| 322 |
var hex = "0123456789abcdef" |
| 323 |
|
| 324 |
|
| 325 |
type encodeState struct { |
| 326 |
bytes.Buffer |
| 327 |
scratch [64]byte |
| 328 |
} |
| 329 |
|
| 330 |
var encodeStatePool sync.Pool |
| 331 |
|
| 332 |
func newEncodeState() *encodeState { |
| 333 |
if v := encodeStatePool.Get(); v != nil { |
| 334 |
e := v.(*encodeState) |
| 335 |
e.Reset() |
| 336 |
return e |
| 337 |
} |
| 338 |
return new(encodeState) |
| 339 |
} |
| 340 |
|
| 341 |
func (e *encodeState) marshal(v interface{}, opts encOpts) (err error) { |
| 342 |
defer func() { |
| 343 |
if r := recover(); r != nil { |
| 344 |
if _, ok := r.(runtime.Error); ok { |
| 345 |
panic(r) |
| 346 |
} |
| 347 |
if s, ok := r.(string); ok { |
| 348 |
panic(s) |
| 349 |
} |
| 350 |
err = r.(error) |
| 351 |
} |
| 352 |
}() |
| 353 |
e.reflectValue(reflect.ValueOf(v), opts) |
| 354 |
return nil |
| 355 |
} |
| 356 |
|
| 357 |
func (e *encodeState) error(err error) { |
| 358 |
panic(err) |
| 359 |
} |
| 360 |
|
| 361 |
func isEmptyValue(v reflect.Value) bool { |
| 362 |
switch v.Kind() { |
| 363 |
case reflect.Array, reflect.Map, reflect.Slice, reflect.String: |
| 364 |
return v.Len() == 0 |
| 365 |
case reflect.Bool: |
| 366 |
return !v.Bool() |
| 367 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 368 |
return v.Int() == 0 |
| 369 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 370 |
return v.Uint() == 0 |
| 371 |
case reflect.Float32, reflect.Float64: |
| 372 |
return v.Float() == 0 |
| 373 |
case reflect.Interface, reflect.Ptr: |
| 374 |
return v.IsNil() |
| 375 |
case reflect.Struct: |
| 376 |
|
| 377 |
|
| 378 |
return func(reflect.Value) bool { |
| 379 |
var ret bool = true |
| 380 |
for i := 0; i < v.NumField(); i++ { |
| 381 |
ret = ret && isEmptyValue(v.Field(i)) |
| 382 |
} |
| 383 |
return ret |
| 384 |
}(v) |
| 385 |
} |
| 386 |
return false |
| 387 |
} |
| 388 |
|
| 389 |
func (e *encodeState) reflectValue(v reflect.Value, opts encOpts) { |
| 390 |
valueEncoder(v)(e, v, opts) |
| 391 |
} |
| 392 |
|
| 393 |
type encOpts struct { |
| 394 |
|
| 395 |
quoted bool |
| 396 |
|
| 397 |
escapeHTML bool |
| 398 |
} |
| 399 |
|
| 400 |
type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts) |
| 401 |
|
| 402 |
var encoderCache sync.Map |
| 403 |
|
| 404 |
func valueEncoder(v reflect.Value) encoderFunc { |
| 405 |
if !v.IsValid() { |
| 406 |
return invalidValueEncoder |
| 407 |
} |
| 408 |
return typeEncoder(v.Type()) |
| 409 |
} |
| 410 |
|
| 411 |
func typeEncoder(t reflect.Type) encoderFunc { |
| 412 |
if fi, ok := encoderCache.Load(t); ok { |
| 413 |
return fi.(encoderFunc) |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
var ( |
| 421 |
wg sync.WaitGroup |
| 422 |
f encoderFunc |
| 423 |
) |
| 424 |
wg.Add(1) |
| 425 |
fi, loaded := encoderCache.LoadOrStore(t, encoderFunc(func(e *encodeState, v reflect.Value, opts encOpts) { |
| 426 |
wg.Wait() |
| 427 |
f(e, v, opts) |
| 428 |
})) |
| 429 |
if loaded { |
| 430 |
return fi.(encoderFunc) |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
f = newTypeEncoder(t, true) |
| 435 |
wg.Done() |
| 436 |
encoderCache.Store(t, f) |
| 437 |
return f |
| 438 |
} |
| 439 |
|
| 440 |
var ( |
| 441 |
marshalerType = reflect.TypeOf(new(json.Marshaler)).Elem() |
| 442 |
textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem() |
| 443 |
) |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc { |
| 448 |
if t.Implements(marshalerType) { |
| 449 |
return marshalerEncoder |
| 450 |
} |
| 451 |
if t.Kind() != reflect.Ptr && allowAddr { |
| 452 |
if reflect.PtrTo(t).Implements(marshalerType) { |
| 453 |
return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false)) |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
if t.Implements(textMarshalerType) { |
| 458 |
return textMarshalerEncoder |
| 459 |
} |
| 460 |
if t.Kind() != reflect.Ptr && allowAddr { |
| 461 |
if reflect.PtrTo(t).Implements(textMarshalerType) { |
| 462 |
return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false)) |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
switch t.Kind() { |
| 467 |
case reflect.Bool: |
| 468 |
return boolEncoder |
| 469 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 470 |
return intEncoder |
| 471 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 472 |
return uintEncoder |
| 473 |
case reflect.Float32: |
| 474 |
return float32Encoder |
| 475 |
case reflect.Float64: |
| 476 |
return float64Encoder |
| 477 |
case reflect.String: |
| 478 |
return stringEncoder |
| 479 |
case reflect.Interface: |
| 480 |
return interfaceEncoder |
| 481 |
case reflect.Struct: |
| 482 |
return newStructEncoder(t) |
| 483 |
case reflect.Map: |
| 484 |
return newMapEncoder(t) |
| 485 |
case reflect.Slice: |
| 486 |
return newSliceEncoder(t) |
| 487 |
case reflect.Array: |
| 488 |
return newArrayEncoder(t) |
| 489 |
case reflect.Ptr: |
| 490 |
return newPtrEncoder(t) |
| 491 |
default: |
| 492 |
return unsupportedTypeEncoder |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
func invalidValueEncoder(e *encodeState, v reflect.Value, _ encOpts) { |
| 497 |
e.WriteString("null") |
| 498 |
} |
| 499 |
|
| 500 |
func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { |
| 501 |
if v.Kind() == reflect.Ptr && v.IsNil() { |
| 502 |
e.WriteString("null") |
| 503 |
return |
| 504 |
} |
| 505 |
m, ok := v.Interface().(json.Marshaler) |
| 506 |
if !ok { |
| 507 |
e.WriteString("null") |
| 508 |
return |
| 509 |
} |
| 510 |
b, err := m.MarshalJSON() |
| 511 |
if err == nil { |
| 512 |
|
| 513 |
err = json.Compact(&e.Buffer, b) |
| 514 |
} |
| 515 |
if err != nil { |
| 516 |
e.error(&MarshalerError{v.Type(), err}) |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
func addrMarshalerEncoder(e *encodeState, v reflect.Value, _ encOpts) { |
| 521 |
va := v.Addr() |
| 522 |
if va.IsNil() { |
| 523 |
e.WriteString("null") |
| 524 |
return |
| 525 |
} |
| 526 |
m := va.Interface().(json.Marshaler) |
| 527 |
b, err := m.MarshalJSON() |
| 528 |
if err == nil { |
| 529 |
|
| 530 |
err = json.Compact(&e.Buffer, b) |
| 531 |
} |
| 532 |
if err != nil { |
| 533 |
e.error(&MarshalerError{v.Type(), err}) |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
func textMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { |
| 538 |
if v.Kind() == reflect.Ptr && v.IsNil() { |
| 539 |
e.WriteString("null") |
| 540 |
return |
| 541 |
} |
| 542 |
m := v.Interface().(encoding.TextMarshaler) |
| 543 |
b, err := m.MarshalText() |
| 544 |
if err != nil { |
| 545 |
e.error(&MarshalerError{v.Type(), err}) |
| 546 |
} |
| 547 |
e.stringBytes(b, opts.escapeHTML) |
| 548 |
} |
| 549 |
|
| 550 |
func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { |
| 551 |
va := v.Addr() |
| 552 |
if va.IsNil() { |
| 553 |
e.WriteString("null") |
| 554 |
return |
| 555 |
} |
| 556 |
m := va.Interface().(encoding.TextMarshaler) |
| 557 |
b, err := m.MarshalText() |
| 558 |
if err != nil { |
| 559 |
e.error(&MarshalerError{v.Type(), err}) |
| 560 |
} |
| 561 |
e.stringBytes(b, opts.escapeHTML) |
| 562 |
} |
| 563 |
|
| 564 |
func boolEncoder(e *encodeState, v reflect.Value, opts encOpts) { |
| 565 |
if opts.quoted { |
| 566 |
e.WriteByte('"') |
| 567 |
} |
| 568 |
if v.Bool() { |
| 569 |
e.WriteString("true") |
| 570 |
} else { |
| 571 |
e.WriteString("false") |
| 572 |
} |
| 573 |
if opts.quoted { |
| 574 |
e.WriteByte('"') |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
func intEncoder(e *encodeState, v reflect.Value, opts encOpts) { |
| 579 |
b := strconv.AppendInt(e.scratch[:0], v.Int(), 10) |
| 580 |
if opts.quoted { |
| 581 |
e.WriteByte('"') |
| 582 |
} |
| 583 |
e.Write(b) |
| 584 |
if opts.quoted { |
| 585 |
e.WriteByte('"') |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
func uintEncoder(e *encodeState, v reflect.Value, opts encOpts) { |
| 590 |
b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10) |
| 591 |
if opts.quoted { |
| 592 |
e.WriteByte('"') |
| 593 |
} |
| 594 |
e.Write(b) |
| 595 |
if opts.quoted { |
| 596 |
e.WriteByte('"') |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
type floatEncoder int |
| 601 |
|
| 602 |
func (bits floatEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { |
| 603 |
f := v.Float() |
| 604 |
if math.IsInf(f, 0) || math.IsNaN(f) { |
| 605 |
e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))}) |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
b := e.scratch[:0] |
| 614 |
abs := math.Abs(f) |
| 615 |
fmt := byte('f') |
| 616 |
|
| 617 |
if abs != 0 { |
| 618 |
if bits == 64 && (abs < 1e-6 || abs >= 1e21) || bits == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) { |
| 619 |
fmt = 'e' |
| 620 |
} |
| 621 |
} |
| 622 |
b = strconv.AppendFloat(b, f, fmt, -1, int(bits)) |
| 623 |
if fmt == 'e' { |
| 624 |
|
| 625 |
n := len(b) |
| 626 |
if n >= 4 && b[n-4] == 'e' && b[n-3] == '-' && b[n-2] == '0' { |
| 627 |
b[n-2] = b[n-1] |
| 628 |
b = b[:n-1] |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
if opts.quoted { |
| 633 |
e.WriteByte('"') |
| 634 |
} |
| 635 |
e.Write(b) |
| 636 |
if opts.quoted { |
| 637 |
e.WriteByte('"') |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
var ( |
| 642 |
float32Encoder = (floatEncoder(32)).encode |
| 643 |
float64Encoder = (floatEncoder(64)).encode |
| 644 |
) |
| 645 |
|
| 646 |
func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) { |
| 647 |
if v.Type() == numberType { |
| 648 |
numStr := v.String() |
| 649 |
|
| 650 |
|
| 651 |
if numStr == "" { |
| 652 |
numStr = "0" |
| 653 |
} |
| 654 |
if !isValidNumber(numStr) { |
| 655 |
e.error(fmt.Errorf(tagLabel+": invalid number literal %q", numStr)) |
| 656 |
} |
| 657 |
e.WriteString(numStr) |
| 658 |
return |
| 659 |
} |
| 660 |
if opts.quoted { |
| 661 |
sb, err := Marshal(v.String()) |
| 662 |
if err != nil { |
| 663 |
e.error(err) |
| 664 |
} |
| 665 |
e.string(string(sb), opts.escapeHTML) |
| 666 |
} else { |
| 667 |
e.string(v.String(), opts.escapeHTML) |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
func interfaceEncoder(e *encodeState, v reflect.Value, opts encOpts) { |
| 672 |
if v.IsNil() { |
| 673 |
e.WriteString("null") |
| 674 |
return |
| 675 |
} |
| 676 |
e.reflectValue(v.Elem(), opts) |
| 677 |
} |
| 678 |
|
| 679 |
func unsupportedTypeEncoder(e *encodeState, v reflect.Value, _ encOpts) { |
| 680 |
e.error(&UnsupportedTypeError{v.Type()}) |
| 681 |
} |
| 682 |
|
| 683 |
type structEncoder struct { |
| 684 |
fields []field |
| 685 |
fieldEncs []encoderFunc |
| 686 |
} |
| 687 |
|
| 688 |
func (se *structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { |
| 689 |
e.WriteByte('{') |
| 690 |
first := true |
| 691 |
for i, f := range se.fields { |
| 692 |
fv := fieldByIndex(v, f.index) |
| 693 |
if !fv.IsValid() || f.omitEmpty && isEmptyValue(fv) { |
| 694 |
continue |
| 695 |
} |
| 696 |
if first { |
| 697 |
first = false |
| 698 |
} else { |
| 699 |
e.WriteByte(',') |
| 700 |
} |
| 701 |
e.string(f.name, opts.escapeHTML) |
| 702 |
e.WriteByte(':') |
| 703 |
opts.quoted = f.quoted |
| 704 |
se.fieldEncs[i](e, fv, opts) |
| 705 |
} |
| 706 |
e.WriteByte('}') |
| 707 |
} |
| 708 |
|
| 709 |
func newStructEncoder(t reflect.Type) encoderFunc { |
| 710 |
fields := cachedTypeFields(t) |
| 711 |
se := &structEncoder{ |
| 712 |
fields: fields, |
| 713 |
fieldEncs: make([]encoderFunc, len(fields)), |
| 714 |
} |
| 715 |
for i, f := range fields { |
| 716 |
se.fieldEncs[i] = typeEncoder(typeByIndex(t, f.index)) |
| 717 |
} |
| 718 |
return se.encode |
| 719 |
} |
| 720 |
|
| 721 |
type mapEncoder struct { |
| 722 |
elemEnc encoderFunc |
| 723 |
} |
| 724 |
|
| 725 |
func (me *mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { |
| 726 |
if v.IsNil() { |
| 727 |
e.WriteString("null") |
| 728 |
return |
| 729 |
} |
| 730 |
e.WriteByte('{') |
| 731 |
|
| 732 |
|
| 733 |
keys := v.MapKeys() |
| 734 |
sv := make([]reflectWithString, len(keys)) |
| 735 |
for i, v := range keys { |
| 736 |
sv[i].v = v |
| 737 |
if err := sv[i].resolve(); err != nil { |
| 738 |
e.error(&MarshalerError{v.Type(), err}) |
| 739 |
} |
| 740 |
} |
| 741 |
sort.Slice(sv, func(i, j int) bool { return sv[i].s < sv[j].s }) |
| 742 |
|
| 743 |
for i, kv := range sv { |
| 744 |
if i > 0 { |
| 745 |
e.WriteByte(',') |
| 746 |
} |
| 747 |
e.string(kv.s, opts.escapeHTML) |
| 748 |
e.WriteByte(':') |
| 749 |
me.elemEnc(e, v.MapIndex(kv.v), opts) |
| 750 |
} |
| 751 |
e.WriteByte('}') |
| 752 |
} |
| 753 |
|
| 754 |
func newMapEncoder(t reflect.Type) encoderFunc { |
| 755 |
switch t.Key().Kind() { |
| 756 |
case reflect.String, |
| 757 |
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, |
| 758 |
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 759 |
default: |
| 760 |
if !t.Key().Implements(textMarshalerType) { |
| 761 |
return unsupportedTypeEncoder |
| 762 |
} |
| 763 |
} |
| 764 |
me := &mapEncoder{typeEncoder(t.Elem())} |
| 765 |
return me.encode |
| 766 |
} |
| 767 |
|
| 768 |
func encodeByteSlice(e *encodeState, v reflect.Value, _ encOpts) { |
| 769 |
if v.IsNil() { |
| 770 |
e.WriteString("null") |
| 771 |
return |
| 772 |
} |
| 773 |
s := v.Bytes() |
| 774 |
e.WriteByte('"') |
| 775 |
if len(s) < 1024 { |
| 776 |
|
| 777 |
dst := make([]byte, base64.StdEncoding.EncodedLen(len(s))) |
| 778 |
base64.StdEncoding.Encode(dst, s) |
| 779 |
e.Write(dst) |
| 780 |
} else { |
| 781 |
|
| 782 |
|
| 783 |
enc := base64.NewEncoder(base64.StdEncoding, e) |
| 784 |
enc.Write(s) |
| 785 |
enc.Close() |
| 786 |
} |
| 787 |
e.WriteByte('"') |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
type sliceEncoder struct { |
| 792 |
arrayEnc encoderFunc |
| 793 |
} |
| 794 |
|
| 795 |
func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { |
| 796 |
if v.IsNil() { |
| 797 |
e.WriteString("null") |
| 798 |
return |
| 799 |
} |
| 800 |
se.arrayEnc(e, v, opts) |
| 801 |
} |
| 802 |
|
| 803 |
func newSliceEncoder(t reflect.Type) encoderFunc { |
| 804 |
|
| 805 |
if t.Elem().Kind() == reflect.Uint8 { |
| 806 |
p := reflect.PtrTo(t.Elem()) |
| 807 |
if !p.Implements(marshalerType) && !p.Implements(textMarshalerType) { |
| 808 |
return encodeByteSlice |
| 809 |
} |
| 810 |
} |
| 811 |
enc := &sliceEncoder{newArrayEncoder(t)} |
| 812 |
return enc.encode |
| 813 |
} |
| 814 |
|
| 815 |
type arrayEncoder struct { |
| 816 |
elemEnc encoderFunc |
| 817 |
} |
| 818 |
|
| 819 |
func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { |
| 820 |
n := v.Len() |
| 821 |
if n == 1 { |
| 822 |
ae.elemEnc(e, v.Index(0), opts) |
| 823 |
return |
| 824 |
} |
| 825 |
e.WriteByte('[') |
| 826 |
for i := 0; i < n; i++ { |
| 827 |
if i > 0 { |
| 828 |
e.WriteByte(',') |
| 829 |
} |
| 830 |
ae.elemEnc(e, v.Index(i), opts) |
| 831 |
} |
| 832 |
e.WriteByte(']') |
| 833 |
} |
| 834 |
|
| 835 |
func newArrayEncoder(t reflect.Type) encoderFunc { |
| 836 |
enc := &arrayEncoder{typeEncoder(t.Elem())} |
| 837 |
return enc.encode |
| 838 |
} |
| 839 |
|
| 840 |
type ptrEncoder struct { |
| 841 |
elemEnc encoderFunc |
| 842 |
} |
| 843 |
|
| 844 |
func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { |
| 845 |
if v.IsNil() { |
| 846 |
e.WriteString("null") |
| 847 |
return |
| 848 |
} |
| 849 |
pe.elemEnc(e, v.Elem(), opts) |
| 850 |
} |
| 851 |
|
| 852 |
func newPtrEncoder(t reflect.Type) encoderFunc { |
| 853 |
enc := &ptrEncoder{typeEncoder(t.Elem())} |
| 854 |
return enc.encode |
| 855 |
} |
| 856 |
|
| 857 |
type condAddrEncoder struct { |
| 858 |
canAddrEnc, elseEnc encoderFunc |
| 859 |
} |
| 860 |
|
| 861 |
func (ce *condAddrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { |
| 862 |
if v.CanAddr() { |
| 863 |
ce.canAddrEnc(e, v, opts) |
| 864 |
} else { |
| 865 |
ce.elseEnc(e, v, opts) |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc { |
| 872 |
enc := &condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc} |
| 873 |
return enc.encode |
| 874 |
} |
| 875 |
|
| 876 |
func isValidTag(s string) bool { |
| 877 |
if s == "" { |
| 878 |
return false |
| 879 |
} |
| 880 |
for _, c := range s { |
| 881 |
switch { |
| 882 |
case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c): |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
default: |
| 887 |
if !unicode.IsLetter(c) && !unicode.IsDigit(c) { |
| 888 |
return false |
| 889 |
} |
| 890 |
} |
| 891 |
} |
| 892 |
return true |
| 893 |
} |
| 894 |
|
| 895 |
func fieldByIndex(v reflect.Value, index []int) reflect.Value { |
| 896 |
for _, i := range index { |
| 897 |
if v.Kind() == reflect.Ptr { |
| 898 |
if v.IsNil() { |
| 899 |
return reflect.Value{} |
| 900 |
} |
| 901 |
v = v.Elem() |
| 902 |
} |
| 903 |
v = v.Field(i) |
| 904 |
} |
| 905 |
return v |
| 906 |
} |
| 907 |
|
| 908 |
func typeByIndex(t reflect.Type, index []int) reflect.Type { |
| 909 |
for _, i := range index { |
| 910 |
if t.Kind() == reflect.Ptr { |
| 911 |
t = t.Elem() |
| 912 |
} |
| 913 |
t = t.Field(i).Type |
| 914 |
} |
| 915 |
return t |
| 916 |
} |
| 917 |
|
| 918 |
type reflectWithString struct { |
| 919 |
v reflect.Value |
| 920 |
s string |
| 921 |
} |
| 922 |
|
| 923 |
func (w *reflectWithString) resolve() error { |
| 924 |
if w.v.Kind() == reflect.String { |
| 925 |
w.s = w.v.String() |
| 926 |
return nil |
| 927 |
} |
| 928 |
if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok { |
| 929 |
buf, err := tm.MarshalText() |
| 930 |
w.s = string(buf) |
| 931 |
return err |
| 932 |
} |
| 933 |
switch w.v.Kind() { |
| 934 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 935 |
w.s = strconv.FormatInt(w.v.Int(), 10) |
| 936 |
return nil |
| 937 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 938 |
w.s = strconv.FormatUint(w.v.Uint(), 10) |
| 939 |
return nil |
| 940 |
} |
| 941 |
panic("unexpected map key type") |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
func (e *encodeState) string(s string, escapeHTML bool) int { |
| 946 |
len0 := e.Len() |
| 947 |
e.WriteByte('"') |
| 948 |
start := 0 |
| 949 |
for i := 0; i < len(s); { |
| 950 |
if b := s[i]; b < utf8.RuneSelf { |
| 951 |
if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) { |
| 952 |
i++ |
| 953 |
continue |
| 954 |
} |
| 955 |
if start < i { |
| 956 |
e.WriteString(s[start:i]) |
| 957 |
} |
| 958 |
switch b { |
| 959 |
case '\\', '"': |
| 960 |
e.WriteByte('\\') |
| 961 |
e.WriteByte(b) |
| 962 |
case '\n': |
| 963 |
e.WriteByte('\\') |
| 964 |
e.WriteByte('n') |
| 965 |
case '\r': |
| 966 |
e.WriteByte('\\') |
| 967 |
e.WriteByte('r') |
| 968 |
case '\t': |
| 969 |
e.WriteByte('\\') |
| 970 |
e.WriteByte('t') |
| 971 |
default: |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
e.WriteString(`\u00`) |
| 978 |
e.WriteByte(hex[b>>4]) |
| 979 |
e.WriteByte(hex[b&0xF]) |
| 980 |
} |
| 981 |
i++ |
| 982 |
start = i |
| 983 |
continue |
| 984 |
} |
| 985 |
c, size := utf8.DecodeRuneInString(s[i:]) |
| 986 |
if c == utf8.RuneError && size == 1 { |
| 987 |
if start < i { |
| 988 |
e.WriteString(s[start:i]) |
| 989 |
} |
| 990 |
e.WriteString(`\ufffd`) |
| 991 |
i += size |
| 992 |
start = i |
| 993 |
continue |
| 994 |
} |
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
if c == '\u2028' || c == '\u2029' { |
| 1003 |
if start < i { |
| 1004 |
e.WriteString(s[start:i]) |
| 1005 |
} |
| 1006 |
e.WriteString(`\u202`) |
| 1007 |
e.WriteByte(hex[c&0xF]) |
| 1008 |
i += size |
| 1009 |
start = i |
| 1010 |
continue |
| 1011 |
} |
| 1012 |
i += size |
| 1013 |
} |
| 1014 |
if start < len(s) { |
| 1015 |
e.WriteString(s[start:]) |
| 1016 |
} |
| 1017 |
e.WriteByte('"') |
| 1018 |
return e.Len() - len0 |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
func (e *encodeState) stringBytes(s []byte, escapeHTML bool) int { |
| 1023 |
len0 := e.Len() |
| 1024 |
e.WriteByte('"') |
| 1025 |
start := 0 |
| 1026 |
for i := 0; i < len(s); { |
| 1027 |
if b := s[i]; b < utf8.RuneSelf { |
| 1028 |
if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) { |
| 1029 |
i++ |
| 1030 |
continue |
| 1031 |
} |
| 1032 |
if start < i { |
| 1033 |
e.Write(s[start:i]) |
| 1034 |
} |
| 1035 |
switch b { |
| 1036 |
case '\\', '"': |
| 1037 |
e.WriteByte('\\') |
| 1038 |
e.WriteByte(b) |
| 1039 |
case '\n': |
| 1040 |
e.WriteByte('\\') |
| 1041 |
e.WriteByte('n') |
| 1042 |
case '\r': |
| 1043 |
e.WriteByte('\\') |
| 1044 |
e.WriteByte('r') |
| 1045 |
case '\t': |
| 1046 |
e.WriteByte('\\') |
| 1047 |
e.WriteByte('t') |
| 1048 |
default: |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
e.WriteString(`\u00`) |
| 1055 |
e.WriteByte(hex[b>>4]) |
| 1056 |
e.WriteByte(hex[b&0xF]) |
| 1057 |
} |
| 1058 |
i++ |
| 1059 |
start = i |
| 1060 |
continue |
| 1061 |
} |
| 1062 |
c, size := utf8.DecodeRune(s[i:]) |
| 1063 |
if c == utf8.RuneError && size == 1 { |
| 1064 |
if start < i { |
| 1065 |
e.Write(s[start:i]) |
| 1066 |
} |
| 1067 |
e.WriteString(`\ufffd`) |
| 1068 |
i += size |
| 1069 |
start = i |
| 1070 |
continue |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
if c == '\u2028' || c == '\u2029' { |
| 1080 |
if start < i { |
| 1081 |
e.Write(s[start:i]) |
| 1082 |
} |
| 1083 |
e.WriteString(`\u202`) |
| 1084 |
e.WriteByte(hex[c&0xF]) |
| 1085 |
i += size |
| 1086 |
start = i |
| 1087 |
continue |
| 1088 |
} |
| 1089 |
i += size |
| 1090 |
} |
| 1091 |
if start < len(s) { |
| 1092 |
e.Write(s[start:]) |
| 1093 |
} |
| 1094 |
e.WriteByte('"') |
| 1095 |
return e.Len() - len0 |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
type field struct { |
| 1100 |
name string |
| 1101 |
nameBytes []byte |
| 1102 |
equalFold func(s, t []byte) bool |
| 1103 |
|
| 1104 |
tag bool |
| 1105 |
index []int |
| 1106 |
typ reflect.Type |
| 1107 |
omitEmpty bool |
| 1108 |
collapsible bool |
| 1109 |
quoted bool |
| 1110 |
} |
| 1111 |
|
| 1112 |
func fillField(f field) field { |
| 1113 |
f.nameBytes = []byte(f.name) |
| 1114 |
f.equalFold = foldFunc(f.nameBytes) |
| 1115 |
return f |
| 1116 |
} |
| 1117 |
|
| 1118 |
|
| 1119 |
type byIndex []field |
| 1120 |
|
| 1121 |
func (x byIndex) Len() int { return len(x) } |
| 1122 |
|
| 1123 |
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] } |
| 1124 |
|
| 1125 |
func (x byIndex) Less(i, j int) bool { |
| 1126 |
for k, xik := range x[i].index { |
| 1127 |
if k >= len(x[j].index) { |
| 1128 |
return false |
| 1129 |
} |
| 1130 |
if xik != x[j].index[k] { |
| 1131 |
return xik < x[j].index[k] |
| 1132 |
} |
| 1133 |
} |
| 1134 |
return len(x[i].index) < len(x[j].index) |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
func typeFields(t reflect.Type) []field { |
| 1141 |
|
| 1142 |
current := []field{} |
| 1143 |
next := []field{{typ: t}} |
| 1144 |
|
| 1145 |
|
| 1146 |
count := map[reflect.Type]int{} |
| 1147 |
nextCount := map[reflect.Type]int{} |
| 1148 |
|
| 1149 |
|
| 1150 |
visited := map[reflect.Type]bool{} |
| 1151 |
|
| 1152 |
|
| 1153 |
var fields []field |
| 1154 |
|
| 1155 |
for len(next) > 0 { |
| 1156 |
current, next = next, current[:0] |
| 1157 |
count, nextCount = nextCount, map[reflect.Type]int{} |
| 1158 |
|
| 1159 |
for _, f := range current { |
| 1160 |
if visited[f.typ] { |
| 1161 |
continue |
| 1162 |
} |
| 1163 |
visited[f.typ] = true |
| 1164 |
|
| 1165 |
|
| 1166 |
for i := 0; i < f.typ.NumField(); i++ { |
| 1167 |
sf := f.typ.Field(i) |
| 1168 |
if sf.Anonymous { |
| 1169 |
t := sf.Type |
| 1170 |
if t.Kind() == reflect.Ptr { |
| 1171 |
t = t.Elem() |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
if !isExported(t.Name()) && t.Kind() != reflect.Struct { |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
continue |
| 1181 |
} |
| 1182 |
} else if sf.PkgPath != "" { |
| 1183 |
|
| 1184 |
continue |
| 1185 |
} |
| 1186 |
tag := sf.Tag.Get(tagLabel) |
| 1187 |
if tag == "-" { |
| 1188 |
continue |
| 1189 |
} |
| 1190 |
if myTag, ok := LoadTag(sf.Tag); ok { |
| 1191 |
if myTag.NoMarshal { |
| 1192 |
continue |
| 1193 |
} |
| 1194 |
} |
| 1195 |
name, opts := parseTag(tag) |
| 1196 |
if !isValidTag(name) { |
| 1197 |
name = "" |
| 1198 |
} |
| 1199 |
index := make([]int, len(f.index)+1) |
| 1200 |
copy(index, f.index) |
| 1201 |
index[len(f.index)] = i |
| 1202 |
|
| 1203 |
ft := sf.Type |
| 1204 |
if ft.Name() == "" && ft.Kind() == reflect.Ptr { |
| 1205 |
|
| 1206 |
ft = ft.Elem() |
| 1207 |
} |
| 1208 |
|
| 1209 |
|
| 1210 |
quoted := false |
| 1211 |
if opts.Contains("string") { |
| 1212 |
switch ft.Kind() { |
| 1213 |
case reflect.Bool, |
| 1214 |
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, |
| 1215 |
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, |
| 1216 |
reflect.Float32, reflect.Float64, |
| 1217 |
reflect.String: |
| 1218 |
quoted = true |
| 1219 |
} |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct { |
| 1224 |
tagged := name != "" |
| 1225 |
if name == "" { |
| 1226 |
name = sf.Name |
| 1227 |
} |
| 1228 |
fields = append(fields, fillField(field{ |
| 1229 |
name: name, |
| 1230 |
tag: tagged, |
| 1231 |
index: index, |
| 1232 |
typ: ft, |
| 1233 |
omitEmpty: opts.Contains(tagOmitEmpty), |
| 1234 |
collapsible: opts.Contains(tagCollapsible), |
| 1235 |
quoted: quoted, |
| 1236 |
})) |
| 1237 |
if count[f.typ] > 1 { |
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
fields = append(fields, fields[len(fields)-1]) |
| 1243 |
} |
| 1244 |
continue |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
nextCount[ft]++ |
| 1249 |
if nextCount[ft] == 1 { |
| 1250 |
next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft})) |
| 1251 |
} |
| 1252 |
} |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
sort.Slice(fields, func(i, j int) bool { |
| 1257 |
x := fields |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
if x[i].name != x[j].name { |
| 1262 |
return x[i].name < x[j].name |
| 1263 |
} |
| 1264 |
if len(x[i].index) != len(x[j].index) { |
| 1265 |
return len(x[i].index) < len(x[j].index) |
| 1266 |
} |
| 1267 |
if x[i].tag != x[j].tag { |
| 1268 |
return x[i].tag |
| 1269 |
} |
| 1270 |
return byIndex(x).Less(i, j) |
| 1271 |
}) |
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
out := fields[:0] |
| 1280 |
for advance, i := 0, 0; i < len(fields); i += advance { |
| 1281 |
|
| 1282 |
|
| 1283 |
fi := fields[i] |
| 1284 |
name := fi.name |
| 1285 |
for advance = 1; i+advance < len(fields); advance++ { |
| 1286 |
fj := fields[i+advance] |
| 1287 |
if fj.name != name { |
| 1288 |
break |
| 1289 |
} |
| 1290 |
} |
| 1291 |
if advance == 1 { |
| 1292 |
out = append(out, fi) |
| 1293 |
continue |
| 1294 |
} |
| 1295 |
dominant, ok := dominantField(fields[i : i+advance]) |
| 1296 |
if ok { |
| 1297 |
out = append(out, dominant) |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
fields = out |
| 1302 |
sort.Sort(byIndex(fields)) |
| 1303 |
|
| 1304 |
return fields |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
func isExported(id string) bool { |
| 1309 |
r, _ := utf8.DecodeRuneInString(id) |
| 1310 |
return unicode.IsUpper(r) |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
func dominantField(fields []field) (field, bool) { |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
length := len(fields[0].index) |
| 1324 |
tagged := -1 |
| 1325 |
for i, f := range fields { |
| 1326 |
if len(f.index) > length { |
| 1327 |
fields = fields[:i] |
| 1328 |
break |
| 1329 |
} |
| 1330 |
if f.tag { |
| 1331 |
if tagged >= 0 { |
| 1332 |
|
| 1333 |
|
| 1334 |
return field{}, false |
| 1335 |
} |
| 1336 |
tagged = i |
| 1337 |
} |
| 1338 |
} |
| 1339 |
if tagged >= 0 { |
| 1340 |
return fields[tagged], true |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
if len(fields) > 1 { |
| 1346 |
return field{}, false |
| 1347 |
} |
| 1348 |
return fields[0], true |
| 1349 |
} |
| 1350 |
|
| 1351 |
var fieldCache struct { |
| 1352 |
value atomic.Value |
| 1353 |
mu sync.Mutex |
| 1354 |
} |
| 1355 |
|
| 1356 |
|
| 1357 |
func cachedTypeFields(t reflect.Type) []field { |
| 1358 |
m, _ := fieldCache.value.Load().(map[reflect.Type][]field) |
| 1359 |
f := m[t] |
| 1360 |
if f != nil { |
| 1361 |
return f |
| 1362 |
} |
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
f = typeFields(t) |
| 1367 |
if f == nil { |
| 1368 |
f = []field{} |
| 1369 |
} |
| 1370 |
|
| 1371 |
fieldCache.mu.Lock() |
| 1372 |
m, _ = fieldCache.value.Load().(map[reflect.Type][]field) |
| 1373 |
newM := make(map[reflect.Type][]field, len(m)+1) |
| 1374 |
for k, v := range m { |
| 1375 |
newM[k] = v |
| 1376 |
} |
| 1377 |
newM[t] = f |
| 1378 |
fieldCache.value.Store(newM) |
| 1379 |
fieldCache.mu.Unlock() |
| 1380 |
return f |
| 1381 |
} |