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

proto3 standard compatibility improvements #143

Merged
merged 14 commits into from
Jun 28, 2021
Merged
1 change: 0 additions & 1 deletion proto3-suite.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ library
Proto3.Suite.JSONPB
Proto3.Suite.Tutorial
Proto3.Suite.Types

Proto3.Suite.DotProto.Internal
Proto3.Suite.DotProto.Generate.Swagger
Proto3.Suite.JSONPB.Class
Expand Down
11 changes: 10 additions & 1 deletion shell.nix
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
(import ./release.nix {}).proto3-suite.env
let fetchNixpkgs = import ./nix/fetchNixpkgs.nix;
nixpkgs = fetchNixpkgs {
rev = "fdfd5ab05444c38a006cb107d7d1ee8cb0b15719";
sha256 = "17hsjpjahl0hff3z2khrcwxygjyyrav2pia3qqlli0sgywfrgf95";
};
pkgs = import nixpkgs {};
in
((import ./release.nix {}).proto3-suite.env).overrideAttrs (super: rec {
buildInputs = super.buildInputs ++ [pkgs.wget pkgs.cacert];
})
1 change: 1 addition & 0 deletions src/Proto3/Suite/DotProto/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ data DotProtoMessagePart
| DotProtoMessageOneOf DotProtoIdentifier [DotProtoField]
| DotProtoMessageDefinition DotProtoDefinition
| DotProtoMessageReserved [DotProtoReservedField]
| DotProtoMessageOption DotProtoOption
deriving (Show, Eq)

instance Arbitrary DotProtoMessagePart where
Expand Down
4 changes: 4 additions & 0 deletions src/Proto3/Suite/DotProto/Generate/Swagger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ instance {-# OVERLAPPING #-} (ToJSONKey k, ToSchema k) => ToSchema (OverrideToSc
declareNamedSchema (Proxy :: Proxy [(k, (OverrideToSchema ByteString))])
where
schema_ = mempty
#if MIN_VERSION_swagger2(2,4,0)
& type_ ?~ SwaggerObject
#else
& type_ .~ SwaggerObject
#endif
& additionalProperties ?~ AdditionalPropertiesSchema (Inline byteSchema)

{-| This is a convenience function that uses type inference to select the
Expand Down
15 changes: 7 additions & 8 deletions src/Proto3/Suite/DotProto/Parsing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,10 @@ singleIdentifier = Single <$> token identifierName
identifier :: ProtoParser DotProtoIdentifier
identifier = token _identifier

-- [note] message and enum types are defined by the proto3 spec to have an optional leading period (messageType and enumType in the spec)
-- what this indicates is, as far as i can tell, not documented, and i haven't found this syntax used in practice
-- it's ommitted but can be fairly easily added if there is in fact a use for it

-- [update] the leading dot denotes that the identifier path starts in global scope
-- i still haven't seen a use case for this but i can add it upon request
-- Parses a full identifier, consuming trailing space.
-- The leading dot denotes that the identifier path starts in global scope.
globalIdentifier :: ProtoParser DotProtoIdentifier
globalIdentifier = token $ string "." >> _identifier

-- Parses a nested identifier, consuming trailing space.
nestedIdentifier :: ProtoParser DotProtoIdentifier
Expand Down Expand Up @@ -162,7 +160,7 @@ primType = try (symbol "double" $> Double)
<|> try (symbol "string" $> String)
<|> try (symbol "bytes" $> Bytes)
<|> try (symbol "bool" $> Bool)
<|> Named <$> identifier
<|> Named <$> (identifier <|> globalIdentifier)

--------------------------------------------------------------------------------
-- top-level parser and version annotation
Expand Down Expand Up @@ -256,7 +254,7 @@ rpcOptions = braces $ many topOption

rpcClause :: ProtoParser (DotProtoIdentifier, Streaming)
rpcClause = do
let sid ctx = (,ctx) <$> identifier
let sid ctx = (,ctx) <$> (identifier <|> globalIdentifier)
-- NB: Distinguish "stream stream.foo" from "stream.foo"
try (symbol "stream" *> sid Streaming) <|> sid NonStreaming

Expand Down Expand Up @@ -296,6 +294,7 @@ messagePart = try (DotProtoMessageDefinition <$> enum)
<|> try (DotProtoMessageDefinition <$> message)
<|> try messageOneOf
<|> try (DotProtoMessageField <$> messageField)
<|> try (DotProtoMessageOption <$> topOption)

messageType :: ProtoParser DotProtoType
messageType = try mapType <|> try repType <|> (Prim <$> primType)
Expand Down
2 changes: 2 additions & 0 deletions src/Proto3/Suite/DotProto/Rendering.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ prettyPrintProtoDefinition opts = defn where
<+> (PP.hcat . PP.punctuate (PP.text ", ") $ pPrint <$> reservations)
<> PP.text ";"
msgPart msgName (DotProtoMessageOneOf name fields) = vbraces (PP.text "oneof" <+> pPrint name) (PP.vcat $ field msgName <$> fields)
msgPart _ (DotProtoMessageOption opt)
= PP.text "option" <+> pPrint opt <> PP.text ";"

field :: DotProtoIdentifier -> DotProtoField -> PP.Doc
field msgName (DotProtoField number mtype name options comments)
Expand Down
11 changes: 11 additions & 0 deletions test-files/leading_dot/data.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package LeadingDot.Rpc.Data;

message Request {
uint32 hello = 1;
}

message Response {
uint32 world = 1;
}
Loading