git.iko.soy/typeable/xml-isogen
on master help sign in
Fix tests and update haddocks
commitb768c7343b4140a51fdcdcad9034200c8d45bd1b authorDenis Redozubov <denis.redozubov@gmail.com> dateWed 23 Oct 2019, 18:13 (7 years ago) parent56f7eaa
3 files changed+29−7
src/Data/THGen/XML.hs+7−3 src/Data/THGen/XML/Internal.hs+20−2 test/TestDefs.hs+2−2
src/Data/THGen/XML.hs+7−3
@@ -40,7 +40,7 @@ to be either @R@, @G@ or @B@.
4040
4141Example 2.
4242
43−> "Message" =:= record Both
43+> "Message" =:= record Both Lens
4444> ! "author"
4545> + "recipient"
4646> ? "message" [t|Text|]
@@ -78,9 +78,12 @@ the data type to be generated. The prefix for the record fields is
7878inferred automatically by taking all of the uppercase letters in the
7979name. You can override it manually like so:
8080
81−> "Reference" "ref" =:= record Parser
81+> "Reference" "ref" =:= record Parser Lens
8282> ...
8383
84+Second argument to @record@ either enabled lens generation for records fields
85+or disables it for use with DuplicateRecordFields and, possibly, generic-lens.
86+
8487To describe a record field you must supply its name as it appears
8588in the XML tag, prefixed by its repetition kind:
8689
@@ -93,7 +96,7 @@ The type of the field is inferred automatically from its name, so
9396if the field is called @"author"@ its type will be @Author@. You can
9497override the type by specifying it in quasiquotes like so:
9598
96−> "Message" =:= record Both
99+> "Message" =:= record Both NoLens
97100> ! "author" [t|Person|].
98101> ...
99102
@@ -109,6 +112,7 @@ module Data.THGen.XML
109112 , PrefixName(..)
110113 , ExhaustivenessName(..)
111114 , GenType(..)
115+ , GenerateLens(..)
112116 , record
113117 , enum
114118 , (!)
src/Data/THGen/XML/Internal.hs+20−2
@@ -24,9 +24,25 @@ import Text.XML.ParentAttributes
2424import qualified Text.XML.Writer as XW
2525
2626
27+-- | Marks the type of code generation:
28+-- Parser - generate only instances for xml parsing (@FromDom@)
29+-- Generator - generate only @ToXML@ instances
30+-- Both - generate @FromDom@ and @ToXML@ instances
2731data GenType = Parser | Generator | Both
2832
29−data GenerateLens = Lens | NoLens
33+{- | Marks the style of code generation regarding records fields
34+@LensRenaming@ - fields get renamed by prefixing the field name with
35+'_' and lowercased capital letter used in the type name.
36+e.g. for
37+
38+> "MyNewDataType" =:= record genType LensRenaming
39+> * "field1"
40+
41+field will be renamed to `_mndtField1` and lens `mndtField1` will be created.
42+
43+@NoLens@ - no field renaming and no lens generation.
44+-}
45+data GenerateLens = LensRenaming | NoLens
3046
3147data XmlFieldPlural
3248 = XmlFieldPluralMandatory -- Occurs exactly 1 time (Identity)
@@ -264,7 +280,9 @@ isoXmlGenerateDatatype
264280 strName = "Xml" ++ strName'
265281 strPrefix = "x" ++ strPrefix'
266282 name = TH.mkName strName
267− fieldName str = "_" ++ strPrefix ++ over _head C.toUpper str
283+ fieldName str = case generateLens of
284+ LensRenaming -> "_" ++ strPrefix ++ over _head C.toUpper str
285+ NoLens -> str
268286 termDecl <- do
269287 let
270288 fields = do
test/TestDefs.hs+2−2
@@ -22,14 +22,14 @@ import Test.QuickCheck.Instances ()
2222 & "TO"
2323 & "US"
2424
25−"Foo" =:= record Both
25+"Foo" =:= record Both NoLens
2626 ^ "Shmuux" [t|XmlQuux|]
2727 + "Bar"
2828 ? "Baz" [t|Text|]
2929 !% "Quux"
3030 ?% "Muux" [t|XmlQuux|]
3131
32−"Root" =:= record Both
32+"Root" =:= record Both LensRename
3333 ! "{http://example.com/ns/my-namespace}Foo"
3434
3535instance Arbitrary XmlRoot where