Skip to content

Commit

Permalink
fix(hydra-typegen): do proper handling of tuples in typegen (#403)
Browse files Browse the repository at this point in the history
affects: @dzlzv/hydra-typegen

tuples of the form (T1, T2, T3) are recursively replaced with [T1,T2,T3] & Codec

ISSUES CLOSED: #398
  • Loading branch information
dzhelezov authored May 21, 2021
1 parent 6baa14e commit bf89dc8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/hydra-typegen/src/generators/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ describe('helpers', () => {
expect(c(prmsReturn())).to.equal(c(`[Type1, Type2 & Codec, Option<Type3>]`))
})

it('should render return type with tupes ', () => {
const prmsReturn = helper.paramsReturnType.bind({
args: ['Type1', 'Vec<(Type1,Type2)>'],
})
expect(c(prmsReturn())).to.equal(c(`[Type1, Vec<[Type1,Type2] & Codec>]`))
})

it('should render return type statement', () => {
const prmsReturnStmt = helper.paramsReturnStmt.bind({
args: ['Type1', 'Type2 & Balance'],
Expand All @@ -21,4 +28,14 @@ describe('helpers', () => {
typeRegistry, 'Type2 & Balance', [this.ctx.params[1].value])]`)
)
})

it('should handle tuple types', () => {
const prmsReturnStmt = helper.paramsReturnStmt.bind({
args: ['Vec<(Type1,Type2,(Balance1,Balance2))>'],
})
expect(c(prmsReturnStmt())).to.equal(
c(`return [createTypeUnsafe<Vec<[Type1,Type2,[Balance1,Balance2] & Codec] & Codec> & Codec>(
typeRegistry, 'Vec<(Type1,Type2,(Balance1,Balance2))>', [this.ctx.params[0].value])]`)
)
})
})
25 changes: 22 additions & 3 deletions packages/hydra-typegen/src/generators/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export function renderNamedArgs(
}

export function renderTypedParams(argTypes: string[]): string {
const returnType = `[${argTypes.join(',')}]`
const returnType = `[${argTypes.map((a) => convertTuples(a)).join(',')}]`
console.log(`Return type: ${returnType}`)
const returnObjects = argTypes.map((argType, index) =>
renderCreateTypeStmt(argType, eventParamValueGetter(index))
)
Expand All @@ -57,10 +58,28 @@ export function renderTypedParams(argTypes: string[]): string {
}

function renderCreateTypeStmt(argType: string, ctxValueGetter: string) {
return `createTypeUnsafe<${argType} & Codec>(
return `createTypeUnsafe<${convertTuples(argType)} & Codec>(
typeRegistry, '${argType}', ${ctxValueGetter}) `
}

/**
* Converts tuple types (X, Y, Z) to [X, Y, Z] & Codec
* @param type
* @returns
*/
export function convertTuples(type: string): string {
let _type = type

// recursively find typles and replace them
// eslint-disable-next-line no-useless-escape
while (_type.match(/\(([^\(].*)\)/g)) {
// eslint-disable-next-line no-useless-escape
_type = _type.replace(/\(([^\(].*)\)/, '[$1] & Codec')
}

return _type
}

export const helper: Handlebars.HelperDeclareSpec = {
imports() {
const { imports } = (this as unknown) as { imports: ImportsDef }
Expand Down Expand Up @@ -89,7 +108,7 @@ export const helper: Handlebars.HelperDeclareSpec = {

paramsReturnType() {
const { args } = (this as unknown) as { args: string[] }
return `[${args.join(',')}]`
return `[${args.map((a) => convertTuples(a)).join(',')}]`
},

paramsReturnStmt() {
Expand Down

0 comments on commit bf89dc8

Please sign in to comment.