dryvPacks

@dryv/scotty-backendv0.1.0

Haskell Scotty routes with Aeson models and separated IO service records.

Scotty BackendBackendUnitPublished 7 days ago2 downloads

dryv pack add @dryv/scotty-backend
13.2 KBRaw ↗
jinja
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}

module {{ file.path.relative | replace('src/', '') | replace('/', '.') | replace('.hs', '') }}
  ( {{ file.symbols.service }}(..)
  , {{ file.symbols.register }}
  ) where

import Control.Monad.IO.Class (liftIO)
import Data.Aeson (FromJSON(..), Value, withObject, (.:), (.:?))
import Data.Int (Int64)
import Data.Scientific (Scientific)
import Data.Text (Text)
import Network.HTTP.Types.Method (StdMethod(..))
import Web.Scotty
import Dryv.Scotty
{%- 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 hs_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 schema_field_type(field) -%}
{%- if field.optional or field.nullable -%}Maybe ({% endif -%}{% if field.array is not none %}[{{ hs_type(field.value) }}]{% else %}{{ hs_type(field.value) }}{% endif %}{%- if field.optional or field.nullable -%}){%- endif -%}
{%- endmacro -%}

{%- macro operation_field_type(field) -%}
{%- if field.optional -%}Maybe ({% endif -%}{{ hs_type(field.value) }}{%- if field.optional -%}){%- endif -%}
{%- endmacro -%}

{%- macro whole_binding(http, kind) -%}
{%- for binding in http.inputs -%}
{%- if binding.port.field is none and ((kind == 'body' and binding.is.body) or (kind == 'query' and binding.is.query_object) or (kind == 'form' and binding.is.form)) -%}x{%- endif -%}
{%- endfor -%}
{%- endmacro -%}

{%- macro success_status(http) -%}
{%- set result = namespace(code=200) -%}
{%- for binding in http.outputs -%}{%- set result.code = binding.status -%}{%- endfor -%}
{{- result.code -}}
{%- endmacro -%}

{%- macro field_var(operation, field) -%}{{ operation.name.camel }}{{ field.name.pascal }}Value{%- endmacro -%}

{%- macro raw_var(operation, field) -%}{{ operation.name.camel }}{{ field.name.pascal }}Raw{%- endmacro -%}

{%- macro parse_schema_values(operation, field, source, name) -%}
{%- set raw = raw_var(operation, field) -%}
{%- set target = field_var(operation, field) -%}
  {{ raw }} <- {{ source }} {{ name | tojson }}
{%- if field.array is not none %}
{%- if field.optional or field.nullable %}
  {{ target }} <-
    if null {{ raw }}
      then pure Nothing
      else Just <$> requiredMany {{ field.name.raw | tojson }} {{ raw }}
{%- else %}
  {{ target }} <- requiredMany {{ field.name.raw | tojson }} {{ raw }}
{%- endif %}
{%- else %}
{%- if field.optional or field.nullable %}
  {{ target }} <- optionalFromValues {{ field.name.raw | tojson }} {{ raw }}
{%- else %}
  {{ target }} <- requiredFromValues {{ field.name.raw | tojson }} {{ raw }}
{%- endif %}
{%- endif -%}
{%- endmacro -%}

{%- macro parse_operation_values(operation, field, source, name) -%}
{%- set raw = raw_var(operation, field) -%}
{%- set target = field_var(operation, field) -%}
  {{ raw }} <- {{ source }} {{ name | tojson }}
{%- if field.optional %}
  {{ target }} <- optionalFromValues {{ field.name.raw | tojson }} {{ raw }}
{%- else %}
  {{ target }} <- requiredFromValues {{ field.name.raw | tojson }} {{ raw }}
{%- endif -%}
{%- endmacro -%}

