git.iko.soy/typeable/xml-isogen
on master help sign in
Add Nillable newtype for type that can have null value (#24)
commit92aae10691017a9575dadcaab09009bb1106d8bc authorYuras <shumovichy@gmail.com> dateTue 11 Feb 2020, 15:42 (7 years ago) parent916e2fc
3 files changed+34
src/Data/THGen/XML.hs+2 src/Text/XML/Nillable.hs+31 xml-isogen.cabal+1
src/Data/THGen/XML.hs+2
@@ -124,10 +124,12 @@ module Data.THGen.XML
124124 , Text
125125 , P.Int
126126 , P.Integer
127+ , Nillable(..)
127128 ) where
128129
129130import Data.THGen.Enum
130131import Data.THGen.Orphans ()
131132import Data.THGen.XML.Internal
132133import Data.Text
134+import Text.XML.Nillable
133135import qualified Prelude as P
src/Text/XML/Nillable.hsnew file+31
@@ -0,0 +1,31 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE MonoLocalBinds #-}
3+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+
5+module Text.XML.Nillable
6+where
7+
8+import Control.DeepSeq
9+import Text.XML.DOM.Parser
10+import Text.XML.Writer
11+import Text.XML.ParentAttributes
12+
13+-- | Type that can have explicit null value
14+--
15+-- Null value is indicated by "nil"="true" attribute.
16+newtype Nillable a = Nillable (Maybe a)
17+ deriving (Eq, Show, Ord, Read, NFData, ToXML)
18+
19+instance ToXmlParentAttributes a => ToXmlParentAttributes (Nillable a) where
20+ toXmlParentAttributes (Nillable Nothing) =
21+ [("{http://www.w3.org/2001/XMLSchema-instance}nil", "true")]
22+ toXmlParentAttributes (Nillable (Just a)) = toXmlParentAttributes a
23+
24+instance FromDom a => FromDom (Nillable a) where
25+ fromDom = do
26+ nil <- parseAttributeMaybe
27+ (matchName "{http://www.w3.org/2001/XMLSchema-instance}nil")
28+ Right
29+ case nil of
30+ Just "true" -> return (Nillable Nothing)
31+ _ -> Nillable . Just <$> fromDom
xml-isogen.cabal+1
@@ -32,6 +32,7 @@ library
3232 , Data.THGen.XML
3333 , Data.THGen.XML.Internal
3434 , Text.XML.ParentAttributes
35+ , Text.XML.Nillable
3536 other-modules: Data.THGen.Compat
3637 , Data.THGen.Orphans
3738 , Text.XML.DOM.Parser.Internal.Content