Skip to content

Diffing and then patching a Set fails #4

@thomasjm

Description

@thomasjm

I'm seeing a problem when I try to diff a change to a Set Text. I think the issue is that the diff is performed by turning the set into JSON and passing it to Data.Aeson.Diff, which then treats the set like a list, and produces a patch with an index that's too large for the FieldLens instance for Set.

Note that this is case-sensitive; if you switch the roles of "a" and "b" then it succeeds.

I think maybe a valid fix would be for the FieldLens Set instance to accept a key equal to the size of the set?

{-# LANGUAGE DeriveGeneric, TemplateHaskell, OverloadedStrings, QuasiQuotes #-}

import Data.Aeson
import Data.Aeson.Diff
import Data.Aeson.Diff.Generic as ADG
import Data.Set as Set
import Data.String.Interpolate
import Relude

main :: IO ()
main = do
  let orig = Set.fromList ["a" :: Text]
  let new = Set.fromList ["a", "b" :: Text]
  let p = diff (toJSON orig) (toJSON new)
  putStrLn [i|Patch: #{p}|]

  let result = ADG.patch p orig
  putStrLn [i|Result: #{result}|]

Output:

> main
Patch: Patch {patchOperations = [Add {changePointer = Pointer {pointerPath = [AKey 1]}, changeValue = String "b"}]}
Result: Error "Operation Add on /1 failed: Index out of bounds"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions