packages feed

hydra-kernel-0.18.1: src/main/haskell/Hydra/Core/Decode/Json/Model.hs

-- Note: this is an automatically generated file. Do not edit.

-- | Term decoders for hydra.core.json.model

module Hydra.Core.Decode.Json.Model where

import qualified Hydra.Core.Errors as Errors
import qualified Hydra.Core.Extract.Model as ExtractModel
import qualified Hydra.Core.Graph as Graph
import qualified Hydra.Core.Json.Model as JsonModel
import qualified Hydra.Core.Lexical as Lexical
import qualified Hydra.Core.Overlay.Haskell.Lib.Eithers as Eithers
import qualified Hydra.Core.Overlay.Haskell.Lib.Maps as Maps
import qualified Hydra.Core.Overlay.Haskell.Lib.Optionals as Optionals
import qualified Hydra.Core.Overlay.Haskell.Lib.Strings as Strings
import qualified Hydra.Core.Model as Model
import qualified Hydra.Core.Rewriting as Rewriting
import qualified Hydra.Core.Util as Util
import Prelude hiding  (Enum, Ordering, decodeFloat, encodeFloat, fail, lines, map, pure, sum, unlines)
import qualified Data.Scientific as Sci
import Data.Void

-- | Decoder for hydra.core.json.model.JsonWriterOptions
jsonWriterOptions :: Graph.Graph -> Model.Term -> Either Errors.DecodingError JsonModel.JsonWriterOptions
jsonWriterOptions cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermRecord v0 ->
        let fieldMap = ExtractModel.toFieldMap v0
        in (Eithers.bind (ExtractModel.requireField "compact" (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralBoolean v2 -> Right v2
            _ -> Left (Errors.DecodingError "expected boolean literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2)) fieldMap cx) (\field_compact -> Eithers.bind (ExtractModel.requireField "indent" (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralString v2 -> Right v2
            _ -> Left (Errors.DecodingError "expected string literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2)) fieldMap cx) (\field_indent -> Eithers.bind (ExtractModel.requireField "maxLineWidth" (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralInteger v2 -> case v2 of
              Model.IntegerValueInt32 v3 -> Right v3
              _ -> Left (Errors.DecodingError "expected int32 value")
            _ -> Left (Errors.DecodingError "expected int32 literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2)) fieldMap cx) (\field_maxLineWidth -> Eithers.bind (ExtractModel.requireField "maxIndentDepth" (ExtractModel.decodeMaybe (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralInteger v2 -> case v2 of
              Model.IntegerValueInt32 v3 -> Right v3
              _ -> Left (Errors.DecodingError "expected int32 value")
            _ -> Left (Errors.DecodingError "expected int32 literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2))) fieldMap cx) (\field_maxIndentDepth -> Right (JsonModel.JsonWriterOptions {
          JsonModel.jsonWriterOptionsCompact = field_compact,
          JsonModel.jsonWriterOptionsIndent = field_indent,
          JsonModel.jsonWriterOptionsMaxLineWidth = field_maxLineWidth,
          JsonModel.jsonWriterOptionsMaxIndentDepth = field_maxIndentDepth}))))))
      _ -> Left (Errors.DecodingError "expected a record of type hydra.core.json.model.JsonWriterOptions")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.json.model.Value
value :: Graph.Graph -> Model.Term -> Either Errors.DecodingError JsonModel.Value
value cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermInject v0 ->
        let field = Model.injectionField v0
            fname = Model.fieldName field
            fterm = Model.fieldTerm field
            variantMap =
                    Maps.fromList [
                      (Model.Name "array", (\input -> Eithers.map (\t -> JsonModel.ValueArray t) (ExtractModel.decodeList value cx input))),
                      (
                        Model.Name "boolean",
                        (\input -> Eithers.map (\t -> JsonModel.ValueBoolean t) (Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
                          Model.TermLiteral v1 -> case v1 of
                            Model.LiteralBoolean v2 -> Right v2
                            _ -> Left (Errors.DecodingError "expected boolean literal")
                          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx input)))),
                      (Model.Name "null", (\input -> Eithers.map (\t -> JsonModel.ValueNull) (ExtractModel.decodeUnit cx input))),
                      (
                        Model.Name "number",
                        (\input -> Eithers.map (\t -> JsonModel.ValueNumber t) (Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
                          Model.TermLiteral v1 -> case v1 of
                            Model.LiteralDecimal v2 -> Right v2
                            _ -> Left (Errors.DecodingError "expected decimal literal")
                          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx input)))),
                      (
                        Model.Name "object",
                        (\input -> Eithers.map (\t -> JsonModel.ValueObject t) (ExtractModel.decodeList (ExtractModel.decodePair (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
                          Model.TermLiteral v1 -> case v1 of
                            Model.LiteralString v2 -> Right v2
                            _ -> Left (Errors.DecodingError "expected string literal")
                          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2)) value) cx input))),
                      (
                        Model.Name "string",
                        (\input -> Eithers.map (\t -> JsonModel.ValueString t) (Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
                          Model.TermLiteral v1 -> case v1 of
                            Model.LiteralString v2 -> Right v2
                            _ -> Left (Errors.DecodingError "expected string literal")
                          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx input))))]
        in (Optionals.match (Maps.lookup fname variantMap) (Left (Errors.DecodingError (Strings.concat [
          "no such field ",
          (Model.unName fname),
          " in union"]))) (\f -> f fterm))
      _ -> Left (Errors.DecodingError "expected union")) (ExtractModel.stripWithDecodingError cx raw)