Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not quite sure if this is an issue/bug or my lack of Haskell knowledge. #34

Closed
markotitel opened this issue Oct 3, 2019 · 2 comments
Closed

Comments

@markotitel
Copy link

Hi,
This is actually my first Haskell build.
This is json-autotype.cabal file.

-- Build information for the package.
name:                json-autotype
version:             3.0.2
synopsis:            Automatic type declaration for JSON input data
description:         Generates datatype declarations with Aeson's `FromJSON` instances
                     from a set of example ".json" files.
                     .
                     To get started you need to install the package,
                     and run "json-autotype" binary on an input ".json" file.
                     That will generate a new Aeson-based JSON parser.
                     .
                     "$ json-autotype input.json -o JSONTypes.hs"
                     .
                     Feel free to tweak the by changing types of the fields
                      - any field type that is instance of `FromJSON` should work.
                     .
                     You may immediately test the parser by calling it as a script:
                     .
                     "$ runghc JSONTypes.hs input.json"
                     .
                     One can now use multiple input files to generate better type description.
                     .
                     Now with Elm code generation support!
                     (If you want your favourite programming language supported too -
                     name your price and mail the author.)
                     .
                     See introduction on  <https://github.com/mgajda/json-autotype>
                     for details.
homepage:            https://github.com/mgajda/json-autotype
license:             BSD3
license-file:        LICENSE
stability:           stable
author:              Michal J. Gajda
maintainer:          [email protected]
                     [email protected]
copyright:           Copyright by Michal J. Gajda '2014-'2019
category:            Data, Tools
build-type:          Simple
extra-source-files:  README.md changelog.md examples/union.json examples/colors.json
cabal-version:       >=1.10
bug-reports:         https://github.com/mgajda/json-autotype/issues
tested-with:         GHC==7.6.1
                   , GHC==7.6.2
                   , GHC==7.6.3
                   , GHC==7.8.2
                   , GHC==7.8.3
                   , GHC==7.8.4
                   , GHC==7.10.1
                   , GHC==7.10.2
                   , GHC==7.10.3
                   , GHC==8.0.1
                   , GHC==8.0.2
                   , GHC==8.2.2
                   , GHC==8.4.1
                   , GHC==8.4.2
                   , GHC==8.4.3
                   , GHC==8.4.4
                   , GHC==8.6.2
                   , GHC==8.6.1
                   , GHC==8.8.1

source-repository head
  type:     git
  location: https://github.com/mgajda/json-autotype.git

library
  exposed-modules:     Data.Aeson.AutoType.CodeGen
                       Data.Aeson.AutoType.Extract
                       Data.Aeson.AutoType.Format
                       Data.Aeson.AutoType.Pretty
                       Data.Aeson.AutoType.Split
                       Data.Aeson.AutoType.Type
                       Data.Aeson.AutoType.Test
                       Data.Aeson.AutoType.Util
  other-modules:       Data.Aeson.AutoType.CodeGen.Generic
                       Data.Aeson.AutoType.CodeGen.Haskell
                       Data.Aeson.AutoType.CodeGen.Elm
                       Data.Aeson.AutoType.CodeGen.HaskellFormat
                       Data.Aeson.AutoType.CodeGen.ElmFormat
  hs-source-dirs:      src

  other-extensions:    TemplateHaskell,
                       ScopedTypeVariables,
                       OverloadedStrings,
                       FlexibleInstances,
                       MultiParamTypeClasses,
                       DeriveDataTypeable,
                       DeriveGeneric,
                       RecordWildCards
  build-depends:       base                 >=4.3  && <5,
                       GenericPretty        >=1.2  && <1.3,
                       aeson                >=1.2.1 && <1.5,
                       containers           >=0.3  && <0.7,
                       filepath             >=1.3  && <1.5,
                       hashable             >=1.2  && <1.4,
                       lens                 >=4.1  && <4.19,
                       mtl                  >=2.1  && <2.3,
                       pretty               >=1.1  && <1.3,
                       process              >=1.1  && <1.7,
                       scientific           >=0.3  && <0.5,
                       text                 >=1.1  && <1.4,
                       uniplate             >=1.6  && <1.7,
                       unordered-containers >=0.2  && <0.3,
                       vector               >=0.9  && <0.13,
                       smallcheck           >=1.0  && <1.2,
                       QuickCheck           >=2.4  && <3.0,
                       json-alt,
                       template-haskell
  default-language:    Haskell2010

