Skip to content

Commit

Permalink
Fix 'updatedInPlace' flag
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Nov 21, 2023
1 parent e5782e5 commit d470d2a
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions migrations/migrate_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ func MigrateNestedValue(
for index := 0; index < count; index++ {
element := array.Get(inter, emptyLocationRange, index)
newElement, elementUpdated := MigrateNestedValue(inter, element, migrate)
if newElement != nil {
array.Set(
inter,
emptyLocationRange,
index,
newElement,
)
}

updatedInPlace = updatedInPlace || elementUpdated

if newElement == nil {
continue
}

array.Set(
inter,
emptyLocationRange,
index,
newElement,
)
}

// The array itself doesn't need to be replaced.
Expand All @@ -78,13 +81,14 @@ func MigrateNestedValue(
existingValue := composite.GetField(inter, interpreter.EmptyLocationRange, fieldName)

migratedValue, valueUpdated := MigrateNestedValue(inter, existingValue, migrate)

updatedInPlace = updatedInPlace || valueUpdated

if migratedValue == nil {
continue
}

composite.SetMember(inter, emptyLocationRange, fieldName, migratedValue)

updatedInPlace = updatedInPlace || valueUpdated
}

// The composite itself does not have to be replaced
Expand All @@ -109,6 +113,9 @@ func MigrateNestedValue(

newKey, keyUpdated := MigrateNestedValue(inter, existingKey, migrate)
newValue, valueUpdated := MigrateNestedValue(inter, existingValue, migrate)

updatedInPlace = updatedInPlace || keyUpdated || valueUpdated

if newKey == nil && newValue == nil {
continue
}
Expand All @@ -133,12 +140,7 @@ func MigrateNestedValue(
valueToSet = newValue
}

// Always wrap with an optional, when inserting to the dictionary.
valueToSet = interpreter.NewUnmeteredSomeValueNonCopying(valueToSet)

dictionary.SetKey(inter, emptyLocationRange, keyToSet, valueToSet)

updatedInPlace = updatedInPlace || keyUpdated || valueUpdated
dictionary.Insert(inter, emptyLocationRange, keyToSet, valueToSet)
}

// The dictionary itself does not have to be replaced
Expand Down

0 comments on commit d470d2a

Please sign in to comment.