Skip to content

Commit

Permalink
fix: handling variants in external and internal types
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Aug 26, 2023
1 parent e4f1f3e commit ed42bab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 68 deletions.
53 changes: 29 additions & 24 deletions src/codegen/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,11 @@ export function findCoreClass(type: string): string | undefined {
if (ANTELOPE_CLASS_MAPPINGS[type]) {
return ANTELOPE_CLASS_MAPPINGS[type]
}
for (const coreType of ANTELOPE_CLASSES) {
const parsedType = parseType(type).split('_').join('')
if (parsedType === coreType.toLowerCase() ||
parsedType.replace(/[0-9]/g, '') === coreType.toLowerCase()) {
return coreType
}
}

const parsedType = parseType(type).split('_').join('')

return ANTELOPE_CLASSES.find((antelopeClass) => parsedType === antelopeClass.toLowerCase()) ||
ANTELOPE_CLASSES.find((antelopeClass) => parsedType.replace(/[0-9]/g, '') === antelopeClass.toLowerCase())
}

export function findCoreType(type: string): string | undefined {
Expand All @@ -138,20 +136,10 @@ export function findInternalType(type: string, typeNamespace: string | null, abi

typeString = parseType(typeString)

const aliasType = findAliasType(typeString, abi)

if (aliasType) {
typeString = aliasType
}

const variantType = findVariantType(typeString, typeNamespace, abi)

if (variantType) {
typeString = variantType
}

const relevantAbitype = findAbiType(typeString, abi)

relevantAbitype && console.log({ findInternalType: relevantAbitype })

if (relevantAbitype) {
typeString = relevantAbitype
}
Expand All @@ -163,7 +151,7 @@ function formatInternalType(typeString: string, namespace: string | null, abi: A
const structNames = abi.structs.map((struct) => struct.name.toLowerCase())

if (structNames.includes(typeString.toLowerCase())) {
return `${namespace ? `${namespace}.` : ''}${generateStructClassName(typeString)}`
return `${namespace ? `${namespace}` : ''}${generateStructClassName(typeString)}`
} else {
return findCoreClass(typeString) || capitalize(typeString)
}
Expand Down Expand Up @@ -195,16 +183,30 @@ function findVariantType(
return
}

return abiVariant.types
.map((variant) => formatInternalType(variant, namespace, abi))
.join(' | ')
return abiVariant.types.join(' | ')
}

export function findAbiType(
typeString: string,
type: string,
abi: ABI.Def,
typeNamespace = ''
): string | undefined {
let typeString = type

const aliasType = findAliasType(typeString, abi)

if (aliasType) {
typeString = aliasType
}

const variantType = findVariantType(typeString, typeNamespace, abi)

if (variantType) {
typeString = variantType
}

variantType && console.log({ findAbiType: variantType, typeString, structs: abi.structs })

const abiType = abi.structs.find((abiType) => abiType.name === typeString)?.name

if (abiType) {
Expand All @@ -219,6 +221,9 @@ export function findExternalType(type: string, abi: ABI.Def, typeNamespace?: str
typeString = parseType(typeString)

const relevantAbitype = findAbiType(typeString, abi, typeNamespace)


relevantAbitype && console.log({findExternalType: relevantAbitype})

if (relevantAbitype) {
typeString = relevantAbitype
Expand Down
2 changes: 1 addition & 1 deletion test/tests/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ suite('codegen', async function () {
}
})
teardown(() => {
// removeCodegenContracts()
removeCodegenContracts()
})
})
41 changes: 0 additions & 41 deletions test/tmp/eosio.ts

This file was deleted.

2 changes: 0 additions & 2 deletions test/utils/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import {codegen} from '../../src/codegen'

export async function generateCodegenContract(contractName: string) {
// Read the ABI from a JSON file
console.log({contractName, path: `test/data/abis/${contractName}.json` })
const abiJson = fs.readFileSync(`test/data/abis/${contractName}.json`, {encoding: 'utf8'})
console.log({abiJson})
const abi = new ABI(JSON.parse(abiJson))

// Generate the code
Expand Down

0 comments on commit ed42bab

Please sign in to comment.