| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Text.Ginger.GVal
Description
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 :: Type -> Type) = GVal {}
- gappend :: forall (m :: Type -> Type). GVal m -> GVal m -> GVal m
- marshalGVal :: forall (m :: Type -> Type) (n :: Type -> Type). 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 :: forall (m :: Type -> Type). GVal m -> Maybe (HashMap Text (GVal m))
- type Function (m :: Type -> Type) = [(Maybe Text, GVal m)] -> m (GVal m)
- matchFuncArgs :: forall (m :: Type -> Type). [Text] -> [(Maybe Text, GVal m)] -> (HashMap Text (GVal m), [GVal m], HashMap Text (GVal m))
- class ToGVal (m :: Type -> Type) a where
- dayToDict :: forall (m :: Type -> Type). Day -> [(Text, GVal m)]
- timeToDict :: forall (m :: Type -> Type). TimeOfDay -> [(Text, GVal m)]
- localTimeToDict :: forall (m :: Type -> Type). LocalTime -> [(Text, GVal m)]
- timeZoneToDict :: forall (m :: Type -> Type). TimeZone -> [(Text, GVal m)]
- timeLocaleToDict :: forall (m :: Type -> Type). TimeLocale -> [(Text, GVal m)]
- zonedTimeToDict :: forall (m :: Type -> Type). ZonedTime -> [(Text, GVal m)]
- scientificToText :: Scientific -> Text
- rawJSONToGVal :: forall (m :: Type -> Type). Value -> GVal m
- fromFunction :: Function m -> GVal m
- type Pair (m :: Type -> Type) = (Text, GVal m)
- dict :: forall (m :: Type -> Type). [Pair m] -> GVal m
- orderedDict :: forall (m :: Type -> Type). [Pair m] -> GVal m
- (~>) :: forall (m :: Type -> Type) a. ToGVal m a => Text -> a -> Pair m
- type Cons (m :: Type -> Type) = [GVal m]
- gcons :: forall (m :: Type -> Type) a. ToGVal m a => a -> Cons m -> Cons m
- (~:) :: forall (m :: Type -> Type) a. ToGVal m a => a -> Cons m -> Cons m
- list :: forall (m :: Type -> Type). Cons m -> GVal m
- isList :: forall (m :: Type -> Type). GVal m -> Bool
- isDict :: forall (m :: Type -> Type). GVal m -> Bool
- lookupIndex :: forall (m :: Type -> Type). Int -> GVal m -> Maybe (GVal m)
- lookupIndexMay :: forall (m :: Type -> Type). Maybe Int -> GVal m -> Maybe (GVal m)
- lookupKey :: forall (m :: Type -> Type). Text -> GVal m -> Maybe (GVal m)
- lookupLoose :: forall (m :: Type -> Type). GVal m -> GVal m -> Maybe (GVal m)
- lookupLooseDef :: forall (m :: Type -> Type). GVal m -> GVal m -> GVal m -> GVal m
- (~!) :: forall (m :: Type -> Type) v. FromGVal m v => GVal m -> GVal m -> Maybe v
- keys :: forall (m :: Type -> Type). GVal m -> Maybe [Text]
- toNumber :: forall (m :: Type -> Type). GVal m -> Maybe Scientific
- toInt :: forall (m :: Type -> Type). GVal m -> Maybe Int
- toInteger :: forall (m :: Type -> Type). GVal m -> Maybe Integer
- toIntDef :: forall (m :: Type -> Type). Int -> GVal m -> Int
- toInt0 :: forall (m :: Type -> Type). GVal m -> Int
- toBoolean :: forall (m :: Type -> Type). GVal m -> Bool
- toFunction :: GVal m -> Maybe (Function m)
- picoToScientific :: Pico -> Scientific
- scientificToPico :: Scientific -> Pico
- class FromGVal (m :: Type -> Type) 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
data GVal (m :: Type -> Type) Source #
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
isNullisTrue, then all ofasFunction,asText,asNumber,asHtml,asList,asDictItems, andlengthshould produceNothing - when
isNullisTrue, thenasBooleanshould produceFalse - when
asNumberis notNothing, thenasBooleanshould only returnFalsefor exactly zero Nothing-ness oflengthshould match one or both ofasList/asDictItems
Constructors
| GVal | |
Fields
| |
Instances
| FromGVal m (GVal m) Source # | |
| ToGVal m (GVal m) Source # | Trivial instance for |
| ToJSON (GVal m) Source # | Conversion to JSON values attempts the following conversions, in order:
Note that the default conversions will never return booleans unless |
| IsString (GVal m) Source # |
|
Defined in Text.Ginger.GVal Methods fromString :: String -> GVal m # | |
| Show (GVal m) Source # | For convenience, |
| PrintfArg (GVal m) Source # | |
Defined in Text.Ginger.GVal | |
| 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 :: forall (m :: Type -> Type) (n :: Type -> Type). 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 :: forall (m :: Type -> Type). GVal m -> Maybe (HashMap Text (GVal m)) Source #
Convenience wrapper around asDictItems to represent a GVal as a HashMap.
Representing functions as GVals
type Function (m :: Type -> Type) = [(Maybe Text, GVal m)] -> m (GVal m) Source #
A function that can be called from within a template execution context.
matchFuncArgs :: forall (m :: Type -> Type). [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 :: Type -> Type) a where Source #
Types that implement conversion to GVal.
Instances
| ToGVal m Value Source # | Convert Aeson |
| ToGVal m ByteString Source # | |
Defined in Text.Ginger.GVal Methods toGVal :: ByteString -> GVal m Source # | |
| ToGVal m ByteString Source # | |
Defined in Text.Ginger.GVal Methods toGVal :: ByteString -> GVal m Source # | |
| ToGVal m Html Source # | This instance is slightly wrong; the It is therefore recommended to avoid passing |
| ToGVal m SourcePos Source # | |
| ToGVal m Scientific Source # | |
Defined in Text.Ginger.GVal Methods toGVal :: Scientific -> GVal m Source # | |
| ToGVal m Text Source # | |
| ToGVal m Text Source # | |
| ToGVal m Day Source # | |
| ToGVal m TimeLocale Source # | |
Defined in Text.Ginger.GVal Methods toGVal :: TimeLocale -> GVal m Source # | |
| ToGVal m LocalTime Source # | |
| ToGVal m TimeOfDay Source # | |
| ToGVal m TimeZone Source # | |
| ToGVal m ZonedTime Source # | |
| ToGVal m Integer Source # | |
| ToGVal m () Source # | |
Defined in Text.Ginger.GVal | |
| ToGVal m Bool Source # | Booleans render as 1 or empty string, and otherwise behave as expected. |
| ToGVal m Char Source # | Single characters are treated as length-1 |
| ToGVal m Int Source # | |
| ToGVal m (KeyMap Value) Source # | |
| ToGVal m (GVal m) Source # | Trivial instance for |
| ToGVal m p => ToGVal m (RuntimeError p) Source # | |
Defined in Text.Ginger.Run.Type Methods toGVal :: RuntimeError p -> GVal m Source # | |
| ToGVal m v => ToGVal m (Maybe v) Source # | |
| ToGVal m v => ToGVal m [v] Source # | Haskell lists become list-like |
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 (a, b) Source # | |
Defined in Text.Ginger.GVal | |
| (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 :: forall (m :: Type -> Type). 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 Scientifics look like integers.
Convenience API for constructing heterogenous dictionaries.
type Pair (m :: Type -> Type) = (Text, GVal m) Source #
A key/value pair, used for constructing dictionary GVals using a compact syntax.
dict :: forall (m :: Type -> Type). [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 :: forall (m :: Type -> Type). [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.
(~>) :: forall (m :: Type -> Type) a. ToGVal m a => Text -> a -> Pair m infixr 8 Source #
Construct a pair from a key and a value.
Convenience API for constructing heterogenous lists
(~:) :: forall (m :: Type -> Type) a. 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 :: forall (m :: Type -> Type). 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 GVals / Marshalling GVal to Haskell
isList :: forall (m :: Type -> Type). GVal m -> Bool Source #
Check if the given GVal is a list-like object
isDict :: forall (m :: Type -> Type). GVal m -> Bool Source #
Check if the given GVal is a dictionary-like object
lookupIndexMay :: forall (m :: Type -> Type). 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 :: forall (m :: Type -> Type). 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 :: forall (m :: Type -> Type). 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 :: forall (m :: Type -> Type). 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 :: forall (m :: Type -> Type). GVal m -> Maybe [Text] Source #
Treat a GVal as a dictionary and list all the keys, with no particular ordering.
toNumber :: forall (m :: Type -> Type). GVal m -> Maybe Scientific Source #
Convert a GVal to a number.
toBoolean :: forall (m :: Type -> Type). 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 :: Type -> Type) a where Source #
Minimal complete definition
Nothing