Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 0.10.0
* [#306](https://github.com/awakesecurity/proto3-suite/pull/306) 0.10.0: Use proto3-wire 1.5.0
* Support proto3-wire 1.5 instead of proto3-wire 1.4.
* Fix `--extraInstanceFile` to support standalone deriving declarations in
addition to regular instance declarations.

# 0.9.5
* [#305](https://github.com/awakesecurity/proto3-suite/pull/305) Avoid unpacked packed fields
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ Available options:
repeated, and paths will be searched in order; the
current directory is used if this option is not
provided)
--extraInstanceFile FILE Additional file to provide instances that would
otherwise be generated. Can be used multiple times.
Types for which instance overrides are given must be
fully qualified.
--extraInstanceFile FILE Additional file to provide instance declarations or
standalone deriving declarations that would otherwise
be generated. Can be used multiple times. Types for
which instance overrides are given must be fully
qualified.
--proto FILE Path to input .proto file
--out DIR Output directory path where generated Haskell modules
will be written (directory is created if it does not
Expand Down
3 changes: 2 additions & 1 deletion proto3-suite.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.2
name: proto3-suite
version: 0.9.5
version: 0.10.0
synopsis: A higher-level API to the proto3-wire library
description:
This library provides a higher-level API to <https://github.com/awakesecurity/proto3-wire the `proto3-wire` library>
Expand Down Expand Up @@ -29,6 +29,7 @@ data-files:
extra-source-files:
CHANGELOG.md,
gen/.gitignore
test-files/extra_instances_deriving.hs

flag dhall
Description: Turn on Dhall interpret and inject codegen
Expand Down
8 changes: 5 additions & 3 deletions src/Proto3/Suite/DotProto/Generate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Proto3.Suite.DotProto.Generate
, hsModuleForDotProto
, renderHsModuleForDotProto
, readDotProtoWithContext
, getExtraInstances
) where

import Control.Applicative
Expand Down Expand Up @@ -256,7 +257,7 @@ hsModuleForDotProto ::
, (?stringType :: StringType)
, (?typeLevelFormat :: Bool)
) =>
-- | Extra user-define instances that override default generated instances
-- | Extra user-defined instances and standalone deriving declarations that override default generated instances
([HsImportDecl], [HsDecl]) ->
-- |
DotProto ->
Expand Down Expand Up @@ -309,8 +310,9 @@ getExtraInstances logger (Turtle.encodeString -> extraInstanceFile) = do
Nothing ->
internalError (T.unpack "Error: Failed to parse instance file")
Just (GHC.L _ m) -> do
let isInstDecl (GHC.L _ GHC.InstD{}) = True
isInstDecl _ = False
let isInstDecl (GHC.L _ GHC.InstD{}) = True
isInstDecl (GHC.L _ GHC.DerivD{}) = True
isInstDecl _ = False
pure (GHC.hsmodImports m, filter isInstDecl (GHC.hsmodDecls m))

-- | This very specific function will only work for the qualification on the very first type
Expand Down
13 changes: 13 additions & 0 deletions test-files/extra_instances_deriving.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{-# LANGUAGE StandaloneDeriving #-}

module ExtraInstances where

data Foo = Foo

helper :: Int
helper = 42

instance Show Foo where
show _ = "Foo"

deriving instance Eq Foo
1 change: 0 additions & 1 deletion tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ tests logger = testGroup "Tests"
, Test.Proto.Generate.Name.testTree
, Test.Proto.Parse.Option.testTree
, Test.Proto.Interval.testTree

#ifdef DHALL
, dhallTests
#endif
Expand Down
24 changes: 22 additions & 2 deletions tests/TestCodeGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module TestCodeGen where

import ArbitraryGeneratedTestTypes ()
import Control.Applicative
import Control.Monad.Except (runExceptT)
import Data.List (sort)
import Control.Monad
#ifdef SWAGGER
import qualified Data.Aeson
Expand Down Expand Up @@ -41,7 +43,8 @@ import Proto3.Suite.DotProto (fieldLikeName, prefixedEnumFiel
import Proto3.Suite.DotProto.AST (DotProtoField(..), DotProtoIdentifier(..),
DotProtoType(..), DotProtoPrimType(..))
import Proto3.Suite.DotProto.Generate
import Proto3.Suite.Haskell.Parser (Logger)
import Proto3.Suite.Haskell.Parser (Logger, renderSDoc)
import qualified GHC.Utils.Outputable as GHC
import Proto3.Suite.JSONPB (FromJSONPB (..), Options (..),
ToJSONPB (..), defaultOptions,
eitherDecode, encode,
Expand All @@ -50,7 +53,7 @@ import Proto3.Suite.Types (Enumerated(..))
import System.Exit
import Test.Proto.ToEncoder (Iterator, Stripping)
import Test.Tasty
import Test.Tasty.HUnit (testCase, (@?=))
import Test.Tasty.HUnit (assertEqual, assertFailure, testCase, (@?=))
import Test.Tasty.QuickCheck (Arbitrary, (===), testProperty)
import qualified Turtle
import qualified Turtle.Format as F
Expand All @@ -72,6 +75,7 @@ codeGenTests logger = testGroup "Code generator unit tests"
, don'tAlterEnumFieldNames
, knownTypeMessages
, pythonInteroperation logger
, extraInstanceParsing logger
#ifdef SWAGGER
, swaggerTests
, swaggerWrapperFormat
Expand All @@ -93,6 +97,22 @@ pythonInteroperation logger = testGroup "Python interoperation" $ do
| otherwise -> []
pure @[] (f logger tt format)

extraInstanceParsing :: Logger -> TestTree
extraInstanceParsing logger =
testCase "getExtraInstances includes standalone deriving declarations" $ do
result <- runExceptT $ getExtraInstances logger "test-files/extra_instances_deriving.hs"
case result of
Left err -> assertFailure (show err)
Right (_imports, decls) -> do
assertEqual "expected 2 declarations (1 instance + 1 standalone deriving)"
2 (length decls)
let names = sort (map (renderSDoc . GHC.ppr) decls)
assertEqual "parsed instance names should match the instances in extra_instances_deriving.hs"
[ "deriving instance Eq Foo"
, "instance Show Foo where\n show _ = \"Foo\""
]
names

#ifdef SWAGGER
swaggerWrapperFormat :: TestTree
swaggerWrapperFormat = testGroup "Swagger Wrapper Format"
Expand Down
2 changes: 1 addition & 1 deletion tools/compile-proto-file/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ parseArgs = info (helper <*> parser) (fullDesc <> progDesc "Compiles a .proto fi
extraInstances = many $ strOption $
long "extraInstanceFile"
<> metavar "FILE"
<> help "Additional file to provide instances that would otherwise be generated. Can be used multiple times. Types for which instance overrides are given must be fully qualified."
<> help "Additional file to provide instance declarations or standalone deriving declarations that would otherwise be generated. Can be used multiple times. Types for which instance overrides are given must be fully qualified."

proto = strOption $
long "proto"
Expand Down
Loading