Skip to content

Commit 4e70e44

Browse files
authored
Merge pull request #48 from input-output-hk/nc/optics
Add optics for modifying name and comment
2 parents 641b777 + d2b4177 commit 4e70e44

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

cuddle.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ library
5151
Codec.CBOR.Cuddle.CDDL.Resolve
5252
Codec.CBOR.Cuddle.Huddle
5353
Codec.CBOR.Cuddle.Huddle.HuddleM
54+
Codec.CBOR.Cuddle.Huddle.Optics
5455
Codec.CBOR.Cuddle.Parser
5556
Codec.CBOR.Cuddle.Pretty
5657

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- | Optics for mutating Huddle rules
2+
module Codec.CBOR.Cuddle.Huddle.Optics (commentL, nameL) where
3+
4+
import Codec.CBOR.Cuddle.Huddle
5+
import Data.Generics.Product (HasField' (field'))
6+
import Data.Text qualified as T
7+
import Optics.Core
8+
9+
mcommentL ::
10+
(HasField' "description" a (Maybe T.Text)) =>
11+
Lens a a (Maybe T.Text) (Maybe T.Text)
12+
mcommentL = field' @"description"
13+
14+
-- | Traversal to the comment field of a description. Using this we can for
15+
-- example set the comment with 'a & commentL .~ "This is a comment"'
16+
commentL ::
17+
(HasField' "description" a (Maybe T.Text)) =>
18+
AffineTraversal a a T.Text T.Text
19+
commentL = mcommentL % _Just
20+
21+
-- | Lens to the name of a rule (or other named entity). Using this we can
22+
-- for example append to the name with 'a & nameL %~ (<> "_1")'
23+
nameL :: Lens (Named a) (Named a) T.Text T.Text
24+
nameL = field' @"name"

0 commit comments

Comments
 (0)