field.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. "use strict";
  2. module.exports = Field;
  3. // extends ReflectionObject
  4. var ReflectionObject = require("./object");
  5. ((Field.prototype = Object.create(ReflectionObject.prototype)).constructor = Field).className = "Field";
  6. var Enum = require("./enum"),
  7. types = require("./types"),
  8. util = require("./util");
  9. var Type; // cyclic
  10. var ruleRe = /^required|optional|repeated$/;
  11. /**
  12. * Constructs a new message field instance. Note that {@link MapField|map fields} have their own class.
  13. * @name Field
  14. * @classdesc Reflected message field.
  15. * @extends FieldBase
  16. * @constructor
  17. * @param {string} name Unique name within its namespace
  18. * @param {number} id Unique id within its namespace
  19. * @param {string} type Value type
  20. * @param {string|Object.<string,*>} [rule="optional"] Field rule
  21. * @param {string|Object.<string,*>} [extend] Extended type if different from parent
  22. * @param {Object.<string,*>} [options] Declared options
  23. */
  24. /**
  25. * Constructs a field from a field descriptor.
  26. * @param {string} name Field name
  27. * @param {IField} json Field descriptor
  28. * @returns {Field} Created field
  29. * @throws {TypeError} If arguments are invalid
  30. */
  31. Field.fromJSON = function fromJSON(name, json) {
  32. return new Field(name, json.id, json.type, json.rule, json.extend, json.options, json.comment);
  33. };
  34. /**
  35. * Not an actual constructor. Use {@link Field} instead.
  36. * @classdesc Base class of all reflected message fields. This is not an actual class but here for the sake of having consistent type definitions.
  37. * @exports FieldBase
  38. * @extends ReflectionObject
  39. * @constructor
  40. * @param {string} name Unique name within its namespace
  41. * @param {number} id Unique id within its namespace
  42. * @param {string} type Value type
  43. * @param {string|Object.<string,*>} [rule="optional"] Field rule
  44. * @param {string|Object.<string,*>} [extend] Extended type if different from parent
  45. * @param {Object.<string,*>} [options] Declared options
  46. * @param {string} [comment] Comment associated with this field
  47. */
  48. function Field(name, id, type, rule, extend, options, comment) {
  49. if (util.isObject(rule)) {
  50. comment = extend;
  51. options = rule;
  52. rule = extend = undefined;
  53. } else if (util.isObject(extend)) {
  54. comment = options;
  55. options = extend;
  56. extend = undefined;
  57. }
  58. ReflectionObject.call(this, name, options);
  59. if (!util.isInteger(id) || id < 0)
  60. throw TypeError("id must be a non-negative integer");
  61. if (!util.isString(type))
  62. throw TypeError("type must be a string");
  63. if (rule !== undefined && !ruleRe.test(rule = rule.toString().toLowerCase()))
  64. throw TypeError("rule must be a string rule");
  65. if (extend !== undefined && !util.isString(extend))
  66. throw TypeError("extend must be a string");
  67. /**
  68. * Field rule, if any.
  69. * @type {string|undefined}
  70. */
  71. if (rule === "proto3_optional") {
  72. rule = "optional";
  73. }
  74. this.rule = rule && rule !== "optional" ? rule : undefined; // toJSON
  75. /**
  76. * Field type.
  77. * @type {string}
  78. */
  79. this.type = type; // toJSON
  80. /**
  81. * Unique field id.
  82. * @type {number}
  83. */
  84. this.id = id; // toJSON, marker
  85. /**
  86. * Extended type if different from parent.
  87. * @type {string|undefined}
  88. */
  89. this.extend = extend || undefined; // toJSON
  90. /**
  91. * Whether this field is required.
  92. * @type {boolean}
  93. */
  94. this.required = rule === "required";
  95. /**
  96. * Whether this field is optional.
  97. * @type {boolean}
  98. */
  99. this.optional = !this.required;
  100. /**
  101. * Whether this field is repeated.
  102. * @type {boolean}
  103. */
  104. this.repeated = rule === "repeated";
  105. /**
  106. * Whether this field is a map or not.
  107. * @type {boolean}
  108. */
  109. this.map = false;
  110. /**
  111. * Message this field belongs to.
  112. * @type {Type|null}
  113. */
  114. this.message = null;
  115. /**
  116. * OneOf this field belongs to, if any,
  117. * @type {OneOf|null}
  118. */
  119. this.partOf = null;
  120. /**
  121. * The field type's default value.
  122. * @type {*}
  123. */
  124. this.typeDefault = null;
  125. /**
  126. * The field's default value on prototypes.
  127. * @type {*}
  128. */
  129. this.defaultValue = null;
  130. /**
  131. * Whether this field's value should be treated as a long.
  132. * @type {boolean}
  133. */
  134. this.long = util.Long ? types.long[type] !== undefined : /* istanbul ignore next */ false;
  135. /**
  136. * Whether this field's value is a buffer.
  137. * @type {boolean}
  138. */
  139. this.bytes = type === "bytes";
  140. /**
  141. * Resolved type if not a basic type.
  142. * @type {Type|Enum|null}
  143. */
  144. this.resolvedType = null;
  145. /**
  146. * Sister-field within the extended type if a declaring extension field.
  147. * @type {Field|null}
  148. */
  149. this.extensionField = null;
  150. /**
  151. * Sister-field within the declaring namespace if an extended field.
  152. * @type {Field|null}
  153. */
  154. this.declaringField = null;
  155. /**
  156. * Internally remembers whether this field is packed.
  157. * @type {boolean|null}
  158. * @private
  159. */
  160. this._packed = null;
  161. /**
  162. * Comment for this field.
  163. * @type {string|null}
  164. */
  165. this.comment = comment;
  166. }
  167. /**
  168. * Determines whether this field is packed. Only relevant when repeated and working with proto2.
  169. * @name Field#packed
  170. * @type {boolean}
  171. * @readonly
  172. */
  173. Object.defineProperty(Field.prototype, "packed", {
  174. get: function() {
  175. // defaults to packed=true if not explicity set to false
  176. if (this._packed === null)
  177. this._packed = this.getOption("packed") !== false;
  178. return this._packed;
  179. }
  180. });
  181. /**
  182. * @override
  183. */
  184. Field.prototype.setOption = function setOption(name, value, ifNotSet) {
  185. if (name === "packed") // clear cached before setting
  186. this._packed = null;
  187. return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet);
  188. };
  189. /**
  190. * Field descriptor.
  191. * @interface IField
  192. * @property {string} [rule="optional"] Field rule
  193. * @property {string} type Field type
  194. * @property {number} id Field id
  195. * @property {Object.<string,*>} [options] Field options
  196. */
  197. /**
  198. * Extension field descriptor.
  199. * @interface IExtensionField
  200. * @extends IField
  201. * @property {string} extend Extended type
  202. */
  203. /**
  204. * Converts this field to a field descriptor.
  205. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  206. * @returns {IField} Field descriptor
  207. */
  208. Field.prototype.toJSON = function toJSON(toJSONOptions) {
  209. var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
  210. return util.toObject([
  211. "rule" , this.rule !== "optional" && this.rule || undefined,
  212. "type" , this.type,
  213. "id" , this.id,
  214. "extend" , this.extend,
  215. "options" , this.options,
  216. "comment" , keepComments ? this.comment : undefined
  217. ]);
  218. };
  219. /**
  220. * Resolves this field's type references.
  221. * @returns {Field} `this`
  222. * @throws {Error} If any reference cannot be resolved
  223. */
  224. Field.prototype.resolve = function resolve() {
  225. if (this.resolved)
  226. return this;
  227. if ((this.typeDefault = types.defaults[this.type]) === undefined) { // if not a basic type, resolve it
  228. this.resolvedType = (this.declaringField ? this.declaringField.parent : this.parent).lookupTypeOrEnum(this.type);
  229. if (this.resolvedType instanceof Type)
  230. this.typeDefault = null;
  231. else // instanceof Enum
  232. this.typeDefault = this.resolvedType.values[Object.keys(this.resolvedType.values)[0]]; // first defined
  233. } else if (this.options && this.options.proto3_optional) {
  234. // proto3 scalar value marked optional; should default to null
  235. this.typeDefault = null;
  236. }
  237. // use explicitly set default value if present
  238. if (this.options && this.options["default"] != null) {
  239. this.typeDefault = this.options["default"];
  240. if (this.resolvedType instanceof Enum && typeof this.typeDefault === "string")
  241. this.typeDefault = this.resolvedType.values[this.typeDefault];
  242. }
  243. // remove unnecessary options
  244. if (this.options) {
  245. if (this.options.packed === true || this.options.packed !== undefined && this.resolvedType && !(this.resolvedType instanceof Enum))
  246. delete this.options.packed;
  247. if (!Object.keys(this.options).length)
  248. this.options = undefined;
  249. }
  250. // convert to internal data type if necesssary
  251. if (this.long) {
  252. this.typeDefault = util.Long.fromNumber(this.typeDefault, this.type.charAt(0) === "u");
  253. /* istanbul ignore else */
  254. if (Object.freeze)
  255. Object.freeze(this.typeDefault); // long instances are meant to be immutable anyway (i.e. use small int cache that even requires it)
  256. } else if (this.bytes && typeof this.typeDefault === "string") {
  257. var buf;
  258. if (util.base64.test(this.typeDefault))
  259. util.base64.decode(this.typeDefault, buf = util.newBuffer(util.base64.length(this.typeDefault)), 0);
  260. else
  261. util.utf8.write(this.typeDefault, buf = util.newBuffer(util.utf8.length(this.typeDefault)), 0);
  262. this.typeDefault = buf;
  263. }
  264. // take special care of maps and repeated fields
  265. if (this.map)
  266. this.defaultValue = util.emptyObject;
  267. else if (this.repeated)
  268. this.defaultValue = util.emptyArray;
  269. else
  270. this.defaultValue = this.typeDefault;
  271. // ensure proper value on prototype
  272. if (this.parent instanceof Type)
  273. this.parent.ctor.prototype[this.name] = this.defaultValue;
  274. return ReflectionObject.prototype.resolve.call(this);
  275. };
  276. /**
  277. * Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript).
  278. * @typedef FieldDecorator
  279. * @type {function}
  280. * @param {Object} prototype Target prototype
  281. * @param {string} fieldName Field name
  282. * @returns {undefined}
  283. */
  284. /**
  285. * Field decorator (TypeScript).
  286. * @name Field.d
  287. * @function
  288. * @param {number} fieldId Field id
  289. * @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"string"|"bool"|"bytes"|Object} fieldType Field type
  290. * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
  291. * @param {T} [defaultValue] Default value
  292. * @returns {FieldDecorator} Decorator function
  293. * @template T extends number | number[] | Long | Long[] | string | string[] | boolean | boolean[] | Uint8Array | Uint8Array[] | Buffer | Buffer[]
  294. */
  295. Field.d = function decorateField(fieldId, fieldType, fieldRule, defaultValue) {
  296. // submessage: decorate the submessage and use its name as the type
  297. if (typeof fieldType === "function")
  298. fieldType = util.decorateType(fieldType).name;
  299. // enum reference: create a reflected copy of the enum and keep reuseing it
  300. else if (fieldType && typeof fieldType === "object")
  301. fieldType = util.decorateEnum(fieldType).name;
  302. return function fieldDecorator(prototype, fieldName) {
  303. util.decorateType(prototype.constructor)
  304. .add(new Field(fieldName, fieldId, fieldType, fieldRule, { "default": defaultValue }));
  305. };
  306. };
  307. /**
  308. * Field decorator (TypeScript).
  309. * @name Field.d
  310. * @function
  311. * @param {number} fieldId Field id
  312. * @param {Constructor<T>|string} fieldType Field type
  313. * @param {"optional"|"required"|"repeated"} [fieldRule="optional"] Field rule
  314. * @returns {FieldDecorator} Decorator function
  315. * @template T extends Message<T>
  316. * @variation 2
  317. */
  318. // like Field.d but without a default value
  319. // Sets up cyclic dependencies (called in index-light)
  320. Field._configure = function configure(Type_) {
  321. Type = Type_;
  322. };