mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-19 13:13:06 +00:00
bulk code format fix (#8707)
This commit is contained in:
@@ -1,15 +1,32 @@
|
||||
import { fromByteWidth } from './bit-width-util.js'
|
||||
import { ValueType } from './value-type.js'
|
||||
import { isNumber, isIndirectNumber, isAVector, fixedTypedVectorElementSize, isFixedTypedVector, isTypedVector, typedVectorElementType, packedType, fixedTypedVectorElementType } from './value-type-util.js'
|
||||
import { indirect, keyForIndex, keyIndex, readFloat, readInt, readUInt } from './reference-util.js'
|
||||
import { fromUTF8Array } from './flexbuffers-util.js';
|
||||
import { BitWidth } from './bit-width.js';
|
||||
import {fromByteWidth} from './bit-width-util.js';
|
||||
import {BitWidth} from './bit-width.js';
|
||||
import {fromUTF8Array} from './flexbuffers-util.js';
|
||||
import {
|
||||
indirect,
|
||||
keyForIndex,
|
||||
keyIndex,
|
||||
readFloat,
|
||||
readInt,
|
||||
readUInt,
|
||||
} from './reference-util.js';
|
||||
import {
|
||||
fixedTypedVectorElementSize,
|
||||
fixedTypedVectorElementType,
|
||||
isAVector,
|
||||
isFixedTypedVector,
|
||||
isIndirectNumber,
|
||||
isNumber,
|
||||
isTypedVector,
|
||||
packedType,
|
||||
typedVectorElementType,
|
||||
} from './value-type-util.js';
|
||||
import {ValueType} from './value-type.js';
|
||||
|
||||
export function toReference(buffer: ArrayBuffer): Reference {
|
||||
const len = buffer.byteLength;
|
||||
|
||||
if (len < 3) {
|
||||
throw "Buffer needs to be bigger than 3";
|
||||
throw 'Buffer needs to be bigger than 3';
|
||||
}
|
||||
|
||||
const dataView = new DataView(buffer);
|
||||
@@ -18,34 +35,78 @@ export function toReference(buffer: ArrayBuffer): Reference {
|
||||
const parentWidth = fromByteWidth(byteWidth);
|
||||
const offset = len - byteWidth - 2;
|
||||
|
||||
return new Reference(dataView, offset, parentWidth, packedType, "/")
|
||||
return new Reference(dataView, offset, parentWidth, packedType, '/');
|
||||
}
|
||||
|
||||
function valueForIndexWithKey(index: number, key: string, dataView: DataView, offset: number, parentWidth: number, byteWidth: number, length: number, path: string): Reference {
|
||||
function valueForIndexWithKey(
|
||||
index: number,
|
||||
key: string,
|
||||
dataView: DataView,
|
||||
offset: number,
|
||||
parentWidth: number,
|
||||
byteWidth: number,
|
||||
length: number,
|
||||
path: string,
|
||||
): Reference {
|
||||
const _indirect = indirect(dataView, offset, parentWidth);
|
||||
const elementOffset = _indirect + index * byteWidth;
|
||||
const packedType = dataView.getUint8(_indirect + length * byteWidth + index);
|
||||
return new Reference(dataView, elementOffset, fromByteWidth(byteWidth), packedType, `${path}/${key}`)
|
||||
return new Reference(
|
||||
dataView,
|
||||
elementOffset,
|
||||
fromByteWidth(byteWidth),
|
||||
packedType,
|
||||
`${path}/${key}`,
|
||||
);
|
||||
}
|
||||
|
||||
export class Reference {
|
||||
private readonly byteWidth: number
|
||||
private readonly valueType: ValueType
|
||||
private _length = -1
|
||||
constructor(private dataView: DataView, private offset: number, private parentWidth: number, private packedType: ValueType, private path: string) {
|
||||
this.byteWidth = 1 << (packedType & 3)
|
||||
this.valueType = packedType >> 2
|
||||
private readonly byteWidth: number;
|
||||
private readonly valueType: ValueType;
|
||||
private _length = -1;
|
||||
constructor(
|
||||
private dataView: DataView,
|
||||
private offset: number,
|
||||
private parentWidth: number,
|
||||
private packedType: ValueType,
|
||||
private path: string,
|
||||
) {
|
||||
this.byteWidth = 1 << (packedType & 3);
|
||||
this.valueType = packedType >> 2;
|
||||
}
|
||||
|
||||
isNull(): boolean { return this.valueType === ValueType.NULL; }
|
||||
isNumber(): boolean { return isNumber(this.valueType) || isIndirectNumber(this.valueType); }
|
||||
isFloat(): boolean { return ValueType.FLOAT === this.valueType || ValueType.INDIRECT_FLOAT === this.valueType; }
|
||||
isInt(): boolean { return this.isNumber() && !this.isFloat(); }
|
||||
isString(): boolean { return ValueType.STRING === this.valueType || ValueType.KEY === this.valueType; }
|
||||
isBool(): boolean { return ValueType.BOOL === this.valueType; }
|
||||
isBlob(): boolean { return ValueType.BLOB === this.valueType; }
|
||||
isVector(): boolean { return isAVector(this.valueType); }
|
||||
isMap(): boolean { return ValueType.MAP === this.valueType; }
|
||||
isNull(): boolean {
|
||||
return this.valueType === ValueType.NULL;
|
||||
}
|
||||
isNumber(): boolean {
|
||||
return isNumber(this.valueType) || isIndirectNumber(this.valueType);
|
||||
}
|
||||
isFloat(): boolean {
|
||||
return (
|
||||
ValueType.FLOAT === this.valueType ||
|
||||
ValueType.INDIRECT_FLOAT === this.valueType
|
||||
);
|
||||
}
|
||||
isInt(): boolean {
|
||||
return this.isNumber() && !this.isFloat();
|
||||
}
|
||||
isString(): boolean {
|
||||
return (
|
||||
ValueType.STRING === this.valueType || ValueType.KEY === this.valueType
|
||||
);
|
||||
}
|
||||
isBool(): boolean {
|
||||
return ValueType.BOOL === this.valueType;
|
||||
}
|
||||
isBlob(): boolean {
|
||||
return ValueType.BLOB === this.valueType;
|
||||
}
|
||||
isVector(): boolean {
|
||||
return isAVector(this.valueType);
|
||||
}
|
||||
isMap(): boolean {
|
||||
return ValueType.MAP === this.valueType;
|
||||
}
|
||||
|
||||
boolValue(): boolean | null {
|
||||
if (this.isBool()) {
|
||||
@@ -62,10 +123,18 @@ export class Reference {
|
||||
return readUInt(this.dataView, this.offset, this.parentWidth);
|
||||
}
|
||||
if (this.valueType === ValueType.INDIRECT_INT) {
|
||||
return readInt(this.dataView, indirect(this.dataView, this.offset, this.parentWidth), fromByteWidth(this.byteWidth));
|
||||
return readInt(
|
||||
this.dataView,
|
||||
indirect(this.dataView, this.offset, this.parentWidth),
|
||||
fromByteWidth(this.byteWidth),
|
||||
);
|
||||
}
|
||||
if (this.valueType === ValueType.INDIRECT_UINT) {
|
||||
return readUInt(this.dataView, indirect(this.dataView, this.offset, this.parentWidth), fromByteWidth(this.byteWidth));
|
||||
return readUInt(
|
||||
this.dataView,
|
||||
indirect(this.dataView, this.offset, this.parentWidth),
|
||||
fromByteWidth(this.byteWidth),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -75,17 +144,28 @@ export class Reference {
|
||||
return readFloat(this.dataView, this.offset, this.parentWidth);
|
||||
}
|
||||
if (this.valueType === ValueType.INDIRECT_FLOAT) {
|
||||
return readFloat(this.dataView, indirect(this.dataView, this.offset, this.parentWidth), fromByteWidth(this.byteWidth));
|
||||
return readFloat(
|
||||
this.dataView,
|
||||
indirect(this.dataView, this.offset, this.parentWidth),
|
||||
fromByteWidth(this.byteWidth),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
numericValue(): number | bigint | null { return this.floatValue() || this.intValue()}
|
||||
numericValue(): number | bigint | null {
|
||||
return this.floatValue() || this.intValue();
|
||||
}
|
||||
|
||||
stringValue(): string | null {
|
||||
if (this.valueType === ValueType.STRING || this.valueType === ValueType.KEY) {
|
||||
if (
|
||||
this.valueType === ValueType.STRING ||
|
||||
this.valueType === ValueType.KEY
|
||||
) {
|
||||
const begin = indirect(this.dataView, this.offset, this.parentWidth);
|
||||
return fromUTF8Array(new Uint8Array(this.dataView.buffer, begin, this.length()));
|
||||
return fromUTF8Array(
|
||||
new Uint8Array(this.dataView.buffer, begin, this.length()),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -106,7 +186,9 @@ export class Reference {
|
||||
}
|
||||
const _indirect = indirect(this.dataView, this.offset, this.parentWidth);
|
||||
const elementOffset = _indirect + key * this.byteWidth;
|
||||
let _packedType = this.dataView.getUint8(_indirect + length * this.byteWidth + key);
|
||||
let _packedType = this.dataView.getUint8(
|
||||
_indirect + length * this.byteWidth + key,
|
||||
);
|
||||
if (isTypedVector(this.valueType)) {
|
||||
const _valueType = typedVectorElementType(this.valueType);
|
||||
_packedType = packedType(_valueType, BitWidth.WIDTH8);
|
||||
@@ -114,12 +196,34 @@ export class Reference {
|
||||
const _valueType = fixedTypedVectorElementType(this.valueType);
|
||||
_packedType = packedType(_valueType, BitWidth.WIDTH8);
|
||||
}
|
||||
return new Reference(this.dataView, elementOffset, fromByteWidth(this.byteWidth), _packedType, `${this.path}[${key}]`);
|
||||
return new Reference(
|
||||
this.dataView,
|
||||
elementOffset,
|
||||
fromByteWidth(this.byteWidth),
|
||||
_packedType,
|
||||
`${this.path}[${key}]`,
|
||||
);
|
||||
}
|
||||
if (typeof key === 'string') {
|
||||
const index = keyIndex(key, this.dataView, this.offset, this.parentWidth, this.byteWidth, length);
|
||||
const index = keyIndex(
|
||||
key,
|
||||
this.dataView,
|
||||
this.offset,
|
||||
this.parentWidth,
|
||||
this.byteWidth,
|
||||
length,
|
||||
);
|
||||
if (index !== null) {
|
||||
return valueForIndexWithKey(index, key, this.dataView, this.offset, this.parentWidth, this.byteWidth, length, this.path)
|
||||
return valueForIndexWithKey(
|
||||
index,
|
||||
key,
|
||||
this.dataView,
|
||||
this.offset,
|
||||
this.parentWidth,
|
||||
this.byteWidth,
|
||||
length,
|
||||
this.path,
|
||||
);
|
||||
}
|
||||
}
|
||||
throw `Key [${key}] is not applicable on ${this.path} of ${this.valueType}`;
|
||||
@@ -132,19 +236,33 @@ export class Reference {
|
||||
}
|
||||
if (isFixedTypedVector(this.valueType)) {
|
||||
this._length = fixedTypedVectorElementSize(this.valueType);
|
||||
} else if (this.valueType === ValueType.BLOB
|
||||
|| this.valueType === ValueType.MAP
|
||||
|| isAVector(this.valueType)) {
|
||||
this._length = readUInt(this.dataView, indirect(this.dataView, this.offset, this.parentWidth) - this.byteWidth, fromByteWidth(this.byteWidth)) as number
|
||||
} else if (
|
||||
this.valueType === ValueType.BLOB ||
|
||||
this.valueType === ValueType.MAP ||
|
||||
isAVector(this.valueType)
|
||||
) {
|
||||
this._length = readUInt(
|
||||
this.dataView,
|
||||
indirect(this.dataView, this.offset, this.parentWidth) - this.byteWidth,
|
||||
fromByteWidth(this.byteWidth),
|
||||
) as number;
|
||||
} else if (this.valueType === ValueType.NULL) {
|
||||
this._length = 0;
|
||||
} else if (this.valueType === ValueType.STRING) {
|
||||
const _indirect = indirect(this.dataView, this.offset, this.parentWidth);
|
||||
let sizeByteWidth = this.byteWidth;
|
||||
size = readUInt(this.dataView, _indirect - sizeByteWidth, fromByteWidth(this.byteWidth));
|
||||
size = readUInt(
|
||||
this.dataView,
|
||||
_indirect - sizeByteWidth,
|
||||
fromByteWidth(this.byteWidth),
|
||||
);
|
||||
while (this.dataView.getInt8(_indirect + (size as number)) !== 0) {
|
||||
sizeByteWidth <<= 1;
|
||||
size = readUInt(this.dataView, _indirect - sizeByteWidth, fromByteWidth(this.byteWidth));
|
||||
size = readUInt(
|
||||
this.dataView,
|
||||
_indirect - sizeByteWidth,
|
||||
fromByteWidth(this.byteWidth),
|
||||
);
|
||||
}
|
||||
this._length = size as number;
|
||||
} else if (this.valueType === ValueType.KEY) {
|
||||
@@ -172,8 +290,23 @@ export class Reference {
|
||||
if (this.isMap()) {
|
||||
const result: Record<string, unknown> = {};
|
||||
for (let i = 0; i < length; i++) {
|
||||
const key = keyForIndex(i, this.dataView, this.offset, this.parentWidth, this.byteWidth);
|
||||
result[key] = valueForIndexWithKey(i, key, this.dataView, this.offset, this.parentWidth, this.byteWidth, length, this.path).toObject();
|
||||
const key = keyForIndex(
|
||||
i,
|
||||
this.dataView,
|
||||
this.offset,
|
||||
this.parentWidth,
|
||||
this.byteWidth,
|
||||
);
|
||||
result[key] = valueForIndexWithKey(
|
||||
i,
|
||||
key,
|
||||
this.dataView,
|
||||
this.offset,
|
||||
this.parentWidth,
|
||||
this.byteWidth,
|
||||
length,
|
||||
this.path,
|
||||
).toObject();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user