-
Notifications
You must be signed in to change notification settings - Fork 24
Description
Consider the following:
import Data.Aeson as A
import Data.Aeson.Diff as AD
import Data.Map as M
foo :: Map Int String
foo = mempty
bar :: Map Int String
bar = M.fromList [(123, "asdf")]
encoded = A.encode (AD.diff (A.toJSON foo) (A.toJSON bar))
decoded :: Patch
Right decoded = A.eitherDecode encoded
applied = AD.patch decoded (A.toJSON foo)
In this example, we're computing a diff between two Map Int String
values. Note that these still produce string keys when encoded:
λ> putStrLn $ decodeUtf8 $ A.encode bar
{"123":"asdf"}
When we compute the diff and encode it, we get the following:
λ> putStrLn $ decodeUtf8 encoded
[{"op":"add","path":"/123","value":"asdf"}]
This "path" seems to be ambiguous, because when we try to decode it:
λ> decoded
Patch {patchOperations = [Add {changePointer = Pointer {pointerPath = [AKey 123]}, changeValue = String "asdf"}]}
The pointerPath
is read in as an AKey
rather than the expected OKey
. As a result, it fails to apply:
λ> applied
Error "Cannot follow pointer [123]. Expected array but got {}"
From briefly looking over RFC6902, it seems like the right thing to do is actually not to make a distinction between OKey
and AKey
? It seems like whether a given path component is an "object index" or "array index" should instead depend on which type of JSON object you find during patching. (I.e. if you encounter an array, then try to parse the value as a numeric index, otherwise treat it as a string key for an object.) What do you think?