executable json-autotype
  main-is:             GenerateJSONParser.hs
  hs-source-dirs:      app common
  other-modules:       CommonCLI
                       Paths_json_autotype
  other-extensions:    TemplateHaskell,
                       ScopedTypeVariables,
                       OverloadedStrings,
                       FlexibleInstances,
                       MultiParamTypeClasses,
                       DeriveDataTypeable,
                       DeriveGeneric,
                       RecordWildCards
  build-depends:       base                 >=4.3  && <5,
                       GenericPretty        >=1.2  && <1.3,
                       aeson                >=1.2.1  && <1.5,
                       bytestring           >=0.9  && <0.11,
                       containers           >=0.3  && <0.7,
                       filepath             >=1.3  && <1.5,
                       hashable             >=1.2  && <1.4,
                       lens                 >=4.1  && <4.19,
                       mtl                  >=2.1  && <2.3,
                       optparse-applicative >=0.12 && <1.0,
                       pretty               >=1.1  && <1.3,
                       process              >=1.1  && <1.7,
                       scientific           >=0.3  && <0.5,
                       text                 >=1.1  && <1.4,
                       uniplate             >=1.6  && <1.7,
                       unordered-containers >=0.2  && <0.3,
                       vector               >=0.9  && <0.13,
                       yaml                 >=0.8  && <0.12,
                       template-haskell,
                       json-autotype,
                       json-alt
  default-language:    Haskell2010
  -- STATIC: ld-options: -static
  -- STATIC: ghc-options: -fPIC

-- * Test suites
-- Test suite with QuickCheck on random values,
-- and extracted types.
test-suite json-autotype-qc-test
  type:                exitcode-stdio-1.0
  main-is:             TestQC.hs
  hs-source-dirs:      test common
  other-extensions:    TemplateHaskell,
                       ScopedTypeVariables,
                       OverloadedStrings,
                       FlexibleInstances,
                       MultiParamTypeClasses,
                       DeriveDataTypeable,
                       DeriveGeneric,
                       RecordWildCards
  build-depends:       base                 >=4.3  && <5,
                       GenericPretty        >=1.2  && <1.3,
                       aeson                >=1.2.1   && <1.5,
                       containers           >=0.3  && <0.7,
                       hashable             >=1.2  && <1.4,
                       lens                 >=4.1  && <4.19,
                       mtl                  >=2.1  && <2.3,
                       pretty               >=1.1  && <1.3,
                       scientific           >=0.3  && <0.5,
                       smallcheck           >=1.0  && <1.2,
                       text                 >=1.1  && <1.4,
                       uniplate             >=1.6  && <1.7,
                       unordered-containers >=0.2  && <0.3,
                       vector               >=0.9  && <0.13,
                       QuickCheck           >=2.4  && <3.0,
                       json-autotype,
                       json-alt
  default-language:    Haskell2010

test-suite json-autotype-examples
  type:                exitcode-stdio-1.0
  main-is:             TestExamples.hs
  other-modules:       CommonCLI
  hs-source-dirs:      test common

  other-extensions:    TemplateHaskell,
                       ScopedTypeVariables,
                       OverloadedStrings,
                       FlexibleInstances,
                       MultiParamTypeClasses,
                       DeriveDataTypeable,
                       DeriveGeneric,
                       RecordWildCards
  build-depends:       base                 >=4.3  && <5,
                       GenericPretty        >=1.2  && <1.3,
                       aeson                >=1.2.1 && <1.5,
                       containers           >=0.3  && <0.7,
                       directory            >=1.1  && <1.4,
                       filepath             >=1.3  && <1.5,
                       hashable             >=1.2  && <1.4,
                       lens                 >=4.1  && <4.19,
                       mtl                  >=2.1  && <2.3,
                       optparse-applicative >=0.11 && <1.0,
                       pretty               >=1.1  && <1.3,
                       process              >=1.1  && <1.7,
                       scientific           >=0.3  && <0.5,
                       smallcheck           >=1.0  && <1.2,
                       text                 >=1.1  && <1.4,
                       unordered-containers >=0.2  && <0.3,
                       uniplate             >=1.6  && <1.7,
                       vector               >=0.9  && <0.13,
                       QuickCheck           >=2.4  && <3.0,
                       template-haskell,
                       json-autotype,
                       json-alt
  default-language:    Haskell2010

