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

Refactor existing native composite values to use 'CompositeValue' #668

Closed
wants to merge 12 commits into from
Prev Previous commit
Next Next commit
Refactor PublicAccountType to use CompositeType
  • Loading branch information
SupunS committed Mar 8, 2021
commit 47a4b91faea44fe7857d6fe17ff1a20207a5b8a3
143 changes: 57 additions & 86 deletions runtime/sema/publicaccount_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,98 +19,69 @@
package sema

import (
"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/common"
)

const PublicAccountTypeName = "PublicAccount"
const PublicAccountAddressField = "address"
const PublicAccountStorageUsedField = "storageUsed"
const PublicAccountStorageCapacityField = "storageCapacity"
const PublicAccountGetCapacityField = "getCapability"
const PublicAccountGetTargetLinkField = "getLinkTarget"
const PublicAccountKeysField = "keys"

// PublicAccountType represents the publicly accessible portion of an account.
//
var PublicAccountType = &SimpleType{
Name: "PublicAccount",
QualifiedName: "PublicAccount",
TypeID: "PublicAccount",
IsInvalid: false,
IsResource: false,
Storable: false,
Equatable: false,
ExternallyReturnable: false,
Members: func(t *SimpleType) map[string]MemberResolver {
return map[string]MemberResolver{
"address": {
Kind: common.DeclarationKindField,
Resolve: func(identifier string, _ ast.Range, _ func(error)) *Member {
return NewPublicConstantFieldMember(
t,
identifier,
&AddressType{},
accountTypeAddressFieldDocString,
)
},
},
"storageUsed": {
Kind: common.DeclarationKindField,
Resolve: func(identifier string, _ ast.Range, _ func(error)) *Member {
return NewPublicConstantFieldMember(
t,
identifier,
&UInt64Type{},
accountTypeStorageUsedFieldDocString,
)
},
},
"storageCapacity": {
Kind: common.DeclarationKindField,
Resolve: func(identifier string, _ ast.Range, _ func(error)) *Member {
return NewPublicConstantFieldMember(
t,
identifier,
&UInt64Type{},
accountTypeStorageCapacityFieldDocString,
)
},
},
"getCapability": {
Kind: common.DeclarationKindFunction,
Resolve: func(identifier string, _ ast.Range, _ func(error)) *Member {
return NewPublicFunctionMember(
t,
identifier,
publicAccountTypeGetCapabilityFunctionType,
publicAccountTypeGetLinkTargetFunctionDocString,
)
},
},
"getLinkTarget": {
Kind: common.DeclarationKindFunction,
Resolve: func(identifier string, _ ast.Range, _ func(error)) *Member {
return NewPublicFunctionMember(
t,
identifier,
accountTypeGetLinkTargetFunctionType,
accountTypeGetLinkTargetFunctionDocString,
)
},
},
"keys": {
Kind: common.DeclarationKindField,
Resolve: func(identifier string, _ ast.Range, _ func(error)) *Member {
return NewPublicConstantFieldMember(
t,
identifier,
PublicAccountKeysType,
accountTypeKeysFieldDocString,
)
},
},
}
},
var PublicAccountType = func() *CompositeType {

NestedTypes: func() *StringTypeOrderedMap {
nestedTypes := NewStringTypeOrderedMap()
nestedTypes.Set(AccountKeysTypeName, PublicAccountKeysType)
return nestedTypes
}(),
}
publicAccountType := &CompositeType{
Identifier: AuthAccountContractsTypeName,
Kind: common.CompositeKindStructure,
}

var members = []*Member{
NewPublicConstantFieldMember(
publicAccountType,
PublicAccountAddressField,
&AddressType{},
accountTypeAddressFieldDocString,
),
NewPublicConstantFieldMember(
publicAccountType,
PublicAccountStorageUsedField,
&UInt64Type{},
accountTypeStorageUsedFieldDocString,
),
NewPublicConstantFieldMember(
publicAccountType,
PublicAccountStorageCapacityField,
&UInt64Type{},
accountTypeStorageCapacityFieldDocString,
),
NewPublicFunctionMember(
publicAccountType,
PublicAccountGetCapacityField,
publicAccountTypeGetCapabilityFunctionType,
publicAccountTypeGetLinkTargetFunctionDocString,
),
NewPublicFunctionMember(
publicAccountType,
PublicAccountGetTargetLinkField,
accountTypeGetLinkTargetFunctionType,
accountTypeGetLinkTargetFunctionDocString,
),
NewPublicConstantFieldMember(
publicAccountType,
PublicAccountKeysField,
PublicAccountKeysType,
accountTypeKeysFieldDocString,
),
}

publicAccountType.Members = GetMembersAsMap(members)
publicAccountType.Fields = getFields(members)
return publicAccountType
}()

// PublicAccountKeysType represents the keys associated with a public account.
var PublicAccountKeysType = func() *CompositeType {
Expand Down