Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
tests/Test/Proto/Generate/CodeGen.hs
Comment thread
j6carey marked this conversation as resolved.
Outdated

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
3 changes: 2 additions & 1 deletion tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import TestDhall

import qualified Test.Proto.Generate.Name
import qualified Test.Proto.Parse.Option
import qualified Test.Proto.Generate.CodeGen
Comment thread
j6carey marked this conversation as resolved.
Outdated
import qualified Test.Proto.Interval
import Test.Proto.ToEncoder (Iterator(Forward, Vector),
Stripping(Keep, Strip), ToEncoder(..))
Expand All @@ -84,7 +85,7 @@ tests logger = testGroup "Tests"
, Test.Proto.Generate.Name.testTree
, Test.Proto.Parse.Option.testTree
, Test.Proto.Interval.testTree

, Test.Proto.Generate.CodeGen.testTree
#ifdef DHALL
, dhallTests
#endif
Expand Down
27 changes: 27 additions & 0 deletions tests/Test/Proto/Generate/CodeGen.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.Proto.Generate.CodeGen (testTree) where

import Control.Monad.Except (runExceptT)
import Proto3.Suite.DotProto.Generate (getExtraInstances)
import Proto3.Suite.Haskell.Parser (initLogger)
import Test.Tasty
import Test.Tasty.HUnit (assertEqual, assertFailure, testCase)

testTree :: TestTree
testTree = testGroup "Code generation"
[ extraInstanceParsing
]

-- | 'getExtraInstances' includes both regular instance declarations and
-- standalone deriving declarations from extra instance files.
extraInstanceParsing :: TestTree
extraInstanceParsing =
testCase "getExtraInstances includes standalone deriving declarations" $ do
logger <- initLogger
result <- runExceptT $ getExtraInstances logger "test-files/extra_instances_deriving.hs"
case result of
Left err -> assertFailure (show err)
Right (_imports, decls) ->
assertEqual "expected 2 declarations (1 instance + 1 standalone deriving)"
2 (length decls)
Comment thread
j6carey marked this conversation as resolved.
Outdated
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