Safe Haskell | None |
---|---|
Language | Haskell2010 |
GVal is a generic unitype value, representing the kind of values that Ginger can understand.
Most of the types in this module are parametrized over an m
type, which
is the host monad for template execution, as passed to runGingerT
. For
most kinds of values, m
is transparent, and in many cases a ToGVal
instance can be written that works for all possible m
; the reason we need
to parametrize the values themselves over the carrier monad is because we
want to support impure functions, which requires access to the underlying
carrier monad (e.g. IO
).
Synopsis
- data GVal m = GVal {}
- gappend :: GVal m -> GVal m -> GVal m
- marshalGVal :: GVal m -> GVal n
- marshalGValEx :: (Functor m, Functor n) => (forall a. m a -> n a) -> (forall a. n a -> m a) -> GVal m -> GVal n
- marshalFunction :: (Functor m, Functor n) => (forall a. m a -> n a) -> (forall a. n a -> m a) -> Function m -> Function n
- asHashMap :: GVal m -> Maybe (HashMap Text (GVal m))
- type Function m = [(Maybe Text, GVal m)] -> m (GVal m)
- matchFuncArgs :: [Text] -> [(Maybe Text, GVal m)] -> (HashMap Text (GVal m), [GVal m], HashMap Text (GVal m))
- class ToGVal m a where
- dayToDict :: Day -> [(Text, GVal m)]
- timeToDict :: TimeOfDay -> [(Text, GVal m)]
- localTimeToDict :: LocalTime -> [(Text, GVal m)]
- timeZoneToDict :: TimeZone -> [(Text, GVal m)]
- timeLocaleToDict :: TimeLocale -> [(Text, GVal m)]
- zonedTimeToDict :: ZonedTime -> [(Text, GVal m)]
- scientificToText :: Scientific -> Text
- rawJSONToGVal :: Value -> GVal m
- fromFunction :: Function m -> GVal m
- type Pair m = (Text, GVal m)
- dict :: [Pair m] -> GVal m
- orderedDict :: [Pair m] -> GVal m
- (~>) :: ToGVal m a => Text -> a -> Pair m
- type Cons m = [GVal m]
- gcons :: ToGVal m a => a -> Cons m -> Cons m
- (~:) :: ToGVal m a => a -> Cons m -> Cons m
- list :: Cons m -> GVal m
- isList :: GVal m -> Bool
- isDict :: GVal m -> Bool
- lookupIndex :: Int -> GVal m -> Maybe (GVal m)
- lookupIndexMay :: Maybe Int -> GVal m -> Maybe (GVal m)
- lookupKey :: Text -> GVal m -> Maybe (GVal m)
- lookupLoose :: GVal m -> GVal m -> Maybe (GVal m)
- lookupLooseDef :: GVal m -> GVal m -> GVal m -> GVal m
- (~!) :: FromGVal m v => GVal m -> GVal m -> Maybe v
- keys :: GVal m -> Maybe [Text]
- toNumber :: GVal m -> Maybe Scientific
- toInt :: GVal m -> Maybe Int
- toInteger :: GVal m -> Maybe Integer
- toIntDef :: Int -> GVal m -> Int
- toInt0 :: GVal m -> Int
- toBoolean :: GVal m -> Bool
- toFunction :: GVal m -> Maybe (Function m)
- picoToScientific :: Pico -> Scientific
- scientificToPico :: Scientific -> Pico
- class FromGVal m a where
- fromGValM :: (MonadFail m, FromGVal m a) => GVal m -> m a
- pairwise :: (a -> b) -> (a, a) -> (b, b)
- packPair :: ([Char], [Char]) -> (Text, Text)
- unpackPair :: (Text, Text) -> ([Char], [Char])
The Ginger Value type
A variant type designed as the unitype for the template language. Any
value referenced in a template, returned from within a template, or used
in a template context, will be a GVal
.
m
, in most cases, should be a Monad
.
Some laws apply here, most notably:
- when
isNull
isTrue
, then all ofasFunction
,asText
,asNumber
,asHtml
,asList
,asDictItems
, andlength
should produceNothing
- when
isNull
isTrue
, thenasBoolean
should produceFalse
- when
asNumber
is notNothing
, thenasBoolean
should only returnFalse
for exactly zero Nothing
-ness oflength
should match one or both ofasList
/asDictItems
GVal | |
|
Instances
FromGVal m (GVal m) Source # | |
ToGVal m (GVal m) Source # | Trivial instance for |
Show (GVal m) Source # | For convenience, |
IsString (GVal m) Source # |
|
Defined in Text.Ginger.GVal fromString :: String -> GVal m # | |
ToJSON (GVal m) Source # | Conversion to JSON values attempts the following conversions, in order:
Note that the default conversions will never return booleans unless |
Defined in Text.Ginger.GVal | |
PrintfArg (GVal m) Source # | |
Defined in Text.Ginger.GVal formatArg :: GVal m -> FieldFormatter # parseFormat :: GVal m -> ModifierParser # | |
Default (GVal m) Source # | The default |
Defined in Text.Ginger.GVal | |
ToHtml (GVal m) Source # | Converting to HTML hooks into the ToHtml instance for |
marshalGVal :: GVal m -> GVal n Source #
Marshal a GVal between carrier monads.
This will lose asFunction
information, because functions cannot be
transferred to other carrier monads, but it will keep all other data
structures intact.
marshalGValEx :: (Functor m, Functor n) => (forall a. m a -> n a) -> (forall a. n a -> m a) -> GVal m -> GVal n Source #
Marshal a GVal between carrier monads.
Unlike marshalGVal
, asFunction
information is retained by hoisting
them using the provided hoisting functions. For Run
monads, which is
what GVal
is typically used with, the hoistRun
function can be used
to construct suitable hoisting functions.
marshalFunction :: (Functor m, Functor n) => (forall a. m a -> n a) -> (forall a. n a -> m a) -> Function m -> Function n Source #
asHashMap :: GVal m -> Maybe (HashMap Text (GVal m)) Source #
Convenience wrapper around asDictItems
to represent a GVal
as a
HashMap
.
Representing functions as GVal
s
type Function m = [(Maybe Text, GVal m)] -> m (GVal m) Source #
A function that can be called from within a template execution context.
matchFuncArgs :: [Text] -> [(Maybe Text, GVal m)] -> (HashMap Text (GVal m), [GVal m], HashMap Text (GVal m)) Source #
Match arguments passed to a function at runtime against a list of declared
argument names.
matchFuncArgs argNames argsPassed
returns (matchedArgs, positionalArgs, namedArgs)
,
where matchedArgs
is a list of arguments matched against declared names
(by name or by position), positionalArgs
are the unused positional
(unnamed) arguments, and namedArgs
are the unused named arguments.
Marshalling from Haskell to GVal
class ToGVal m a where Source #
Types that implement conversion to GVal
.
Instances
ToGVal m Value Source # | Convert Aeson |
ToGVal m Html Source # | This instance is slightly wrong; the It is therefore recommended to avoid passing |
ToGVal m ByteString Source # | |
Defined in Text.Ginger.GVal toGVal :: ByteString -> GVal m Source # | |
ToGVal m ByteString Source # | |
Defined in Text.Ginger.GVal toGVal :: ByteString -> GVal m Source # | |
ToGVal m Text Source # | |
ToGVal m Text Source # | |
ToGVal m Char Source # | Single characters are treated as length-1 |
ToGVal m Bool Source # | Booleans render as 1 or empty string, and otherwise behave as expected. |
ToGVal m ZonedTime Source # | |
ToGVal m TimeLocale Source # | |
Defined in Text.Ginger.GVal toGVal :: TimeLocale -> GVal m Source # | |
ToGVal m TimeZone Source # | |
ToGVal m LocalTime Source # | |
ToGVal m TimeOfDay Source # | |
ToGVal m Day Source # | |
ToGVal m Scientific Source # | |
Defined in Text.Ginger.GVal toGVal :: Scientific -> GVal m Source # | |
ToGVal m Integer Source # | |
ToGVal m Int Source # | |
ToGVal m () Source # | |
Defined in Text.Ginger.GVal | |
ToGVal m SourcePos Source # | |
ToGVal m v => ToGVal m [v] Source # | Haskell lists become list-like |
Defined in Text.Ginger.GVal | |
ToGVal m v => ToGVal m (Maybe v) Source # | |
ToGVal m (GVal m) Source # | Trivial instance for |
ToGVal m p => ToGVal m (RuntimeError p) Source # | |
Defined in Text.Ginger.Run.Type toGVal :: RuntimeError p -> GVal m Source # | |
(ToGVal m a, ToGVal m b) => ToGVal m (a, b) Source # | |
Defined in Text.Ginger.GVal | |
ToGVal m v => ToGVal m (Map Text v) Source # | |
ToGVal m v => ToGVal m (HashMap Text v) Source # | |
(ToGVal m a, ToGVal m b, ToGVal m c) => ToGVal m (a, b, c) Source # | |
Defined in Text.Ginger.GVal | |
(ToGVal m a, ToGVal m b, ToGVal m c, ToGVal m d) => ToGVal m (a, b, c, d) Source # | |
Defined in Text.Ginger.GVal |
timeLocaleToDict :: TimeLocale -> [(Text, GVal m)] Source #
scientificToText :: Scientific -> Text Source #
Silly helper function, needed to bypass the default Show
instance of
Scientific
in order to make integral Scientific
s look like integers.
rawJSONToGVal :: Value -> GVal m Source #
Convenience API for constructing heterogenous dictionaries.
type Pair m = (Text, GVal m) Source #
A key/value pair, used for constructing dictionary GVals using a compact syntax.
dict :: [Pair m] -> GVal m Source #
Construct a dictionary GVal from a list of pairs. Internally, this uses a hashmap, so element order will not be preserved.
orderedDict :: [Pair m] -> GVal m Source #
Construct an ordered dictionary GVal from a list of pairs. Internally, this conversion uses both a hashmap (for O(1) lookup) and the original list, so element order is preserved, but there is a bit of a memory overhead.
Convenience API for constructing heterogenous lists
(~:) :: ToGVal m a => a -> Cons m -> Cons m infixr 5 Source #
This operator allows constructing heterogenous lists using cons-style syntax, e.g.:
>>>
asText $ list ("Found " ~: (6 :: Int) ~: " items" ~: [] :: [GVal IO])
"Found 6 items"
list :: Cons m -> GVal m Source #
Construct a GVal from a list of GVals. This is equivalent to the toGVal
implementation of [GVal m]
, but typed more narrowly for clarity and
disambiguation.
Inspecting GVal
s / Marshalling GVal
to Haskell
lookupIndexMay :: Maybe Int -> GVal m -> Maybe (GVal m) Source #
Helper function; look up a value by an integer index when the index may or
may not be available. If no index is given, return Nothing
.
lookupKey :: Text -> GVal m -> Maybe (GVal m) Source #
Strictly-typed lookup: treat value as a dictionary-like object and look up the value at a given key.
lookupLoose :: GVal m -> GVal m -> Maybe (GVal m) Source #
Loosely-typed lookup: try dictionary-style lookup first (treat index as a string, and container as a dictionary), if that doesn't yield anything (either because the index is not string-ish, or because the container doesn't provide dictionary-style access), try index-based lookup.
lookupLooseDef :: GVal m -> GVal m -> GVal m -> GVal m Source #
Like lookupLoose
, but fall back to the given default value if
the key is not in the dictionary, or if the indexee is not a
dictionary-like object.
keys :: GVal m -> Maybe [Text] Source #
Treat a GVal
as a dictionary and list all the keys, with no particular
ordering.
toBoolean :: GVal m -> Bool Source #
Loose cast to boolean.
Numeric zero, empty strings, empty lists, empty objects, Null
, and boolean
False
are considered falsy, anything else (including functions) is
considered true-ish.
picoToScientific :: Pico -> Scientific Source #
scientificToPico :: Scientific -> Pico Source #
class FromGVal m a where Source #
Nothing