Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. module.exports = encoder;
  3. var Enum = require("./enum"),
  4. types = require("./types"),
  5. util = require("./util");
  6. /**
  7. * Generates a partial message type encoder.
  8. * @param {Codegen} gen Codegen instance
  9. * @param {Field} field Reflected field
  10. * @param {number} fieldIndex Field index
  11. * @param {string} ref Variable reference
  12. * @returns {Codegen} Codegen instance
  13. * @ignore
  14. */
  15. function genTypePartial(gen, field, fieldIndex, ref) {
  16. return field.resolvedType.group
  17. ? gen("types[%i].encode(%s,w.uint32(%i)).uint32(%i)", fieldIndex, ref, (field.id << 3 | 3) >>> 0, (field.id << 3 | 4) >>> 0)
  18. : gen("types[%i].encode(%s,w.uint32(%i).fork()).ldelim()", fieldIndex, ref, (field.id << 3 | 2) >>> 0);
  19. }
  20. /**
  21. * Generates an encoder specific to the specified message type.
  22. * @param {Type} mtype Message type
  23. * @returns {Codegen} Codegen instance
  24. */
  25. function encoder(mtype) {
  26. /* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
  27. var gen = util.codegen(["m", "w"], mtype.name + "$encode")
  28. ("if(!w)")
  29. ("w=Writer.create()");
  30. var i, ref;
  31. // "when a message is serialized its known fields should be written sequentially by field number"
  32. var fields = /* initializes */ mtype.fieldsArray.slice().sort(util.compareFieldsById);
  33. for (var i = 0; i < fields.length; ++i) {
  34. var field = fields[i].resolve(),
  35. index = mtype._fieldsArray.indexOf(field),
  36. type = field.resolvedType instanceof Enum ? "int32" : field.type,
  37. wireType = types.basic[type];
  38. ref = "m" + util.safeProp(field.name);
  39. // Map fields
  40. if (field.map) {
  41. gen
  42. ("if(%s!=null&&Object.hasOwnProperty.call(m,%j)){", ref, field.name) // !== undefined && !== null
  43. ("for(var ks=Object.keys(%s),i=0;i<ks.length;++i){", ref)
  44. ("w.uint32(%i).fork().uint32(%i).%s(ks[i])", (field.id << 3 | 2) >>> 0, 8 | types.mapKey[field.keyType], field.keyType);
  45. if (wireType === undefined) gen
  46. ("types[%i].encode(%s[ks[i]],w.uint32(18).fork()).ldelim().ldelim()", index, ref); // can't be groups
  47. else gen
  48. (".uint32(%i).%s(%s[ks[i]]).ldelim()", 16 | wireType, type, ref);
  49. gen
  50. ("}")
  51. ("}");
  52. // Repeated fields
  53. } else if (field.repeated) { gen
  54. ("if(%s!=null&&%s.length){", ref, ref); // !== undefined && !== null
  55. // Packed repeated
  56. if (field.packed && types.packed[type] !== undefined) { gen
  57. ("w.uint32(%i).fork()", (field.id << 3 | 2) >>> 0)
  58. ("for(var i=0;i<%s.length;++i)", ref)
  59. ("w.%s(%s[i])", type, ref)
  60. ("w.ldelim()");
  61. // Non-packed
  62. } else { gen
  63. ("for(var i=0;i<%s.length;++i)", ref);
  64. if (wireType === undefined)
  65. genTypePartial(gen, field, index, ref + "[i]");
  66. else gen
  67. ("w.uint32(%i).%s(%s[i])", (field.id << 3 | wireType) >>> 0, type, ref);
  68. } gen
  69. ("}");
  70. // Non-repeated
  71. } else {
  72. if (field.optional) gen
  73. ("if(%s!=null&&Object.hasOwnProperty.call(m,%j))", ref, field.name); // !== undefined && !== null
  74. if (wireType === undefined)
  75. genTypePartial(gen, field, index, ref);
  76. else gen
  77. ("w.uint32(%i).%s(%s)", (field.id << 3 | wireType) >>> 0, type, ref);
  78. }
  79. }
  80. return gen
  81. ("return w");
  82. /* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
  83. }