-- Test suite with Haskell code generation and compilation
test-suite json-autotype-gen-test
  type:                exitcode-stdio-1.0
  main-is:             GenerateTestJSON.hs
  hs-source-dirs:      test common
  other-modules:       CommonCLI

  other-extensions:    TemplateHaskell,
                       ScopedTypeVariables,
                       OverloadedStrings,
                       FlexibleInstances,
                       MultiParamTypeClasses,
                       DeriveDataTypeable,
                       DeriveGeneric,
                       RecordWildCards
  build-depends:       base                 >=4.3  && <5,
                       GenericPretty        >=1.2  && <1.3,
                       aeson                >=1.2.1  && <1.5,
                       bytestring           >=0.9  && <0.11,
                       containers           >=0.3  && <0.7,
                       directory            >=1.1  && <1.4,
                       filepath             >=1.3  && <1.5,
                       hashable             >=1.2  && <1.4,
                       optparse-applicative >=0.12 && <1.0,
                       lens                 >=4.1  && <4.19,
                       mtl                  >=2.1  && <2.3,
                       pretty               >=1.1  && <1.3,
                       process              >=1.1  && <1.7,
                       scientific           >=0.3  && <0.5,
                       smallcheck           >=1.0  && <1.2,
                       text                 >=1.1  && <1.4,
                       uniplate             >=1.6  && <1.7,
                       unordered-containers >=0.2  && <0.3,
                       vector               >=0.9  && <0.13,
                       QuickCheck           >=2.4  && <3.0,
                       template-haskell,
                       json-autotype,
                       json-alt
  default-language:    Haskell2010

Ghc version:

The Glorious Glasgow Haskell Compilation System, version 7.10.3

Cabal version:

cabal-install version 1.22.6.0
using version 1.22.5.0 of the Cabal library

I've built the binary with cabal install -j

Then went to dist/build/json-autotype.

Executed json-autotype gosalad-echo.json
gosalad-echo.json content:

{
        "authHost": "https://auth.com",
        "authEndpoint": "/broadcasting/auth",
        "clients": [],
        "database": "redis",
        "databaseConfig": {
                "redis": {}
        },
        "devMode": true,
        "host": null,
        "port": "6008",
        "protocol": "http",
        "socketio": {},
        "sslCertPath": "",
        "sslKeyPath": "",
        "sslCertChainPath": "",
        "sslPassphrase": "",
        "apiOriginAllow": {
                "allowCors": false,
                "allowOrigin": "",
                "allowMethods": "",
                "allowHeaders": ""
        }
}

Error I've got:

Processing "gosalad-echo.json"
-- redis=socketio