{%- macro parse_bound_operation_field(operation, field) -%}
{%- set state = namespace(found=false) -%}
{%- for binding in operation.facets.http.inputs -%}
{%- if binding.port.field is not none and binding.port.field.raw == field.name.raw -%}
{%- set state.found=true -%}
{%- set target = field_var(operation, field) -%}
{%- set label = binding.name or binding.port.name.raw -%}
{%- if binding.is.path %}
  {{ raw_var(operation, field) }} <- (pathParamMaybe {{ label | tojson }} :: ActionM (Maybe Text))
{%- if field.optional %}
  {{ target }} <- parseOptional {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- else %}
  {{ target }} <- parseRequired {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- endif %}
{%- elif binding.is.query %}
{{ parse_operation_values(operation, field, 'queryValues', label) }}
{%- elif binding.is.header %}
  {{ raw_var(operation, field) }} <- header {{ label | tojson }}
{%- if field.optional %}
  {{ target }} <- parseOptional {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- else %}
  {{ target }} <- parseRequired {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- endif %}
{%- elif binding.is.cookie %}
  {{ raw_var(operation, field) }} <- cookieValue {{ label | tojson }}
{%- if field.optional %}
  {{ target }} <- parseOptional {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- else %}
  {{ target }} <- parseRequired {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- endif %}
{%- elif binding.is.form %}
{{ parse_operation_values(operation, field, 'formValues', label) }}
{%- elif binding.is.body %}
{%- if field.optional %}
  {{ target }} <- bodyOptional {{ field.name.raw | tojson }} {{ operation.name.camel }}Body
{%- else %}
  {{ target }} <- bodyRequired {{ field.name.raw | tojson }} {{ operation.name.camel }}Body
{%- endif %}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- if not state.found %}
{%- if field.optional %}
  let {{ field_var(operation, field) }} = Nothing
{%- else %}
  {{ field_var(operation, field) }} <- badRequest "required operation field has no HTTP binding"
{%- endif %}
{%- endif -%}
{%- endmacro -%}

{%- macro parse_bound_field(operation, field) -%}
{%- set state = namespace(found=false) -%}
{%- for binding in operation.facets.http.inputs -%}
{%- if binding.port.field is not none and binding.port.field.raw == field.name.raw -%}
{%- set state.found=true -%}
{%- set target = field_var(operation, field) -%}
{%- set label = binding.name or binding.port.name.raw -%}
{%- if binding.is.path %}
  {{ raw_var(operation, field) }} <- (pathParamMaybe {{ label | tojson }} :: ActionM (Maybe Text))
{%- if field.optional %}
  {{ target }} <- parseOptional {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- else %}
  {{ target }} <- parseRequired {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- endif %}
{%- elif binding.is.query %}
{{ parse_schema_values(operation, field, 'queryValues', label) }}
{%- elif binding.is.header %}
  {{ raw_var(operation, field) }} <- header {{ label | tojson }}
{%- if field.optional %}
  {{ target }} <- parseOptional {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- else %}
  {{ target }} <- parseRequired {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- endif %}
{%- elif binding.is.cookie %}
  {{ raw_var(operation, field) }} <- cookieValue {{ label | tojson }}
{%- if field.optional %}
  {{ target }} <- parseOptional {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- else %}
  {{ target }} <- parseRequired {{ field.name.raw | tojson }} {{ raw_var(operation, field) }}
{%- endif %}
{%- elif binding.is.form %}
{{ parse_schema_values(operation, field, 'formValues', label) }}
{%- elif binding.is.body %}
{%- if field.optional %}
  {{ target }} <- bodyOptional {{ field.name.raw | tojson }} {{ operation.name.camel }}Body
{%- else %}
  {{ target }} <- bodyRequired {{ field.name.raw | tojson }} {{ operation.name.camel }}Body
{%- endif %}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- if not state.found %}
{%- if field.optional %}
  let {{ field_var(operation, field) }} = Nothing
{%- else %}
  {{ field_var(operation, field) }} <- badRequest "required operation field has no HTTP binding"
{%- endif %}
{%- endif -%}
{%- endmacro -%}

{%- for operation in operations %}
{%- if operation.input and operation.input.kind != 'schema' %}
data {{ operation.name.pascal }}Input = {{ operation.name.pascal }}Input
  {
{%- for field in operation.input.fields %}
    {{ hs_ident(field.name.camel) }} :: {{ operation_field_type(field) }}{% if not loop.last %},{% endif %}
{%- endfor %}
  } deriving (Eq, Show)

instance FromJSON {{ operation.name.pascal }}Input where
  parseJSON = withObject "{{ operation.name.pascal }}Input" $ \o ->
    {{ operation.name.pascal }}Input
{%- for field in operation.input.fields %}
      <$> o .{% if field.optional %}:?{% else %}:{% endif %} {{ field.name.raw | tojson }}

