Skip to content

Commit

Permalink
Hide the constructors and raw record fields of message types. (#174)
Browse files Browse the repository at this point in the history
Finishes #158.

They're not that useful for constructing values (especially after adding the
"unknown fields" case) and were previously mostly used for documentation.  Now
that #172 has landed, we have a better source of documentation that doesn't
expose internal details.

Also change the `Show` instance for messages to be a wrapper around
`showMessageShort`.  This both hides the now-inaccessible record fields and
shortens the output in most cases.  For example:

```
> print (def :: DescriptorProto)
{}
> print (def & #name .~ "hello" & #field .~ [def, def & #name .~ "bye"]:: DescriptorProto)
{name: "hello" field { } field { name: "bye" }}
```

Also bump the resolver for bootstrapping to match the regular build.
  • Loading branch information
judah authored Aug 26, 2018
1 parent e17132f commit 4a97752
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 62 deletions.
7 changes: 3 additions & 4 deletions proto-lens-protoc/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## v0.4
- Split out `proto-lens-setup` and `proto-lens-runtime` into separate
packages.
- Hide the constructors and record fields of message types, and make `Show`
instances call `showMessageShort`.
- Generate explicit `NFData` instances for each type.

## v0.3.1.1
Expand All @@ -24,14 +26,11 @@
- Don't generate Haskell modules if they won't be used. (#126)
- Bundle enum pattern synonyms exports with their type. (#136)
- Split the `Message` class into separate methods. (#139)
- Refactor the `FieldDescriptorType. (#147)
- Refactor the `FieldDescriptorType`. (#147)
- Add a case to proto3 enums for unknown values. (#137)
- Track consolidation of `proto-lens-descriptors` into `proto-lens`. (#140)
- Generate service definitions using promoted datatypes. (#154)
- Generate prisms for `oneof` message fields. (#160)
- Build with `haskell-src-exts-1.20.*`. (#170)
- Add Haddock comments to fields. (#172)
- Don't unnecessarily touch files. (#177)

## v0.2.2.3
- Don't camel-case message names. This reverts behavior which was added
Expand Down
6 changes: 6 additions & 0 deletions proto-lens-protoc/src/Data/ProtoLens/Compiler/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ alt p e = Syntax.Alt () p (Syntax.UnGuardedRhs () e) Nothing
stringExp :: String -> Exp
stringExp = Syntax.Lit () . string

charExp :: Char -> Exp
charExp = Syntax.Lit () . char

tuple :: [Exp] -> Exp
tuple = Syntax.Tuple () Syntax.Boxed

Expand Down Expand Up @@ -407,6 +410,9 @@ pLitInt n = Syntax.PLit () sign $ Syntax.Int () n' (show n')
string :: String -> Syntax.Literal ()
string s = Syntax.String () s (show s)

char :: Char -> Syntax.Literal ()
char c = Syntax.Char () c [c]

modifyModuleName :: (String -> String) -> ModuleName -> ModuleName
modifyModuleName f (Syntax.ModuleName _ unpacked) =
Syntax.ModuleName () $ f unpacked
Expand Down
17 changes: 13 additions & 4 deletions proto-lens-protoc/src/Data/ProtoLens/Compiler/Generate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ messageComment fieldModName n fields = unlines

generateMessageExports :: MessageInfo Name -> [ExportSpec]
generateMessageExports m =
map (exportAll . unQual)
$ messageName m : map oneofTypeName (messageOneofFields m)
-- Hide the message contructor, but expose "oneof" case constructors.
exportWith (unQual $ messageName m) []
: map (exportAll . unQual . oneofTypeName)
(messageOneofFields m)

generateServiceDecls :: Env QName -> ServiceInfo -> [Decl]
generateServiceDecls env si =
Expand Down Expand Up @@ -254,9 +256,16 @@ generateMessageDecls fieldModName syntaxType env protoName info =
]
++ [(messageUnknownFields info, "Data.ProtoLens.FieldSet")]
]
$ deriving' ["Prelude.Show", "Prelude.Eq", "Prelude.Ord"]
$ deriving' ["Prelude.Eq", "Prelude.Ord"]
-- instance Show Bar where
-- showsPrec __x __s = showChar '{' (showString (showMessageShort __x) (showChar '}' s))
, uncommented $
instDecl [] ("Prelude.Show" `ihApp` [dataType])
[[match "showsPrec" ["_", "__x", "__s"]
$ "Prelude.showChar" @@ charExp '{'
@@ ("Prelude.showString" @@ ("Data.ProtoLens.showMessageShort" @@ "__x")
@@ ("Prelude.showChar" @@ charExp '}' @@ "__s"))]]
] ++

-- oneof field data type declarations
-- proto: message Foo {
-- oneof bar {
Expand Down
4 changes: 2 additions & 2 deletions proto-lens-tests/tests/enum_test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module Main where
import Proto.Enum
( Bar(..)
, Baz(..)
, TwoBazs(..)
, Foo(..)
, TwoBazs
, Foo
, Foo'Baz(..)
-- explicit import to make sure we get pattern synonyms
, Alias(..)
Expand Down
11 changes: 4 additions & 7 deletions proto-lens-tests/tests/oneof_test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ main = testMain
-- A oneof type and constructor that overlap with an enum
trivial (Disambiguated'EnumCon' 42 :: Disambiguated'EnumType')

-- And we don't change the message or enum types and constructors.
trivial (Disambiguated'MessageTypeA
{_Disambiguated'MessageTypeA'_unknownFields = []}
:: Disambiguated'MessageTypeA)
trivial (Disambiguated'MessageTypeB
{_Disambiguated'MessageTypeB'_unknownFields = []}
:: Disambiguated'MessageTypeB)
-- And we don't change the types of messages, nor the types or
-- constructors of enums.
trivial (def :: Disambiguated'MessageTypeA)
trivial (def :: Disambiguated'MessageTypeB)
trivial (Disambiguated'EnumCon :: Disambiguated'EnumType)

, testCase "not disambiguated names" $ do
Expand Down
2 changes: 1 addition & 1 deletion proto-lens-tests/tests/proto3_test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Data.Monoid ((<>))
import Proto.Proto3
( Foo
, Foo'FooEnum(..)
, Foo'Sub(..)
, Foo'Sub
, Strings
)
import Proto.Proto3_Fields
Expand Down
25 changes: 20 additions & 5 deletions proto-lens/src/Proto/Google/Protobuf/Compiler/Plugin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
{-# OPTIONS_GHC -fno-warn-unused-imports#-}
{-# OPTIONS_GHC -fno-warn-duplicate-exports#-}
module Proto.Google.Protobuf.Compiler.Plugin
(CodeGeneratorRequest(..), CodeGeneratorResponse(..),
CodeGeneratorResponse'File(..))
(CodeGeneratorRequest(), CodeGeneratorResponse(),
CodeGeneratorResponse'File())
where
import qualified Control.DeepSeq
import qualified Lens.Labels.Prism
Expand Down Expand Up @@ -44,7 +44,12 @@ data CodeGeneratorRequest = CodeGeneratorRequest{_CodeGeneratorRequest'fileToGen
![Proto.Google.Protobuf.Descriptor.FileDescriptorProto],
_CodeGeneratorRequest'_unknownFields ::
!Data.ProtoLens.FieldSet}
deriving (Prelude.Show, Prelude.Eq, Prelude.Ord)
deriving (Prelude.Eq, Prelude.Ord)
instance Prelude.Show CodeGeneratorRequest where
showsPrec _ __x __s
= Prelude.showChar '{'
(Prelude.showString (Data.ProtoLens.showMessageShort __x)
(Prelude.showChar '}' __s))
instance Lens.Labels.HasLens' CodeGeneratorRequest "fileToGenerate"
([Data.Text.Text])
where
Expand Down Expand Up @@ -141,7 +146,12 @@ data CodeGeneratorResponse = CodeGeneratorResponse{_CodeGeneratorResponse'error
![CodeGeneratorResponse'File],
_CodeGeneratorResponse'_unknownFields ::
!Data.ProtoLens.FieldSet}
deriving (Prelude.Show, Prelude.Eq, Prelude.Ord)
deriving (Prelude.Eq, Prelude.Ord)
instance Prelude.Show CodeGeneratorResponse where
showsPrec _ __x __s
= Prelude.showChar '{'
(Prelude.showString (Data.ProtoLens.showMessageShort __x)
(Prelude.showChar '}' __s))
instance Lens.Labels.HasLens' CodeGeneratorResponse "error"
(Data.Text.Text)
where
Expand Down Expand Up @@ -222,7 +232,12 @@ data CodeGeneratorResponse'File = CodeGeneratorResponse'File{_CodeGeneratorRespo
!(Prelude.Maybe Data.Text.Text),
_CodeGeneratorResponse'File'_unknownFields
:: !Data.ProtoLens.FieldSet}
deriving (Prelude.Show, Prelude.Eq, Prelude.Ord)
deriving (Prelude.Eq, Prelude.Ord)
instance Prelude.Show CodeGeneratorResponse'File where
showsPrec _ __x __s
= Prelude.showChar '{'
(Prelude.showString (Data.ProtoLens.showMessageShort __x)
(Prelude.showChar '}' __s))
instance Lens.Labels.HasLens' CodeGeneratorResponse'File "name"
(Data.Text.Text)
where
Expand Down
Loading

0 comments on commit 4a97752

Please sign in to comment.