| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package codec |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"encoding/base64" |
| 12 |
"errors" |
| 13 |
"fmt" |
| 14 |
"go/format" |
| 15 |
"io" |
| 16 |
"io/ioutil" |
| 17 |
"math/rand" |
| 18 |
"os" |
| 19 |
"reflect" |
| 20 |
"regexp" |
| 21 |
"sort" |
| 22 |
"strconv" |
| 23 |
"strings" |
| 24 |
"sync" |
| 25 |
"text/template" |
| 26 |
"time" |
| 27 |
|
| 28 |
"unicode" |
| 29 |
"unicode/utf8" |
| 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 |
const genVersion = 25 |
| 157 |
|
| 158 |
const ( |
| 159 |
genCodecPkg = "codec1978" |
| 160 |
genTempVarPfx = "yy" |
| 161 |
genTopLevelVarName = "x" |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
genAnythingCanBeNil = true |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
genUseOneFunctionForDecStructMap = true |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
genStructCanonical = true |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
genFastpathCanonical = true |
| 183 |
|
| 184 |
|
| 185 |
genFastpathTrimTypes = true |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
genDecStructArrayInlineLoopCheck = true |
| 192 |
) |
| 193 |
|
| 194 |
type genStructMapStyle uint8 |
| 195 |
type genStringDecAsBytes string |
| 196 |
type genStringDecZC string |
| 197 |
|
| 198 |
var genStringDecAsBytesTyp = reflect.TypeOf(genStringDecAsBytes("")) |
| 199 |
var genStringDecZCTyp = reflect.TypeOf(genStringDecZC("")) |
| 200 |
var genFormats = []string{"Json", "Cbor", "Msgpack", "Binc", "Simple"} |
| 201 |
|
| 202 |
const ( |
| 203 |
genStructMapStyleConsolidated genStructMapStyle = iota |
| 204 |
genStructMapStyleLenPrefix |
| 205 |
genStructMapStyleCheckBreak |
| 206 |
) |
| 207 |
|
| 208 |
var ( |
| 209 |
errGenAllTypesSamePkg = errors.New("All types must be in the same package") |
| 210 |
errGenExpectArrayOrMap = errors.New("unexpected type - expecting array/map/slice") |
| 211 |
errGenUnexpectedTypeFastpath = errors.New("fast-path: unexpected type - requires map or slice") |
| 212 |
|
| 213 |
genBase64enc = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789__") |
| 214 |
genQNameRegex = regexp.MustCompile(`[A-Za-z_.]+`) |
| 215 |
) |
| 216 |
|
| 217 |
type genBuf struct { |
| 218 |
buf []byte |
| 219 |
} |
| 220 |
|
| 221 |
func (x *genBuf) sIf(b bool, s, t string) *genBuf { |
| 222 |
if b { |
| 223 |
x.buf = append(x.buf, s...) |
| 224 |
} else { |
| 225 |
x.buf = append(x.buf, t...) |
| 226 |
} |
| 227 |
return x |
| 228 |
} |
| 229 |
func (x *genBuf) s(s string) *genBuf { x.buf = append(x.buf, s...); return x } |
| 230 |
func (x *genBuf) b(s []byte) *genBuf { x.buf = append(x.buf, s...); return x } |
| 231 |
func (x *genBuf) v() string { return string(x.buf) } |
| 232 |
func (x *genBuf) f(s string, args ...interface{}) { x.s(fmt.Sprintf(s, args...)) } |
| 233 |
func (x *genBuf) reset() { |
| 234 |
if x.buf != nil { |
| 235 |
x.buf = x.buf[:0] |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
type genRunner struct { |
| 241 |
w io.Writer |
| 242 |
c uint64 |
| 243 |
f uint64 |
| 244 |
|
| 245 |
t []reflect.Type |
| 246 |
tc reflect.Type |
| 247 |
te map[uintptr]bool |
| 248 |
td map[uintptr]bool |
| 249 |
tz map[uintptr]bool |
| 250 |
|
| 251 |
cp string |
| 252 |
|
| 253 |
im map[string]reflect.Type |
| 254 |
imn map[string]string |
| 255 |
imc uint64 |
| 256 |
|
| 257 |
is map[reflect.Type]struct{} |
| 258 |
bp string |
| 259 |
|
| 260 |
cpfx string |
| 261 |
|
| 262 |
ty map[reflect.Type]struct{} |
| 263 |
tm map[reflect.Type]struct{} |
| 264 |
ts []reflect.Type |
| 265 |
|
| 266 |
xs string |
| 267 |
hn string |
| 268 |
|
| 269 |
ti *TypeInfos |
| 270 |
|
| 271 |
|
| 272 |
jsonOnlyWhen, toArrayWhen, omitEmptyWhen *bool |
| 273 |
|
| 274 |
nx bool |
| 275 |
} |
| 276 |
|
| 277 |
type genIfClause struct { |
| 278 |
hasIf bool |
| 279 |
} |
| 280 |
|
| 281 |
func (g *genIfClause) end(x *genRunner) { |
| 282 |
if g.hasIf { |
| 283 |
x.line("}") |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
func (g *genIfClause) c(last bool) (v string) { |
| 288 |
if last { |
| 289 |
if g.hasIf { |
| 290 |
v = " } else { " |
| 291 |
} |
| 292 |
} else if g.hasIf { |
| 293 |
v = " } else if " |
| 294 |
} else { |
| 295 |
v = "if " |
| 296 |
g.hasIf = true |
| 297 |
} |
| 298 |
return |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
func Gen(w io.Writer, buildTags, pkgName, uid string, noExtensions bool, |
| 306 |
jsonOnlyWhen, toArrayWhen, omitEmptyWhen *bool, |
| 307 |
ti *TypeInfos, types ...reflect.Type) (warnings []string) { |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
if len(types) == 0 { |
| 313 |
return |
| 314 |
} |
| 315 |
x := genRunner{ |
| 316 |
w: w, |
| 317 |
t: types, |
| 318 |
te: make(map[uintptr]bool), |
| 319 |
td: make(map[uintptr]bool), |
| 320 |
tz: make(map[uintptr]bool), |
| 321 |
im: make(map[string]reflect.Type), |
| 322 |
imn: make(map[string]string), |
| 323 |
is: make(map[reflect.Type]struct{}), |
| 324 |
tm: make(map[reflect.Type]struct{}), |
| 325 |
ty: make(map[reflect.Type]struct{}), |
| 326 |
ts: []reflect.Type{}, |
| 327 |
bp: genImportPath(types[0]), |
| 328 |
xs: uid, |
| 329 |
ti: ti, |
| 330 |
jsonOnlyWhen: jsonOnlyWhen, |
| 331 |
toArrayWhen: toArrayWhen, |
| 332 |
omitEmptyWhen: omitEmptyWhen, |
| 333 |
|
| 334 |
nx: noExtensions, |
| 335 |
} |
| 336 |
if x.ti == nil { |
| 337 |
x.ti = defTypeInfos |
| 338 |
} |
| 339 |
if x.xs == "" { |
| 340 |
rr := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 341 |
x.xs = strconv.FormatInt(rr.Int63n(9999), 10) |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
x.cp = genImportPath(reflect.TypeOf(x)) |
| 346 |
x.imn[x.cp] = genCodecPkg |
| 347 |
|
| 348 |
|
| 349 |
for i := 0; i < len(x.t); { |
| 350 |
t := x.t[i] |
| 351 |
|
| 352 |
if genImportPath(t) != x.bp { |
| 353 |
halt.onerror(errGenAllTypesSamePkg) |
| 354 |
} |
| 355 |
ti1 := x.ti.get(rt2id(t), t) |
| 356 |
if ti1.flagMissingFielder || ti1.flagMissingFielderPtr { |
| 357 |
|
| 358 |
warnings = append(warnings, fmt.Sprintf("type: '%v' not generated; implements codec.MissingFielder", t)) |
| 359 |
copy(x.t[i:], x.t[i+1:]) |
| 360 |
x.t = x.t[:len(x.t)-1] |
| 361 |
continue |
| 362 |
} |
| 363 |
x.genRefPkgs(t) |
| 364 |
i++ |
| 365 |
} |
| 366 |
|
| 367 |
x.line("// +build go1.6") |
| 368 |
if buildTags != "" { |
| 369 |
x.line("// +build " + buildTags) |
| 370 |
} |
| 371 |
x.line(` |
| 372 |
|
| 373 |
// Code generated by codecgen - DO NOT EDIT. |
| 374 |
|
| 375 |
`) |
| 376 |
x.line("package " + pkgName) |
| 377 |
x.line("") |
| 378 |
x.line("import (") |
| 379 |
if x.cp != x.bp { |
| 380 |
x.cpfx = genCodecPkg + "." |
| 381 |
x.linef("%s \"%s\"", genCodecPkg, x.cp) |
| 382 |
} |
| 383 |
|
| 384 |
imKeys := make([]string, 0, len(x.im)) |
| 385 |
for k := range x.im { |
| 386 |
imKeys = append(imKeys, k) |
| 387 |
} |
| 388 |
sort.Strings(imKeys) |
| 389 |
for _, k := range imKeys { |
| 390 |
if k == x.imn[k] { |
| 391 |
x.linef("\"%s\"", k) |
| 392 |
} else { |
| 393 |
x.linef("%s \"%s\"", x.imn[k], k) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
for _, k := range [...]string{"runtime", "errors", "strconv", "sort"} { |
| 398 |
if _, ok := x.im[k]; !ok { |
| 399 |
x.line("\"" + k + "\"") |
| 400 |
} |
| 401 |
} |
| 402 |
x.line(")") |
| 403 |
x.line("") |
| 404 |
|
| 405 |
x.line("const (") |
| 406 |
x.linef("// ----- content types ----") |
| 407 |
x.linef("codecSelferCcUTF8%s = %v", x.xs, int64(cUTF8)) |
| 408 |
x.linef("codecSelferCcRAW%s = %v", x.xs, int64(cRAW)) |
| 409 |
x.linef("// ----- value types used ----") |
| 410 |
for _, vt := range [...]valueType{ |
| 411 |
valueTypeArray, valueTypeMap, valueTypeString, |
| 412 |
valueTypeInt, valueTypeUint, valueTypeFloat, |
| 413 |
valueTypeNil, |
| 414 |
} { |
| 415 |
x.linef("codecSelferValueType%s%s = %v", vt.String(), x.xs, int64(vt)) |
| 416 |
} |
| 417 |
|
| 418 |
x.linef("codecSelferBitsize%s = uint8(32 << (^uint(0) >> 63))", x.xs) |
| 419 |
x.linef("codecSelferDecContainerLenNil%s = %d", x.xs, int64(containerLenNil)) |
| 420 |
x.line(")") |
| 421 |
x.line("var (") |
| 422 |
x.line("errCodecSelferOnlyMapOrArrayEncodeToStruct" + x.xs + " = " + "errors.New(`only encoded map or array can be decoded into a struct`)") |
| 423 |
x.line("_ sort.Interface = nil") |
| 424 |
x.line(")") |
| 425 |
x.line("") |
| 426 |
|
| 427 |
x.hn = "codecSelfer" + x.xs |
| 428 |
x.line("type " + x.hn + " struct{}") |
| 429 |
x.line("") |
| 430 |
x.linef("func %sFalse() bool { return false }", x.hn) |
| 431 |
x.linef("func %sTrue() bool { return true }", x.hn) |
| 432 |
x.line("") |
| 433 |
|
| 434 |
|
| 435 |
for _, s := range []string{"string", "uint64", "int64", "float64"} { |
| 436 |
x.linef("type %s%sSlice []%s", x.hn, s, s) |
| 437 |
x.linef("func (p %s%sSlice) Len() int { return len(p) }", x.hn, s) |
| 438 |
x.linef("func (p %s%sSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] }", x.hn, s) |
| 439 |
x.linef("func (p %s%sSlice) Less(i, j int) bool { return p[uint(i)] < p[uint(j)] }", x.hn, s) |
| 440 |
} |
| 441 |
|
| 442 |
x.line("") |
| 443 |
x.varsfxreset() |
| 444 |
x.line("func init() {") |
| 445 |
x.linef("if %sGenVersion != %v {", x.cpfx, genVersion) |
| 446 |
x.line("_, file, _, _ := runtime.Caller(0)") |
| 447 |
x.linef("ver := strconv.FormatInt(int64(%sGenVersion), 10)", x.cpfx) |
| 448 |
x.outf(`panic(errors.New("codecgen version mismatch: current: %v, need " + ver + ". Re-generate file: " + file))`, genVersion) |
| 449 |
x.linef("}") |
| 450 |
if len(imKeys) > 0 { |
| 451 |
x.line("if false { // reference the types, but skip this branch at build/run time") |
| 452 |
for _, k := range imKeys { |
| 453 |
t := x.im[k] |
| 454 |
x.linef("var _ %s.%s", x.imn[k], t.Name()) |
| 455 |
} |
| 456 |
x.line("} ") |
| 457 |
} |
| 458 |
x.line("}") |
| 459 |
x.line("") |
| 460 |
|
| 461 |
|
| 462 |
for _, t := range x.t { |
| 463 |
x.tc = t |
| 464 |
x.linef("func (%s) codecSelferViaCodecgen() {}", x.genTypeName(t)) |
| 465 |
x.selfer(true) |
| 466 |
x.selfer(false) |
| 467 |
x.tryGenIsZero(t) |
| 468 |
} |
| 469 |
|
| 470 |
for _, t := range x.ts { |
| 471 |
rtid := rt2id(t) |
| 472 |
|
| 473 |
x.varsfxreset() |
| 474 |
x.linef("func (x %s) enc%s(v %s%s, e *%sEncoder) {", x.hn, x.genMethodNameT(t), x.arr2str(t, "*"), x.genTypeName(t), x.cpfx) |
| 475 |
x.genRequiredMethodVars(true) |
| 476 |
switch t.Kind() { |
| 477 |
case reflect.Array, reflect.Slice, reflect.Chan: |
| 478 |
x.encListFallback("v", t) |
| 479 |
case reflect.Map: |
| 480 |
x.encMapFallback("v", t) |
| 481 |
default: |
| 482 |
halt.onerror(errGenExpectArrayOrMap) |
| 483 |
} |
| 484 |
x.line("}") |
| 485 |
x.line("") |
| 486 |
|
| 487 |
|
| 488 |
x.varsfxreset() |
| 489 |
x.linef("func (x %s) dec%s(v *%s, d *%sDecoder) {", x.hn, x.genMethodNameT(t), x.genTypeName(t), x.cpfx) |
| 490 |
x.genRequiredMethodVars(false) |
| 491 |
switch t.Kind() { |
| 492 |
case reflect.Array, reflect.Slice, reflect.Chan: |
| 493 |
x.decListFallback("v", rtid, t) |
| 494 |
case reflect.Map: |
| 495 |
x.decMapFallback("v", rtid, t) |
| 496 |
default: |
| 497 |
halt.onerror(errGenExpectArrayOrMap) |
| 498 |
} |
| 499 |
x.line("}") |
| 500 |
x.line("") |
| 501 |
} |
| 502 |
|
| 503 |
for t := range x.ty { |
| 504 |
x.tryGenIsZero(t) |
| 505 |
x.line("") |
| 506 |
} |
| 507 |
|
| 508 |
x.line("") |
| 509 |
return |
| 510 |
} |
| 511 |
|
| 512 |
func (x *genRunner) checkForSelfer(t reflect.Type, varname string) bool { |
| 513 |
|
| 514 |
|
| 515 |
return varname != genTopLevelVarName |
| 516 |
} |
| 517 |
|
| 518 |
func (x *genRunner) arr2str(t reflect.Type, s string) string { |
| 519 |
if t.Kind() == reflect.Array { |
| 520 |
return s |
| 521 |
} |
| 522 |
return "" |
| 523 |
} |
| 524 |
|
| 525 |
func (x *genRunner) genRequiredMethodVars(encode bool) { |
| 526 |
x.line("var h " + x.hn) |
| 527 |
if encode { |
| 528 |
x.line("z, r := " + x.cpfx + "GenHelper().Encoder(e)") |
| 529 |
} else { |
| 530 |
x.line("z, r := " + x.cpfx + "GenHelper().Decoder(d)") |
| 531 |
} |
| 532 |
x.line("_, _, _ = h, z, r") |
| 533 |
} |
| 534 |
|
| 535 |
func (x *genRunner) genRefPkgs(t reflect.Type) { |
| 536 |
if _, ok := x.is[t]; ok { |
| 537 |
return |
| 538 |
} |
| 539 |
x.is[t] = struct{}{} |
| 540 |
tpkg, tname := genImportPath(t), t.Name() |
| 541 |
if tpkg != "" && tpkg != x.bp && tpkg != x.cp && tname != "" && tname[0] >= 'A' && tname[0] <= 'Z' { |
| 542 |
if _, ok := x.im[tpkg]; !ok { |
| 543 |
x.im[tpkg] = t |
| 544 |
if idx := strings.LastIndex(tpkg, "/"); idx < 0 { |
| 545 |
x.imn[tpkg] = tpkg |
| 546 |
} else { |
| 547 |
x.imc++ |
| 548 |
x.imn[tpkg] = "pkg" + strconv.FormatUint(x.imc, 10) + "_" + genGoIdentifier(tpkg[idx+1:], false) |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
switch t.Kind() { |
| 553 |
case reflect.Array, reflect.Slice, reflect.Ptr, reflect.Chan: |
| 554 |
x.genRefPkgs(t.Elem()) |
| 555 |
case reflect.Map: |
| 556 |
x.genRefPkgs(t.Elem()) |
| 557 |
x.genRefPkgs(t.Key()) |
| 558 |
case reflect.Struct: |
| 559 |
for i := 0; i < t.NumField(); i++ { |
| 560 |
if fname := t.Field(i).Name; fname != "" && fname[0] >= 'A' && fname[0] <= 'Z' { |
| 561 |
x.genRefPkgs(t.Field(i).Type) |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
func (x *genRunner) sayFalse() string { |
| 569 |
x.f++ |
| 570 |
if x.f%2 == 0 { |
| 571 |
return x.hn + "False()" |
| 572 |
} |
| 573 |
return "false" |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
func (x *genRunner) sayTrue() string { |
| 578 |
x.f++ |
| 579 |
if x.f%2 == 0 { |
| 580 |
return x.hn + "True()" |
| 581 |
} |
| 582 |
return "true" |
| 583 |
} |
| 584 |
|
| 585 |
func (x *genRunner) varsfx() string { |
| 586 |
x.c++ |
| 587 |
return strconv.FormatUint(x.c, 10) |
| 588 |
} |
| 589 |
|
| 590 |
func (x *genRunner) varsfxreset() { |
| 591 |
x.c = 0 |
| 592 |
} |
| 593 |
|
| 594 |
func (x *genRunner) out(s string) { |
| 595 |
_, err := io.WriteString(x.w, s) |
| 596 |
genCheckErr(err) |
| 597 |
} |
| 598 |
|
| 599 |
func (x *genRunner) outf(s string, params ...interface{}) { |
| 600 |
_, err := fmt.Fprintf(x.w, s, params...) |
| 601 |
genCheckErr(err) |
| 602 |
} |
| 603 |
|
| 604 |
func (x *genRunner) line(s string) { |
| 605 |
x.out(s) |
| 606 |
if len(s) == 0 || s[len(s)-1] != '\n' { |
| 607 |
x.out("\n") |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
func (x *genRunner) lineIf(s string) { |
| 612 |
if s != "" { |
| 613 |
x.line(s) |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
func (x *genRunner) linef(s string, params ...interface{}) { |
| 618 |
x.outf(s, params...) |
| 619 |
if len(s) == 0 || s[len(s)-1] != '\n' { |
| 620 |
x.out("\n") |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
func (x *genRunner) genTypeName(t reflect.Type) (n string) { |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
var ptrPfx string |
| 631 |
for t.Kind() == reflect.Ptr { |
| 632 |
ptrPfx += "*" |
| 633 |
t = t.Elem() |
| 634 |
} |
| 635 |
if tn := t.Name(); tn != "" { |
| 636 |
return ptrPfx + x.genTypeNamePrim(t) |
| 637 |
} |
| 638 |
switch t.Kind() { |
| 639 |
case reflect.Map: |
| 640 |
return ptrPfx + "map[" + x.genTypeName(t.Key()) + "]" + x.genTypeName(t.Elem()) |
| 641 |
case reflect.Slice: |
| 642 |
return ptrPfx + "[]" + x.genTypeName(t.Elem()) |
| 643 |
case reflect.Array: |
| 644 |
return ptrPfx + "[" + strconv.FormatInt(int64(t.Len()), 10) + "]" + x.genTypeName(t.Elem()) |
| 645 |
case reflect.Chan: |
| 646 |
return ptrPfx + t.ChanDir().String() + " " + x.genTypeName(t.Elem()) |
| 647 |
default: |
| 648 |
if t == intfTyp { |
| 649 |
return ptrPfx + "interface{}" |
| 650 |
} else { |
| 651 |
return ptrPfx + x.genTypeNamePrim(t) |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
func (x *genRunner) genTypeNamePrim(t reflect.Type) (n string) { |
| 657 |
if t.Name() == "" { |
| 658 |
return t.String() |
| 659 |
} else if genImportPath(t) == "" || genImportPath(t) == genImportPath(x.tc) { |
| 660 |
return t.Name() |
| 661 |
} else { |
| 662 |
return x.imn[genImportPath(t)] + "." + t.Name() |
| 663 |
|
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
func (x *genRunner) genZeroValueR(t reflect.Type) string { |
| 668 |
|
| 669 |
switch t.Kind() { |
| 670 |
case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func, |
| 671 |
reflect.Slice, reflect.Map, reflect.Invalid: |
| 672 |
return "nil" |
| 673 |
case reflect.Bool: |
| 674 |
return "false" |
| 675 |
case reflect.String: |
| 676 |
return `""` |
| 677 |
case reflect.Struct, reflect.Array: |
| 678 |
return x.genTypeName(t) + "{}" |
| 679 |
default: |
| 680 |
return "0" |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
func (x *genRunner) genMethodNameT(t reflect.Type) (s string) { |
| 685 |
return genMethodNameT(t, x.tc) |
| 686 |
} |
| 687 |
|
| 688 |
func (x *genRunner) tryGenIsZero(t reflect.Type) (done bool) { |
| 689 |
if t.Kind() != reflect.Struct || t.Implements(isCodecEmptyerTyp) { |
| 690 |
return |
| 691 |
} |
| 692 |
|
| 693 |
rtid := rt2id(t) |
| 694 |
|
| 695 |
if _, ok := x.tz[rtid]; ok { |
| 696 |
delete(x.ty, t) |
| 697 |
return |
| 698 |
} |
| 699 |
|
| 700 |
x.tz[rtid] = true |
| 701 |
delete(x.ty, t) |
| 702 |
|
| 703 |
ti := x.ti.get(rtid, t) |
| 704 |
tisfi := ti.sfi.source() |
| 705 |
varname := genTopLevelVarName |
| 706 |
|
| 707 |
x.linef("func (%s *%s) IsCodecEmpty() bool {", varname, x.genTypeName(t)) |
| 708 |
|
| 709 |
anonSeen := make(map[reflect.Type]bool) |
| 710 |
var omitline genBuf |
| 711 |
for _, si := range tisfi { |
| 712 |
if si.path.parent != nil { |
| 713 |
root := si.path.root() |
| 714 |
if anonSeen[root.typ] { |
| 715 |
continue |
| 716 |
} |
| 717 |
anonSeen[root.typ] = true |
| 718 |
} |
| 719 |
t2 := genOmitEmptyLinePreChecks(varname, t, si, &omitline, true) |
| 720 |
|
| 721 |
if t2.Type.Kind() != reflect.Ptr { |
| 722 |
x.doEncOmitEmptyLine(t2, varname, &omitline) |
| 723 |
omitline.s(" || ") |
| 724 |
} |
| 725 |
} |
| 726 |
omitline.s(" false") |
| 727 |
x.linef("return !(%s)", omitline.v()) |
| 728 |
|
| 729 |
x.line("}") |
| 730 |
x.line("") |
| 731 |
return true |
| 732 |
} |
| 733 |
|
| 734 |
func (x *genRunner) selfer(encode bool) { |
| 735 |
t := x.tc |
| 736 |
|
| 737 |
t0 := t |
| 738 |
|
| 739 |
|
| 740 |
isptr := !encode || t.Kind() == reflect.Array || (t.Kind() == reflect.Struct && t != timeTyp) |
| 741 |
x.varsfxreset() |
| 742 |
|
| 743 |
fnSigPfx := "func (" + genTopLevelVarName + " " |
| 744 |
if isptr { |
| 745 |
fnSigPfx += "*" |
| 746 |
} |
| 747 |
fnSigPfx += x.genTypeName(t) |
| 748 |
x.out(fnSigPfx) |
| 749 |
|
| 750 |
if isptr { |
| 751 |
t = reflect.PtrTo(t) |
| 752 |
} |
| 753 |
if encode { |
| 754 |
x.line(") CodecEncodeSelf(e *" + x.cpfx + "Encoder) {") |
| 755 |
x.genRequiredMethodVars(true) |
| 756 |
if t0.Kind() == reflect.Struct { |
| 757 |
x.linef("if z.EncBasicHandle().CheckCircularRef { z.EncEncode(%s); return }", genTopLevelVarName) |
| 758 |
} |
| 759 |
x.encVar(genTopLevelVarName, t) |
| 760 |
} else { |
| 761 |
x.line(") CodecDecodeSelf(d *" + x.cpfx + "Decoder) {") |
| 762 |
x.genRequiredMethodVars(false) |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
x.dec(genTopLevelVarName, t0, true) |
| 768 |
} |
| 769 |
x.line("}") |
| 770 |
x.line("") |
| 771 |
|
| 772 |
if encode || t0.Kind() != reflect.Struct { |
| 773 |
return |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
if genUseOneFunctionForDecStructMap { |
| 778 |
x.out(fnSigPfx) |
| 779 |
x.line(") codecDecodeSelfFromMap(l int, d *" + x.cpfx + "Decoder) {") |
| 780 |
x.genRequiredMethodVars(false) |
| 781 |
x.decStructMap(genTopLevelVarName, "l", rt2id(t0), t0, genStructMapStyleConsolidated) |
| 782 |
x.line("}") |
| 783 |
x.line("") |
| 784 |
} else { |
| 785 |
x.out(fnSigPfx) |
| 786 |
x.line(") codecDecodeSelfFromMapLenPrefix(l int, d *" + x.cpfx + "Decoder) {") |
| 787 |
x.genRequiredMethodVars(false) |
| 788 |
x.decStructMap(genTopLevelVarName, "l", rt2id(t0), t0, genStructMapStyleLenPrefix) |
| 789 |
x.line("}") |
| 790 |
x.line("") |
| 791 |
|
| 792 |
x.out(fnSigPfx) |
| 793 |
x.line(") codecDecodeSelfFromMapCheckBreak(l int, d *" + x.cpfx + "Decoder) {") |
| 794 |
x.genRequiredMethodVars(false) |
| 795 |
x.decStructMap(genTopLevelVarName, "l", rt2id(t0), t0, genStructMapStyleCheckBreak) |
| 796 |
x.line("}") |
| 797 |
x.line("") |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
x.out(fnSigPfx) |
| 802 |
x.line(") codecDecodeSelfFromArray(l int, d *" + x.cpfx + "Decoder) {") |
| 803 |
x.genRequiredMethodVars(false) |
| 804 |
x.decStructArray(genTopLevelVarName, "l", "return", rt2id(t0), t0) |
| 805 |
x.line("}") |
| 806 |
x.line("") |
| 807 |
|
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
func (x *genRunner) xtraSM(varname string, t reflect.Type, ti *typeInfo, encode, isptr bool) { |
| 812 |
var ptrPfx, addrPfx string |
| 813 |
if isptr { |
| 814 |
ptrPfx = "*" |
| 815 |
} else { |
| 816 |
addrPfx = "&" |
| 817 |
} |
| 818 |
if encode { |
| 819 |
x.linef("h.enc%s((%s%s)(%s), e)", x.genMethodNameT(t), ptrPfx, x.genTypeName(t), varname) |
| 820 |
} else { |
| 821 |
x.linef("h.dec%s((*%s)(%s%s), d)", x.genMethodNameT(t), x.genTypeName(t), addrPfx, varname) |
| 822 |
} |
| 823 |
x.registerXtraT(t, ti) |
| 824 |
} |
| 825 |
|
| 826 |
func (x *genRunner) registerXtraT(t reflect.Type, ti *typeInfo) { |
| 827 |
|
| 828 |
tk := t.Kind() |
| 829 |
if tk == reflect.Ptr { |
| 830 |
x.registerXtraT(t.Elem(), nil) |
| 831 |
return |
| 832 |
} |
| 833 |
if _, ok := x.tm[t]; ok { |
| 834 |
return |
| 835 |
} |
| 836 |
|
| 837 |
switch tk { |
| 838 |
case reflect.Chan, reflect.Slice, reflect.Array, reflect.Map: |
| 839 |
default: |
| 840 |
return |
| 841 |
} |
| 842 |
|
| 843 |
if ti == nil { |
| 844 |
ti = x.ti.get(rt2id(t), t) |
| 845 |
} |
| 846 |
if _, rtidu := genFastpathUnderlying(t, ti.rtid, ti); fastpathAvIndex(rtidu) != -1 { |
| 847 |
return |
| 848 |
} |
| 849 |
x.tm[t] = struct{}{} |
| 850 |
x.ts = append(x.ts, t) |
| 851 |
|
| 852 |
x.registerXtraT(t.Elem(), nil) |
| 853 |
if tk == reflect.Map { |
| 854 |
x.registerXtraT(t.Key(), nil) |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
func (x *genRunner) encVar(varname string, t reflect.Type) { |
| 861 |
var checkNil bool |
| 862 |
|
| 863 |
|
| 864 |
switch t.Kind() { |
| 865 |
case reflect.Ptr, reflect.Interface, reflect.Chan: |
| 866 |
checkNil = true |
| 867 |
} |
| 868 |
x.encVarChkNil(varname, t, checkNil) |
| 869 |
} |
| 870 |
|
| 871 |
func (x *genRunner) encVarChkNil(varname string, t reflect.Type, checkNil bool) { |
| 872 |
if checkNil { |
| 873 |
x.linef("if %s == nil { r.EncodeNil() } else {", varname) |
| 874 |
} |
| 875 |
|
| 876 |
switch t.Kind() { |
| 877 |
case reflect.Ptr: |
| 878 |
telem := t.Elem() |
| 879 |
tek := telem.Kind() |
| 880 |
if tek == reflect.Array || (tek == reflect.Struct && telem != timeTyp) { |
| 881 |
x.enc(varname, genNonPtr(t), true) |
| 882 |
break |
| 883 |
} |
| 884 |
i := x.varsfx() |
| 885 |
x.line(genTempVarPfx + i + " := *" + varname) |
| 886 |
x.enc(genTempVarPfx+i, genNonPtr(t), false) |
| 887 |
case reflect.Struct, reflect.Array: |
| 888 |
if t == timeTyp { |
| 889 |
x.enc(varname, t, false) |
| 890 |
break |
| 891 |
} |
| 892 |
i := x.varsfx() |
| 893 |
x.line(genTempVarPfx + i + " := &" + varname) |
| 894 |
x.enc(genTempVarPfx+i, t, true) |
| 895 |
default: |
| 896 |
x.enc(varname, t, false) |
| 897 |
} |
| 898 |
|
| 899 |
if checkNil { |
| 900 |
x.line("}") |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
func (x *genRunner) enc(varname string, t reflect.Type, isptr bool) { |
| 909 |
rtid := rt2id(t) |
| 910 |
ti2 := x.ti.get(rtid, t) |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
mi := x.varsfx() |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
var hasIf genIfClause |
| 925 |
defer hasIf.end(x) |
| 926 |
|
| 927 |
var ptrPfx, addrPfx string |
| 928 |
if isptr { |
| 929 |
ptrPfx = "*" |
| 930 |
} else { |
| 931 |
addrPfx = "&" |
| 932 |
} |
| 933 |
|
| 934 |
if t == timeTyp { |
| 935 |
x.linef("%s z.EncBasicHandle().TimeBuiltin() { r.EncodeTime(%s%s)", hasIf.c(false), ptrPfx, varname) |
| 936 |
|
| 937 |
} |
| 938 |
if t == rawTyp { |
| 939 |
x.linef("%s z.EncRaw(%s%s)", hasIf.c(true), ptrPfx, varname) |
| 940 |
return |
| 941 |
} |
| 942 |
if t == rawExtTyp { |
| 943 |
x.linef("%s r.EncodeRawExt(%s%s)", hasIf.c(true), addrPfx, varname) |
| 944 |
return |
| 945 |
} |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
if !x.nx && varname != genTopLevelVarName && t != genStringDecAsBytesTyp && |
| 950 |
t != genStringDecZCTyp && genImportPath(t) != "" && t.Name() != "" { |
| 951 |
yy := fmt.Sprintf("%sxt%s", genTempVarPfx, mi) |
| 952 |
x.linef("%s %s := z.Extension(%s); %s != nil { z.EncExtension(%s, %s) ", |
| 953 |
hasIf.c(false), yy, varname, yy, varname, yy) |
| 954 |
} |
| 955 |
|
| 956 |
if x.checkForSelfer(t, varname) { |
| 957 |
if ti2.flagSelfer { |
| 958 |
x.linef("%s %s.CodecEncodeSelf(e)", hasIf.c(true), varname) |
| 959 |
return |
| 960 |
} |
| 961 |
if ti2.flagSelferPtr { |
| 962 |
if isptr { |
| 963 |
x.linef("%s %s.CodecEncodeSelf(e)", hasIf.c(true), varname) |
| 964 |
} else { |
| 965 |
x.linef("%s %ssf%s := &%s", hasIf.c(true), genTempVarPfx, mi, varname) |
| 966 |
x.linef("%ssf%s.CodecEncodeSelf(e)", genTempVarPfx, mi) |
| 967 |
} |
| 968 |
return |
| 969 |
} |
| 970 |
|
| 971 |
if _, ok := x.te[rtid]; ok { |
| 972 |
x.linef("%s %s.CodecEncodeSelf(e)", hasIf.c(true), varname) |
| 973 |
return |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
inlist := false |
| 978 |
for _, t0 := range x.t { |
| 979 |
if t == t0 { |
| 980 |
inlist = true |
| 981 |
if x.checkForSelfer(t, varname) { |
| 982 |
x.linef("%s %s.CodecEncodeSelf(e)", hasIf.c(true), varname) |
| 983 |
return |
| 984 |
} |
| 985 |
break |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
var rtidAdded bool |
| 990 |
if t == x.tc { |
| 991 |
x.te[rtid] = true |
| 992 |
rtidAdded = true |
| 993 |
} |
| 994 |
|
| 995 |
if ti2.flagBinaryMarshaler { |
| 996 |
x.linef("%s z.EncBinary() { z.EncBinaryMarshal(%s%v) ", hasIf.c(false), ptrPfx, varname) |
| 997 |
} else if ti2.flagBinaryMarshalerPtr { |
| 998 |
x.linef("%s z.EncBinary() { z.EncBinaryMarshal(%s%v) ", hasIf.c(false), addrPfx, varname) |
| 999 |
} |
| 1000 |
|
| 1001 |
if ti2.flagJsonMarshaler { |
| 1002 |
x.linef("%s !z.EncBinary() && z.IsJSONHandle() { z.EncJSONMarshal(%s%v) ", hasIf.c(false), ptrPfx, varname) |
| 1003 |
} else if ti2.flagJsonMarshalerPtr { |
| 1004 |
x.linef("%s !z.EncBinary() && z.IsJSONHandle() { z.EncJSONMarshal(%s%v) ", hasIf.c(false), addrPfx, varname) |
| 1005 |
} else if ti2.flagTextMarshaler { |
| 1006 |
x.linef("%s !z.EncBinary() { z.EncTextMarshal(%s%v) ", hasIf.c(false), ptrPfx, varname) |
| 1007 |
} else if ti2.flagTextMarshalerPtr { |
| 1008 |
x.linef("%s !z.EncBinary() { z.EncTextMarshal(%s%v) ", hasIf.c(false), addrPfx, varname) |
| 1009 |
} |
| 1010 |
|
| 1011 |
x.lineIf(hasIf.c(true)) |
| 1012 |
|
| 1013 |
switch t.Kind() { |
| 1014 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 1015 |
x.line("r.EncodeInt(int64(" + varname + "))") |
| 1016 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 1017 |
x.line("r.EncodeUint(uint64(" + varname + "))") |
| 1018 |
case reflect.Float32: |
| 1019 |
x.line("r.EncodeFloat32(float32(" + varname + "))") |
| 1020 |
case reflect.Float64: |
| 1021 |
x.line("r.EncodeFloat64(float64(" + varname + "))") |
| 1022 |
case reflect.Complex64: |
| 1023 |
x.linef("z.EncEncodeComplex64(complex64(%s))", varname) |
| 1024 |
case reflect.Complex128: |
| 1025 |
x.linef("z.EncEncodeComplex128(complex128(%s))", varname) |
| 1026 |
case reflect.Bool: |
| 1027 |
x.line("r.EncodeBool(bool(" + varname + "))") |
| 1028 |
case reflect.String: |
| 1029 |
x.linef("r.EncodeString(string(%s))", varname) |
| 1030 |
case reflect.Chan: |
| 1031 |
x.xtraSM(varname, t, ti2, true, false) |
| 1032 |
|
| 1033 |
case reflect.Array: |
| 1034 |
_, rtidu := genFastpathUnderlying(t, rtid, ti2) |
| 1035 |
if fastpathAvIndex(rtidu) != -1 { |
| 1036 |
g := x.newFastpathGenV(ti2.key) |
| 1037 |
x.linef("z.F.%sV((%s)(%s[:]), e)", g.MethodNamePfx("Enc", false), x.genTypeName(ti2.key), varname) |
| 1038 |
} else { |
| 1039 |
x.xtraSM(varname, t, ti2, true, true) |
| 1040 |
} |
| 1041 |
case reflect.Slice: |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
x.linef("if %s == nil { r.EncodeNil() } else {", varname) |
| 1050 |
if rtid == uint8SliceTypId { |
| 1051 |
x.line("r.EncodeStringBytesRaw([]byte(" + varname + "))") |
| 1052 |
} else { |
| 1053 |
tu, rtidu := genFastpathUnderlying(t, rtid, ti2) |
| 1054 |
if fastpathAvIndex(rtidu) != -1 { |
| 1055 |
g := x.newFastpathGenV(tu) |
| 1056 |
if rtid == rtidu { |
| 1057 |
x.linef("z.F.%sV(%s, e)", g.MethodNamePfx("Enc", false), varname) |
| 1058 |
} else { |
| 1059 |
x.linef("z.F.%sV((%s)(%s), e)", g.MethodNamePfx("Enc", false), x.genTypeName(tu), varname) |
| 1060 |
} |
| 1061 |
} else { |
| 1062 |
x.xtraSM(varname, t, ti2, true, false) |
| 1063 |
} |
| 1064 |
} |
| 1065 |
x.linef("} // end block: if %s slice == nil", varname) |
| 1066 |
case reflect.Map: |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
x.linef("if %s == nil { r.EncodeNil() } else {", varname) |
| 1073 |
tu, rtidu := genFastpathUnderlying(t, rtid, ti2) |
| 1074 |
if fastpathAvIndex(rtidu) != -1 { |
| 1075 |
g := x.newFastpathGenV(tu) |
| 1076 |
if rtid == rtidu { |
| 1077 |
x.linef("z.F.%sV(%s, e)", g.MethodNamePfx("Enc", false), varname) |
| 1078 |
} else { |
| 1079 |
x.linef("z.F.%sV((%s)(%s), e)", g.MethodNamePfx("Enc", false), x.genTypeName(tu), varname) |
| 1080 |
} |
| 1081 |
} else { |
| 1082 |
x.xtraSM(varname, t, ti2, true, false) |
| 1083 |
} |
| 1084 |
x.linef("} // end block: if %s map == nil", varname) |
| 1085 |
case reflect.Struct: |
| 1086 |
if !inlist { |
| 1087 |
delete(x.te, rtid) |
| 1088 |
x.line("z.EncFallback(" + varname + ")") |
| 1089 |
break |
| 1090 |
} |
| 1091 |
x.encStruct(varname, rtid, t) |
| 1092 |
default: |
| 1093 |
if rtidAdded { |
| 1094 |
delete(x.te, rtid) |
| 1095 |
} |
| 1096 |
x.line("z.EncFallback(" + varname + ")") |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
func (x *genRunner) encZero(t reflect.Type) { |
| 1101 |
switch t.Kind() { |
| 1102 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 1103 |
x.line("r.EncodeInt(0)") |
| 1104 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 1105 |
x.line("r.EncodeUint(0)") |
| 1106 |
case reflect.Float32: |
| 1107 |
x.line("r.EncodeFloat32(0)") |
| 1108 |
case reflect.Float64: |
| 1109 |
x.line("r.EncodeFloat64(0)") |
| 1110 |
case reflect.Complex64: |
| 1111 |
x.line("z.EncEncodeComplex64(0)") |
| 1112 |
case reflect.Complex128: |
| 1113 |
x.line("z.EncEncodeComplex128(0)") |
| 1114 |
case reflect.Bool: |
| 1115 |
x.line("r.EncodeBool(false)") |
| 1116 |
case reflect.String: |
| 1117 |
x.linef(`r.EncodeString("")`) |
| 1118 |
default: |
| 1119 |
x.line("r.EncodeNil()") |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
func genOmitEmptyLinePreChecks(varname string, t reflect.Type, si *structFieldInfo, omitline *genBuf, oneLevel bool) (t2 reflect.StructField) { |
| 1124 |
|
| 1125 |
t2typ := t |
| 1126 |
varname3 := varname |
| 1127 |
|
| 1128 |
|
| 1129 |
fullpath := si.path.fullpath() |
| 1130 |
for i, path := range fullpath { |
| 1131 |
for t2typ.Kind() == reflect.Ptr { |
| 1132 |
t2typ = t2typ.Elem() |
| 1133 |
} |
| 1134 |
t2 = t2typ.Field(int(path.index)) |
| 1135 |
t2typ = t2.Type |
| 1136 |
varname3 = varname3 + "." + t2.Name |
| 1137 |
|
| 1138 |
|
| 1139 |
if i+1 < len(fullpath) && t2typ.Kind() == reflect.Ptr { |
| 1140 |
omitline.s(varname3).s(" != nil && ") |
| 1141 |
} |
| 1142 |
if oneLevel { |
| 1143 |
break |
| 1144 |
} |
| 1145 |
} |
| 1146 |
return |
| 1147 |
} |
| 1148 |
|
| 1149 |
func (x *genRunner) doEncOmitEmptyLine(t2 reflect.StructField, varname string, buf *genBuf) { |
| 1150 |
x.f = 0 |
| 1151 |
x.encOmitEmptyLine(t2, varname, buf) |
| 1152 |
} |
| 1153 |
|
| 1154 |
func (x *genRunner) encOmitEmptyLine(t2 reflect.StructField, varname string, buf *genBuf) { |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
varname2 := varname + "." + t2.Name |
| 1159 |
switch t2.Type.Kind() { |
| 1160 |
case reflect.Struct: |
| 1161 |
rtid2 := rt2id(t2.Type) |
| 1162 |
ti2 := x.ti.get(rtid2, t2.Type) |
| 1163 |
|
| 1164 |
if ti2.rtid == timeTypId { |
| 1165 |
buf.s("!(").s(varname2).s(".IsZero())") |
| 1166 |
break |
| 1167 |
} |
| 1168 |
if ti2.flagIsZeroerPtr || ti2.flagIsZeroer { |
| 1169 |
buf.s("!(").s(varname2).s(".IsZero())") |
| 1170 |
break |
| 1171 |
} |
| 1172 |
if t2.Type.Implements(isCodecEmptyerTyp) { |
| 1173 |
buf.s("!(").s(varname2).s(".IsCodecEmpty())") |
| 1174 |
break |
| 1175 |
} |
| 1176 |
_, ok := x.tz[rtid2] |
| 1177 |
if ok { |
| 1178 |
buf.s("!(").s(varname2).s(".IsCodecEmpty())") |
| 1179 |
break |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
if genImportPath(t2.Type) == x.bp { |
| 1184 |
x.ty[t2.Type] = struct{}{} |
| 1185 |
buf.s("!(").s(varname2).s(".IsCodecEmpty())") |
| 1186 |
break |
| 1187 |
} |
| 1188 |
if ti2.flagComparable { |
| 1189 |
buf.s(varname2).s(" != ").s(x.genZeroValueR(t2.Type)) |
| 1190 |
break |
| 1191 |
} |
| 1192 |
|
| 1193 |
buf.s(x.sayFalse()) |
| 1194 |
var wrote bool |
| 1195 |
for i, n := 0, t2.Type.NumField(); i < n; i++ { |
| 1196 |
f := t2.Type.Field(i) |
| 1197 |
if f.PkgPath != "" { |
| 1198 |
continue |
| 1199 |
} |
| 1200 |
buf.s(" || ") |
| 1201 |
x.encOmitEmptyLine(f, varname2, buf) |
| 1202 |
wrote = true |
| 1203 |
} |
| 1204 |
if !wrote { |
| 1205 |
buf.s(" || ").s(x.sayTrue()) |
| 1206 |
} |
| 1207 |
|
| 1208 |
case reflect.Bool: |
| 1209 |
buf.s("bool(").s(varname2).s(")") |
| 1210 |
case reflect.Map, reflect.Slice, reflect.Chan: |
| 1211 |
buf.s("len(").s(varname2).s(") != 0") |
| 1212 |
case reflect.Array: |
| 1213 |
tlen := t2.Type.Len() |
| 1214 |
if tlen == 0 { |
| 1215 |
buf.s(x.sayFalse()) |
| 1216 |
} else if t2.Type.Comparable() { |
| 1217 |
buf.s(varname2).s(" != ").s(x.genZeroValueR(t2.Type)) |
| 1218 |
} else { |
| 1219 |
|
| 1220 |
|
| 1221 |
buf.s(x.sayTrue()) |
| 1222 |
} |
| 1223 |
default: |
| 1224 |
buf.s(varname2).s(" != ").s(x.genZeroValueR(t2.Type)) |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
func (x *genRunner) encStruct(varname string, rtid uintptr, t reflect.Type) { |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
ti := x.ti.get(rtid, t) |
| 1234 |
i := x.varsfx() |
| 1235 |
|
| 1236 |
numfieldsvar := genTempVarPfx + "q" + i |
| 1237 |
ti2arrayvar := genTempVarPfx + "r" + i |
| 1238 |
struct2arrvar := genTempVarPfx + "2arr" + i |
| 1239 |
|
| 1240 |
tisfi := ti.sfi.source() |
| 1241 |
|
| 1242 |
type genFQN struct { |
| 1243 |
i string |
| 1244 |
fqname string |
| 1245 |
nilLine genBuf |
| 1246 |
nilVar string |
| 1247 |
canNil bool |
| 1248 |
sf reflect.StructField |
| 1249 |
} |
| 1250 |
|
| 1251 |
genFQNs := make([]genFQN, len(tisfi)) |
| 1252 |
si2Pos := make(map[*structFieldInfo]int) |
| 1253 |
|
| 1254 |
for j, si := range tisfi { |
| 1255 |
si2Pos[si] = j |
| 1256 |
q := &genFQNs[j] |
| 1257 |
q.i = x.varsfx() |
| 1258 |
q.nilVar = genTempVarPfx + "n" + q.i |
| 1259 |
q.canNil = false |
| 1260 |
q.fqname = varname |
| 1261 |
{ |
| 1262 |
t2typ := t |
| 1263 |
fullpath := si.path.fullpath() |
| 1264 |
for _, path := range fullpath { |
| 1265 |
for t2typ.Kind() == reflect.Ptr { |
| 1266 |
t2typ = t2typ.Elem() |
| 1267 |
} |
| 1268 |
q.sf = t2typ.Field(int(path.index)) |
| 1269 |
t2typ = q.sf.Type |
| 1270 |
q.fqname += "." + q.sf.Name |
| 1271 |
if t2typ.Kind() == reflect.Ptr { |
| 1272 |
if !q.canNil { |
| 1273 |
q.nilLine.f("%s == nil", q.fqname) |
| 1274 |
q.canNil = true |
| 1275 |
} else { |
| 1276 |
q.nilLine.f(" || %s == nil", q.fqname) |
| 1277 |
} |
| 1278 |
} |
| 1279 |
} |
| 1280 |
} |
| 1281 |
} |
| 1282 |
|
| 1283 |
|
| 1284 |
x.linef("%s := z.EncBasicHandle().StructToArray", struct2arrvar) |
| 1285 |
|
| 1286 |
x.linef("_ = %s", struct2arrvar) |
| 1287 |
x.linef("const %s bool = %v // struct tag has 'toArray'", ti2arrayvar, ti.toArray) |
| 1288 |
|
| 1289 |
for j := range genFQNs { |
| 1290 |
q := &genFQNs[j] |
| 1291 |
if q.canNil { |
| 1292 |
x.linef("var %s bool = %s", q.nilVar, q.nilLine.v()) |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
omitEmptySometimes := x.omitEmptyWhen == nil |
| 1302 |
omitEmptyAlways := (x.omitEmptyWhen != nil && *(x.omitEmptyWhen)) |
| 1303 |
|
| 1304 |
|
| 1305 |
toArraySometimes := x.toArrayWhen == nil |
| 1306 |
toArrayAlways := (x.toArrayWhen != nil && *(x.toArrayWhen)) |
| 1307 |
toArrayNever := (x.toArrayWhen != nil && !(*(x.toArrayWhen))) |
| 1308 |
|
| 1309 |
if (omitEmptySometimes && ti.anyOmitEmpty) || omitEmptyAlways { |
| 1310 |
x.linef("var %s = [%v]bool{ // should field at this index be written?", numfieldsvar, len(tisfi)) |
| 1311 |
|
| 1312 |
for _, si := range tisfi { |
| 1313 |
if omitEmptySometimes && !si.path.omitEmpty { |
| 1314 |
x.linef("true, // %s", si.encName) |
| 1315 |
continue |
| 1316 |
} |
| 1317 |
var omitline genBuf |
| 1318 |
t2 := genOmitEmptyLinePreChecks(varname, t, si, &omitline, false) |
| 1319 |
x.doEncOmitEmptyLine(t2, varname, &omitline) |
| 1320 |
x.linef("%s, // %s", omitline.v(), si.encName) |
| 1321 |
} |
| 1322 |
x.line("}") |
| 1323 |
x.linef("_ = %s", numfieldsvar) |
| 1324 |
} |
| 1325 |
|
| 1326 |
if toArraySometimes { |
| 1327 |
x.linef("if %s || %s {", ti2arrayvar, struct2arrvar) |
| 1328 |
} |
| 1329 |
if toArraySometimes || toArrayAlways { |
| 1330 |
x.linef("z.EncWriteArrayStart(%d)", len(tisfi)) |
| 1331 |
|
| 1332 |
for j, si := range tisfi { |
| 1333 |
doOmitEmptyCheck := (omitEmptySometimes && si.path.omitEmpty) || omitEmptyAlways |
| 1334 |
q := &genFQNs[j] |
| 1335 |
|
| 1336 |
if q.canNil { |
| 1337 |
x.linef("if %s { z.EncWriteArrayElem(); r.EncodeNil() } else { ", q.nilVar) |
| 1338 |
} |
| 1339 |
x.linef("z.EncWriteArrayElem()") |
| 1340 |
if doOmitEmptyCheck { |
| 1341 |
x.linef("if %s[%v] {", numfieldsvar, j) |
| 1342 |
} |
| 1343 |
x.encVarChkNil(q.fqname, q.sf.Type, false) |
| 1344 |
if doOmitEmptyCheck { |
| 1345 |
x.linef("} else {") |
| 1346 |
x.encZero(q.sf.Type) |
| 1347 |
x.linef("}") |
| 1348 |
} |
| 1349 |
if q.canNil { |
| 1350 |
x.line("}") |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
x.line("z.EncWriteArrayEnd()") |
| 1355 |
} |
| 1356 |
if toArraySometimes { |
| 1357 |
x.linef("} else {") |
| 1358 |
} |
| 1359 |
if toArraySometimes || toArrayNever { |
| 1360 |
if (omitEmptySometimes && ti.anyOmitEmpty) || omitEmptyAlways { |
| 1361 |
x.linef("var %snn%s int", genTempVarPfx, i) |
| 1362 |
x.linef("for _, b := range %s { if b { %snn%s++ } }", numfieldsvar, genTempVarPfx, i) |
| 1363 |
x.linef("z.EncWriteMapStart(%snn%s)", genTempVarPfx, i) |
| 1364 |
x.linef("%snn%s = %v", genTempVarPfx, i, 0) |
| 1365 |
} else { |
| 1366 |
x.linef("z.EncWriteMapStart(%d)", len(tisfi)) |
| 1367 |
} |
| 1368 |
|
| 1369 |
fn := func(tisfi []*structFieldInfo) { |
| 1370 |
|
| 1371 |
for _, si := range tisfi { |
| 1372 |
pos := si2Pos[si] |
| 1373 |
q := &genFQNs[pos] |
| 1374 |
doOmitEmptyCheck := (omitEmptySometimes && si.path.omitEmpty) || omitEmptyAlways |
| 1375 |
if doOmitEmptyCheck { |
| 1376 |
x.linef("if %s[%v] {", numfieldsvar, pos) |
| 1377 |
} |
| 1378 |
x.linef("z.EncWriteMapElemKey()") |
| 1379 |
|
| 1380 |
|
| 1381 |
switch ti.keyType { |
| 1382 |
case valueTypeInt: |
| 1383 |
x.linef("r.EncodeInt(z.M.Int(strconv.ParseInt(`%s`, 10, 64)))", si.encName) |
| 1384 |
case valueTypeUint: |
| 1385 |
x.linef("r.EncodeUint(z.M.Uint(strconv.ParseUint(`%s`, 10, 64)))", si.encName) |
| 1386 |
case valueTypeFloat: |
| 1387 |
x.linef("r.EncodeFloat64(z.M.Float(strconv.ParseFloat(`%s`, 64)))", si.encName) |
| 1388 |
default: |
| 1389 |
if x.jsonOnlyWhen == nil { |
| 1390 |
if si.path.encNameAsciiAlphaNum { |
| 1391 |
x.linef(`if z.IsJSONHandle() { z.WriteStr("\"%s\"") } else { `, si.encName) |
| 1392 |
} |
| 1393 |
x.linef("r.EncodeString(`%s`)", si.encName) |
| 1394 |
if si.path.encNameAsciiAlphaNum { |
| 1395 |
x.linef("}") |
| 1396 |
} |
| 1397 |
} else if *(x.jsonOnlyWhen) { |
| 1398 |
if si.path.encNameAsciiAlphaNum { |
| 1399 |
x.linef(`z.WriteStr("\"%s\"")`, si.encName) |
| 1400 |
} else { |
| 1401 |
x.linef("r.EncodeString(`%s`)", si.encName) |
| 1402 |
} |
| 1403 |
} else { |
| 1404 |
x.linef("r.EncodeString(`%s`)", si.encName) |
| 1405 |
} |
| 1406 |
} |
| 1407 |
x.line("z.EncWriteMapElemValue()") |
| 1408 |
if q.canNil { |
| 1409 |
x.line("if " + q.nilVar + " { r.EncodeNil() } else { ") |
| 1410 |
x.encVarChkNil(q.fqname, q.sf.Type, false) |
| 1411 |
x.line("}") |
| 1412 |
} else { |
| 1413 |
x.encVarChkNil(q.fqname, q.sf.Type, false) |
| 1414 |
} |
| 1415 |
if doOmitEmptyCheck { |
| 1416 |
x.line("}") |
| 1417 |
} |
| 1418 |
} |
| 1419 |
} |
| 1420 |
|
| 1421 |
if genStructCanonical { |
| 1422 |
x.linef("if z.EncBasicHandle().Canonical {") |
| 1423 |
fn(ti.sfi.sorted()) |
| 1424 |
x.linef("} else {") |
| 1425 |
fn(ti.sfi.source()) |
| 1426 |
x.linef("}") |
| 1427 |
} else { |
| 1428 |
fn(tisfi) |
| 1429 |
} |
| 1430 |
|
| 1431 |
x.line("z.EncWriteMapEnd()") |
| 1432 |
} |
| 1433 |
if toArraySometimes { |
| 1434 |
x.linef("} ") |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
func (x *genRunner) encListFallback(varname string, t reflect.Type) { |
| 1439 |
x.linef("if %s == nil { r.EncodeNil(); return }", varname) |
| 1440 |
elemBytes := t.Elem().Kind() == reflect.Uint8 |
| 1441 |
if t.AssignableTo(uint8SliceTyp) { |
| 1442 |
x.linef("r.EncodeStringBytesRaw([]byte(%s))", varname) |
| 1443 |
return |
| 1444 |
} |
| 1445 |
if t.Kind() == reflect.Array && elemBytes { |
| 1446 |
x.linef("r.EncodeStringBytesRaw(((*[%d]byte)(%s))[:])", t.Len(), varname) |
| 1447 |
return |
| 1448 |
} |
| 1449 |
i := x.varsfx() |
| 1450 |
if t.Kind() == reflect.Chan { |
| 1451 |
type ts struct { |
| 1452 |
Label, Chan, Slice, Sfx string |
| 1453 |
} |
| 1454 |
tm, err := template.New("").Parse(genEncChanTmpl) |
| 1455 |
genCheckErr(err) |
| 1456 |
x.linef("if %s == nil { r.EncodeNil() } else { ", varname) |
| 1457 |
x.linef("var sch%s []%s", i, x.genTypeName(t.Elem())) |
| 1458 |
err = tm.Execute(x.w, &ts{"Lsch" + i, varname, "sch" + i, i}) |
| 1459 |
genCheckErr(err) |
| 1460 |
if elemBytes { |
| 1461 |
x.linef("r.EncodeStringBytesRaw([]byte(%s))", "sch"+i) |
| 1462 |
x.line("}") |
| 1463 |
return |
| 1464 |
} |
| 1465 |
varname = "sch" + i |
| 1466 |
} |
| 1467 |
|
| 1468 |
x.line("z.EncWriteArrayStart(len(" + varname + "))") |
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
x.linef("for %sv%s := range %s {", genTempVarPfx, i, varname) |
| 1476 |
x.linef("z.EncWriteArrayElem()") |
| 1477 |
x.encVar(fmt.Sprintf("%s[%sv%s]", varname, genTempVarPfx, i), t.Elem()) |
| 1478 |
x.line("}") |
| 1479 |
|
| 1480 |
x.line("z.EncWriteArrayEnd()") |
| 1481 |
if t.Kind() == reflect.Chan { |
| 1482 |
x.line("}") |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
func (x *genRunner) encMapFallback(varname string, t reflect.Type) { |
| 1487 |
x.linef("if %s == nil { r.EncodeNil()", varname) |
| 1488 |
x.line("} else if z.EncBasicHandle().Canonical {") |
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
tkey := t.Key() |
| 1496 |
tkind := tkey.Kind() |
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
predeclared := tkey.PkgPath() == "" && tkey.Name() != "" |
| 1505 |
|
| 1506 |
canonSortKind := reflect.Invalid |
| 1507 |
switch tkind { |
| 1508 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 1509 |
canonSortKind = reflect.Int64 |
| 1510 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 1511 |
canonSortKind = reflect.Uint64 |
| 1512 |
case reflect.Float32, reflect.Float64: |
| 1513 |
canonSortKind = reflect.Float64 |
| 1514 |
case reflect.String: |
| 1515 |
canonSortKind = reflect.String |
| 1516 |
} |
| 1517 |
|
| 1518 |
var i string = x.varsfx() |
| 1519 |
|
| 1520 |
fnCanonNumBoolStrKind := func() { |
| 1521 |
if !predeclared { |
| 1522 |
x.linef("var %svv%s %s", genTempVarPfx, i, x.genTypeName(tkey)) |
| 1523 |
x.linef("%sencfn%s := z.EncFnGivenAddr(&%svv%s)", genTempVarPfx, i, genTempVarPfx, i) |
| 1524 |
} |
| 1525 |
|
| 1526 |
x.linef("%ss%s := make([]%s, 0, len(%s))", genTempVarPfx, i, canonSortKind, varname) |
| 1527 |
x.linef("for k, _ := range %s {", varname) |
| 1528 |
x.linef(" %ss%s = append(%ss%s, %s(k))", genTempVarPfx, i, genTempVarPfx, i, canonSortKind) |
| 1529 |
x.linef("}") |
| 1530 |
x.linef("sort.Sort(%s%sSlice(%ss%s))", x.hn, canonSortKind, genTempVarPfx, i) |
| 1531 |
x.linef("z.EncWriteMapStart(len(%s))", varname) |
| 1532 |
x.linef("for _, %sv%s := range %ss%s {", genTempVarPfx, i, genTempVarPfx, i) |
| 1533 |
x.linef(" z.EncWriteMapElemKey()") |
| 1534 |
if predeclared { |
| 1535 |
switch tkind { |
| 1536 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32: |
| 1537 |
x.linef("r.EncodeInt(int64(%sv%s))", genTempVarPfx, i) |
| 1538 |
case reflect.Int64: |
| 1539 |
x.linef("r.EncodeInt(%sv%s)", genTempVarPfx, i) |
| 1540 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: |
| 1541 |
x.linef("r.EncodeUint(%sv%s)", genTempVarPfx, i) |
| 1542 |
case reflect.Uint64: |
| 1543 |
x.linef("r.EncodeUint(uint64(%sv%s))", genTempVarPfx, i) |
| 1544 |
case reflect.Float32: |
| 1545 |
x.linef("r.EncodeFloat32(float32(%sv%s))", genTempVarPfx, i) |
| 1546 |
case reflect.Float64: |
| 1547 |
x.linef("r.EncodeFloat64(%sv%s)", genTempVarPfx, i) |
| 1548 |
case reflect.String: |
| 1549 |
x.linef("r.EncodeString(%sv%s)", genTempVarPfx, i) |
| 1550 |
} |
| 1551 |
} else { |
| 1552 |
x.linef("%svv%s = %s(%sv%s)", genTempVarPfx, i, x.genTypeName(tkey), genTempVarPfx, i) |
| 1553 |
x.linef("z.EncEncodeNumBoolStrKindGivenAddr(&%svv%s, %sencfn%s)", genTempVarPfx, i, genTempVarPfx, i) |
| 1554 |
} |
| 1555 |
x.linef(" z.EncWriteMapElemValue()") |
| 1556 |
vname := genTempVarPfx + "e" + i |
| 1557 |
if predeclared { |
| 1558 |
x.linef("%s := %s[%s(%sv%s)]", vname, varname, x.genTypeName(tkey), genTempVarPfx, i) |
| 1559 |
} else { |
| 1560 |
x.linef("%s := %s[%svv%s]", vname, varname, genTempVarPfx, i) |
| 1561 |
} |
| 1562 |
x.encVar(vname, t.Elem()) |
| 1563 |
x.linef("}") |
| 1564 |
|
| 1565 |
x.line("z.EncWriteMapEnd()") |
| 1566 |
|
| 1567 |
} |
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
if canonSortKind != reflect.Invalid { |
| 1584 |
fnCanonNumBoolStrKind() |
| 1585 |
} else { |
| 1586 |
x.linef("z.EncEncodeMapNonNil(%s)", varname) |
| 1587 |
} |
| 1588 |
|
| 1589 |
x.line("} else {") |
| 1590 |
|
| 1591 |
x.linef("z.EncWriteMapStart(len(%s))", varname) |
| 1592 |
x.linef("for %sk%s, %sv%s := range %s {", genTempVarPfx, i, genTempVarPfx, i, varname) |
| 1593 |
x.linef("z.EncWriteMapElemKey()") |
| 1594 |
x.encVar(genTempVarPfx+"k"+i, t.Key()) |
| 1595 |
x.line("z.EncWriteMapElemValue()") |
| 1596 |
x.encVar(genTempVarPfx+"v"+i, t.Elem()) |
| 1597 |
x.line("}") |
| 1598 |
x.line("z.EncWriteMapEnd()") |
| 1599 |
|
| 1600 |
x.line("}") |
| 1601 |
} |
| 1602 |
|
| 1603 |
func (x *genRunner) decVarInitPtr(varname, nilvar string, t reflect.Type, si *structFieldInfo, |
| 1604 |
newbuf, nilbuf *genBuf) (varname3 string, t2 reflect.StructField) { |
| 1605 |
|
| 1606 |
|
| 1607 |
varname3 = varname |
| 1608 |
t2typ := t |
| 1609 |
t2kind := t2typ.Kind() |
| 1610 |
var nilbufed bool |
| 1611 |
if si != nil { |
| 1612 |
fullpath := si.path.fullpath() |
| 1613 |
for _, path := range fullpath { |
| 1614 |
|
| 1615 |
if t2typ.Kind() == reflect.Ptr { |
| 1616 |
t2typ = t2typ.Elem() |
| 1617 |
} |
| 1618 |
t2 = t2typ.Field(int(path.index)) |
| 1619 |
t2typ = t2.Type |
| 1620 |
varname3 = varname3 + "." + t2.Name |
| 1621 |
t2kind = t2typ.Kind() |
| 1622 |
if t2kind != reflect.Ptr { |
| 1623 |
continue |
| 1624 |
} |
| 1625 |
if newbuf != nil { |
| 1626 |
if len(newbuf.buf) > 0 { |
| 1627 |
newbuf.s("\n") |
| 1628 |
} |
| 1629 |
newbuf.f("if %s == nil { %s = new(%s) }", varname3, varname3, x.genTypeName(t2typ.Elem())) |
| 1630 |
} |
| 1631 |
if nilbuf != nil { |
| 1632 |
if !nilbufed { |
| 1633 |
nilbuf.s("if ").s(varname3).s(" != nil") |
| 1634 |
nilbufed = true |
| 1635 |
} else { |
| 1636 |
nilbuf.s(" && ").s(varname3).s(" != nil") |
| 1637 |
} |
| 1638 |
} |
| 1639 |
} |
| 1640 |
} |
| 1641 |
if nilbuf != nil { |
| 1642 |
if nilbufed { |
| 1643 |
nilbuf.s(" { ").s("// remove the if-true\n") |
| 1644 |
} |
| 1645 |
if nilvar != "" { |
| 1646 |
nilbuf.s(nilvar).s(" = true") |
| 1647 |
} else if tk := t2typ.Kind(); tk == reflect.Ptr { |
| 1648 |
if strings.IndexByte(varname3, '.') != -1 || strings.IndexByte(varname3, '[') != -1 { |
| 1649 |
nilbuf.s(varname3).s(" = nil") |
| 1650 |
} else { |
| 1651 |
nilbuf.s("*").s(varname3).s(" = ").s(x.genZeroValueR(t2typ.Elem())) |
| 1652 |
} |
| 1653 |
} else { |
| 1654 |
nilbuf.s(varname3).s(" = ").s(x.genZeroValueR(t2typ)) |
| 1655 |
} |
| 1656 |
if nilbufed { |
| 1657 |
nilbuf.s("}") |
| 1658 |
} |
| 1659 |
} |
| 1660 |
return |
| 1661 |
} |
| 1662 |
|
| 1663 |
|
| 1664 |
func (x *genRunner) decVarMain(varname, rand string, t reflect.Type, checkNotNil bool) { |
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
var varname2 string |
| 1672 |
if t.Kind() != reflect.Ptr { |
| 1673 |
if t.PkgPath() != "" || !x.decTryAssignPrimitive(varname, t, false) { |
| 1674 |
x.dec(varname, t, false) |
| 1675 |
} |
| 1676 |
} else { |
| 1677 |
if checkNotNil { |
| 1678 |
x.linef("if %s == nil { %s = new(%s) }", varname, varname, x.genTypeName(t.Elem())) |
| 1679 |
} |
| 1680 |
|
| 1681 |
|
| 1682 |
var ptrPfx string |
| 1683 |
for t = t.Elem(); t.Kind() == reflect.Ptr; t = t.Elem() { |
| 1684 |
ptrPfx += "*" |
| 1685 |
if checkNotNil { |
| 1686 |
x.linef("if %s%s == nil { %s%s = new(%s)}", ptrPfx, varname, ptrPfx, varname, x.genTypeName(t)) |
| 1687 |
} |
| 1688 |
} |
| 1689 |
|
| 1690 |
|
| 1691 |
if ptrPfx == "" { |
| 1692 |
x.dec(varname, t, true) |
| 1693 |
} else { |
| 1694 |
varname2 = genTempVarPfx + "z" + rand |
| 1695 |
x.line(varname2 + " := " + ptrPfx + varname) |
| 1696 |
x.dec(varname2, t, true) |
| 1697 |
} |
| 1698 |
} |
| 1699 |
} |
| 1700 |
|
| 1701 |
|
| 1702 |
func (x *genRunner) decVar(varname, nilvar string, t reflect.Type, canBeNil, checkNotNil bool) { |
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
i := x.varsfx() |
| 1712 |
if t.Kind() == reflect.Ptr { |
| 1713 |
var buf genBuf |
| 1714 |
x.decVarInitPtr(varname, nilvar, t, nil, nil, &buf) |
| 1715 |
x.linef("if r.TryNil() { %s } else {", buf.buf) |
| 1716 |
x.decVarMain(varname, i, t, checkNotNil) |
| 1717 |
x.line("} ") |
| 1718 |
} else { |
| 1719 |
x.decVarMain(varname, i, t, checkNotNil) |
| 1720 |
} |
| 1721 |
} |
| 1722 |
|
| 1723 |
|
| 1724 |
func (x *genRunner) dec(varname string, t reflect.Type, isptr bool) { |
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
rtid := rt2id(t) |
| 1729 |
ti2 := x.ti.get(rtid, t) |
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
mi := x.varsfx() |
| 1736 |
|
| 1737 |
var hasIf genIfClause |
| 1738 |
defer hasIf.end(x) |
| 1739 |
|
| 1740 |
var ptrPfx, addrPfx string |
| 1741 |
if isptr { |
| 1742 |
ptrPfx = "*" |
| 1743 |
} else { |
| 1744 |
addrPfx = "&" |
| 1745 |
} |
| 1746 |
if t == timeTyp { |
| 1747 |
x.linef("%s z.DecBasicHandle().TimeBuiltin() { %s%v = r.DecodeTime()", hasIf.c(false), ptrPfx, varname) |
| 1748 |
|
| 1749 |
} |
| 1750 |
if t == rawTyp { |
| 1751 |
x.linef("%s %s%v = z.DecRaw()", hasIf.c(true), ptrPfx, varname) |
| 1752 |
return |
| 1753 |
} |
| 1754 |
|
| 1755 |
if t == rawExtTyp { |
| 1756 |
x.linef("%s r.DecodeExt(%s%v, 0, nil)", hasIf.c(true), addrPfx, varname) |
| 1757 |
return |
| 1758 |
} |
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
if !x.nx && varname != genTopLevelVarName && t != genStringDecAsBytesTyp && |
| 1765 |
t != genStringDecZCTyp && genImportPath(t) != "" && t.Name() != "" { |
| 1766 |
|
| 1767 |
yy := fmt.Sprintf("%sxt%s", genTempVarPfx, mi) |
| 1768 |
x.linef("%s %s := z.Extension(%s); %s != nil { z.DecExtension(%s%s, %s) ", hasIf.c(false), yy, varname, yy, addrPfx, varname, yy) |
| 1769 |
} |
| 1770 |
|
| 1771 |
if x.checkForSelfer(t, varname) { |
| 1772 |
if ti2.flagSelfer { |
| 1773 |
x.linef("%s %s.CodecDecodeSelf(d)", hasIf.c(true), varname) |
| 1774 |
return |
| 1775 |
} |
| 1776 |
if ti2.flagSelferPtr { |
| 1777 |
x.linef("%s %s.CodecDecodeSelf(d)", hasIf.c(true), varname) |
| 1778 |
return |
| 1779 |
} |
| 1780 |
if _, ok := x.td[rtid]; ok { |
| 1781 |
x.linef("%s %s.CodecDecodeSelf(d)", hasIf.c(true), varname) |
| 1782 |
return |
| 1783 |
} |
| 1784 |
} |
| 1785 |
|
| 1786 |
inlist := false |
| 1787 |
for _, t0 := range x.t { |
| 1788 |
if t == t0 { |
| 1789 |
inlist = true |
| 1790 |
if x.checkForSelfer(t, varname) { |
| 1791 |
x.linef("%s %s.CodecDecodeSelf(d)", hasIf.c(true), varname) |
| 1792 |
return |
| 1793 |
} |
| 1794 |
break |
| 1795 |
} |
| 1796 |
} |
| 1797 |
|
| 1798 |
var rtidAdded bool |
| 1799 |
if t == x.tc { |
| 1800 |
x.td[rtid] = true |
| 1801 |
rtidAdded = true |
| 1802 |
} |
| 1803 |
|
| 1804 |
if ti2.flagBinaryUnmarshaler { |
| 1805 |
x.linef("%s z.DecBinary() { z.DecBinaryUnmarshal(%s%v) ", hasIf.c(false), ptrPfx, varname) |
| 1806 |
} else if ti2.flagBinaryUnmarshalerPtr { |
| 1807 |
x.linef("%s z.DecBinary() { z.DecBinaryUnmarshal(%s%v) ", hasIf.c(false), addrPfx, varname) |
| 1808 |
} |
| 1809 |
if ti2.flagJsonUnmarshaler { |
| 1810 |
x.linef("%s !z.DecBinary() && z.IsJSONHandle() { z.DecJSONUnmarshal(%s%v)", hasIf.c(false), ptrPfx, varname) |
| 1811 |
} else if ti2.flagJsonUnmarshalerPtr { |
| 1812 |
x.linef("%s !z.DecBinary() && z.IsJSONHandle() { z.DecJSONUnmarshal(%s%v)", hasIf.c(false), addrPfx, varname) |
| 1813 |
} else if ti2.flagTextUnmarshaler { |
| 1814 |
x.linef("%s !z.DecBinary() { z.DecTextUnmarshal(%s%v)", hasIf.c(false), ptrPfx, varname) |
| 1815 |
} else if ti2.flagTextUnmarshalerPtr { |
| 1816 |
x.linef("%s !z.DecBinary() { z.DecTextUnmarshal(%s%v)", hasIf.c(false), addrPfx, varname) |
| 1817 |
} |
| 1818 |
|
| 1819 |
x.lineIf(hasIf.c(true)) |
| 1820 |
|
| 1821 |
if x.decTryAssignPrimitive(varname, t, isptr) { |
| 1822 |
return |
| 1823 |
} |
| 1824 |
|
| 1825 |
switch t.Kind() { |
| 1826 |
case reflect.Chan: |
| 1827 |
x.xtraSM(varname, t, ti2, false, isptr) |
| 1828 |
case reflect.Array: |
| 1829 |
_, rtidu := genFastpathUnderlying(t, rtid, ti2) |
| 1830 |
if fastpathAvIndex(rtidu) != -1 { |
| 1831 |
g := x.newFastpathGenV(ti2.key) |
| 1832 |
x.linef("z.F.%sN((%s)(%s[:]), d)", g.MethodNamePfx("Dec", false), x.genTypeName(ti2.key), varname) |
| 1833 |
} else { |
| 1834 |
x.xtraSM(varname, t, ti2, false, isptr) |
| 1835 |
} |
| 1836 |
case reflect.Slice: |
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
if rtid == uint8SliceTypId { |
| 1844 |
x.linef("%s%s = z.DecodeBytesInto(%s(%s[]byte)(%s))", ptrPfx, varname, ptrPfx, ptrPfx, varname) |
| 1845 |
} else { |
| 1846 |
tu, rtidu := genFastpathUnderlying(t, rtid, ti2) |
| 1847 |
if fastpathAvIndex(rtidu) != -1 { |
| 1848 |
g := x.newFastpathGenV(tu) |
| 1849 |
if rtid == rtidu { |
| 1850 |
x.linef("z.F.%sX(%s%s, d)", g.MethodNamePfx("Dec", false), addrPfx, varname) |
| 1851 |
} else { |
| 1852 |
x.linef("z.F.%sX((*%s)(%s%s), d)", g.MethodNamePfx("Dec", false), x.genTypeName(tu), addrPfx, varname) |
| 1853 |
} |
| 1854 |
} else { |
| 1855 |
x.xtraSM(varname, t, ti2, false, isptr) |
| 1856 |
|
| 1857 |
} |
| 1858 |
} |
| 1859 |
case reflect.Map: |
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
tu, rtidu := genFastpathUnderlying(t, rtid, ti2) |
| 1866 |
if fastpathAvIndex(rtidu) != -1 { |
| 1867 |
g := x.newFastpathGenV(tu) |
| 1868 |
if rtid == rtidu { |
| 1869 |
x.linef("z.F.%sX(%s%s, d)", g.MethodNamePfx("Dec", false), addrPfx, varname) |
| 1870 |
} else { |
| 1871 |
x.linef("z.F.%sX((*%s)(%s%s), d)", g.MethodNamePfx("Dec", false), x.genTypeName(tu), addrPfx, varname) |
| 1872 |
} |
| 1873 |
} else { |
| 1874 |
x.xtraSM(varname, t, ti2, false, isptr) |
| 1875 |
} |
| 1876 |
case reflect.Struct: |
| 1877 |
if inlist { |
| 1878 |
|
| 1879 |
if isptr || strings.IndexByte(varname, '.') != -1 || strings.IndexByte(varname, '[') != -1 { |
| 1880 |
x.decStruct(varname, rtid, t) |
| 1881 |
} else { |
| 1882 |
varname2 := genTempVarPfx + "j" + mi |
| 1883 |
x.line(varname2 + " := &" + varname) |
| 1884 |
x.decStruct(varname2, rtid, t) |
| 1885 |
} |
| 1886 |
} else { |
| 1887 |
|
| 1888 |
x.line("z.DecFallback(" + addrPfx + varname + ", false)") |
| 1889 |
} |
| 1890 |
default: |
| 1891 |
if rtidAdded { |
| 1892 |
delete(x.te, rtid) |
| 1893 |
} |
| 1894 |
x.line("z.DecFallback(" + addrPfx + varname + ", true)") |
| 1895 |
} |
| 1896 |
} |
| 1897 |
|
| 1898 |
func (x *genRunner) decTryAssignPrimitive(varname string, t reflect.Type, isptr bool) (done bool) { |
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
var ptr string |
| 1904 |
if isptr { |
| 1905 |
ptr = "*" |
| 1906 |
} |
| 1907 |
switch t.Kind() { |
| 1908 |
case reflect.Int: |
| 1909 |
x.linef("%s%s = (%s)(z.C.IntV(r.DecodeInt64(), codecSelferBitsize%s))", ptr, varname, x.genTypeName(t), x.xs) |
| 1910 |
case reflect.Int8: |
| 1911 |
x.linef("%s%s = (%s)(z.C.IntV(r.DecodeInt64(), 8))", ptr, varname, x.genTypeName(t)) |
| 1912 |
case reflect.Int16: |
| 1913 |
x.linef("%s%s = (%s)(z.C.IntV(r.DecodeInt64(), 16))", ptr, varname, x.genTypeName(t)) |
| 1914 |
case reflect.Int32: |
| 1915 |
x.linef("%s%s = (%s)(z.C.IntV(r.DecodeInt64(), 32))", ptr, varname, x.genTypeName(t)) |
| 1916 |
case reflect.Int64: |
| 1917 |
x.linef("%s%s = (%s)(r.DecodeInt64())", ptr, varname, x.genTypeName(t)) |
| 1918 |
|
| 1919 |
case reflect.Uint: |
| 1920 |
x.linef("%s%s = (%s)(z.C.UintV(r.DecodeUint64(), codecSelferBitsize%s))", ptr, varname, x.genTypeName(t), x.xs) |
| 1921 |
case reflect.Uint8: |
| 1922 |
x.linef("%s%s = (%s)(z.C.UintV(r.DecodeUint64(), 8))", ptr, varname, x.genTypeName(t)) |
| 1923 |
case reflect.Uint16: |
| 1924 |
x.linef("%s%s = (%s)(z.C.UintV(r.DecodeUint64(), 16))", ptr, varname, x.genTypeName(t)) |
| 1925 |
case reflect.Uint32: |
| 1926 |
x.linef("%s%s = (%s)(z.C.UintV(r.DecodeUint64(), 32))", ptr, varname, x.genTypeName(t)) |
| 1927 |
case reflect.Uint64: |
| 1928 |
x.linef("%s%s = (%s)(r.DecodeUint64())", ptr, varname, x.genTypeName(t)) |
| 1929 |
case reflect.Uintptr: |
| 1930 |
x.linef("%s%s = (%s)(z.C.UintV(r.DecodeUint64(), codecSelferBitsize%s))", ptr, varname, x.genTypeName(t), x.xs) |
| 1931 |
|
| 1932 |
case reflect.Float32: |
| 1933 |
x.linef("%s%s = (%s)(z.DecDecodeFloat32())", ptr, varname, x.genTypeName(t)) |
| 1934 |
case reflect.Float64: |
| 1935 |
x.linef("%s%s = (%s)(r.DecodeFloat64())", ptr, varname, x.genTypeName(t)) |
| 1936 |
|
| 1937 |
case reflect.Complex64: |
| 1938 |
x.linef("%s%s = (%s)(complex(z.DecDecodeFloat32(), 0))", ptr, varname, x.genTypeName(t)) |
| 1939 |
case reflect.Complex128: |
| 1940 |
x.linef("%s%s = (%s)(complex(r.DecodeFloat64(), 0))", ptr, varname, x.genTypeName(t)) |
| 1941 |
|
| 1942 |
case reflect.Bool: |
| 1943 |
x.linef("%s%s = (%s)(r.DecodeBool())", ptr, varname, x.genTypeName(t)) |
| 1944 |
case reflect.String: |
| 1945 |
if t == genStringDecAsBytesTyp { |
| 1946 |
x.linef("%s%s = r.DecodeStringAsBytes()", ptr, varname) |
| 1947 |
} else if t == genStringDecZCTyp { |
| 1948 |
x.linef("%s%s = (string)(z.DecStringZC(r.DecodeStringAsBytes()))", ptr, varname) |
| 1949 |
} else { |
| 1950 |
x.linef("%s%s = (%s)(z.DecStringZC(r.DecodeStringAsBytes()))", ptr, varname, x.genTypeName(t)) |
| 1951 |
} |
| 1952 |
default: |
| 1953 |
return false |
| 1954 |
} |
| 1955 |
return true |
| 1956 |
} |
| 1957 |
|
| 1958 |
func (x *genRunner) decListFallback(varname string, rtid uintptr, t reflect.Type) { |
| 1959 |
if t.AssignableTo(uint8SliceTyp) { |
| 1960 |
x.line("*" + varname + " = z.DecodeBytesInto(*((*[]byte)(" + varname + ")))") |
| 1961 |
return |
| 1962 |
} |
| 1963 |
if t.Kind() == reflect.Array && t.Elem().Kind() == reflect.Uint8 { |
| 1964 |
x.linef("r.DecodeBytes( ((*[%d]byte)(%s))[:])", t.Len(), varname) |
| 1965 |
return |
| 1966 |
} |
| 1967 |
type tstruc struct { |
| 1968 |
TempVar string |
| 1969 |
Sfx string |
| 1970 |
Rand string |
| 1971 |
Varname string |
| 1972 |
CTyp string |
| 1973 |
Typ string |
| 1974 |
Immutable bool |
| 1975 |
Size int |
| 1976 |
} |
| 1977 |
telem := t.Elem() |
| 1978 |
ts := tstruc{genTempVarPfx, x.xs, x.varsfx(), varname, x.genTypeName(t), x.genTypeName(telem), genIsImmutable(telem), int(telem.Size())} |
| 1979 |
|
| 1980 |
funcs := make(template.FuncMap) |
| 1981 |
|
| 1982 |
funcs["decLineVar"] = func(varname string) string { |
| 1983 |
x.decVar(varname, "", telem, false, true) |
| 1984 |
return "" |
| 1985 |
} |
| 1986 |
funcs["var"] = func(s string) string { |
| 1987 |
return ts.TempVar + s + ts.Rand |
| 1988 |
} |
| 1989 |
funcs["xs"] = func() string { |
| 1990 |
return ts.Sfx |
| 1991 |
} |
| 1992 |
funcs["zero"] = func() string { |
| 1993 |
return x.genZeroValueR(telem) |
| 1994 |
} |
| 1995 |
funcs["isArray"] = func() bool { |
| 1996 |
return t.Kind() == reflect.Array |
| 1997 |
} |
| 1998 |
funcs["isSlice"] = func() bool { |
| 1999 |
return t.Kind() == reflect.Slice |
| 2000 |
} |
| 2001 |
funcs["isChan"] = func() bool { |
| 2002 |
return t.Kind() == reflect.Chan |
| 2003 |
} |
| 2004 |
tm, err := template.New("").Funcs(funcs).Parse(genDecListTmpl) |
| 2005 |
genCheckErr(err) |
| 2006 |
genCheckErr(tm.Execute(x.w, &ts)) |
| 2007 |
} |
| 2008 |
|
| 2009 |
func (x *genRunner) decMapFallback(varname string, rtid uintptr, t reflect.Type) { |
| 2010 |
type tstruc struct { |
| 2011 |
TempVar string |
| 2012 |
Sfx string |
| 2013 |
Rand string |
| 2014 |
Varname string |
| 2015 |
KTyp string |
| 2016 |
Typ string |
| 2017 |
Size int |
| 2018 |
} |
| 2019 |
telem := t.Elem() |
| 2020 |
tkey := t.Key() |
| 2021 |
ts := tstruc{ |
| 2022 |
genTempVarPfx, x.xs, x.varsfx(), varname, x.genTypeName(tkey), |
| 2023 |
x.genTypeName(telem), int(telem.Size() + tkey.Size()), |
| 2024 |
} |
| 2025 |
|
| 2026 |
funcs := make(template.FuncMap) |
| 2027 |
funcs["decElemZero"] = func() string { |
| 2028 |
return x.genZeroValueR(telem) |
| 2029 |
} |
| 2030 |
funcs["decElemKindImmutable"] = func() bool { |
| 2031 |
return genIsImmutable(telem) |
| 2032 |
} |
| 2033 |
funcs["decElemKindPtr"] = func() bool { |
| 2034 |
return telem.Kind() == reflect.Ptr |
| 2035 |
} |
| 2036 |
funcs["decElemKindIntf"] = func() bool { |
| 2037 |
return telem.Kind() == reflect.Interface |
| 2038 |
} |
| 2039 |
funcs["decLineVarKStrBytes"] = func(varname string) string { |
| 2040 |
x.decVar(varname, "", genStringDecAsBytesTyp, false, true) |
| 2041 |
return "" |
| 2042 |
} |
| 2043 |
funcs["decLineVarKStrZC"] = func(varname string) string { |
| 2044 |
x.decVar(varname, "", genStringDecZCTyp, false, true) |
| 2045 |
return "" |
| 2046 |
} |
| 2047 |
funcs["decLineVarK"] = func(varname string) string { |
| 2048 |
x.decVar(varname, "", tkey, false, true) |
| 2049 |
return "" |
| 2050 |
} |
| 2051 |
funcs["decLineVar"] = func(varname, decodedNilVarname string) string { |
| 2052 |
x.decVar(varname, decodedNilVarname, telem, false, true) |
| 2053 |
return "" |
| 2054 |
} |
| 2055 |
funcs["var"] = func(s string) string { |
| 2056 |
return ts.TempVar + s + ts.Rand |
| 2057 |
} |
| 2058 |
funcs["xs"] = func() string { |
| 2059 |
return ts.Sfx |
| 2060 |
} |
| 2061 |
|
| 2062 |
tm, err := template.New("").Funcs(funcs).Parse(genDecMapTmpl) |
| 2063 |
genCheckErr(err) |
| 2064 |
genCheckErr(tm.Execute(x.w, &ts)) |
| 2065 |
} |
| 2066 |
|
| 2067 |
func (x *genRunner) decStructMapSwitch(kName string, varname string, rtid uintptr, t reflect.Type) { |
| 2068 |
ti := x.ti.get(rtid, t) |
| 2069 |
tisfi := ti.sfi.source() |
| 2070 |
x.line("switch string(" + kName + ") {") |
| 2071 |
var newbuf, nilbuf genBuf |
| 2072 |
for _, si := range tisfi { |
| 2073 |
x.line("case \"" + si.encName + "\":") |
| 2074 |
newbuf.reset() |
| 2075 |
nilbuf.reset() |
| 2076 |
varname3, t2 := x.decVarInitPtr(varname, "", t, si, &newbuf, &nilbuf) |
| 2077 |
if len(newbuf.buf) > 0 { |
| 2078 |
x.linef("if r.TryNil() { %s } else { %s", nilbuf.buf, newbuf.buf) |
| 2079 |
} |
| 2080 |
x.decVarMain(varname3, x.varsfx(), t2.Type, false) |
| 2081 |
if len(newbuf.buf) > 0 { |
| 2082 |
x.line("}") |
| 2083 |
} |
| 2084 |
} |
| 2085 |
x.line("default:") |
| 2086 |
|
| 2087 |
x.linef("z.DecStructFieldNotFound(-1, string(%s))", kName) |
| 2088 |
x.linef("} // end switch %s", kName) |
| 2089 |
} |
| 2090 |
|
| 2091 |
func (x *genRunner) decStructMap(varname, lenvarname string, rtid uintptr, t reflect.Type, style genStructMapStyle) { |
| 2092 |
tpfx := genTempVarPfx |
| 2093 |
ti := x.ti.get(rtid, t) |
| 2094 |
i := x.varsfx() |
| 2095 |
kName := tpfx + "s" + i |
| 2096 |
|
| 2097 |
switch style { |
| 2098 |
case genStructMapStyleLenPrefix: |
| 2099 |
x.linef("for %sj%s := 0; %sj%s < %s; %sj%s++ {", tpfx, i, tpfx, i, lenvarname, tpfx, i) |
| 2100 |
case genStructMapStyleCheckBreak: |
| 2101 |
x.linef("for %sj%s := 0; !z.DecCheckBreak(); %sj%s++ {", tpfx, i, tpfx, i) |
| 2102 |
default: |
| 2103 |
x.linef("var %shl%s bool = %s >= 0", tpfx, i, lenvarname) |
| 2104 |
x.linef("for %sj%s := 0; ; %sj%s++ {", tpfx, i, tpfx, i) |
| 2105 |
x.linef("if %shl%s { if %sj%s >= %s { break }", tpfx, i, tpfx, i, lenvarname) |
| 2106 |
x.line("} else { if z.DecCheckBreak() { break }; }") |
| 2107 |
} |
| 2108 |
x.line("z.DecReadMapElemKey()") |
| 2109 |
|
| 2110 |
|
| 2111 |
switch ti.keyType { |
| 2112 |
case valueTypeInt: |
| 2113 |
x.linef("%s := strconv.AppendInt(z.DecScratchArrayBuffer()[:0], r.DecodeInt64(), 10)", kName) |
| 2114 |
case valueTypeUint: |
| 2115 |
x.linef("%s := strconv.AppendUint(z.DecScratchArrayBuffer()[:0], r.DecodeUint64(), 10)", kName) |
| 2116 |
case valueTypeFloat: |
| 2117 |
x.linef("%s := strconv.AppendFloat(z.DecScratchArrayBuffer()[:0], r.DecodeFloat64(), 'f', -1, 64)", kName) |
| 2118 |
default: |
| 2119 |
x.linef("%s := r.DecodeStringAsBytes()", kName) |
| 2120 |
} |
| 2121 |
|
| 2122 |
x.line("z.DecReadMapElemValue()") |
| 2123 |
x.decStructMapSwitch(kName, varname, rtid, t) |
| 2124 |
|
| 2125 |
x.line("} // end for " + tpfx + "j" + i) |
| 2126 |
} |
| 2127 |
|
| 2128 |
func (x *genRunner) decStructArray(varname, lenvarname, breakString string, rtid uintptr, t reflect.Type) { |
| 2129 |
tpfx := genTempVarPfx |
| 2130 |
i := x.varsfx() |
| 2131 |
ti := x.ti.get(rtid, t) |
| 2132 |
tisfi := ti.sfi.source() |
| 2133 |
x.linef("var %sj%s int", tpfx, i) |
| 2134 |
x.linef("var %sb%s bool", tpfx, i) |
| 2135 |
x.linef("var %shl%s bool = %s >= 0", tpfx, i, lenvarname) |
| 2136 |
if !genDecStructArrayInlineLoopCheck { |
| 2137 |
x.linef("var %sfn%s = func() bool { ", tpfx, i) |
| 2138 |
x.linef("%sj%s++; if %shl%s { %sb%s = %sj%s > %s } else { %sb%s = z.DecCheckBreak() };", |
| 2139 |
tpfx, i, tpfx, i, tpfx, i, |
| 2140 |
tpfx, i, lenvarname, tpfx, i) |
| 2141 |
x.linef("if %sb%s { z.DecReadArrayEnd(); return true }; return false", tpfx, i) |
| 2142 |
x.linef("} // end func %sfn%s", tpfx, i) |
| 2143 |
} |
| 2144 |
var newbuf, nilbuf genBuf |
| 2145 |
for _, si := range tisfi { |
| 2146 |
if genDecStructArrayInlineLoopCheck { |
| 2147 |
x.linef("%sj%s++; if %shl%s { %sb%s = %sj%s > %s } else { %sb%s = z.DecCheckBreak() }", |
| 2148 |
tpfx, i, tpfx, i, tpfx, i, |
| 2149 |
tpfx, i, lenvarname, tpfx, i) |
| 2150 |
x.linef("if %sb%s { z.DecReadArrayEnd(); %s }", tpfx, i, breakString) |
| 2151 |
} else { |
| 2152 |
x.linef("if %sfn%s() { %s }", tpfx, i, breakString) |
| 2153 |
} |
| 2154 |
x.line("z.DecReadArrayElem()") |
| 2155 |
newbuf.reset() |
| 2156 |
nilbuf.reset() |
| 2157 |
varname3, t2 := x.decVarInitPtr(varname, "", t, si, &newbuf, &nilbuf) |
| 2158 |
if len(newbuf.buf) > 0 { |
| 2159 |
x.linef("if r.TryNil() { %s } else { %s", nilbuf.buf, newbuf.buf) |
| 2160 |
} |
| 2161 |
x.decVarMain(varname3, x.varsfx(), t2.Type, false) |
| 2162 |
if len(newbuf.buf) > 0 { |
| 2163 |
x.line("}") |
| 2164 |
} |
| 2165 |
} |
| 2166 |
|
| 2167 |
x.line("for {") |
| 2168 |
x.linef("%sj%s++; if %shl%s { %sb%s = %sj%s > %s } else { %sb%s = z.DecCheckBreak() }", |
| 2169 |
tpfx, i, tpfx, i, tpfx, i, |
| 2170 |
tpfx, i, lenvarname, tpfx, i) |
| 2171 |
x.linef("if %sb%s { break }", tpfx, i) |
| 2172 |
x.line("z.DecReadArrayElem()") |
| 2173 |
x.linef(`z.DecStructFieldNotFound(%sj%s - 1, "")`, tpfx, i) |
| 2174 |
x.line("}") |
| 2175 |
} |
| 2176 |
|
| 2177 |
func (x *genRunner) decStruct(varname string, rtid uintptr, t reflect.Type) { |
| 2178 |
|
| 2179 |
i := x.varsfx() |
| 2180 |
x.linef("%sct%s := r.ContainerType()", genTempVarPfx, i) |
| 2181 |
x.linef("if %sct%s == codecSelferValueTypeNil%s {", genTempVarPfx, i, x.xs) |
| 2182 |
x.linef("*(%s) = %s{}", varname, x.genTypeName(t)) |
| 2183 |
x.linef("} else if %sct%s == codecSelferValueTypeMap%s {", genTempVarPfx, i, x.xs) |
| 2184 |
x.line(genTempVarPfx + "l" + i + " := z.DecReadMapStart()") |
| 2185 |
x.linef("if %sl%s == 0 {", genTempVarPfx, i) |
| 2186 |
if genUseOneFunctionForDecStructMap { |
| 2187 |
x.line("} else { ") |
| 2188 |
x.linef("%s.codecDecodeSelfFromMap(%sl%s, d)", varname, genTempVarPfx, i) |
| 2189 |
} else { |
| 2190 |
x.line("} else if " + genTempVarPfx + "l" + i + " > 0 { ") |
| 2191 |
x.line(varname + ".codecDecodeSelfFromMapLenPrefix(" + genTempVarPfx + "l" + i + ", d)") |
| 2192 |
x.line("} else {") |
| 2193 |
x.line(varname + ".codecDecodeSelfFromMapCheckBreak(" + genTempVarPfx + "l" + i + ", d)") |
| 2194 |
} |
| 2195 |
x.line("}") |
| 2196 |
x.line("z.DecReadMapEnd()") |
| 2197 |
|
| 2198 |
|
| 2199 |
x.linef("} else if %sct%s == codecSelferValueTypeArray%s {", genTempVarPfx, i, x.xs) |
| 2200 |
x.line(genTempVarPfx + "l" + i + " := z.DecReadArrayStart()") |
| 2201 |
x.linef("if %sl%s != 0 {", genTempVarPfx, i) |
| 2202 |
x.linef("%s.codecDecodeSelfFromArray(%sl%s, d)", varname, genTempVarPfx, i) |
| 2203 |
x.line("}") |
| 2204 |
x.line("z.DecReadArrayEnd()") |
| 2205 |
|
| 2206 |
x.line("} else { ") |
| 2207 |
x.line("panic(errCodecSelferOnlyMapOrArrayEncodeToStruct" + x.xs + ")") |
| 2208 |
x.line("} ") |
| 2209 |
} |
| 2210 |
|
| 2211 |
|
| 2212 |
|
| 2213 |
type fastpathGenV struct { |
| 2214 |
|
| 2215 |
MapKey string |
| 2216 |
Elem string |
| 2217 |
Primitive string |
| 2218 |
Size int |
| 2219 |
NoCanonical bool |
| 2220 |
} |
| 2221 |
|
| 2222 |
func (x *genRunner) newFastpathGenV(t reflect.Type) (v fastpathGenV) { |
| 2223 |
v.NoCanonical = !genFastpathCanonical |
| 2224 |
switch t.Kind() { |
| 2225 |
case reflect.Slice, reflect.Array: |
| 2226 |
te := t.Elem() |
| 2227 |
v.Elem = x.genTypeName(te) |
| 2228 |
v.Size = int(te.Size()) |
| 2229 |
case reflect.Map: |
| 2230 |
te := t.Elem() |
| 2231 |
tk := t.Key() |
| 2232 |
v.Elem = x.genTypeName(te) |
| 2233 |
v.MapKey = x.genTypeName(tk) |
| 2234 |
v.Size = int(te.Size() + tk.Size()) |
| 2235 |
default: |
| 2236 |
halt.onerror(errGenUnexpectedTypeFastpath) |
| 2237 |
} |
| 2238 |
return |
| 2239 |
} |
| 2240 |
|
| 2241 |
func (x *fastpathGenV) MethodNamePfx(prefix string, prim bool) string { |
| 2242 |
var name []byte |
| 2243 |
if prefix != "" { |
| 2244 |
name = append(name, prefix...) |
| 2245 |
} |
| 2246 |
if prim { |
| 2247 |
name = append(name, genTitleCaseName(x.Primitive)...) |
| 2248 |
} else { |
| 2249 |
if x.MapKey == "" { |
| 2250 |
name = append(name, "Slice"...) |
| 2251 |
} else { |
| 2252 |
name = append(name, "Map"...) |
| 2253 |
name = append(name, genTitleCaseName(x.MapKey)...) |
| 2254 |
} |
| 2255 |
name = append(name, genTitleCaseName(x.Elem)...) |
| 2256 |
} |
| 2257 |
return string(name) |
| 2258 |
} |
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
func genImportPath(t reflect.Type) (s string) { |
| 2266 |
s = t.PkgPath() |
| 2267 |
if genCheckVendor { |
| 2268 |
|
| 2269 |
s = genStripVendor(s) |
| 2270 |
} |
| 2271 |
return |
| 2272 |
} |
| 2273 |
|
| 2274 |
|
| 2275 |
func genGoIdentifier(s string, checkFirstChar bool) string { |
| 2276 |
b := make([]byte, 0, len(s)) |
| 2277 |
t := make([]byte, 4) |
| 2278 |
var n int |
| 2279 |
for i, r := range s { |
| 2280 |
if checkFirstChar && i == 0 && !unicode.IsLetter(r) { |
| 2281 |
b = append(b, '_') |
| 2282 |
} |
| 2283 |
|
| 2284 |
if unicode.IsLetter(r) || unicode.IsDigit(r) { |
| 2285 |
n = utf8.EncodeRune(t, r) |
| 2286 |
b = append(b, t[:n]...) |
| 2287 |
} else { |
| 2288 |
b = append(b, '_') |
| 2289 |
} |
| 2290 |
} |
| 2291 |
return string(b) |
| 2292 |
} |
| 2293 |
|
| 2294 |
func genNonPtr(t reflect.Type) reflect.Type { |
| 2295 |
for t.Kind() == reflect.Ptr { |
| 2296 |
t = t.Elem() |
| 2297 |
} |
| 2298 |
return t |
| 2299 |
} |
| 2300 |
|
| 2301 |
func genFastpathUnderlying(t reflect.Type, rtid uintptr, ti *typeInfo) (tu reflect.Type, rtidu uintptr) { |
| 2302 |
tu = t |
| 2303 |
rtidu = rtid |
| 2304 |
if ti.flagHasPkgPath { |
| 2305 |
tu = ti.fastpathUnderlying |
| 2306 |
rtidu = rt2id(tu) |
| 2307 |
} |
| 2308 |
return |
| 2309 |
} |
| 2310 |
|
| 2311 |
func genTitleCaseName(s string) string { |
| 2312 |
switch s { |
| 2313 |
case "interface{}", "interface {}": |
| 2314 |
return "Intf" |
| 2315 |
case "[]byte", "[]uint8", "bytes": |
| 2316 |
return "Bytes" |
| 2317 |
default: |
| 2318 |
return strings.ToUpper(s[0:1]) + s[1:] |
| 2319 |
} |
| 2320 |
} |
| 2321 |
|
| 2322 |
func genMethodNameT(t reflect.Type, tRef reflect.Type) (n string) { |
| 2323 |
var ptrPfx string |
| 2324 |
for t.Kind() == reflect.Ptr { |
| 2325 |
ptrPfx += "Ptrto" |
| 2326 |
t = t.Elem() |
| 2327 |
} |
| 2328 |
tstr := t.String() |
| 2329 |
if tn := t.Name(); tn != "" { |
| 2330 |
if tRef != nil && genImportPath(t) == genImportPath(tRef) { |
| 2331 |
return ptrPfx + tn |
| 2332 |
} else { |
| 2333 |
if genQNameRegex.MatchString(tstr) { |
| 2334 |
return ptrPfx + strings.Replace(tstr, ".", "_", 1000) |
| 2335 |
} else { |
| 2336 |
return ptrPfx + genCustomTypeName(tstr) |
| 2337 |
} |
| 2338 |
} |
| 2339 |
} |
| 2340 |
switch t.Kind() { |
| 2341 |
case reflect.Map: |
| 2342 |
return ptrPfx + "Map" + genMethodNameT(t.Key(), tRef) + genMethodNameT(t.Elem(), tRef) |
| 2343 |
case reflect.Slice: |
| 2344 |
return ptrPfx + "Slice" + genMethodNameT(t.Elem(), tRef) |
| 2345 |
case reflect.Array: |
| 2346 |
return ptrPfx + "Array" + strconv.FormatInt(int64(t.Len()), 10) + genMethodNameT(t.Elem(), tRef) |
| 2347 |
case reflect.Chan: |
| 2348 |
var cx string |
| 2349 |
switch t.ChanDir() { |
| 2350 |
case reflect.SendDir: |
| 2351 |
cx = "ChanSend" |
| 2352 |
case reflect.RecvDir: |
| 2353 |
cx = "ChanRecv" |
| 2354 |
default: |
| 2355 |
cx = "Chan" |
| 2356 |
} |
| 2357 |
return ptrPfx + cx + genMethodNameT(t.Elem(), tRef) |
| 2358 |
default: |
| 2359 |
if t == intfTyp { |
| 2360 |
return ptrPfx + "Interface" |
| 2361 |
} else { |
| 2362 |
if tRef != nil && genImportPath(t) == genImportPath(tRef) { |
| 2363 |
if t.Name() != "" { |
| 2364 |
return ptrPfx + t.Name() |
| 2365 |
} else { |
| 2366 |
return ptrPfx + genCustomTypeName(tstr) |
| 2367 |
} |
| 2368 |
} else { |
| 2369 |
|
| 2370 |
|
| 2371 |
|
| 2372 |
if t.Name() != "" && genQNameRegex.MatchString(tstr) { |
| 2373 |
return ptrPfx + strings.Replace(tstr, ".", "_", 1000) |
| 2374 |
} else { |
| 2375 |
return ptrPfx + genCustomTypeName(tstr) |
| 2376 |
} |
| 2377 |
} |
| 2378 |
} |
| 2379 |
} |
| 2380 |
} |
| 2381 |
|
| 2382 |
|
| 2383 |
|
| 2384 |
func genCustomTypeName(tstr string) string { |
| 2385 |
len2 := genBase64enc.EncodedLen(len(tstr)) |
| 2386 |
bufx := make([]byte, len2) |
| 2387 |
genBase64enc.Encode(bufx, []byte(tstr)) |
| 2388 |
for i := len2 - 1; i >= 0; i-- { |
| 2389 |
if bufx[i] == '=' { |
| 2390 |
len2-- |
| 2391 |
} else { |
| 2392 |
break |
| 2393 |
} |
| 2394 |
} |
| 2395 |
return string(bufx[:len2]) |
| 2396 |
} |
| 2397 |
|
| 2398 |
func genIsImmutable(t reflect.Type) (v bool) { |
| 2399 |
return scalarBitset.isset(byte(t.Kind())) |
| 2400 |
} |
| 2401 |
|
| 2402 |
type genInternal struct { |
| 2403 |
Version int |
| 2404 |
Values []fastpathGenV |
| 2405 |
Formats []string |
| 2406 |
} |
| 2407 |
|
| 2408 |
func (x genInternal) FastpathLen() (l int) { |
| 2409 |
for _, v := range x.Values { |
| 2410 |
|
| 2411 |
if v.Primitive == "" { |
| 2412 |
l++ |
| 2413 |
} |
| 2414 |
} |
| 2415 |
return |
| 2416 |
} |
| 2417 |
|
| 2418 |
func genInternalZeroValue(s string) string { |
| 2419 |
switch s { |
| 2420 |
case "interface{}", "interface {}": |
| 2421 |
return "nil" |
| 2422 |
case "[]byte", "[]uint8", "bytes": |
| 2423 |
return "nil" |
| 2424 |
case "bool": |
| 2425 |
return "false" |
| 2426 |
case "string": |
| 2427 |
return `""` |
| 2428 |
default: |
| 2429 |
return "0" |
| 2430 |
} |
| 2431 |
} |
| 2432 |
|
| 2433 |
var genInternalNonZeroValueIdx [6]uint64 |
| 2434 |
var genInternalNonZeroValueStrs = [...][6]string{ |
| 2435 |
{`"string-is-an-interface-1"`, "true", `"some-string-1"`, `[]byte("some-string-1")`, "11.1", "111"}, |
| 2436 |
{`"string-is-an-interface-2"`, "false", `"some-string-2"`, `[]byte("some-string-2")`, "22.2", "77"}, |
| 2437 |
{`"string-is-an-interface-3"`, "true", `"some-string-3"`, `[]byte("some-string-3")`, "33.3e3", "127"}, |
| 2438 |
} |
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
func genInternalNonZeroValue(s string) string { |
| 2443 |
var i int |
| 2444 |
switch s { |
| 2445 |
case "interface{}", "interface {}": |
| 2446 |
i = 0 |
| 2447 |
case "bool": |
| 2448 |
i = 1 |
| 2449 |
case "string": |
| 2450 |
i = 2 |
| 2451 |
case "bytes", "[]byte", "[]uint8": |
| 2452 |
i = 3 |
| 2453 |
case "float32", "float64", "float", "double", "complex", "complex64", "complex128": |
| 2454 |
i = 4 |
| 2455 |
default: |
| 2456 |
i = 5 |
| 2457 |
} |
| 2458 |
genInternalNonZeroValueIdx[i]++ |
| 2459 |
idx := genInternalNonZeroValueIdx[i] |
| 2460 |
slen := uint64(len(genInternalNonZeroValueStrs)) |
| 2461 |
return genInternalNonZeroValueStrs[idx%slen][i] |
| 2462 |
} |
| 2463 |
|
| 2464 |
|
| 2465 |
func genInternalEncCommandAsString(s string, vname string) string { |
| 2466 |
switch s { |
| 2467 |
case "uint64": |
| 2468 |
return "e.e.EncodeUint(" + vname + ")" |
| 2469 |
case "uint", "uint8", "uint16", "uint32": |
| 2470 |
return "e.e.EncodeUint(uint64(" + vname + "))" |
| 2471 |
case "int64": |
| 2472 |
return "e.e.EncodeInt(" + vname + ")" |
| 2473 |
case "int", "int8", "int16", "int32": |
| 2474 |
return "e.e.EncodeInt(int64(" + vname + "))" |
| 2475 |
case "[]byte", "[]uint8", "bytes": |
| 2476 |
return "e.e.EncodeStringBytesRaw(" + vname + ")" |
| 2477 |
case "string": |
| 2478 |
return "e.e.EncodeString(" + vname + ")" |
| 2479 |
case "float32": |
| 2480 |
return "e.e.EncodeFloat32(" + vname + ")" |
| 2481 |
case "float64": |
| 2482 |
return "e.e.EncodeFloat64(" + vname + ")" |
| 2483 |
case "bool": |
| 2484 |
return "e.e.EncodeBool(" + vname + ")" |
| 2485 |
|
| 2486 |
|
| 2487 |
default: |
| 2488 |
return "e.encode(" + vname + ")" |
| 2489 |
} |
| 2490 |
} |
| 2491 |
|
| 2492 |
|
| 2493 |
func genInternalDecCommandAsString(s string, mapkey bool) string { |
| 2494 |
switch s { |
| 2495 |
case "uint": |
| 2496 |
return "uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))" |
| 2497 |
case "uint8": |
| 2498 |
return "uint8(chkOvf.UintV(d.d.DecodeUint64(), 8))" |
| 2499 |
case "uint16": |
| 2500 |
return "uint16(chkOvf.UintV(d.d.DecodeUint64(), 16))" |
| 2501 |
case "uint32": |
| 2502 |
return "uint32(chkOvf.UintV(d.d.DecodeUint64(), 32))" |
| 2503 |
case "uint64": |
| 2504 |
return "d.d.DecodeUint64()" |
| 2505 |
case "uintptr": |
| 2506 |
return "uintptr(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))" |
| 2507 |
case "int": |
| 2508 |
return "int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))" |
| 2509 |
case "int8": |
| 2510 |
return "int8(chkOvf.IntV(d.d.DecodeInt64(), 8))" |
| 2511 |
case "int16": |
| 2512 |
return "int16(chkOvf.IntV(d.d.DecodeInt64(), 16))" |
| 2513 |
case "int32": |
| 2514 |
return "int32(chkOvf.IntV(d.d.DecodeInt64(), 32))" |
| 2515 |
case "int64": |
| 2516 |
return "d.d.DecodeInt64()" |
| 2517 |
|
| 2518 |
case "string": |
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
|
| 2523 |
return "d.stringZC(d.d.DecodeStringAsBytes())" |
| 2524 |
case "[]byte", "[]uint8", "bytes": |
| 2525 |
return "d.d.DecodeBytes([]byte{})" |
| 2526 |
case "float32": |
| 2527 |
return "float32(d.decodeFloat32())" |
| 2528 |
case "float64": |
| 2529 |
return "d.d.DecodeFloat64()" |
| 2530 |
case "complex64": |
| 2531 |
return "complex(d.decodeFloat32(), 0)" |
| 2532 |
case "complex128": |
| 2533 |
return "complex(d.d.DecodeFloat64(), 0)" |
| 2534 |
case "bool": |
| 2535 |
return "d.d.DecodeBool()" |
| 2536 |
default: |
| 2537 |
halt.onerror(errors.New("gen internal: unknown type for decode: " + s)) |
| 2538 |
} |
| 2539 |
return "" |
| 2540 |
} |
| 2541 |
|
| 2542 |
|
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 |
|
| 2547 |
|
| 2548 |
|
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
|
| 2562 |
|
| 2563 |
|
| 2564 |
|
| 2565 |
|
| 2566 |
|
| 2567 |
func genInternalSortType(s string, elem bool) string { |
| 2568 |
if elem { |
| 2569 |
return s |
| 2570 |
} |
| 2571 |
return s + "Slice" |
| 2572 |
} |
| 2573 |
|
| 2574 |
|
| 2575 |
func genStripVendor(s string) string { |
| 2576 |
|
| 2577 |
|
| 2578 |
const vendorStart = "vendor/" |
| 2579 |
const vendorInline = "/vendor/" |
| 2580 |
if i := strings.LastIndex(s, vendorInline); i >= 0 { |
| 2581 |
s = s[i+len(vendorInline):] |
| 2582 |
} else if strings.HasPrefix(s, vendorStart) { |
| 2583 |
s = s[len(vendorStart):] |
| 2584 |
} |
| 2585 |
return s |
| 2586 |
} |
| 2587 |
|
| 2588 |
|
| 2589 |
var genInternalV = genInternal{Version: genVersion} |
| 2590 |
var genInternalTmplFuncs template.FuncMap |
| 2591 |
var genInternalOnce sync.Once |
| 2592 |
|
| 2593 |
func genInternalInit() { |
| 2594 |
wordSizeBytes := int(intBitsize) / 8 |
| 2595 |
|
| 2596 |
typesizes := map[string]int{ |
| 2597 |
"interface{}": 2 * wordSizeBytes, |
| 2598 |
"string": 2 * wordSizeBytes, |
| 2599 |
"[]byte": 3 * wordSizeBytes, |
| 2600 |
"uint": 1 * wordSizeBytes, |
| 2601 |
"uint8": 1, |
| 2602 |
"uint16": 2, |
| 2603 |
"uint32": 4, |
| 2604 |
"uint64": 8, |
| 2605 |
"uintptr": 1 * wordSizeBytes, |
| 2606 |
"int": 1 * wordSizeBytes, |
| 2607 |
"int8": 1, |
| 2608 |
"int16": 2, |
| 2609 |
"int32": 4, |
| 2610 |
"int64": 8, |
| 2611 |
"float32": 4, |
| 2612 |
"float64": 8, |
| 2613 |
"complex64": 8, |
| 2614 |
"complex128": 16, |
| 2615 |
"bool": 1, |
| 2616 |
} |
| 2617 |
|
| 2618 |
|
| 2619 |
|
| 2620 |
|
| 2621 |
var types = [...]string{ |
| 2622 |
"interface{}", |
| 2623 |
"string", |
| 2624 |
"[]byte", |
| 2625 |
"float32", |
| 2626 |
"float64", |
| 2627 |
"uint", |
| 2628 |
"uint8", |
| 2629 |
"uint16", |
| 2630 |
"uint32", |
| 2631 |
"uint64", |
| 2632 |
"uintptr", |
| 2633 |
"int", |
| 2634 |
"int8", |
| 2635 |
"int16", |
| 2636 |
"int32", |
| 2637 |
"int64", |
| 2638 |
"bool", |
| 2639 |
} |
| 2640 |
|
| 2641 |
var primitivetypes, slicetypes, mapkeytypes, mapvaltypes []string |
| 2642 |
|
| 2643 |
primitivetypes = types[:] |
| 2644 |
slicetypes = types[:] |
| 2645 |
mapkeytypes = types[:] |
| 2646 |
mapvaltypes = types[:] |
| 2647 |
|
| 2648 |
if genFastpathTrimTypes { |
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
slicetypes = genInternalFastpathSliceTypes() |
| 2653 |
mapkeytypes = genInternalFastpathMapKeyTypes() |
| 2654 |
mapvaltypes = genInternalFastpathMapValueTypes() |
| 2655 |
} |
| 2656 |
|
| 2657 |
|
| 2658 |
|
| 2659 |
|
| 2660 |
|
| 2661 |
|
| 2662 |
|
| 2663 |
|
| 2664 |
|
| 2665 |
|
| 2666 |
|
| 2667 |
|
| 2668 |
var gt = genInternal{Version: genVersion, Formats: genFormats} |
| 2669 |
|
| 2670 |
|
| 2671 |
|
| 2672 |
for _, s := range primitivetypes { |
| 2673 |
gt.Values = append(gt.Values, |
| 2674 |
fastpathGenV{Primitive: s, Size: typesizes[s], NoCanonical: !genFastpathCanonical}) |
| 2675 |
} |
| 2676 |
for _, s := range slicetypes { |
| 2677 |
|
| 2678 |
|
| 2679 |
|
| 2680 |
gt.Values = append(gt.Values, |
| 2681 |
fastpathGenV{Elem: s, Size: typesizes[s], NoCanonical: !genFastpathCanonical}) |
| 2682 |
} |
| 2683 |
for _, s := range mapkeytypes { |
| 2684 |
|
| 2685 |
|
| 2686 |
|
| 2687 |
|
| 2688 |
for _, ms := range mapvaltypes { |
| 2689 |
gt.Values = append(gt.Values, |
| 2690 |
fastpathGenV{MapKey: s, Elem: ms, Size: typesizes[s] + typesizes[ms], NoCanonical: !genFastpathCanonical}) |
| 2691 |
} |
| 2692 |
} |
| 2693 |
|
| 2694 |
funcs := make(template.FuncMap) |
| 2695 |
|
| 2696 |
funcs["encmd"] = genInternalEncCommandAsString |
| 2697 |
funcs["decmd"] = genInternalDecCommandAsString |
| 2698 |
funcs["zerocmd"] = genInternalZeroValue |
| 2699 |
funcs["nonzerocmd"] = genInternalNonZeroValue |
| 2700 |
funcs["hasprefix"] = strings.HasPrefix |
| 2701 |
funcs["sorttype"] = genInternalSortType |
| 2702 |
|
| 2703 |
genInternalV = gt |
| 2704 |
genInternalTmplFuncs = funcs |
| 2705 |
} |
| 2706 |
|
| 2707 |
|
| 2708 |
func genInternalGoFile(r io.Reader, w io.Writer) (err error) { |
| 2709 |
genInternalOnce.Do(genInternalInit) |
| 2710 |
|
| 2711 |
gt := genInternalV |
| 2712 |
|
| 2713 |
t := template.New("").Funcs(genInternalTmplFuncs) |
| 2714 |
|
| 2715 |
tmplstr, err := ioutil.ReadAll(r) |
| 2716 |
if err != nil { |
| 2717 |
return |
| 2718 |
} |
| 2719 |
|
| 2720 |
if t, err = t.Parse(string(tmplstr)); err != nil { |
| 2721 |
return |
| 2722 |
} |
| 2723 |
|
| 2724 |
var out bytes.Buffer |
| 2725 |
err = t.Execute(&out, gt) |
| 2726 |
if err != nil { |
| 2727 |
return |
| 2728 |
} |
| 2729 |
|
| 2730 |
bout, err := format.Source(out.Bytes()) |
| 2731 |
if err != nil { |
| 2732 |
w.Write(out.Bytes()) |
| 2733 |
|
| 2734 |
return |
| 2735 |
} |
| 2736 |
w.Write(bout) |
| 2737 |
return |
| 2738 |
} |
| 2739 |
|
| 2740 |
func genInternalFastpathSliceTypes() []string { |
| 2741 |
return []string{ |
| 2742 |
"interface{}", |
| 2743 |
"string", |
| 2744 |
"[]byte", |
| 2745 |
"float32", |
| 2746 |
"float64", |
| 2747 |
|
| 2748 |
|
| 2749 |
"uint8", |
| 2750 |
|
| 2751 |
|
| 2752 |
"uint64", |
| 2753 |
|
| 2754 |
"int", |
| 2755 |
|
| 2756 |
|
| 2757 |
"int32", |
| 2758 |
"int64", |
| 2759 |
"bool", |
| 2760 |
} |
| 2761 |
} |
| 2762 |
|
| 2763 |
func genInternalFastpathMapKeyTypes() []string { |
| 2764 |
return []string{ |
| 2765 |
|
| 2766 |
"string", |
| 2767 |
|
| 2768 |
|
| 2769 |
|
| 2770 |
|
| 2771 |
"uint8", |
| 2772 |
|
| 2773 |
|
| 2774 |
"uint64", |
| 2775 |
|
| 2776 |
"int", |
| 2777 |
|
| 2778 |
|
| 2779 |
"int32", |
| 2780 |
|
| 2781 |
|
| 2782 |
} |
| 2783 |
} |
| 2784 |
|
| 2785 |
func genInternalFastpathMapValueTypes() []string { |
| 2786 |
return []string{ |
| 2787 |
"interface{}", |
| 2788 |
"string", |
| 2789 |
"[]byte", |
| 2790 |
|
| 2791 |
"uint8", |
| 2792 |
|
| 2793 |
|
| 2794 |
"uint64", |
| 2795 |
|
| 2796 |
"int", |
| 2797 |
|
| 2798 |
|
| 2799 |
"int32", |
| 2800 |
|
| 2801 |
|
| 2802 |
"float64", |
| 2803 |
"bool", |
| 2804 |
} |
| 2805 |
} |
| 2806 |
|
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
|
| 2815 |
|
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
|
| 2820 |
func genInternalSortableTypes() []string { |
| 2821 |
return genInternalFastpathMapKeyTypes() |
| 2822 |
} |
| 2823 |
|
| 2824 |
|
| 2825 |
|
| 2826 |
|
| 2827 |
|
| 2828 |
|
| 2829 |
func genInternalSortablePlusTypes() []string { |
| 2830 |
return []string{ |
| 2831 |
"string", |
| 2832 |
"float64", |
| 2833 |
"uint64", |
| 2834 |
|
| 2835 |
"int64", |
| 2836 |
|
| 2837 |
"time", |
| 2838 |
"bytes", |
| 2839 |
} |
| 2840 |
} |
| 2841 |
|
| 2842 |
func genTypeForShortName(s string) string { |
| 2843 |
switch s { |
| 2844 |
case "time": |
| 2845 |
return "time.Time" |
| 2846 |
case "bytes": |
| 2847 |
return "[]byte" |
| 2848 |
} |
| 2849 |
return s |
| 2850 |
} |
| 2851 |
|
| 2852 |
func genArgs(args ...interface{}) map[string]interface{} { |
| 2853 |
m := make(map[string]interface{}, len(args)/2) |
| 2854 |
for i := 0; i < len(args); { |
| 2855 |
m[args[i].(string)] = args[i+1] |
| 2856 |
i += 2 |
| 2857 |
} |
| 2858 |
return m |
| 2859 |
} |
| 2860 |
|
| 2861 |
func genEndsWith(s0 string, sn ...string) bool { |
| 2862 |
for _, s := range sn { |
| 2863 |
if strings.HasSuffix(s0, s) { |
| 2864 |
return true |
| 2865 |
} |
| 2866 |
} |
| 2867 |
return false |
| 2868 |
} |
| 2869 |
|
| 2870 |
func genCheckErr(err error) { |
| 2871 |
halt.onerror(err) |
| 2872 |
} |
| 2873 |
|
| 2874 |
func genRunSortTmpl2Go(fnameIn, fnameOut string) { |
| 2875 |
var err error |
| 2876 |
|
| 2877 |
funcs := make(template.FuncMap) |
| 2878 |
funcs["sortables"] = genInternalSortableTypes |
| 2879 |
funcs["sortablesplus"] = genInternalSortablePlusTypes |
| 2880 |
funcs["tshort"] = genTypeForShortName |
| 2881 |
funcs["endswith"] = genEndsWith |
| 2882 |
funcs["args"] = genArgs |
| 2883 |
|
| 2884 |
t := template.New("").Funcs(funcs) |
| 2885 |
fin, err := os.Open(fnameIn) |
| 2886 |
genCheckErr(err) |
| 2887 |
defer fin.Close() |
| 2888 |
fout, err := os.Create(fnameOut) |
| 2889 |
genCheckErr(err) |
| 2890 |
defer fout.Close() |
| 2891 |
tmplstr, err := ioutil.ReadAll(fin) |
| 2892 |
genCheckErr(err) |
| 2893 |
t, err = t.Parse(string(tmplstr)) |
| 2894 |
genCheckErr(err) |
| 2895 |
var out bytes.Buffer |
| 2896 |
err = t.Execute(&out, 0) |
| 2897 |
genCheckErr(err) |
| 2898 |
bout, err := format.Source(out.Bytes()) |
| 2899 |
if err != nil { |
| 2900 |
fout.Write(out.Bytes()) |
| 2901 |
} |
| 2902 |
genCheckErr(err) |
| 2903 |
|
| 2904 |
_, err = fout.Write(bout) |
| 2905 |
genCheckErr(err) |
| 2906 |
} |
| 2907 |
|
| 2908 |
func genRunTmpl2Go(fnameIn, fnameOut string) { |
| 2909 |
|
| 2910 |
fin, err := os.Open(fnameIn) |
| 2911 |
genCheckErr(err) |
| 2912 |
defer fin.Close() |
| 2913 |
fout, err := os.Create(fnameOut) |
| 2914 |
genCheckErr(err) |
| 2915 |
defer fout.Close() |
| 2916 |
err = genInternalGoFile(fin, fout) |
| 2917 |
genCheckErr(err) |
| 2918 |
} |
| 2919 |
|
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 2923 |
func (path *structFieldInfoPathNode) root() *structFieldInfoPathNode { |
| 2924 |
TOP: |
| 2925 |
if path.parent != nil { |
| 2926 |
path = path.parent |
| 2927 |
goto TOP |
| 2928 |
} |
| 2929 |
return path |
| 2930 |
} |
| 2931 |
|
| 2932 |
func (path *structFieldInfoPathNode) fullpath() (p []*structFieldInfoPathNode) { |
| 2933 |
|
| 2934 |
|
| 2935 |
d := path.depth() |
| 2936 |
p = make([]*structFieldInfoPathNode, d) |
| 2937 |
for d--; d >= 0; d-- { |
| 2938 |
p[d] = path |
| 2939 |
path = path.parent |
| 2940 |
} |
| 2941 |
return |
| 2942 |
} |