Skip to content

Commit

Permalink
Clena up toEncodingData and fromEncodingData generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos committed May 28, 2024
1 parent 952991f commit bf8208f
Showing 1 changed file with 62 additions and 47 deletions.
109 changes: 62 additions & 47 deletions typescript_templates/model.vm
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ false##
true##
#end
#end
## Returns "true" if the type is a primative, meaning it's not an object that requires manipulation
## when converting to & from encoding data.
#macro ( isPrimativeType $param )
#if ( $param.algorandFormat == "uint64" || $param.type == "integer" || $param.arrayType == "integer" )
true##
#elseif ( $param.type == "boolean" || $param.arrayType == "boolean" )
true##
#elseif( $param.type == "string" || $param.arrayType == "string" )
#if ( $param.algorandFormat == "Address" || $param.algorandFormat == "SignedTransaction" )
false##
#else
true##
#end
#else
false##
#end
#end
## Create an expression to assign a field in a constructor
#macro ( constructorAssignType $className $prop )
#set( $argType = "#toSdkType($className, $prop, true)" )
Expand Down Expand Up @@ -185,72 +202,70 @@ $unknown.type ## force a template failure with an unknown type
## Create an expression to assign a field in the toEncodingData function
#macro ( fieldToEncodingData $prop )
#set( $value = "this.#paramName($prop)" )
#if ( $prop.algorandFormat == "BlockHeader" )
blockHeaderToEncodingData($value)##
#elseif ( $prop.algorandFormat == "Address" || $prop.type == "Address" )
#if ( !$prop.required )
#set( $isPrimative = "#isPrimativeType($prop)" == "true" )
#set( $needsUndefinedCheck = !$isPrimative && !$prop.required )
#if ( $needsUndefinedCheck )
typeof ${value} !== "undefined" ? ##
#end
#if ( $prop.arrayType )
${value}.map(addr => addr.toString())##
#else
${value}.toString()##
#end
#if ( !$prop.required )
: undefined##
#if ( $prop.arrayType && !$isPrimative )## No need to map a primative type
${value}.map(v => ##
#set ( $value = "v" )
#end
#elseif ( "#isClassType($prop)" == "false" && $prop.algorandFormat != "SignedTransaction" && $prop.type != "object" )
#if ( $prop.algorandFormat == "BlockHeader" )
blockHeaderToEncodingData($value)##
#elseif ( $prop.algorandFormat == "Address" )
${value}.toString()##
#elseif ( $isPrimative )
$value##
#elseif ( $prop.arrayType )
#if ( !$prop.required )
typeof ${value} !== "undefined" ? ##
#end
${value}.map(v => v.toEncodingData())##
#if ( !$prop.required )
: undefined##
#end
#else
#if ( !$prop.required )
typeof ${value} !== "undefined" ? ##
#end
${value}.toEncodingData()##
#if ( !$prop.required )
: undefined##
#end
#if ( $prop.arrayType && !$isPrimative )
)##
#end
#if ( $needsUndefinedCheck )
: undefined##
#end
#end
## Create an expression to assign a field in the fromEncodingData function
#macro ( fromEncodingDataAssignType $value $prop $className )
#set( $sdkType = "#toSdkType($className, $prop, false)" )
#if ( $sdkType == "SignedTransaction[]" )
#set ( $sdkType = "SignedTransaction" )
#set( $isPrimative = "#isPrimativeType($prop)" == "true" || $prop.algorandFormat == "Address" )## Addresses are encoded as strings, so treat them as primatives here
#set( $needsUndefinedCheck = !$isPrimative && !$prop.required )
#if ( $needsUndefinedCheck )
typeof ${value} !== "undefined" ? ##
#end
#if ( $prop.algorandFormat == "BlockHeader" )
blockHeaderFromEncodingData($value)##
#elseif ( "#isClassType($prop)" == "false" && $sdkType != "SignedTransaction" && $sdkType != "UntypedValue")
$value##
#elseif ( $prop.arrayType )
#if ( $sdkType == "SignedTransaction" || $sdkType == "UntypedValue")
#set ( $mapping = ".map(${sdkType}.fromEncodingData)" )
#if ( $prop.arrayType && !$isPrimative )
#if ( $prop.required )
(${value} ?? [])##
#else
#set ( $mapping = ".map(${prop.arrayType}.fromEncodingData)" )
$value##
#end
#if ($prop.required)
($value ?? [])$mapping##
#else
typeof $value !== 'undefined' ? $value$mapping : undefined##
.map((v: unknown) => ##
#set ( $value = "v" )
#elseif ( $prop.required && !$isPrimative )
#set ( $value = "($value ?? new Map())" )##
#end
#if ( $prop.algorandFormat == "BlockHeader" )
blockHeaderFromEncodingData($value)##
#elseif ( $isPrimative )
${value}##
#else
#if ( $sdkType == "SignedTransaction" || $sdkType == "UntypedValue")
#set ( $assignment = "${sdkType}.fromEncodingData" )
#if ( $prop.algorandFormat == "SignedTransaction" )
SignedTransaction##
#elseif ( $prop.type == "object" )
UntypedValue##
#elseif ( $prop.arrayType )
${prop.arrayType}##
#else
#set ( $assignment = "${prop.refType}.fromEncodingData" )
${prop.refType}##
#end
#if ($prop.required)
$assignment($value ?? {})##
#else
typeof $value !== 'undefined' ? $assignment($value) : undefined##
.fromEncodingData($value)
#end
#if ( $prop.arrayType && !$isPrimative )
)##
#end
#if ( $needsUndefinedCheck )
: undefined##
#end
#end
#macro ( questionMarkIfOptional $param )
Expand Down

0 comments on commit bf8208f

Please sign in to comment.