Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm run format #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/karabo-hash/bin_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function readSchema(parser: BinaryDecoder): Types.Schema {
if (parser.pos - op !== l) {
throw new Error('Parser failed parsing Schema');
}
return new Types.Schema({name: name, hash: hsh });
return new Types.Schema({ name: name, hash: hsh });
}

function parserUndefined(parser: BinaryDecoder, type: number): Types.Hash {
Expand Down Expand Up @@ -172,7 +172,7 @@ const parsers = [
readVectorChar, // ByteArray = 37
];

function getParser(typeNumber: number) : ParserFunction {
function getParser(typeNumber: number): ParserFunction {
const parser = parsers[typeNumber];
if (typeof parser != 'function') {
throw new Error('failed to find parser for type ' + typeNumber);
Expand Down
60 changes: 30 additions & 30 deletions src/karabo-hash/bin_writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function encodeString(parser: BinaryEncoder, data: string): ArrayBuffer {
function encodeVectorHash(parser: BinaryEncoder, data: Types.HashValue[]): ArrayBuffer {
// Writes each string in the vector to its own ArrayBuffer
let stringsLength = 0;
const strBuffers = data.map((element : Types.HashValue) => {
const strBuffers = data.map((element: Types.HashValue) => {
const strBuffer = parser.encodeHashValue(element);
stringsLength += strBuffer.byteLength;
return strBuffer.slice(0);
Expand All @@ -117,7 +117,7 @@ function encodeVectorHash(parser: BinaryEncoder, data: Types.HashValue[]): Array
}

function makeVectorEncoder<Type>(elementEncoder: any) {
return (parser: BinaryEncoder, data: Type[]) : ArrayBuffer => {
return (parser: BinaryEncoder, data: Type[]): ArrayBuffer => {
const sizeBuffer = new ArrayBuffer(4);
new DataView(sizeBuffer).setUint32(0, data.length, true);
const slice_ = [sizeBuffer];
Expand Down Expand Up @@ -165,61 +165,61 @@ class BinaryEncoder {
if (value instanceof Types.Bool) {
return encodeBoolean(this, value.value_);
} else if (value instanceof Types.VectorBool) {
return makeVectorEncoder(encodeBoolean)(this, value.value_);
return makeVectorEncoder(encodeBoolean)(this, value.value_);
} else if (value instanceof Types.Char) {
return encodeChar(this, value.value_);
return encodeChar(this, value.value_);
} else if (value instanceof Types.VectorChar) {
return makeVectorEncoder(encodeChar)(this, Array.from(value.value_));
return makeVectorEncoder(encodeChar)(this, Array.from(value.value_));
} else if (value instanceof Types.Int8) {
return encodeInt8(this, value.value_);
return encodeInt8(this, value.value_);
} else if (value instanceof Types.VectorInt8) {
return makeVectorEncoder(encodeInt8)(this, value.value_);
return makeVectorEncoder(encodeInt8)(this, value.value_);
} else if (value instanceof Types.UInt8) {
return encodeUInt8(this, value.value_);
return encodeUInt8(this, value.value_);
} else if (value instanceof Types.VectorUInt8) {
return makeVectorEncoder(encodeUInt8)(this, value.value_);
return makeVectorEncoder(encodeUInt8)(this, value.value_);
} else if (value instanceof Types.Int16) {
return encodeInt16(this, value.value_);
return encodeInt16(this, value.value_);
} else if (value instanceof Types.VectorInt16) {
return makeVectorEncoder(encodeInt16)(this, value.value_);
return makeVectorEncoder(encodeInt16)(this, value.value_);
} else if (value instanceof Types.UInt16) {
return encodeUInt16(this, value.value_);
return encodeUInt16(this, value.value_);
} else if (value instanceof Types.VectorUInt16) {
return makeVectorEncoder(encodeUInt16)(this, value.value_);
return makeVectorEncoder(encodeUInt16)(this, value.value_);
} else if (value instanceof Types.Int32) {
return encodeInt32(this, value.value_);
return encodeInt32(this, value.value_);
} else if (value instanceof Types.VectorInt32) {
return makeVectorEncoder(encodeInt32)(this, value.value_);
return makeVectorEncoder(encodeInt32)(this, value.value_);
} else if (value instanceof Types.UInt32) {
return encodeUInt32(this, value.value_);
return encodeUInt32(this, value.value_);
} else if (value instanceof Types.VectorUInt32) {
return makeVectorEncoder(encodeUInt32)(this, value.value_);
return makeVectorEncoder(encodeUInt32)(this, value.value_);
} else if (value instanceof Types.Int64) {
return encodeInt64(this, BigInt(value.value_));
return encodeInt64(this, BigInt(value.value_));
} else if (value instanceof Types.VectorInt64) {
return makeVectorEncoder(encodeInt64)(this, value.value_);
return makeVectorEncoder(encodeInt64)(this, value.value_);
} else if (value instanceof Types.UInt64) {
return encodeUInt64(this, BigInt(value.value_));
return encodeUInt64(this, BigInt(value.value_));
} else if (value instanceof Types.VectorUInt64) {
return makeVectorEncoder(encodeUInt64)(this, value.value_);
return makeVectorEncoder(encodeUInt64)(this, value.value_);
} else if (value instanceof Types.Float32) {
return encodeFloat32(this, value.value_);
return encodeFloat32(this, value.value_);
} else if (value instanceof Types.VectorFloat32) {
return makeVectorEncoder(encodeFloat32)(this, value.value_);
return makeVectorEncoder(encodeFloat32)(this, value.value_);
} else if (value instanceof Types.Float64) {
return encodeFloat64(this, value.value_);
return encodeFloat64(this, value.value_);
} else if (value instanceof Types.VectorFloat64) {
return makeVectorEncoder(encodeFloat64)(this, value.value_);
return makeVectorEncoder(encodeFloat64)(this, value.value_);
} else if (value instanceof Types.String) {
return encodeString(this, value.value_);
return encodeString(this, value.value_);
} else if (value instanceof Types.VectorString) {
return makeVectorEncoder(encodeString)(this, value.value_);
return makeVectorEncoder(encodeString)(this, value.value_);
} else if (value instanceof Types.Hash) {
return this.encodeHashValue(value.value_);
return this.encodeHashValue(value.value_);
} else if (value instanceof Types.VectorHash) {
return encodeVectorHash(this, value.value_);
return encodeVectorHash(this, value.value_);
} else if (value instanceof Types.Schema) {
return encodeSchema(this, value.value_);
return encodeSchema(this, value.value_);
}
throw new Error(`failed to encode type ${value.type_} ${JSON.stringify(value)}`);
}
Expand Down
66 changes: 35 additions & 31 deletions src/karabo-hash/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ export enum HashTypes {
ByteArray = 37,
}

type ValueTypes = number|
number[]|
string|
string[]|
bigint|
bigint[]|
HashValue|
HashValue[]|
SchemaValue|
boolean|
boolean[]|
Uint8Array;
type ValueTypes =
| number
| number[]
| string
| string[]
| bigint
| bigint[]
| HashValue
| HashValue[]
| SchemaValue
| boolean
| boolean[]
| Uint8Array;

export interface KaraboType {
type_: HashTypes;
Expand All @@ -69,51 +70,54 @@ export class HashValue {
[key: string]: HashNode;
}

export class Hash implements KaraboType{
export class Hash implements KaraboType {
readonly type_ = HashTypes.Hash;

constructor(public value_: HashValue) {}

getNode(path: string) {
const ret = path.split('.').reduce(
(currentNode : HashNode | undefined, subPath : string) => {
if (currentNode !== undefined &&
currentNode.value.value_ instanceof HashValue &&
subPath in currentNode.value.value_)
{
(currentNode: HashNode | undefined, subPath: string) => {
if (
currentNode !== undefined &&
currentNode.value.value_ instanceof HashValue &&
subPath in currentNode.value.value_
) {
return currentNode.value.value_[subPath];
}
return undefined;
}, {value: this, attrs:{}});
},
{ value: this, attrs: {} },
);
return ret;
}

getValue(path: string) : ValueTypes|undefined {
getValue(path: string): ValueTypes | undefined {
const node = this.getNode(path);
return (node !== undefined)? node.value.value_ : undefined;
return node !== undefined ? node.value.value_ : undefined;
}

getAttributes(path: string) : Attributes|undefined {
getAttributes(path: string): Attributes | undefined {
const node = this.getNode(path);
return (node !== undefined)? node.attrs : undefined;
return node !== undefined ? node.attrs : undefined;
}

getAttributeValue(path: string, attributeKey: string) : ValueTypes|undefined {
getAttributeValue(path: string, attributeKey: string): ValueTypes | undefined {
const attrs = this.getAttributes(path);
return (attrs !== undefined)? attrs[attributeKey].value_ : undefined;
return attrs !== undefined ? attrs[attributeKey].value_ : undefined;
}

public *items() : IterableIterator<[string, ValueTypes]> {
public *items(): IterableIterator<[string, ValueTypes]> {
for (const [key, node] of Object.entries(this.value_)) {
yield [key, node.value.value_];
}
}

public *iterall() : IterableIterator<[string, ValueTypes, {[key: string]: ValueTypes}]> {
public *iterall(): IterableIterator<[string, ValueTypes, { [key: string]: ValueTypes }]> {
for (const [key, node] of Object.entries(this.value_)) {
const simple_attrs : { [key: string]: any } = {};
const simple_attrs: { [key: string]: any } = {};
for (const [key, value] of Object.entries(node.attrs)) {
simple_attrs[key] = value.value_;
simple_attrs[key] = value.value_;
}
yield [key, node.value.value_, simple_attrs];
}
Expand All @@ -130,7 +134,7 @@ function getType_and_Value(value: any) {
} else if (Number.isInteger(value) && value < new UInt64(0n).max && value > new UInt64(0n).min) {
return [UInt64, value];
} else if (Number.isInteger(value)) {
throw new Error(`failed to identify karabo supported type for integer ${value}`)
throw new Error(`failed to identify karabo supported type for integer ${value}`);
} else {
return [Float64, value];
}
Expand Down Expand Up @@ -159,7 +163,7 @@ function getType_and_Value(value: any) {
return element;
}
return makeHashValue(element);
})
}),
];
default:
throw new Error(`cannot find suitable karabo type for ${sub_type} ${JSON.stringify(value)}`);
Expand Down