Skip to content

Commit

Permalink
Migrate static types of arrays and dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Feb 28, 2024
1 parent 2dd12d3 commit 8c66514
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
60 changes: 60 additions & 0 deletions migrations/statictypes/account_type_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,46 @@ func TestMigratingValuesWithAccountStaticType(t *testing.T) {
require.NoError(t, err)

testCases := map[string]testCase{
"dictionary_value": {
storedValue: interpreter.NewDictionaryValue(
inter,
interpreter.EmptyLocationRange,
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeString,
interpreter.PrimitiveStaticTypePublicAccount,

Check failure on line 890 in migrations/statictypes/account_type_migration_test.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: interpreter.PrimitiveStaticTypePublicAccount is deprecated: PrimitiveStaticTypePublicAccount only exists for migration purposes. (staticcheck)

Check failure on line 890 in migrations/statictypes/account_type_migration_test.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: interpreter.PrimitiveStaticTypePublicAccount is deprecated: PrimitiveStaticTypePublicAccount only exists for migration purposes. (staticcheck)
),
),
expectedValue: interpreter.NewDictionaryValue(
inter,
interpreter.EmptyLocationRange,
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeString,
unauthorizedAccountReferenceType,
),
),
},
"array_value": {
storedValue: interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.NewVariableSizedStaticType(
nil,
interpreter.PrimitiveStaticTypePublicAccount,

Check failure on line 909 in migrations/statictypes/account_type_migration_test.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: interpreter.PrimitiveStaticTypePublicAccount is deprecated: PrimitiveStaticTypePublicAccount only exists for migration purposes. (staticcheck)

Check failure on line 909 in migrations/statictypes/account_type_migration_test.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: interpreter.PrimitiveStaticTypePublicAccount is deprecated: PrimitiveStaticTypePublicAccount only exists for migration purposes. (staticcheck)
),
common.Address{},
),
expectedValue: interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.NewVariableSizedStaticType(
nil,
unauthorizedAccountReferenceType,
),
common.Address{},
),
},
"account_capability_value": {
storedValue: interpreter.NewUnmeteredCapabilityValue(
123,
Expand Down Expand Up @@ -955,6 +995,26 @@ func TestMigratingValuesWithAccountStaticType(t *testing.T) {
BorrowType: unauthorizedAccountReferenceType,
},
},
"capability_dictionary": {
storedValue: interpreter.NewDictionaryValue(
inter,
interpreter.EmptyLocationRange,
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeString,
interpreter.NewCapabilityStaticType(nil, interpreter.PrimitiveStaticTypePublicAccount),

Check failure on line 1005 in migrations/statictypes/account_type_migration_test.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: interpreter.PrimitiveStaticTypePublicAccount is deprecated: PrimitiveStaticTypePublicAccount only exists for migration purposes. (staticcheck)

Check failure on line 1005 in migrations/statictypes/account_type_migration_test.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: interpreter.PrimitiveStaticTypePublicAccount is deprecated: PrimitiveStaticTypePublicAccount only exists for migration purposes. (staticcheck)
),
),
expectedValue: interpreter.NewDictionaryValue(
inter,
interpreter.EmptyLocationRange,
interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeString,
interpreter.NewCapabilityStaticType(nil, unauthorizedAccountReferenceType),
),
),
},
}

// Store values
Expand Down
47 changes: 46 additions & 1 deletion migrations/statictypes/statictype_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (m *StaticTypeMigration) Migrate(
_ interpreter.StorageKey,
_ interpreter.StorageMapKey,
value interpreter.Value,
_ *interpreter.Interpreter,
inter *interpreter.Interpreter,
) (newValue interpreter.Value, err error) {
switch value := value.(type) {
case interpreter.TypeValue:
Expand Down Expand Up @@ -129,6 +129,51 @@ func (m *StaticTypeMigration) Migrate(
value.CapabilityID,
value.TargetPath,
), nil

case *interpreter.ArrayValue:
convertedElementType := m.maybeConvertStaticType(value.Type, nil)
if convertedElementType == nil {
return
}

iterator := value.Iterator(inter, interpreter.EmptyLocationRange)

return interpreter.NewArrayValueWithIterator(
inter,
convertedElementType.(interpreter.ArrayStaticType),
value.GetOwner(),
uint64(value.Count()),
func() interpreter.Value {
return iterator.Next(inter, interpreter.EmptyLocationRange)
},
), nil

case *interpreter.DictionaryValue:
convertedElementType := m.maybeConvertStaticType(value.Type, nil)
if convertedElementType == nil {
return
}

var keysAndValues []interpreter.Value

iterator := value.Iterator()
for {
keyValue, value := iterator.Next(inter)
if keyValue == nil {
break
}

keysAndValues = append(keysAndValues, keyValue)
keysAndValues = append(keysAndValues, value)
}

return interpreter.NewDictionaryValueWithAddress(
inter,
interpreter.EmptyLocationRange,
convertedElementType.(*interpreter.DictionaryStaticType),
value.GetOwner(),
keysAndValues...,
), nil
}

return
Expand Down

0 comments on commit 8c66514

Please sign in to comment.