Skip to content

Commit

Permalink
Add support for variable string serializers (#6)
Browse files Browse the repository at this point in the history
* Add support for variable string serializers

* Create sixty-nails-reflect.md

* Update sixty-nails-reflect.md
  • Loading branch information
lorisleiva authored Feb 15, 2023
1 parent 6a09e85 commit d28f4dc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .changeset/sixty-nails-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@metaplex-foundation/umi-core': patch
'@metaplex-foundation/umi-serializer-beet': patch
---

Add support for variable string serializers

There are now three ways to serialize/deserialize a string:

```ts
// With prefix.
umi.serializer.string().serialize('A');
// -> 0x0100000041

// Fixed.
umi.serializer.fixedString(8).serialize('A');
// -> 0x4100000000000000

// Variable.
umi.serializer.variableString().serialize('A');
// -> 0x41
```
17 changes: 16 additions & 1 deletion packages/umi-core/src/SerializerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export interface SerializerInterface {
) => Serializer<string>;

/**
* Creates serializer of fixed length strings.
* Creates a fixed-length serializer for strings.
*
* @param bytes - The fixed number of bytes to read.
* @param content - The string serializer to use for the content. Defaults to `utf8`.
Expand All @@ -214,6 +214,17 @@ export interface SerializerInterface {
description?: string
) => Serializer<string>;

/**
* Creates a variable-length serializer for strings.
*
* @param content - The string serializer to use for the content. Defaults to `utf8`.
* @param description - A custom description for the serializer.
*/
variableString: (
content?: Serializer<string>,
description?: string
) => Serializer<string>;

/**
* Creates a boolean serializer.
*
Expand Down Expand Up @@ -337,6 +348,10 @@ export class NullSerializer implements SerializerInterface {
throw this.error;
}

variableString(): Serializer<string> {
throw this.error;
}

bool(): Serializer<boolean> {
throw this.error;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/umi-serializer-beet/src/BeetSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,18 @@ export class BeetSerializer implements SerializerInterface {
);
}

variableString(
content?: Serializer<string>,
description?: string
): Serializer<string> {
const contentSerializer = content ?? utf8;
return {
...contentSerializer,
description:
description ?? `variableString(${contentSerializer.description})`,
};
}

bool(size?: NumberSerializer, description?: string): Serializer<boolean> {
const serializer = size ?? u8();
if (serializer.fixedSize === null) {
Expand Down
14 changes: 14 additions & 0 deletions packages/umi-serializer-beet/test/BeetSerializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,20 @@ test('it can serialize fixed strings', (t) => {
t.is(doffset(fixedString(5, base58), '7893000000'), 5);
});

test('it can serialize variable strings', (t) => {
const { variableString } = new BeetSerializer();
t.is(variableString().description, 'variableString(utf8)');
t.is(variableString(undefined, 'My string').description, 'My string');
t.is(variableString().fixedSize, null);
t.is(variableString().maxSize, null);

// It simply delegates to the content serializer.
t.is(s(variableString(), 'Hello World!'), '48656c6c6f20576f726c6421');
t.is(d(variableString(), '48656c6c6f20576f726c6421'), 'Hello World!');
t.is(doffset(variableString(), '48656c6c6f20576f726c6421'), 12);
t.is(sd(variableString(), 'Hello World!'), 'Hello World!');
});

test('it can serialize bytes', (t) => {
const { bytes } = new BeetSerializer();
t.is(bytes.description, 'bytes');
Expand Down

0 comments on commit d28f4dc

Please sign in to comment.