|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const assert_1 = require("../lib/assert"); |
| 4 | +const util_inspect_1 = require("../lib/util-inspect"); |
| 5 | +const abstract_1 = require("./abstract"); |
| 6 | +/** |
| 7 | + * A type storing a fixed value. |
| 8 | + * The value takes up no space in the value bytes, |
| 9 | + * only the type bytes. |
| 10 | + * Functions as an [[EnumType]] with only one value. |
| 11 | + * |
| 12 | + * Example: |
| 13 | + * ````javascript |
| 14 | + * //Encodes a JSON literal value |
| 15 | + * let type = new sb.ChoiceType([ |
| 16 | + * new sb.StructType({ |
| 17 | + * type: new sb.SingletonType({ |
| 18 | + * type: new sb.StringType, |
| 19 | + * value: 'boolean' |
| 20 | + * }), |
| 21 | + * value: new sb.BooleanType |
| 22 | + * }), |
| 23 | + * new sb.StructType({ |
| 24 | + * type: new sb.SingletonType({ |
| 25 | + * type: new sb.StringType, |
| 26 | + * value: 'number' |
| 27 | + * }), |
| 28 | + * value: new sb.DoubleType |
| 29 | + * }), |
| 30 | + * new sb.StructType({ |
| 31 | + * type: new sb.SingletonType({ |
| 32 | + * type: new sb.StringType, |
| 33 | + * value: 'string' |
| 34 | + * }), |
| 35 | + * value: new sb.StringType |
| 36 | + * }) |
| 37 | + * ]) |
| 38 | + * ```` |
| 39 | + * |
| 40 | + * @param E The type of the value |
| 41 | + */ |
| 42 | +class SingletonType extends abstract_1.default { |
| 43 | + /** |
| 44 | + * @param type The type that can serialize this type's value |
| 45 | + * @param value The value to serialize |
| 46 | + * @throws If `value` cannot be serialized by `type` |
| 47 | + */ |
| 48 | + constructor({ type, value }) { |
| 49 | + super(); |
| 50 | + assert_1.default.instanceOf(type, abstract_1.default); |
| 51 | + this.type = type; |
| 52 | + this.value = value; |
| 53 | + } |
| 54 | + static get _value() { |
| 55 | + return 0x59; |
| 56 | + } |
| 57 | + get singletonValueBuffer() { |
| 58 | + return this.cachedValueBuffer || |
| 59 | + (this.cachedValueBuffer = this.type.valueBuffer(this.value)); |
| 60 | + } |
| 61 | + addToBuffer(buffer) { |
| 62 | + /*istanbul ignore else*/ |
| 63 | + if (super.addToBuffer(buffer)) { |
| 64 | + this.type.addToBuffer(buffer); |
| 65 | + buffer.addAll(this.singletonValueBuffer); |
| 66 | + return true; |
| 67 | + } |
| 68 | + /*istanbul ignore next*/ |
| 69 | + return false; |
| 70 | + } |
| 71 | + /** |
| 72 | + * Appends value bytes to an [[AppendableBuffer]] according to the type |
| 73 | + * |
| 74 | + * Example: |
| 75 | + * ````javascript |
| 76 | + * type.writeValue(buffer, {type: 'boolean', value: true}) |
| 77 | + * type.writeValue(buffer, {type: 'string', value: 'abc'}) |
| 78 | + * ```` |
| 79 | + * @param buffer The buffer to which to append |
| 80 | + * @param value The value to write |
| 81 | + * @throws If the value doesn't match the type, e.g. `new sb.StringType().writeValue(buffer, 23)` |
| 82 | + */ |
| 83 | + writeValue(buffer, value) { |
| 84 | + this.isBuffer(buffer); |
| 85 | + try { |
| 86 | + assert_1.default.equal(this.type.valueBuffer(value), this.singletonValueBuffer); |
| 87 | + } |
| 88 | + catch (_a) { |
| 89 | + assert_1.default.fail(`Expected ${util_inspect_1.inspect(this.value)} but got ${util_inspect_1.inspect(value)}`); |
| 90 | + } |
| 91 | + } |
| 92 | + consumeValue() { |
| 93 | + return { value: this.value, length: 0 }; |
| 94 | + } |
| 95 | + equals(otherType) { |
| 96 | + if (!super.equals(otherType)) |
| 97 | + return false; |
| 98 | + const otherSingletonType = otherType; |
| 99 | + if (!this.type.equals(otherSingletonType.type)) |
| 100 | + return false; |
| 101 | + try { |
| 102 | + assert_1.default.equal(this.value, otherSingletonType.value); |
| 103 | + } |
| 104 | + catch (_a) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + return true; |
| 108 | + } |
| 109 | +} |
| 110 | +exports.default = SingletonType; |
0 commit comments