Skip to content

Commit

Permalink
Merge pull request #475 from michavie/codemetadata-codec
Browse files Browse the repository at this point in the history
CodeMetadata Codec
  • Loading branch information
andreibancioiu committed Sep 2, 2024
2 parents 3901f3f + 5b3ee51 commit e8389da
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/smartcontracts/codec/codemetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CodeMetadata } from "../codeMetadata";
import { CodeMetadataValue } from "../typesystem/codeMetadata";

export class CodeMetadataCodec {
decodeNested(buffer: Buffer): [CodeMetadataValue, number] {
const codeMetadata = CodeMetadata.fromBuffer(buffer);

return [new CodeMetadataValue(codeMetadata), length];
}

decodeTopLevel(buffer: Buffer): CodeMetadataValue {
const codeMetadata = CodeMetadata.fromBuffer(buffer);

return new CodeMetadataValue(codeMetadata);
}

encodeNested(codeMetadata: CodeMetadataValue): Buffer {
return codeMetadata.valueOf().toBuffer();
}

encodeTopLevel(codeMetadata: CodeMetadataValue): Buffer {
return codeMetadata.valueOf().toBuffer();
}
}
8 changes: 8 additions & 0 deletions src/smartcontracts/codec/primitive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CodeMetadataValue,
PrimitiveType,
PrimitiveValue,
onPrimitiveTypeSelect,
Expand All @@ -19,6 +20,7 @@ import { H256Value } from "../typesystem/h256";
import { BytesBinaryCodec } from "./bytes";
import { TokenIdentifierCodec } from "./tokenIdentifier";
import { TokenIdentifierValue } from "../typesystem/tokenIdentifier";
import { CodeMetadataCodec } from "./codemetadata";
import { NothingCodec } from "./nothing";
import { StringBinaryCodec } from "./string";

