Skip to content

Commit

Permalink
feat: add typegen support for string slice (#1468)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored Dec 5, 2023
1 parent 5ac9673 commit 5e544e1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fast-forks-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-typegen": minor
---

add string slice support in typegen
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ abi EchoValues {

fn echo_str_8(value: str[8]) -> str[8];

fn echo_str(value: str) -> str;

fn echo_tuple(tuple: (u8, bool, u64)) -> (u8, bool, u64);

fn echo_b512(input: B512) -> B512;
Expand All @@ -18,6 +20,15 @@ impl EchoValues for Contract {
value
}

// Avoid executing this function; it is currently unsupported.
// Refer to: https://github.com/FuelLabs/sway/issues/5110 for more details.
// The function is defined here to include the type 'str' in the ABI,
// ensuring Typegen can accurately interpret it.
// For further information, see: https://github.com/FuelLabs/fuels-ts/issues/1469
fn echo_str(value: str) -> str {
value
}

fn echo_str_8(value: str[8]) -> str[8] {
value
}
Expand Down
23 changes: 23 additions & 0 deletions packages/abi-typegen/src/abi/types/StrSliceType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { IType } from '../../types/interfaces/IType';

import { AType } from './AType';

export class StrSliceType extends AType implements IType {
public static swayType = 'str';

public name = 'strSlice';

static MATCH_REGEX: RegExp = /^str$/m;

static isSuitableFor(params: { type: string }) {
return StrSliceType.MATCH_REGEX.test(params.type);
}

public parseComponentsAttributes(_params: { types: IType[] }) {
this.attributes = {
inputLabel: 'StrSlice',
outputLabel: 'StrSlice',
};
return this.attributes;
}
}
33 changes: 33 additions & 0 deletions packages/abi-typegen/src/abi/types/StrSlicesType.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { BoolType } from './BoolType';
import { StdStringType } from './StdStringType';
import { StrSliceType } from './StrSliceType';
import { StrType } from './StrType';

describe('StrSlicesType.ts', () => {
test('should properly parse type attributes', () => {
const strSlices = new StrSliceType({
rawAbiType: {
components: null,
typeParameters: null,
typeId: 1,
type: StrSliceType.swayType,
},
});

strSlices.parseComponentsAttributes({ types: [] });

const suitableForStrSlices = StrSliceType.isSuitableFor({ type: StrSliceType.swayType });
const suitableForU16 = StrSliceType.isSuitableFor({ type: BoolType.swayType });
const suitableForStr = StrSliceType.isSuitableFor({ type: StrType.swayType });
const suitableForStdString = StrSliceType.isSuitableFor({ type: StdStringType.swayType });

expect(suitableForStrSlices).toEqual(true);
expect(suitableForU16).toEqual(false);
expect(suitableForStr).toEqual(false);
expect(suitableForStdString).toEqual(false);

expect(strSlices.attributes.inputLabel).toEqual('StrSlice');
expect(strSlices.attributes.outputLabel).toEqual('StrSlice');
expect(strSlices.requiredFuelsMembersImports).toStrictEqual([]);
});
});
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/utils/supportedTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { supportedTypes } from './supportedTypes';

describe('supportedTypes.ts', () => {
test('should export all supported types', () => {
expect(supportedTypes.length).toEqual(20);
expect(supportedTypes.length).toEqual(21);
});
});
2 changes: 2 additions & 0 deletions packages/abi-typegen/src/utils/supportedTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { OptionType } from '../abi/types/OptionType';
import { RawUntypedPtr } from '../abi/types/RawUntypedPtr';
import { RawUntypedSlice } from '../abi/types/RawUntypedSlice';
import { StdStringType } from '../abi/types/StdStringType';
import { StrSliceType } from '../abi/types/StrSliceType';
import { StrType } from '../abi/types/StrType';
import { StructType } from '../abi/types/StructType';
import { TupleType } from '../abi/types/TupleType';
Expand All @@ -32,6 +33,7 @@ export const supportedTypes = [
RawUntypedSlice,
StdStringType,
StrType,
StrSliceType,
StructType,
TupleType,
U16Type,
Expand Down

0 comments on commit 5e544e1

Please sign in to comment.