11{-# OPTIONS -Wwarn=orphans #-}
2+ {-# LANGUAGE BlockArguments #-}
23{-# LANGUAGE DeriveAnyClass #-}
34{-# LANGUAGE DeriveFunctor #-}
45{-# LANGUAGE DeriveGeneric #-}
@@ -23,7 +24,13 @@ module FF.Types where
2324
2425import Control.Monad.Except (throwError )
2526import Data.Aeson qualified as JSON
26- import Data.Aeson.Extra (ToJSON , singletonObjectSum , toJSON , (.=) )
27+ import Data.Aeson.Extra (
28+ ToJSON ,
29+ fieldLabelModifier ,
30+ singletonObjectSum ,
31+ toJSON ,
32+ (.=) ,
33+ )
2734import Data.Aeson.Key qualified as JSON.Key
2835import Data.Aeson.KeyMap qualified as JSON.KeyMap
2936import Data.Aeson.TH (defaultOptions , deriveToJSON )
@@ -56,11 +63,7 @@ import RON.Data.Time (Day)
5663import RON.Error (Error (Error ))
5764import RON.Schema.TH (mkReplicated )
5865import RON.Storage (Collection , DocId , collectionName , loadDocument )
59- import RON.Storage.Backend (
60- DocId (DocId ),
61- Document (Document ),
62- MonadStorage ,
63- )
66+ import RON.Storage.Backend (DocId (DocId ), Document (Document ), MonadStorage )
6467import RON.Storage.Backend qualified
6568import RON.Text.Serialize (serializeUuid )
6669import RON.Types (Atom (AUuid ), ObjectRef , UUID )
@@ -254,28 +257,33 @@ data instance View Note = NoteView
254257 }
255258 deriving (Eq , Show )
256259
257- deriveToJSON defaultOptions ''LinkType
258- deriveToJSON defaultOptions ''Status
259- deriveToJSON singletonObjectSum ''NoteStatus
260- deriveToJSON defaultOptions ''Tag
261- deriveToJSON defaultOptions ''Track
262-
263- $ (fold <$> for [''Link, ''Note] (deriveToJSON defaultOptions))
260+ $ ( fold
261+ <$> for
262+ [ (''Note, defaultOptions{fieldLabelModifier = drop 5 })
263+ , (''NoteStatus, singletonObjectSum)
264+ , (''Status, defaultOptions)
265+ , (''Tag, defaultOptions{fieldLabelModifier = drop 4 })
266+ , (''Track, defaultOptions{fieldLabelModifier = drop 6 })
267+ ]
268+ \ (name, options) -> deriveToJSON options name
269+ )
264270
265271instance ToJSON (View Note ) where
266272 toJSON NoteView {note, tags} =
267- JSON. Object $ JSON.KeyMap. insert " note_tags " tags' noteObj
273+ modifyObject ( JSON.KeyMap. insert " tags " tags') $ toJSON note
268274 where
269- noteObj = case toJSON note of
270- JSON. Object obj -> obj
271- _ -> error " Note must be serialized to Object"
272275 tags' =
273276 JSON. Object
274277 . JSON.KeyMap. fromMap
275278 . Map. mapKeysMonotonic JSON.Key. fromText
276279 . fmap JSON. String
277280 $ tags
278281
282+ modifyObject :: (JSON. Object -> JSON. Object ) -> JSON. Value -> JSON. Value
283+ modifyObject f val = case val of
284+ JSON. Object obj -> JSON. Object $ f obj
285+ _ -> error " Note must be serialized to Object"
286+
279287type ModeMap = Map TaskMode
280288
281289-- | Sub-status of an 'Active' task from the perspective of the user.
0 commit comments