Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve external types and JSON codec #2401

Merged
merged 17 commits into from
Mar 30, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove type ID from function type and restricted type
turbolent committed Mar 24, 2023
commit 490bcbd22996904c952056e7aaaa65f7e3c08f6e
15 changes: 5 additions & 10 deletions encoding/json/decode.go
Original file line number Diff line number Diff line change
@@ -894,16 +894,15 @@ func (d *Decoder) decodeFieldType(valueJSON any, results typeDecodingResults) ca
)
}

func (d *Decoder) decodeFunctionType(returnValue, parametersValue, id any, results typeDecodingResults) cadence.Type {
func (d *Decoder) decodeFunctionType(returnValue, parametersValue any, results typeDecodingResults) cadence.Type {
parameters := d.decodeParamTypes(toSlice(parametersValue), results)
returnType := d.decodeType(returnValue, results)

return cadence.NewMeteredFunctionType(
d.gauge,
"",
parameters,
returnType,
).WithID(toString(id))
)
}

func (d *Decoder) decodeNominalType(
@@ -1026,21 +1025,20 @@ func (d *Decoder) decodeNominalType(
func (d *Decoder) decodeRestrictedType(
typeValue any,
restrictionsValue []any,
typeIDValue string,
results typeDecodingResults,
) cadence.Type {
typ := d.decodeType(typeValue, results)

restrictions := make([]cadence.Type, 0, len(restrictionsValue))
for _, restriction := range restrictionsValue {
restrictions = append(restrictions, d.decodeType(restriction, results))
}

return cadence.NewMeteredRestrictedType(
d.gauge,
"",
typ,
restrictions,
).WithID(typeIDValue)
)
}

type typeDecodingResults map[string]cadence.Type
@@ -1069,16 +1067,13 @@ func (d *Decoder) decodeType(valueJSON any, results typeDecodingResults) cadence
case "Function":
returnValue := obj.Get(returnKey)
parametersValue := obj.Get(parametersKey)
idValue := obj.Get(typeIDKey)
return d.decodeFunctionType(returnValue, parametersValue, idValue, results)
return d.decodeFunctionType(returnValue, parametersValue, results)
case "Restriction":
restrictionsValue := obj.Get(restrictionsKey)
typeIDValue := toString(obj.Get(typeIDKey))
typeValue := obj.Get(typeKey)
return d.decodeRestrictedType(
typeValue,
toSlice(restrictionsValue),
typeIDValue,
results,
)
case "Optional":
4 changes: 0 additions & 4 deletions encoding/json/encode.go
Original file line number Diff line number Diff line change
@@ -178,7 +178,6 @@ type jsonReferenceType struct {

type jsonRestrictedType struct {
Kind string `json:"kind"`
TypeID string `json:"typeID"`
Type jsonValue `json:"type"`
Restrictions []jsonValue `json:"restrictions"`
}
@@ -192,7 +191,6 @@ type jsonParameterType struct {
type jsonFunctionType struct {
Return jsonValue `json:"return"`
Kind string `json:"kind"`
TypeID string `json:"typeID"`
Parameters []jsonParameterType `json:"parameters"`
}

@@ -826,7 +824,6 @@ func prepareType(typ cadence.Type, results typePreparationResults) jsonValue {
case *cadence.FunctionType:
return jsonFunctionType{
Kind: "Function",
TypeID: typ.ID(),
Return: prepareType(typ.ReturnType, results),
Parameters: prepareParameters(typ.Parameters, results),
}
@@ -843,7 +840,6 @@ func prepareType(typ cadence.Type, results typePreparationResults) jsonValue {
}
return jsonRestrictedType{
Kind: "Restriction",
TypeID: typ.ID(),
Type: prepareType(typ.Type, results),
Restrictions: restrictions,
}
13 changes: 5 additions & 8 deletions encoding/json/encoding_test.go
Original file line number Diff line number Diff line change
@@ -2399,12 +2399,12 @@ func TestEncodeType(t *testing.T) {
testEncodeAndDecode(
t,
cadence.TypeValue{
StaticType: (&cadence.FunctionType{
StaticType: &cadence.FunctionType{
Parameters: []cadence.Parameter{
{Label: "qux", Identifier: "baz", Type: cadence.StringType{}},
},
ReturnType: cadence.IntType{},
}).WithID("Foo"),
},
},
// language=json
`
@@ -2413,7 +2413,6 @@ func TestEncodeType(t *testing.T) {
"value": {
"staticType": {
"kind": "Function",
"typeID": "Foo",
"return": {
"kind": "Int"
},
@@ -2466,12 +2465,12 @@ func TestEncodeType(t *testing.T) {
testEncodeAndDecode(
t,
cadence.TypeValue{
StaticType: (&cadence.RestrictedType{
StaticType: &cadence.RestrictedType{
Restrictions: []cadence.Type{
cadence.StringType{},
},
Type: cadence.IntType{},
}).WithID("Int{String}"),
},
},
// language=json
`
@@ -2480,7 +2479,6 @@ func TestEncodeType(t *testing.T) {
"value": {
"staticType": {
"kind": "Restriction",
"typeID": "Int{String}",
"type": {
"kind": "Int"
},
@@ -3260,7 +3258,7 @@ func TestExportFunctionValue(t *testing.T) {
FunctionType: (&cadence.FunctionType{
Parameters: []cadence.Parameter{},
ReturnType: cadence.VoidType{},
}).WithID("(():Void)"),
}),
},
// language=json
`
@@ -3269,7 +3267,6 @@ func TestExportFunctionValue(t *testing.T) {
"value": {
"functionType": {
"kind": "Function",
"typeID": "(():Void)",
"parameters": [],
"return": {
"kind": "Void"
6 changes: 2 additions & 4 deletions runtime/convertTypes.go
Original file line number Diff line number Diff line change
@@ -424,10 +424,9 @@ func exportFunctionType(

return cadence.NewMeteredFunctionType(
gauge,
"",
convertedParameters,
convertedReturnType,
).WithID(string(t.ID()))
)
}

func exportReferenceType(
@@ -460,10 +459,9 @@ func exportRestrictedType(

return cadence.NewMeteredRestrictedType(
gauge,
"",
convertedType,
restrictions,
).WithID(string(t.ID()))
)
}

func exportCapabilityType(
17 changes: 10 additions & 7 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
@@ -133,7 +133,10 @@ func TestExportValue(t *testing.T) {
},
}

testFunctionType := cadence.NewFunctionType("(():Void)", []cadence.Parameter{}, cadence.VoidType{})
testFunctionType := cadence.NewFunctionType(
[]cadence.Parameter{},
cadence.VoidType{},
)

for _, tt := range []exportTest{
{
@@ -1959,7 +1962,7 @@ func TestExportTypeValue(t *testing.T) {

assert.Equal(t,
cadence.TypeValue{
StaticType: (&cadence.RestrictedType{
StaticType: &cadence.RestrictedType{
Type: &cadence.StructType{
QualifiedIdentifier: "S",
Location: TestLocation,
@@ -1972,7 +1975,7 @@ func TestExportTypeValue(t *testing.T) {
Fields: []cadence.Field{},
},
},
}).WithID("S.test.S{S.test.SI}"),
},
},
actual,
)
@@ -2237,10 +2240,10 @@ func TestExportCompositeValueWithFunctionValueField(t *testing.T) {
},
{
Identifier: "f",
Type: (&cadence.FunctionType{
Type: &cadence.FunctionType{
Parameters: []cadence.Parameter{},
ReturnType: cadence.VoidType{},
}).WithID("(():Void)"),
},
},
},
}
@@ -2249,10 +2252,10 @@ func TestExportCompositeValueWithFunctionValueField(t *testing.T) {
expected := cadence.NewStruct([]cadence.Value{
cadence.NewInt(42),
cadence.Function{
FunctionType: (&cadence.FunctionType{
FunctionType: &cadence.FunctionType{
Parameters: []cadence.Parameter{},
ReturnType: cadence.VoidType{},
}).WithID("(():Void)"),
},
},
}).WithType(fooStructType)

12 changes: 6 additions & 6 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
@@ -2736,10 +2736,10 @@ func TestRuntimeScriptReturnSpecial(t *testing.T) {
}
`,
expected: cadence.Function{
FunctionType: (&cadence.FunctionType{
FunctionType: &cadence.FunctionType{
Parameters: []cadence.Parameter{},
ReturnType: cadence.IntType{},
}).WithID("(():Int)"),
},
},
},
)
@@ -2757,7 +2757,7 @@ func TestRuntimeScriptReturnSpecial(t *testing.T) {
}
`,
expected: cadence.Function{
FunctionType: (&cadence.FunctionType{
FunctionType: &cadence.FunctionType{
Parameters: []cadence.Parameter{
{
Label: sema.ArgumentLabelNotRequired,
@@ -2766,7 +2766,7 @@ func TestRuntimeScriptReturnSpecial(t *testing.T) {
},
},
ReturnType: cadence.NeverType{},
}).WithID("((String):Never)"),
},
},
},
)
@@ -2789,10 +2789,10 @@ func TestRuntimeScriptReturnSpecial(t *testing.T) {
}
`,
expected: cadence.Function{
FunctionType: (&cadence.FunctionType{
FunctionType: &cadence.FunctionType{
Parameters: []cadence.Parameter{},
ReturnType: cadence.VoidType{},
}).WithID("(():Void)"),
},
},
},
)
22 changes: 4 additions & 18 deletions types.go
Original file line number Diff line number Diff line change
@@ -1702,38 +1702,31 @@ type FunctionType struct {
}

func NewFunctionType(
typeID string,
parameters []Parameter,
returnType Type,
) *FunctionType {
return &FunctionType{
typeID: typeID,
Parameters: parameters,
ReturnType: returnType,
}
}

func NewMeteredFunctionType(
gauge common.MemoryGauge,
typeID string,
parameters []Parameter,
returnType Type,
) *FunctionType {
common.UseMemory(gauge, common.CadenceFunctionTypeMemoryUsage)
return NewFunctionType(typeID, parameters, returnType)
return NewFunctionType(parameters, returnType)
}

func (*FunctionType) isType() {}

func (t *FunctionType) ID() string {
// TODO:
return t.typeID
}

func (t *FunctionType) WithID(id string) *FunctionType {
t.typeID = id
return t
}

func (t *FunctionType) Equal(other Type) bool {
otherType, ok := other.(*FunctionType)
if !ok {
@@ -1819,38 +1812,31 @@ type RestrictedType struct {
}

func NewRestrictedType(
typeID string,
typ Type,
restrictions []Type,
) *RestrictedType {
return &RestrictedType{
typeID: typeID,
Type: typ,
Restrictions: restrictions,
}
}

func NewMeteredRestrictedType(
gauge common.MemoryGauge,
typeID string,
typ Type,
restrictions []Type,
) *RestrictedType {
common.UseMemory(gauge, common.CadenceRestrictedTypeMemoryUsage)
return NewRestrictedType(typeID, typ, restrictions)
return NewRestrictedType(typ, restrictions)
}

func (*RestrictedType) isType() {}

func (t *RestrictedType) ID() string {
// TODO:
return t.typeID
}

func (t *RestrictedType) WithID(id string) *RestrictedType {
t.typeID = id
return t
}

func (t *RestrictedType) Equal(other Type) bool {
otherType, ok := other.(*RestrictedType)
if !ok {
22 changes: 21 additions & 1 deletion types_test.go
Original file line number Diff line number Diff line change
@@ -169,9 +169,29 @@ func TestType_ID(t *testing.T) {
"S.test.BarI",
},
{
(&RestrictedType{}).WithID("S.test.Foo{S.test.FooI}"),
&RestrictedType{
Type: &ResourceType{
Location: utils.TestLocation,
QualifiedIdentifier: "Foo",
},
Restrictions: []Type{
&ResourceInterfaceType{
Location: utils.TestLocation,
QualifiedIdentifier: "FooI",
},
},
},
"S.test.Foo{S.test.FooI}",
},
{
&FunctionType{
Parameters: []Parameter{
{Type: IntType{}},
},
ReturnType: StringType{},
},
"((Int):String)",
},
{
&EventType{
QualifiedIdentifier: "Event",
Loading