{%- endif %}
{%- endfor %}
data {{ file.symbols.service }} = {{ file.symbols.service }}
  {
{%- for operation in operations %}
    {{ hs_ident(operation.name.camel) }}
      :: {% if operation.input %}{% if operation.input.kind == 'schema' %}{{ hs_type(operation.input.value) }}{% else %}{{ operation.name.pascal }}Input{% endif %} -> {% endif %}IO {% if operation.output %}({{ hs_type(operation.output.value) }}){% else %}(){% endif %}{% if not loop.last %},{% endif %}
{%- endfor %}
  }

{{ file.symbols.register }} :: {{ file.symbols.service }} -> ScottyM ()
{{ file.symbols.register }} service = do
{%- for operation in operations %}
{%- set http = operation.facets.http %}
  addroute {{ http.method }} {{ (http.path.local | replace('{', ':') | replace('}', '')) | tojson }} $ do
{%- if http.input_groups.multipart %}
    respondJson 501 ("multipart bindings require an explicit Haskell semantic mapping" :: Text)
{%- else %}
{%- if http.input_groups.body and not whole_binding(http, 'body') %}
    {{ operation.name.camel }}Body <- (jsonData :: ActionM Value)
{%- endif %}
{%- if operation.input %}
{%- if operation.input.kind == 'schema' %}
{%- if whole_binding(http, 'body') %}
    {{ operation.name.camel }}Input <- (jsonData :: ActionM ({{ hs_type(operation.input.value) }}))
{%- elif whole_binding(http, 'query') %}
{%- for field in operation.input.value.schema.fields %}
{{ parse_schema_values(operation, field, 'queryValues', field.name.raw) }}
{%- endfor %}
    let {{ operation.name.camel }}Input =
          {{ hs_type(operation.input.value) }}
{%- for field in operation.input.value.schema.fields %}
            {{ field_var(operation, field) }}
{%- endfor %}
{%- elif whole_binding(http, 'form') %}
{%- for field in operation.input.value.schema.fields %}
{{ parse_schema_values(operation, field, 'formValues', field.name.raw) }}
{%- endfor %}
    let {{ operation.name.camel }}Input =
          {{ hs_type(operation.input.value) }}
{%- for field in operation.input.value.schema.fields %}
            {{ field_var(operation, field) }}
{%- endfor %}
{%- else %}
{%- for field in operation.input.value.schema.fields %}
{{ parse_bound_field(operation, field) }}
{%- endfor %}
    let {{ operation.name.camel }}Input =
          {{ hs_type(operation.input.value) }}
{%- for field in operation.input.value.schema.fields %}
            {{ field_var(operation, field) }}
{%- endfor %}
{%- endif %}
{%- else %}
{%- if whole_binding(http, 'body') %}
    {{ operation.name.camel }}Input <- (jsonData :: ActionM {{ operation.name.pascal }}Input)
{%- elif whole_binding(http, 'query') %}
{%- for field in operation.input.fields %}
{{ parse_operation_values(operation, field, 'queryValues', field.name.raw) }}
{%- endfor %}
    let {{ operation.name.camel }}Input =
          {{ operation.name.pascal }}Input
{%- for field in operation.input.fields %}
            {{ field_var(operation, field) }}
{%- endfor %}
{%- elif whole_binding(http, 'form') %}
{%- for field in operation.input.fields %}
{{ parse_operation_values(operation, field, 'formValues', field.name.raw) }}
{%- endfor %}
    let {{ operation.name.camel }}Input =
          {{ operation.name.pascal }}Input
{%- for field in operation.input.fields %}
            {{ field_var(operation, field) }}
{%- endfor %}
{%- else %}
{%- for field in operation.input.fields %}
{{ parse_bound_operation_field(operation, field) }}
{%- endfor %}
    let {{ operation.name.camel }}Input =
          {{ operation.name.pascal }}Input
{%- for field in operation.input.fields %}
            {{ field_var(operation, field) }}
{%- endfor %}
{%- endif %}
{%- endif %}
{%- endif %}

{%- if operation.output %}
    output <- liftIO ({{ hs_ident(operation.name.camel) }} service{% if operation.input %} {{ operation.name.camel }}Input{% endif %})
    respondJson {{ success_status(http) }} output
{%- else %}
    liftIO ({{ hs_ident(operation.name.camel) }} service{% if operation.input %} {{ operation.name.camel }}Input{% endif %})
    respondEmpty {{ success_status(http) }}
{%- endif %}
{%- endif %}

{%- endfor %}