Data.YAML.Schema

HsYAML-0.2.1.4: 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 #

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 #

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

jsonSchemaResolver :: SchemaResolver #

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

coreSchemaResolver :: SchemaResolver #

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

# Schema encoders

## YAML 1.2 Schema encoders

data SchemaEncoder #

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 #

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

Since: 0.2.0

jsonSchemaEncoder :: SchemaEncoder #

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

Since: 0.2.0

coreSchemaEncoder :: SchemaEncoder #

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 #

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 #

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 #

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 #

Encode Double

Since: 0.2.0

encodeBool :: Bool -> Text #

Encode Boolean

Since: 0.2.0

encodeInt :: Integer -> Text #

Encode Integer

Since: 0.2.0