James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 1 | // This library provides a few basic reflection utilities for Flatbuffers. |
| 2 | // Currently, this only really supports the level of reflection that would |
| 3 | // be necessary to convert a serialized flatbuffer to JSON using just a |
| 4 | // reflection.Schema flatbuffer describing the type. |
| 5 | // The current implementation is also not necessarily robust to invalidly |
| 6 | // constructed flatbuffers. |
| 7 | // See reflection_test_main.ts for sample usage. |
| 8 | |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 9 | import * as aos from 'org_frc971/aos/configuration_generated'; |
| 10 | import * as reflection from '../../../external/com_github_google_flatbuffers/reflection/reflection_generated'; |
| 11 | import {ByteBuffer} from 'flatbuffers'; |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 12 | |
| 13 | // Returns the size, in bytes, of the given type. For vectors/strings/etc. |
| 14 | // returns the size of the offset. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 15 | function typeSize(baseType: reflection.BaseType): number { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 16 | switch (baseType) { |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 17 | case reflection.BaseType.None: |
| 18 | case reflection.BaseType.UType: |
| 19 | case reflection.BaseType.Bool: |
| 20 | case reflection.BaseType.Byte: |
| 21 | case reflection.BaseType.UByte: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 22 | return 1; |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 23 | case reflection.BaseType.Short: |
| 24 | case reflection.BaseType.UShort: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 25 | return 2; |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 26 | case reflection.BaseType.Int: |
| 27 | case reflection.BaseType.UInt: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 28 | return 4; |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 29 | case reflection.BaseType.Long: |
| 30 | case reflection.BaseType.ULong: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 31 | return 8; |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 32 | case reflection.BaseType.Float: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 33 | return 4; |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 34 | case reflection.BaseType.Double: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 35 | return 8; |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 36 | case reflection.BaseType.String: |
| 37 | case reflection.BaseType.Vector: |
| 38 | case reflection.BaseType.Obj: |
| 39 | case reflection.BaseType.Union: |
| 40 | case reflection.BaseType.Array: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 41 | return 4; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Returns whether the given type is a scalar type. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 46 | function isScalar(baseType: reflection.BaseType): boolean { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 47 | switch (baseType) { |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 48 | case reflection.BaseType.UType: |
| 49 | case reflection.BaseType.Bool: |
| 50 | case reflection.BaseType.Byte: |
| 51 | case reflection.BaseType.UByte: |
| 52 | case reflection.BaseType.Short: |
| 53 | case reflection.BaseType.UShort: |
| 54 | case reflection.BaseType.Int: |
| 55 | case reflection.BaseType.UInt: |
| 56 | case reflection.BaseType.Long: |
| 57 | case reflection.BaseType.ULong: |
| 58 | case reflection.BaseType.Float: |
| 59 | case reflection.BaseType.Double: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 60 | return true; |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 61 | case reflection.BaseType.None: |
| 62 | case reflection.BaseType.String: |
| 63 | case reflection.BaseType.Vector: |
| 64 | case reflection.BaseType.Obj: |
| 65 | case reflection.BaseType.Union: |
| 66 | case reflection.BaseType.Array: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Returns whether the given type is integer or not. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 72 | function isInteger(baseType: reflection.BaseType): boolean { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 73 | switch (baseType) { |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 74 | case reflection.BaseType.UType: |
| 75 | case reflection.BaseType.Bool: |
| 76 | case reflection.BaseType.Byte: |
| 77 | case reflection.BaseType.UByte: |
| 78 | case reflection.BaseType.Short: |
| 79 | case reflection.BaseType.UShort: |
| 80 | case reflection.BaseType.Int: |
| 81 | case reflection.BaseType.UInt: |
| 82 | case reflection.BaseType.Long: |
| 83 | case reflection.BaseType.ULong: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 84 | return true; |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 85 | case reflection.BaseType.Float: |
| 86 | case reflection.BaseType.Double: |
| 87 | case reflection.BaseType.None: |
| 88 | case reflection.BaseType.String: |
| 89 | case reflection.BaseType.Vector: |
| 90 | case reflection.BaseType.Obj: |
| 91 | case reflection.BaseType.Union: |
| 92 | case reflection.BaseType.Array: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Returns whether the given type is a long--this is needed to know whether it |
| 98 | // can be represented by the normal javascript number (8-byte integers require a |
| 99 | // special type, since the native number type is an 8-byte double, which won't |
| 100 | // represent 8-byte integers to full precision). |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 101 | function isLong(baseType: reflection.BaseType): boolean { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 102 | return isInteger(baseType) && (typeSize(baseType) > 4); |
| 103 | } |
| 104 | |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 105 | // Stores the data associated with a Table within a given buffer. |
| 106 | export class Table { |
| 107 | // Wrapper to represent an object (Table or Struct) within a ByteBuffer. |
| 108 | // The ByteBuffer is the raw data associated with the object. |
| 109 | // typeIndex is an index into the schema object vector for the parser |
| 110 | // object that this is associated with. |
| 111 | // offset is the absolute location within the buffer where the root of the |
| 112 | // object is. |
| 113 | // Note that a given Table assumes that it is being used with a particular |
| 114 | // Schema object. |
| 115 | // External users should generally not be using this constructor directly. |
| 116 | constructor( |
| 117 | public readonly bb: ByteBuffer, |
| 118 | public readonly typeIndex: number, public readonly offset: number) {} |
| 119 | // Constructs a Table object for the root of a ByteBuffer--this assumes that |
| 120 | // the type of the Table is the root table of the Parser that you are using. |
| 121 | static getRootTable(bb: ByteBuffer): Table { |
| 122 | return new Table(bb, -1, bb.readInt32(bb.position()) + bb.position()); |
| 123 | } |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 124 | static getNamedTable( |
| 125 | bb: ByteBuffer, schema: reflection.Schema, type: string, |
| 126 | offset: number): Table { |
| 127 | for (let ii = 0; ii < schema.objectsLength(); ++ii) { |
| 128 | if (schema.objects(ii).name() == type) { |
| 129 | return new Table(bb, ii, offset); |
| 130 | } |
| 131 | } |
| 132 | throw new Error('Unable to find type ' + type + ' in schema.'); |
| 133 | } |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 134 | // Reads a scalar of a given type at a given offset. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 135 | readScalar(fieldType: reflection.BaseType, offset: number) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 136 | switch (fieldType) { |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 137 | case reflection.BaseType.UType: |
| 138 | case reflection.BaseType.Bool: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 139 | return this.bb.readUint8(offset); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 140 | case reflection.BaseType.Byte: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 141 | return this.bb.readInt8(offset); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 142 | case reflection.BaseType.UByte: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 143 | return this.bb.readUint8(offset); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 144 | case reflection.BaseType.Short: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 145 | return this.bb.readInt16(offset); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 146 | case reflection.BaseType.UShort: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 147 | return this.bb.readUint16(offset); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 148 | case reflection.BaseType.Int: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 149 | return this.bb.readInt32(offset); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 150 | case reflection.BaseType.UInt: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 151 | return this.bb.readUint32(offset); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 152 | case reflection.BaseType.Long: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 153 | return this.bb.readInt64(offset); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 154 | case reflection.BaseType.ULong: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 155 | return this.bb.readUint64(offset); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 156 | case reflection.BaseType.Float: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 157 | return this.bb.readFloat32(offset); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 158 | case reflection.BaseType.Double: |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 159 | return this.bb.readFloat64(offset); |
| 160 | } |
Philipp Schrader | 47445a0 | 2020-11-14 17:31:04 -0800 | [diff] [blame] | 161 | throw new Error('Unsupported message type ' + fieldType); |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 162 | } |
| 163 | }; |
| 164 | |
| 165 | // The Parser class uses a Schema to provide all the utilities required to |
| 166 | // parse flatbuffers that have a type that is the same as the root_type defined |
| 167 | // by the Schema. |
| 168 | // The classical usage would be to, e.g., be reading a channel with a type of |
| 169 | // "aos.FooBar". At startup, you would construct a Parser from the channel's |
| 170 | // Schema. When a message is received on the channel , you would then use |
| 171 | // Table.getRootTable() on the received buffer to construct the Table, and |
| 172 | // then access the members using the various methods of the Parser (or just |
| 173 | // convert the entire object to a javascript Object/JSON using toObject()). |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 174 | // There are three basic ways to access fields in a Table: |
| 175 | // 1) Call toObject(), which turns the entire table into a javascript object. |
| 176 | // This is not meant to be particularly fast, but is useful to, e.g., |
| 177 | // convert something to JSON, or as a debugging tool. |
| 178 | // 2) Use the read*Lambda() accessors: These return a function that lets you |
| 179 | // access the specified field given a table. This is used by the plotter |
| 180 | // to repeatedly access the same field on a bunch of tables of the same type, |
| 181 | // without having to redo all the reflection-related work on every access. |
| 182 | // 3) Use the read*() accessors: These just call the lambda returned by |
| 183 | // read*Lambda() for you, as a convenience. This is cleaner to use, but for |
| 184 | // repeated lookups on tables of the same type, this may be inefficient. |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 185 | export class Parser { |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 186 | constructor(private readonly schema: reflection.Schema) {} |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 187 | |
| 188 | // Parse a Table to a javascript object. This is can be used, e.g., to convert |
| 189 | // a flatbuffer Table to JSON. |
| 190 | // If readDefaults is set to true, then scalar fields will be filled out with |
| 191 | // their default values if not populated; if readDefaults is false and the |
| 192 | // field is not populated, the resulting object will not populate the field. |
| 193 | toObject(table: Table, readDefaults: boolean = false) { |
| 194 | const result = {}; |
| 195 | const schema = this.getType(table.typeIndex); |
| 196 | const numFields = schema.fieldsLength(); |
| 197 | for (let ii = 0; ii < numFields; ++ii) { |
| 198 | const field = schema.fields(ii); |
| 199 | const baseType = field.type().baseType(); |
| 200 | let fieldValue = null; |
| 201 | if (isScalar(baseType)) { |
| 202 | fieldValue = this.readScalar(table, field.name(), readDefaults); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 203 | } else if (baseType === reflection.BaseType.String) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 204 | fieldValue = this.readString(table, field.name()); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 205 | } else if (baseType === reflection.BaseType.Obj) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 206 | const subTable = this.readTable(table, field.name()); |
| 207 | if (subTable !== null) { |
| 208 | fieldValue = this.toObject(subTable, readDefaults); |
| 209 | } |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 210 | } else if (baseType === reflection.BaseType.Vector) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 211 | const elementType = field.type().element(); |
| 212 | if (isScalar(elementType)) { |
| 213 | fieldValue = this.readVectorOfScalars(table, field.name()); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 214 | } else if (elementType === reflection.BaseType.String) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 215 | fieldValue = this.readVectorOfStrings(table, field.name()); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 216 | } else if (elementType === reflection.BaseType.Obj) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 217 | const tables = this.readVectorOfTables(table, field.name()); |
| 218 | if (tables !== null) { |
| 219 | fieldValue = []; |
| 220 | for (const table of tables) { |
| 221 | fieldValue.push(this.toObject(table, readDefaults)); |
| 222 | } |
| 223 | } |
| 224 | } else { |
| 225 | throw new Error('Vectors of Unions and Arrays are not supported.'); |
| 226 | } |
| 227 | } else { |
| 228 | throw new Error( |
| 229 | 'Unions and Arrays are not supported in field ' + field.name()); |
| 230 | } |
| 231 | if (fieldValue !== null) { |
| 232 | result[field.name()] = fieldValue; |
| 233 | } |
| 234 | } |
| 235 | return result; |
| 236 | } |
| 237 | |
| 238 | // Returns the Object definition associated with the given type index. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 239 | getType(typeIndex: number): reflection.Object_ { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 240 | if (typeIndex === -1) { |
| 241 | return this.schema.rootTable(); |
| 242 | } |
| 243 | if (typeIndex < 0 || typeIndex > this.schema.objectsLength()) { |
| 244 | throw new Error("Type index out-of-range."); |
| 245 | } |
| 246 | return this.schema.objects(typeIndex); |
| 247 | } |
| 248 | |
| 249 | // Retrieves the Field schema for the given field name within a given |
| 250 | // type index. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 251 | getField(fieldName: string, typeIndex: number): reflection.Field { |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 252 | const schema: reflection.Object_ = this.getType(typeIndex); |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 253 | const numFields = schema.fieldsLength(); |
| 254 | for (let ii = 0; ii < numFields; ++ii) { |
| 255 | const field = schema.fields(ii); |
| 256 | const name = field.name(); |
| 257 | if (fieldName === name) { |
| 258 | return field; |
| 259 | } |
| 260 | } |
James Kuszmaul | c4ae11c | 2020-12-26 16:26:58 -0800 | [diff] [blame] | 261 | throw new Error( |
| 262 | 'Couldn\'t find field ' + fieldName + ' in object ' + schema.name() + |
| 263 | '.'); |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Reads a scalar with the given field name from a Table. If readDefaults |
| 267 | // is set to false and the field is unset, we will return null. If |
| 268 | // readDefaults is true and the field is unset, we will look-up the default |
| 269 | // value for the field and return that. |
| 270 | // For 64-bit fields, returns a flatbuffer Long rather than a standard number. |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 271 | readScalar(table: Table, fieldName: string, readDefaults: boolean = false): |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 272 | number|BigInt|null { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 273 | return this.readScalarLambda( |
| 274 | table.typeIndex, fieldName, readDefaults)(table); |
| 275 | } |
| 276 | // Like readScalar(), except that this returns an accessor for the specified |
| 277 | // field, rather than the value of the field itself. |
| 278 | // Note that the *Lambda() methods take a typeIndex instead of a Table, which |
| 279 | // can be obtained using table.typeIndex. |
| 280 | readScalarLambda( |
| 281 | typeIndex: number, fieldName: string, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 282 | readDefaults: boolean = false): (t: Table) => number | BigInt | null { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 283 | const field = this.getField(fieldName, typeIndex); |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 284 | const fieldType = field.type(); |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 285 | const isStruct = this.getType(typeIndex).isStruct(); |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 286 | if (!isScalar(fieldType.baseType())) { |
| 287 | throw new Error('Field ' + fieldName + ' is not a scalar type.'); |
| 288 | } |
| 289 | |
| 290 | if (isStruct) { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 291 | const baseType = fieldType.baseType(); |
| 292 | return (t: Table) => { |
| 293 | return t.readScalar(baseType, t.offset + field.offset()); |
| 294 | }; |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 295 | } |
| 296 | |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 297 | return (t: Table) => { |
| 298 | const offset = t.offset + t.bb.__offset(t.offset, field.offset()); |
| 299 | if (offset === t.offset) { |
| 300 | if (!readDefaults) { |
| 301 | return null; |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 302 | } |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 303 | if (isInteger(fieldType.baseType())) { |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 304 | return field.defaultInteger(); |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 305 | } else { |
| 306 | return field.defaultReal(); |
| 307 | } |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 308 | } |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 309 | return t.readScalar(fieldType.baseType(), offset); |
| 310 | }; |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 311 | } |
| 312 | // Reads a string with the given field name from the provided Table. |
| 313 | // If the field is unset, returns null. |
| 314 | readString(table: Table, fieldName: string): string|null { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 315 | return this.readStringLambda(table.typeIndex, fieldName)(table); |
| 316 | } |
| 317 | |
| 318 | readStringLambda(typeIndex: number, fieldName: string): |
| 319 | (t: Table) => string | null { |
| 320 | const field = this.getField(fieldName, typeIndex); |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 321 | const fieldType = field.type(); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 322 | if (fieldType.baseType() !== reflection.BaseType.String) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 323 | throw new Error('Field ' + fieldName + ' is not a string.'); |
| 324 | } |
| 325 | |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 326 | |
| 327 | return (t: Table) => { |
| 328 | const offsetToOffset = |
| 329 | t.offset + t.bb.__offset(t.offset, field.offset()); |
| 330 | if (offsetToOffset === t.offset) { |
| 331 | return null; |
| 332 | } |
| 333 | return t.bb.__string(offsetToOffset) as string; |
| 334 | }; |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 335 | } |
| 336 | // Reads a sub-message from the given Table. The sub-message may either be |
| 337 | // a struct or a Table. Returns null if the sub-message is not set. |
| 338 | readTable(table: Table, fieldName: string): Table|null { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 339 | return this.readTableLambda(table.typeIndex, fieldName)(table); |
| 340 | } |
| 341 | readTableLambda(typeIndex: number, fieldName: string): (t: Table) => Table|null { |
| 342 | const field = this.getField(fieldName, typeIndex); |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 343 | const fieldType = field.type(); |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 344 | const parentIsStruct = this.getType(typeIndex).isStruct(); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 345 | if (fieldType.baseType() !== reflection.BaseType.Obj) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 346 | throw new Error('Field ' + fieldName + ' is not an object type.'); |
| 347 | } |
| 348 | |
| 349 | if (parentIsStruct) { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 350 | return (t: Table) => { |
| 351 | return new Table(t.bb, fieldType.index(), t.offset + field.offset()); |
| 352 | }; |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | const elementIsStruct = this.getType(fieldType.index()).isStruct(); |
| 356 | |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 357 | return (table: Table) => { |
| 358 | const offsetToOffset = |
| 359 | table.offset + table.bb.__offset(table.offset, field.offset()); |
| 360 | if (offsetToOffset === table.offset) { |
| 361 | return null; |
| 362 | } |
| 363 | |
| 364 | const objectStart = elementIsStruct ? offsetToOffset : |
| 365 | table.bb.__indirect(offsetToOffset); |
| 366 | return new Table(table.bb, fieldType.index(), objectStart); |
| 367 | }; |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 368 | } |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 369 | // Reads a vector of scalars (like readScalar, may return a vector of BigInt's |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 370 | // instead). Also, will return null if the vector is not set. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 371 | readVectorOfScalars(table: Table, fieldName: string): number[]|BigInt[]|null { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 372 | return this.readVectorOfScalarsLambda(table.typeIndex, fieldName)(table); |
| 373 | } |
| 374 | |
| 375 | readVectorOfScalarsLambda(typeIndex: number, fieldName: string): |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 376 | (t: Table) => number[] | BigInt[] | null { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 377 | const field = this.getField(fieldName, typeIndex); |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 378 | const fieldType = field.type(); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 379 | if (fieldType.baseType() !== reflection.BaseType.Vector) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 380 | throw new Error('Field ' + fieldName + ' is not an vector.'); |
| 381 | } |
| 382 | if (!isScalar(fieldType.element())) { |
| 383 | throw new Error('Field ' + fieldName + ' is not an vector of scalars.'); |
| 384 | } |
| 385 | |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 386 | return (table: Table) => { |
| 387 | const offsetToOffset = |
| 388 | table.offset + table.bb.__offset(table.offset, field.offset()); |
| 389 | if (offsetToOffset === table.offset) { |
| 390 | return null; |
| 391 | } |
| 392 | const numElements = table.bb.__vector_len(offsetToOffset); |
| 393 | const result = []; |
| 394 | const baseOffset = table.bb.__vector(offsetToOffset); |
| 395 | const scalarSize = typeSize(fieldType.element()); |
| 396 | for (let ii = 0; ii < numElements; ++ii) { |
| 397 | result.push(table.readScalar( |
| 398 | fieldType.element(), baseOffset + scalarSize * ii)); |
| 399 | } |
| 400 | return result; |
| 401 | }; |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 402 | } |
| 403 | // Reads a vector of tables. Returns null if vector is not set. |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 404 | readVectorOfTables(table: Table, fieldName: string): Table[]|null { |
| 405 | return this.readVectorOfTablesLambda(table.typeIndex, fieldName)(table); |
| 406 | } |
| 407 | readVectorOfTablesLambda(typeIndex: number, fieldName: string): |
| 408 | (t: Table) => Table[] | null { |
| 409 | const field = this.getField(fieldName, typeIndex); |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 410 | const fieldType = field.type(); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 411 | if (fieldType.baseType() !== reflection.BaseType.Vector) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 412 | throw new Error('Field ' + fieldName + ' is not an vector.'); |
| 413 | } |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 414 | if (fieldType.element() !== reflection.BaseType.Obj) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 415 | throw new Error('Field ' + fieldName + ' is not an vector of objects.'); |
| 416 | } |
| 417 | |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 418 | const elementSchema = this.getType(fieldType.index()); |
| 419 | const elementIsStruct = elementSchema.isStruct(); |
| 420 | const elementSize = elementIsStruct ? elementSchema.bytesize() : |
| 421 | typeSize(fieldType.element()); |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 422 | |
| 423 | return (table: Table) => { |
| 424 | const offsetToOffset = |
| 425 | table.offset + table.bb.__offset(table.offset, field.offset()); |
| 426 | if (offsetToOffset === table.offset) { |
| 427 | return null; |
| 428 | } |
| 429 | const numElements = table.bb.__vector_len(offsetToOffset); |
| 430 | const result = []; |
| 431 | const baseOffset = table.bb.__vector(offsetToOffset); |
| 432 | for (let ii = 0; ii < numElements; ++ii) { |
| 433 | const elementOffset = baseOffset + elementSize * ii; |
| 434 | result.push(new Table( |
| 435 | table.bb, fieldType.index(), |
| 436 | elementIsStruct ? elementOffset : |
| 437 | table.bb.__indirect(elementOffset))); |
| 438 | } |
| 439 | return result; |
| 440 | }; |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 441 | } |
| 442 | // Reads a vector of strings. Returns null if not set. |
| 443 | readVectorOfStrings(table: Table, fieldName: string): string[]|null { |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 444 | return this.readVectorOfStringsLambda(table.typeIndex, fieldName)(table); |
| 445 | } |
| 446 | readVectorOfStringsLambda(typeIndex: number, fieldName: string): |
| 447 | (t: Table) => string[] | null { |
| 448 | const field = this.getField(fieldName, typeIndex); |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 449 | const fieldType = field.type(); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 450 | if (fieldType.baseType() !== reflection.BaseType.Vector) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 451 | throw new Error('Field ' + fieldName + ' is not an vector.'); |
| 452 | } |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 453 | if (fieldType.element() !== reflection.BaseType.String) { |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 454 | throw new Error('Field ' + fieldName + ' is not an vector of strings.'); |
| 455 | } |
| 456 | |
James Kuszmaul | f8355ff | 2021-12-19 22:08:45 -0800 | [diff] [blame] | 457 | return (table: Table) => { |
| 458 | const offsetToOffset = |
| 459 | table.offset + table.bb.__offset(table.offset, field.offset()); |
| 460 | if (offsetToOffset === table.offset) { |
| 461 | return null; |
| 462 | } |
| 463 | const numElements = table.bb.__vector_len(offsetToOffset); |
| 464 | const result = []; |
| 465 | const baseOffset = table.bb.__vector(offsetToOffset); |
| 466 | const offsetSize = typeSize(fieldType.element()); |
| 467 | for (let ii = 0; ii < numElements; ++ii) { |
| 468 | result.push(table.bb.__string(baseOffset + offsetSize * ii)); |
| 469 | } |
| 470 | return result; |
| 471 | }; |
James Kuszmaul | abb7713 | 2020-08-01 19:56:16 -0700 | [diff] [blame] | 472 | } |
| 473 | } |