fwcd / fwcd/swift-binary-coder

Cannot re-read input or amend output

Abierto
#4 2 comentarios 1 reacción 0 asignados Ver en GitHub
Lenguaje dominante
Swift
Estrellas
4
Forks
1
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

The standard encoder/decoder implementations provided by Apple (e.g. JSONDecoder) allow you to take multiple attempts at reading data in different formats. For example, you could try reading a field as a String, and then handle the error if that fails and try again to read it as an Int.

Similarly you can write to the same output object multiple times with difference objects to get an aggregate output, as in this example: https://gist.github.com/nicklockwood/ff51117ac8139248507ecc84a1ed7fed

Unfortunately this doesn't work with your current BinaryEncoder implementation.

For decoding the problem appears to be that the BinaryDecodingState is shared between containers, which means that once one container has read a value, it's permanently popped from the internal Data object and cannot be read again by another container. This seems like it should be straightforward to fix by using a resettable cursor variable for the current data read position, but I've not attempted to work out a detailed implementation.

For encoding I guess the problem is that you write out the data immediately to the output, making it impossible to add additional fields to the same object using another container. I'm not sure what the solution is there, although this seems like a less common requirement than multiple reads (I only just discovered that it was even possible with JSONDecoder today!).

In any case, it would be great to fix the re-reading limitation in BinaryDecoder at least.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.