Expand All @@ -32,6 +34,7 @@ export class PrimitiveBinaryCodec {
private readonly bytesCodec: BytesBinaryCodec;
private readonly stringCodec: StringBinaryCodec;
private readonly tokenIdentifierCodec: TokenIdentifierCodec;
private readonly codeMetadataCodec: CodeMetadataCodec;
private readonly nothingCodec: NothingCodec;

constructor(binaryCodec: BinaryCodec) {
Expand All @@ -44,6 +47,7 @@ export class PrimitiveBinaryCodec {
this.bytesCodec = new BytesBinaryCodec();
this.stringCodec = new StringBinaryCodec();
this.tokenIdentifierCodec = new TokenIdentifierCodec();
this.codeMetadataCodec = new CodeMetadataCodec();
this.nothingCodec = new NothingCodec();
}

Expand All @@ -56,6 +60,7 @@ export class PrimitiveBinaryCodec {
onString: () => this.stringCodec.decodeNested(buffer),
onH256: () => this.h256Codec.decodeNested(buffer),
onTokenIndetifier: () => this.tokenIdentifierCodec.decodeNested(buffer),
onCodeMetadata: () => this.codeMetadataCodec.decodeNested(buffer),
onNothing: () => this.nothingCodec.decodeNested(),
});
}
Expand All @@ -69,6 +74,7 @@ export class PrimitiveBinaryCodec {
onString: () => this.stringCodec.decodeTopLevel(buffer),
onH256: () => this.h256Codec.decodeTopLevel(buffer),
onTokenIndetifier: () => this.tokenIdentifierCodec.decodeTopLevel(buffer),
onCodeMetadata: () => this.codeMetadataCodec.decodeTopLevel(buffer),
onNothing: () => this.nothingCodec.decodeTopLevel(),
});
}
Expand All @@ -82,6 +88,7 @@ export class PrimitiveBinaryCodec {
onString: () => this.stringCodec.encodeNested(<StringValue>value),
onH256: () => this.h256Codec.encodeNested(<H256Value>value),
onTypeIdentifier: () => this.tokenIdentifierCodec.encodeNested(<TokenIdentifierValue>value),
onCodeMetadata: () => this.codeMetadataCodec.encodeNested(<CodeMetadataValue>value),
onNothing: () => this.nothingCodec.encodeNested(),
});
}
Expand All @@ -95,6 +102,7 @@ export class PrimitiveBinaryCodec {
onString: () => this.stringCodec.encodeTopLevel(<StringValue>value),
onH256: () => this.h256Codec.encodeTopLevel(<H256Value>value),
onTypeIdentifier: () => this.tokenIdentifierCodec.encodeTopLevel(<TokenIdentifierValue>value),
onCodeMetadata: () => this.codeMetadataCodec.encodeTopLevel(<CodeMetadataValue>value),
onNothing: () => this.nothingCodec.encodeTopLevel(),
});
}
Expand Down
1 change: 1 addition & 0 deletions src/smartcontracts/typesystem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./address";
export * from "./algebraic";
export * from "./boolean";
export * from "./bytes";
export * from "./codeMetadata";
export * from "./composite";
export * from "./endpoint";
export * from "./enum";
Expand Down
20 changes: 14 additions & 6 deletions src/smartcontracts/typesystem/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import * as errors from "../../errors";
import { AddressType, AddressValue } from "./address";
import { BooleanType, BooleanValue } from "./boolean";
import { BytesType, BytesValue } from "./bytes";
import { CodeMetadataType, CodeMetadataValue } from "./codeMetadata";
import { EnumType, EnumValue } from "./enum";
import { OptionType, OptionValue, List, ListType } from "./generic";
import { List, ListType, OptionType, OptionValue } from "./generic";
import { ArrayVec, ArrayVecType } from "./genericArray";
import { H256Type, H256Value } from "./h256";
import { NumericalType, NumericalValue } from "./numerical";
import { NothingType, NothingValue } from "./nothing";
import { NumericalType, NumericalValue } from "./numerical";
import { StringType, StringValue } from "./string";
import { Struct, StructType } from "./struct";
import { TokenIdentifierType, TokenIdentifierValue } from "./tokenIdentifier";
import { Tuple, TupleType } from "./tuple";
import { Type, PrimitiveType, PrimitiveValue } from "./types";
import { ArrayVec, ArrayVecType } from "./genericArray";
import { TypedValue } from "./types";
import { StringType, StringValue } from "./string";
import { PrimitiveType, PrimitiveValue, Type, TypedValue } from "./types";

// TODO: Extend functionality or rename wrt. restricted / reduced functionality (not all types are handled: composite, variadic).
export function onTypeSelect<TResult>(
Expand Down Expand Up @@ -110,6 +110,7 @@ export function onPrimitiveValueSelect<TResult>(
onString: () => TResult;
onH256: () => TResult;
onTypeIdentifier: () => TResult;
onCodeMetadata: () => TResult;
onNothing: () => TResult;
onOther?: () => TResult;
},
Expand All @@ -135,6 +136,9 @@ export function onPrimitiveValueSelect<TResult>(
if (value.hasExactClass(TokenIdentifierValue.ClassName)) {
return selectors.onTypeIdentifier();
}
if (value.hasExactClass(CodeMetadataValue.ClassName)) {
return selectors.onCodeMetadata();
}
if (value.hasExactClass(NothingValue.ClassName)) {
return selectors.onNothing();
}
Expand All @@ -155,6 +159,7 @@ export function onPrimitiveTypeSelect<TResult>(
onString: () => TResult;
onH256: () => TResult;
onTokenIndetifier: () => TResult;
onCodeMetadata: () => TResult;
onNothing: () => TResult;
onOther?: () => TResult;
},
Expand All @@ -180,6 +185,9 @@ export function onPrimitiveTypeSelect<TResult>(
if (type.hasExactClass(TokenIdentifierType.ClassName)) {
return selectors.onTokenIndetifier();
}
if (type.hasExactClass(CodeMetadataType.ClassName)) {
return selectors.onCodeMetadata();
}
if (type.hasExactClass(NothingType.ClassName)) {
return selectors.onNothing();
}
Expand Down

0 comments on commit e8389da

Please sign in to comment.