If the value pushed a container before throwing, pop it back off to restore state

This commit is contained in:
Oleksii Dykan
2018-02-20 23:00:29 +01:00
parent 1384814716
commit 5b8627dbbe

View File

@@ -393,11 +393,20 @@ extension _FirebaseEncoder {
}
// The value should request a container from the _FirebaseEncoder.
let depth = storage.count
try value.encode(to: self)
let depth = self.storage.count
do {
try value.encode(to: self)
} catch {
// If the value pushed a container before throwing, pop it back off to restore state.
if self.storage.count > depth {
let _ = self.storage.popContainer()
}
throw error
}
// The top container should be a new container.
guard storage.count > depth else {
guard self.storage.count > depth else {
return nil
}