dryvPacks

@dryv/haskell-client-sdkv0.1.0

Typed Haskell HTTP client with Aeson wire models and explicit operation functions.

Haskell Client SDKClientsUnitPublished 7 days ago2 downloads

dryv pack add @dryv/haskell-client-sdk
7.3 KBRaw ↗
jinja
{-# LANGUAGE OverloadedStrings #-}

module {{ file.path.relative | replace('src/', '') | replace('/', '.') | replace('.hs', '') }}
  ( {{ file.symbol }}(..)
  , validate{{ file.symbol }}
  ) where

import Data.Aeson (FromJSON(..), ToJSON(..), Value, object, withObject, (.:), (.:?), (.=))
import Data.Int (Int64)
import Data.Maybe (catMaybes)
import Data.Scientific (Scientific)
import Data.Text (Text)
import qualified Data.Text as T
import Text.Regex.TDFA ((=~))
{%- for dependency in file.dependencies %}
{%- if dependency.symbol and not dependency.is_self %}
import qualified {{ dependency.path.relative | replace('src/', '') | replace('/', '.') | replace('.hs', '') }} as Dep{{ loop.index }}
{%- endif %}
{%- endfor %}

{%- macro hs_ident(name) -%}
{%- if name in ['case','class','data','default','deriving','do','else','foreign','if','import','in','infix','infixl','infixr','instance','let','module','newtype','of','then','type','where','qualified','as','hiding','forall','mdo','family','role','pattern','static','group','by','using','stock','anyclass','via'] -%}{{ name }}_{%- else -%}{{ name }}{%- endif -%}
{%- endmacro -%}

{%- macro dep_symbol(link) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id and dependency.symbol and not dependency.is_self -%}Dep{{ loop.index }}.{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}

{%- macro dep_validator(link) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id and dependency.symbol and not dependency.is_self -%}Dep{{ loop.index }}.validate{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}

{%- macro base_type(value) -%}
{%- if value is none or value.is.unknown or value.is.dynamic or value.is.generic or value.is.composite or value.is.union or value.is.tuple or value.is.literal or value.is.record -%}Value
{%- elif value.is.schema -%}{{ dep_symbol(value.schema) or 'Value' }}
{%- elif value.is.enum -%}{{ dep_symbol(value.property.enum) or 'Value' }}
{%- elif value.is.boolean -%}Bool
{%- elif value.is.integer -%}Int64
{%- elif value.is.number or value.is.decimal or value.is.money -%}Scientific
{%- elif value.is.string or value.is.temporal or value.is.uuid -%}Text
{%- else -%}Value
{%- endif -%}
{%- endmacro -%}

{%- macro storage_type(field) -%}
{%- if field.array is not none -%}[{{ base_type(field.value) }}]{%- else -%}{{ base_type(field.value) }}{%- endif -%}
{%- endmacro -%}

{%- macro field_type(field) -%}
{%- if field.optional or field.nullable -%}Maybe {{ storage_type(field) }}{%- else -%}{{ storage_type(field) }}{%- endif -%}
{%- endmacro -%}

{%- macro format_regex(value) -%}
{%- if value.is.uuid -%}^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$
{%- elif value.is.ulid -%}^[0-9A-HJKMNP-TV-Z]{26}$
{%- elif value.is.e164 -%}^\+[1-9][0-9]{1,14}$
{%- elif value.is.phone -%}^\+?[0-9 ().-]{7,20}$
{%- elif value.is.slug -%}^[a-z0-9]+(?:-[a-z0-9]+)*$
{%- elif value.is.hex_color -%}^#?(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
{%- elif value.is.hex -%}^[0-9a-fA-F]+$
{%- elif value.is.jwt -%}^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$
{%- elif value.is.semver -%}^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$
{%- elif value.is.country_code -%}^[A-Z]{2}$
{%- elif value.is.currency_code -%}^[A-Z]{3}$
{%- endif -%}
{%- endmacro -%}

{%- macro scalar_checks(field, expression, label) -%}
[]
{%- set value = field.value -%}
{%- if value.is.string -%}
{%- set constraints = value.property.primitive.constraints -%}
{%- set length = constraints.length -%}
{%- if length is not none and length.has_exact %}
  ++ [{{ label | tojson }} <> " must contain exactly {{ length.exact }} characters" | T.length {{ expression }} /= {{ length.exact }}]
{%- endif %}
{%- if length is not none and length.has_min %}
  ++ [{{ label | tojson }} <> " is too short" | T.length {{ expression }} < {{ length.min }}]
{%- endif %}
{%- if length is not none and length.has_max %}
  ++ [{{ label | tojson }} <> " is too long" | T.length {{ expression }} > {{ length.max }}]
{%- endif %}
{%- if value.is.email %}
  ++ [{{ label | tojson }} <> " must be a valid email" | not ((T.unpack {{ expression }}) =~ ("^[^@[:space:]]+@[^@[:space:]]+\\.[^@[:space:]]+$" :: String))]
{%- elif value.is.url or value.is.uri %}
  ++ [{{ label | tojson }} <> " must be a valid URL" | not ((T.unpack {{ expression }}) =~ ("^[A-Za-z][A-Za-z0-9+.-]*://[^[:space:]]+$" :: String))]
{%- endif %}
{%- if format_regex(value) %}
  ++ [{{ label | tojson }} <> " has invalid format" | not ((T.unpack {{ expression }}) =~ ({{ format_regex(value) | tojson }} :: String))]
{%- endif %}
{%- if constraints.pattern is not none %}
{%- for pattern in constraints.pattern.match %}
  ++ [{{ label | tojson }} <> " does not match required pattern" | not ((T.unpack {{ expression }}) =~ ({{ pattern | tojson }} :: String))]
{%- endfor %}
{%- endif %}
{%- elif value.is.integer or value.is.number or value.is.decimal or value.is.money -%}
{%- set range = value.property.primitive.constraints.range -%}
{%- if range is not none and range.has_min %}
  ++ [{{ label | tojson }} <> " is below minimum" | {{ expression }} < {{ range.min }}]
{%- endif %}
{%- if range is not none and range.has_max %}
  ++ [{{ label | tojson }} <> " is above maximum" | {{ expression }} > {{ range.max }}]
{%- endif %}
{%- endif -%}
{%- endmacro -%}

{%- macro value_checks(field, expression) -%}
{{ scalar_checks(field, expression, field.name.raw) }}
{%- if field.value.is.schema and dep_validator(field.value.schema) %}
  ++ {{ dep_validator(field.value.schema) }} {{ expression }}
{%- endif -%}
{%- endmacro -%}

{%- macro field_checks(field, expression) -%}
{%- if field.array is not none -%}
[]
{%- if field.array.has_min %}
  ++ [{{ field.name.raw | tojson }} <> " has too few items" | length {{ expression }} < {{ field.array.min }}]
{%- endif %}
{%- if field.array.has_max %}
  ++ [{{ field.name.raw | tojson }} <> " has too many items" | length {{ expression }} > {{ field.array.max }}]
{%- endif %}
  ++ concatMap (\\item -> {{ value_checks(field, 'item') }}) {{ expression }}
{%- else -%}
{{ value_checks(field, expression) }}
{%- endif -%}
{%- endmacro -%}

data {{ file.symbol }} = {{ file.symbol }}
  {
{%- for field in schema.fields %}
    {{ hs_ident(field.name.camel) }} :: {{ field_type(field) }}{% if not loop.last %},{% endif %}
{%- endfor %}
  } deriving (Eq, Show)

instance FromJSON {{ file.symbol }} where
  parseJSON = withObject "{{ file.symbol }}" $ \o ->
    {{ file.symbol }}
{%- for field in schema.fields %}
      <$> o .{% if field.optional %}:?{% else %}:{% endif %} {{ field.name.raw | tojson }}
{%- endfor %}

instance ToJSON {{ file.symbol }} where
  toJSON value =
    object $ catMaybes
      [
{%- for field in schema.fields %}
{%- if field.optional %}
        ({{ field.name.raw | tojson }} .=) <$> {{ hs_ident(field.name.camel) }} value{% if not loop.last %},{% endif %}
{%- else %}
        Just ({{ field.name.raw | tojson }} .= {{ hs_ident(field.name.camel) }} value){% if not loop.last %},{% endif %}
{%- endif %}
{%- endfor %}
      ]

validate{{ file.symbol }} :: {{ file.symbol }} -> [Text]
validate{{ file.symbol }} value =
  []
{%- for field in schema.fields %}
{%- set accessor = hs_ident(field.name.camel) ~ ' value' %}
{%- if field.optional or field.nullable %}
  ++ maybe [] (\\item -> {{ field_checks(field, 'item') }}) ({{ accessor }})
{%- else %}
  ++ ({{ field_checks(field, accessor) }})
{%- endif %}
{%- endfor %}