Skip to content

Commit

Permalink
Merge pull request #2551 from onflow/bastian/re-add-typeids
Browse files Browse the repository at this point in the history
Bring back type IDs in JSON encoding of funtion types and restricted types
  • Loading branch information
turbolent authored Jun 8, 2023
2 parents 37e11a4 + d139e71 commit 44d48c8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 4 additions & 0 deletions encoding/json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ type jsonReferenceType struct {

type jsonRestrictedType struct {
Kind string `json:"kind"`
TypeID string `json:"typeID"`
Type jsonValue `json:"type"`
Restrictions []jsonValue `json:"restrictions"`
}
Expand All @@ -195,6 +196,7 @@ type jsonParameterType struct {

type jsonFunctionType struct {
Kind string `json:"kind"`
TypeID string `json:"typeID"`
TypeParameters []jsonTypeParameter `json:"typeParameters"`
Parameters []jsonParameterType `json:"parameters"`
Return jsonValue `json:"return"`
Expand Down Expand Up @@ -898,6 +900,7 @@ func prepareType(typ cadence.Type, results typePreparationResults) jsonValue {
case *cadence.FunctionType:
return jsonFunctionType{
Kind: "Function",
TypeID: typ.ID(),
TypeParameters: prepareTypeParameters(typ.TypeParameters, results),
Parameters: prepareParameters(typ.Parameters, results),
Return: prepareType(typ.ReturnType, results),
Expand All @@ -915,6 +918,7 @@ func prepareType(typ cadence.Type, results typePreparationResults) jsonValue {
}
return jsonRestrictedType{
Kind: "Restriction",
TypeID: typ.ID(),
Type: prepareType(typ.Type, results),
Restrictions: restrictions,
}
Expand Down
3 changes: 3 additions & 0 deletions encoding/json/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2462,6 +2462,7 @@ func TestEncodeType(t *testing.T) {
"value": {
"staticType": {
"kind": "Function",
"typeID": "(<T>(String):Int)",
"return": {
"kind": "Int"
},
Expand Down Expand Up @@ -2536,6 +2537,7 @@ func TestEncodeType(t *testing.T) {
"value": {
"staticType": {
"kind": "Restriction",
"typeID": "Int{String}",
"type": {
"kind": "Int"
},
Expand Down Expand Up @@ -3402,6 +3404,7 @@ func TestExportFunctionValue(t *testing.T) {
"value": {
"functionType": {
"kind": "Function",
"typeID": "(():Void)",
"parameters": [],
"typeParameters": [],
"return": {
Expand Down
34 changes: 20 additions & 14 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2766,9 +2766,11 @@ func TestRuntimeScriptReturnSpecial(t *testing.T) {
}
`,
expected: cadence.Function{
FunctionType: &cadence.FunctionType{
ReturnType: cadence.IntType{},
},
FunctionType: cadence.TypeWithCachedTypeID(
&cadence.FunctionType{
ReturnType: cadence.IntType{},
},
).(*cadence.FunctionType),
},
},
)
Expand All @@ -2786,16 +2788,18 @@ func TestRuntimeScriptReturnSpecial(t *testing.T) {
}
`,
expected: cadence.Function{
FunctionType: &cadence.FunctionType{
Parameters: []cadence.Parameter{
{
Label: sema.ArgumentLabelNotRequired,
Identifier: "message",
Type: cadence.StringType{},
FunctionType: cadence.TypeWithCachedTypeID(
&cadence.FunctionType{
Parameters: []cadence.Parameter{
{
Label: sema.ArgumentLabelNotRequired,
Identifier: "message",
Type: cadence.StringType{},
},
},
ReturnType: cadence.NeverType{},
},
ReturnType: cadence.NeverType{},
},
).(*cadence.FunctionType),
},
},
)
Expand All @@ -2818,9 +2822,11 @@ func TestRuntimeScriptReturnSpecial(t *testing.T) {
}
`,
expected: cadence.Function{
FunctionType: &cadence.FunctionType{
ReturnType: cadence.VoidType{},
},
FunctionType: cadence.TypeWithCachedTypeID(
&cadence.FunctionType{
ReturnType: cadence.VoidType{},
},
).(*cadence.FunctionType),
},
},
)
Expand Down

0 comments on commit 44d48c8

Please sign in to comment.