IzumiSy / elm-firestore

*Experimental* A type-safe Firestore integration for Elm
https://package.elm-lang.org/packages/IzumiSy/elm-firestore/latest/
MIT License
14 stars 2 forks source link

list codec decode fails on empty list #57

Open DavidDTA opened 2 years ago

DavidDTA commented 2 years ago

If you write a list of 0 length using the list codec and then try to read it back, it fails with the error:

Problem with the value at json.arrayValue: {} Expecting an OBJECT with a field named values

DavidDTA commented 2 years ago

I was able to solve the problem with the following diff (against 12.0.0):

diff --git a/src/Firestore/Decode.elm b/src/Firestore/Decode.elm
index 65f643d..920d4f2 100644
--- a/src/Firestore/Decode.elm
+++ b/src/Firestore/Decode.elm
@@ -31,6 +31,7 @@ import Firestore.Types.Geopoint as Geopoint
 import Firestore.Types.Reference as Reference
 import Iso8601
 import Json.Decode as Decode
+import Json.Decode.Extra as ExDecode
 import Json.Decode.Pipeline as Pipeline
 import Time

@@ -146,7 +147,8 @@ string =
 list : Field a -> Field (List a)
 list (Field elementDecoder) =
     Decode.list elementDecoder
-        |> Decode.field "values"
+        |> ExDecode.optionalField "values"
+        |> Decode.map (Maybe.withDefault [])
         |> Decode.field "arrayValue"
         |> Field
IzumiSy commented 2 years ago

Nice catch!

Can you give me a PR with that diff?