Skip to content

Commit

Permalink
fix: generating structs with correct boolean types
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Aug 29, 2024
1 parent 6b74e4a commit 5ec27a1
Show file tree
Hide file tree
Showing 4 changed files with 817 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/commands/contract/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ export function extractDecorator(type: string): {type: string; decorator?: strin
if (type.includes(decorator)) {
type = type.replace(decorator, '')

if (type.toLowerCase().includes('bool?')) {
console.log({type, decorators})
}

return {type, decorator}
}
}
Expand Down
26 changes: 23 additions & 3 deletions src/commands/contract/structs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,22 @@ function findFieldStructType(
namespace: string | undefined,
abi: ABI.Def
): ts.Identifier | ts.StringLiteral {
const fieldTypeString = extractDecorator(findFieldTypeString(typeString, namespace, abi)).type
let fieldTypeString = extractDecorator(findFieldTypeString(typeString, namespace, abi)).type

if (typeString.toLowerCase().includes('bool?')) {
console.log({
typeString,
fieldTypeString,
extractDecoratorDirectly: extractDecorator(typeString).type,
findFieldTypeString: findFieldTypeString(typeString, namespace, abi),
})
}

if (['string', 'string[]', 'boolean', 'boolean[]'].includes(fieldTypeString.toLowerCase())) {
if (
['string', 'string[]', 'boolean', 'boolean[]', 'bool'].includes(
fieldTypeString.toLowerCase()
)
) {
return ts.factory.createStringLiteral(formatFieldString(fieldTypeString))
}

Expand All @@ -351,6 +364,13 @@ export function findFieldTypeString(
): string {
const fieldType = findInternalType(typeString, namespace, abi)

if (fieldType === 'Bool?') {
console.log({
typeString,
fieldType,
})
}

if (['String', 'Number'].includes(fieldType)) {
return fieldType.toLowerCase()
}
Expand All @@ -371,7 +391,7 @@ export function findFieldTypeString(
}

function formatFieldString(typeString: string): string {
if (typeString === 'boolean') {
if (typeString === 'boolean' || typeString === 'Bool') {
return 'bool'
}

Expand Down
Loading

0 comments on commit 5ec27a1

Please sign in to comment.