{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards     #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE TypeOperators       #-}
{-# LANGUAGE DeriveGeneric       #-}
-- | DO NOT EDIT THIS FILE MANUALLY!
--   It was automatically generated by `json-autotype`.

module JsonEmptyKey where

import           System.Exit        (exitFailure, exitSuccess)
import           System.IO          (stderr, hPutStrLn)
import qualified Data.ByteString.Lazy.Char8 as BSL
import           System.Environment (getArgs)
import           Control.Monad      (forM_, mzero, join)
import           Control.Applicative
import           Data.Aeson.AutoType.Alternative
import           Data.Aeson(eitherDecode, Value(..), FromJSON(..), ToJSON(..),
                            pairs,
                            (.:), (.:?), (.=), object)
import           Data.Monoid((<>))
import           Data.Text (Text)
import qualified GHC.Generics

data Redis = Redis { 

  } deriving (Show,Eq,GHC.Generics.Generic)


instance FromJSON Redis where
  parseJSON (Object v) = return  Redis
  parseJSON _          = mzero


instance ToJSON Redis where
  toJSON     (Redis {}) = object []



data DatabaseConfig = DatabaseConfig { 
    databaseConfigRedis :: Redis
  } deriving (Show,Eq,GHC.Generics.Generic)


instance FromJSON DatabaseConfig where
  parseJSON (Object v) = DatabaseConfig <$> v .:  "redis"
  parseJSON _          = mzero


instance ToJSON DatabaseConfig where
  toJSON     (DatabaseConfig {..}) = object ["redis" .= databaseConfigRedis]
  toEncoding (DatabaseConfig {..}) = pairs  ("redis" .= databaseConfigRedis)


data ApiOriginAllow = ApiOriginAllow { 
    apiOriginAllowAllowMethods :: Text,
    apiOriginAllowAllowHeaders :: Text,
    apiOriginAllowAllowOrigin :: Text,
    apiOriginAllowAllowCors :: Bool
  } deriving (Show,Eq,GHC.Generics.Generic)


instance FromJSON ApiOriginAllow where
  parseJSON (Object v) = ApiOriginAllow <$> v .:  "allowMethods" <*> v .:  "allowHeaders" <*> v .:  "allowOrigin" <*> v .:  "allowCors"
  parseJSON _          = mzero


instance ToJSON ApiOriginAllow where
  toJSON     (ApiOriginAllow {..}) = object ["allowMethods" .= apiOriginAllowAllowMethods, "allowHeaders" .= apiOriginAllowAllowHeaders, "allowOrigin" .= apiOriginAllowAllowOrigin, "allowCors" .= apiOriginAllowAllowCors]
  toEncoding (ApiOriginAllow {..}) = pairs  ("allowMethods" .= apiOriginAllowAllowMethods<>"allowHeaders" .= apiOriginAllowAllowHeaders<>"allowOrigin" .= apiOriginAllowAllowOrigin<>"allowCors" .= apiOriginAllowAllowCors)


data TopLevel = TopLevel { 
    topLevelDatabaseConfig :: DatabaseConfig,
    topLevelDatabase :: Text,
    topLevelProtocol :: Text,
    topLevelSslCertPath :: Text,
    topLevelSslCertChainPath :: Text,
    topLevelDevMode :: Bool,
    topLevelClients :: [(Maybe Value)],
    topLevelApiOriginAllow :: ApiOriginAllow,
    topLevelAuthHost :: Text,
    topLevelSslPassphrase :: Text,
    topLevelHost :: (Maybe Value),
    topLevelSocketio :: Redis,
    topLevelAuthEndpoint :: Text,
    topLevelSslKeyPath :: Text,
    topLevelPort :: Text
  } deriving (Show,Eq,GHC.Generics.Generic)


instance FromJSON TopLevel where
  parseJSON (Object v) = TopLevel <$> v .:  "databaseConfig" <*> v .:  "database" <*> v .:  "protocol" <*> v .:  "sslCertPath" <*> v .:  "sslCertChainPath" <*> v .:  "devMode" <*> v .:  "clients" <*> v .:  "apiOriginAllow" <*> v .:  "authHost" <*> v .:  "sslPassphrase" <*> v .:? "host" <*> v .:  "socketio" <*> v .:  "authEndpoint" <*> v .:  "sslKeyPath" <*> v .:  "port"
  parseJSON _          = mzero


instance ToJSON TopLevel where
  toJSON     (TopLevel {..}) = object ["databaseConfig" .= topLevelDatabaseConfig, "database" .= topLevelDatabase, "protocol" .= topLevelProtocol, "sslCertPath" .= topLevelSslCertPath, "sslCertChainPath" .= topLevelSslCertChainPath, "devMode" .= topLevelDevMode, "clients" .= topLevelClients, "apiOriginAllow" .= topLevelApiOriginAllow, "authHost" .= topLevelAuthHost, "sslPassphrase" .= topLevelSslPassphrase, "host" .= topLevelHost, "socketio" .= topLevelSocketio, "authEndpoint" .= topLevelAuthEndpoint, "sslKeyPath" .= topLevelSslKeyPath, "port" .= topLevelPort]
  toEncoding (TopLevel {..}) = pairs  ("databaseConfig" .= topLevelDatabaseConfig<>"database" .= topLevelDatabase<>"protocol" .= topLevelProtocol<>"sslCertPath" .= topLevelSslCertPath<>"sslCertChainPath" .= topLevelSslCertChainPath<>"devMode" .= topLevelDevMode<>"clients" .= topLevelClients<>"apiOriginAllow" .= topLevelApiOriginAllow<>"authHost" .= topLevelAuthHost<>"sslPassphrase" .= topLevelSslPassphrase<>"host" .= topLevelHost<>"socketio" .= topLevelSocketio<>"authEndpoint" .= topLevelAuthEndpoint<>"sslKeyPath" .= topLevelSslKeyPath<>"port" .= topLevelPort)




parse :: FilePath -> IO TopLevel
parse filename = do
    input <- BSL.readFile filename
    case eitherDecode input of
      Left  err -> fatal $ case (eitherDecode input :: Either String Value) of
                           Left  err -> "Invalid JSON file: " ++ filename ++ " ++ err"
                           Right _   -> "Mismatched JSON value from file: " ++ filename
                                     ++ "\n" ++ err
      Right r   -> return (r :: TopLevel)
  where
    fatal :: String -> IO a
    fatal msg = do hPutStrLn stderr msg
                   exitFailure

main :: IO ()
main = do
  filenames <- getArgs
  forM_ filenames (\f -> parse f >>= (\p -> p `seq` putStrLn $ "Successfully parsed " ++ f))
  exitSuccess

STACK_EXEC=Nothing CABAL_SANDBOX_CONFIG=Nothing
Running Haskell module: ["runghc"]["","gosalad-echo.json"]

gosalad-echo.json:2:59: parse error on input ‘,’
@mgajda
Copy link
Collaborator

mgajda commented Oct 7, 2019

Not sure if this is bug in json-autotype, or rather aeson.

@mgajda
Copy link
Collaborator

mgajda commented Feb 6, 2020

The bug may be due to empty command line argument in the middle.

I recommend that you run json-autotype code generation without immediate testing with the compiler --no-test, and then check that the output works when you compile with runghc manually.

If this works, it would be duplicate of #22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants