No title

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
data Item = Item
    { _item_title :: Text
    , _item_author :: Text
    } deriving (Eq, Show)
makeLenses ''Item

-- Way one

data ItemModification
    = UpdateTitle Text
    | UpdateAuthor Text
data ItemPatch = ItemPatch [ItemModification]
instance Patch ItemPatch where
    type PatchTarget ItemPatch = Item
    apply (ItemPatch mods) orig = Just $ mods <&> (\case (UpdateTitle t) -> item_title .~ t; (UpdateAuthor t) -> item_author .~ t) & foldl (&) orig


-- Way two

data ItemTag a where
    TitleTag :: ItemTag Text
    AuthorTag :: ItemTag Text

deriveGEq ''ItemTag
deriveGCompare ''ItemTag

data ItemPatch2 = ItemPatch2 (DMap ItemTag Identity)

instance Patch ItemPatch2 where
    type PatchTarget ItemPatch2 = Item
    apply (ItemPatch2 mods) orig = Just $
        [ (item_title .~) <$> (mods & DM.lookup TitleTag <&> runIdentity)
        , (item_author .~) <$> (mods & DM.lookup AuthorTag <&> runIdentity) ] & catMaybes & foldl (&) orig