HsYAML-0.2.1.5: Pure Haskell YAML 1.2 processor

Copyright | © Herbert Valerio Riedel 2015-2018
License | GPL-2.0-or-later
Safe Haskell | Safe
Language | Haskell2010

Data.YAML.Schema

Contents

- Schema resolvers
  - YAML 1.2 Schema resolvers
- Schema encoders
  - YAML 1.2 Schema encoders
  - Custom Schema encoding

Description

Predefined YAML 1.2 Schema resolvers and encoders as well as support for defining custom resolvers and encoders.

Since: 0.2.0.0

Synopsis

- data SchemaResolver = SchemaResolver {
  - schemaResolverScalar :: Tag -> ScalarStyle -> Text -> Either String Scalar
  - schemaResolverSequence :: Tag -> Either String Tag
  - schemaResolverMapping :: Tag -> Either String Tag
  - schemaResolverMappingDuplicates :: Bool
  }
- failsafeSchemaResolver :: SchemaResolver
- jsonSchemaResolver :: SchemaResolver
- coreSchemaResolver :: SchemaResolver
- data SchemaEncoder = SchemaEncoder {
  - schemaEncoderScalar :: Scalar -> Either String (Tag, ScalarStyle, Text)
  - schemaEncoderSequence :: Tag -> Either String Tag
  - schemaEncoderMapping :: Tag -> Either String Tag
  }
- failsafeSchemaEncoder :: SchemaEncoder
- jsonSchemaEncoder :: SchemaEncoder
- coreSchemaEncoder :: SchemaEncoder
- setScalarStyle :: (Scalar -> Either String (Tag, ScalarStyle, Text)) -> SchemaEncoder -> SchemaEncoder
- isPlainChar :: Char -> Bool
- isAmbiguous :: SchemaResolver -> Text -> Bool
- encodeDouble :: Double -> Text
- encodeBool :: Bool -> Text
- encodeInt :: Integer -> Text

# Schema resolvers

## YAML 1.2 Schema resolvers

data SchemaResolver Source #

Definition of a YAML 1.2 Schema

A YAML schema defines how implicit tags are resolved to concrete tags and how data is represented textually in YAML.

Constructors

SchemaResolver |

Fields

- schemaResolverScalar :: Tag -> ScalarStyle -> Text -> Either String Scalar
- schemaResolverSequence :: Tag -> Either String Tag
- schemaResolverMapping :: Tag -> Either String Tag
- schemaResolverMappingDuplicates :: Bool

failsafeSchemaResolver :: SchemaResolver Source #

"Failsafe" schema resolver as specified in YAML 1.2 / 10.1.2. Tag Resolution

jsonSchemaResolver :: SchemaResolver Source #

Strict JSON schema resolver as specified in YAML 1.2 / 10.2.2. Tag Resolution

coreSchemaResolver :: SchemaResolver Source #

Core schema resolver as specified in YAML 1.2 / 10.3.2. Tag Resolution

# Schema encoders

## YAML 1.2 Schema encoders

data SchemaEncoder Source #

Since: 0.2.0

Constructors

SchemaEncoder |

Fields

- schemaEncoderScalar :: Scalar -> Either String (Tag, ScalarStyle, Text)
- schemaEncoderSequence :: Tag -> Either String Tag
- schemaEncoderMapping :: Tag -> Either String Tag

failsafeSchemaEncoder :: SchemaEncoder Source #

"Failsafe" schema encoder as specified in YAML 1.2 / 10.1.2. Tag Resolution

Since: 0.2.0

jsonSchemaEncoder :: SchemaEncoder Source #

Strict JSON schema encoder as specified in YAML 1.2 / 10.2.2. Tag Resolution

Since: 0.2.0

coreSchemaEncoder :: SchemaEncoder Source #

Core schema encoder as specified in YAML 1.2 / 10.3.2. Tag Resolution

Since: 0.2.0

## Custom Schema encoding

According to YAML 1.2 the recommended default `SchemaEncoder` is `coreSchemaEncoder` under which `Scalar`s are encoded as follows:

- String which are made of Plain Characters (see `isPlainChar`), unambiguous (see `isAmbiguous`) and do not contain any leading/trailing spaces are encoded as `Plain` `Scalar`.
- Rest of the strings are encoded in DoubleQuotes
- Booleans are encoded using `encodeBool`
- Double values are encoded using `encodeDouble`
- Integral values are encoded using `encodeInt`

setScalarStyle :: (Scalar -> Either String (Tag, ScalarStyle, Text)) -> SchemaEncoder -> SchemaEncoder Source #

Set the `Scalar` style in the encoded YAML. This is a function that decides for each `Scalar` the type of YAML string to output.

WARNING: You must ensure that special strings (like `"true"`/`"false"`/`"null"`/`"1234"`) are not encoded with the `Plain` style, because then they will be decoded as boolean, null or numeric values. You can use `isAmbiguous` to detect them.

NOTE: For different `SchemaResolver`s, different strings are ambiguous. For example, `"true"` is not ambiguous for `failsafeSchemaResolver`.

Since: 0.2.0

isPlainChar :: Char -> Bool Source #

These are some characters which can be used in `Plain` `Scalar`s safely without any quotes (see Indicator Characters).

NOTE: This does not mean that other characters (like `"\n"` and other special characters like `"-?:,[]{}#&*!,>%@`"'"`) cannot be used in `Plain` `Scalar`s.

Since: 0.2.0

isAmbiguous :: SchemaResolver -> Text -> Bool Source #

Returns True if the string can be decoded by the given `SchemaResolver` into a `Scalar` which is not a of type `SStr`.

    >>> isAmbiguous coreSchemaResolver "true"
    True

    >>> isAmbiguous failSchemaResolver "true"
    False

Since: 0.2.0

encodeDouble :: Double -> Text Source #

Encode Double

Since: 0.2.0

encodeBool :: Bool -> Text Source #

Encode Boolean

Since: 0.2.0

encodeInt :: Integer -> Text Source #

Encode Integer

Since: 0.2.0