git.iko.soy/rowtype-yoga/purescript-protobuf
on master help sign in
Some cleanup and comments.
1b6e29cJames Brock
README.md+22−9
@@ -63,10 +63,16 @@ To generate Purescript .purs files from .proto files, run:
63[nix-shell]$
63[nix-shell]$
64```
64```
65
65
66−If you don't want to use Nix, then install the Purescript toolchain and *protoc*.
66+If you don't want to use Nix, then install the Purescript toolchain and `protoc`,
67+and add the executable script
68+[`bin/protoc-gen-purescript`](bin/protoc-gen-purescript)
69+to your `PATH`.
67
70
68## Writing programs with the generated code
71## Writing programs with the generated code
69
72
73+The code generator will use the `package` import statement in the `.proto` file
74+and the base `.proto` file name as the Purescript module name for that file.
75+
70A message in a `shapes.proto` file declared as
76A message in a `shapes.proto` file declared as
71
77
72```
78```
@@ -78,7 +84,8 @@ message Rectangle {
78}
84}
79```
85```
80
86
87+will export these four names from module `Interproc.Shapes` in a
81−will export these four names in a generated `shapes.Interproc.purs` file.
88+generated `shapes.Interproc.purs` file.
82
89
831. A message data type
901. A message data type
84
91
@@ -86,6 +93,9 @@ will export these four names in a generated `shapes.Interproc.purs` file.
86 newtype Rectangle = Rectangle { width :: Maybe Number, height :: Maybe Number }
93 newtype Rectangle = Rectangle { width :: Maybe Number, height :: Maybe Number }
87 ```
94 ```
88
95
96+ The message data type will also include an `__unknown_fields` field which
97+ is required for conformance.
98+
892. A message maker which constructs a message from a `Record`
992. A message maker which constructs a message from a `Record`
90 with some message fields
100 with some message fields
91
101
@@ -93,7 +103,11 @@ will export these four names in a generated `shapes.Interproc.purs` file.
93 mkRectangle :: forall r. Record r -> Rectangle
103 mkRectangle :: forall r. Record r -> Rectangle
94 ```
104 ```
95
105
106+ All message fields are optional, and can be elided when making a message.
107+ There are some extra type constraints, not shown here, which will cause a
108+ compiler error if we try to add a field which is not in the message data type.
109+
96− All message fields are optional, and can be omitted from the `Record`. If we want the compiler to check that we've explicitly supplied all the fields,
110+ If we want the compiler to check that we've explicitly supplied all the fields,
97 then we can use the ordinary message data type constructor.
111 then we can use the ordinary message data type constructor.
98
112
993. A message encoder which works with
1133. A message encoder which works with
@@ -235,10 +249,7 @@ The path to the protobuf definition which failed to parse will be included
235in the `ParseError String` and delimited by `'/'`, something
249in the `ParseError String` and delimited by `'/'`, something
236like `"Message1 / string_field_1 / Invalid UTF8 encoding."`.
250like `"Message1 / string_field_1 / Invalid UTF8 encoding."`.
237
251
238−### Imports
252+### Protobuf Imports
239−
240−The code generator will use the `package` statement in the `.proto` file
241−and the base file name as the Purescript module name for that file.
242
253
243The Protobuf
254The Protobuf
244[`import`](https://developers.google.com/protocol-buffers/docs/proto3#importing_definitions)
255[`import`](https://developers.google.com/protocol-buffers/docs/proto3#importing_definitions)
@@ -251,10 +262,12 @@ statement in the imported file.
251
262
252For that reason, we can only use top-level
263For that reason, we can only use top-level
253(not [nested](https://developers.google.com/protocol-buffers/docs/proto3#nested))
264(not [nested](https://developers.google.com/protocol-buffers/docs/proto3#nested))
254−`message` and `enum` types from an `import`.
265+`message` and `enum` types from a Protobuf `import`.
266+
267+### PureScript Imports
255
268
256The generated Purescript code will usually have module imports which cause
269The generated Purescript code will usually have module imports which cause
257−the `purs` compiler to emit warnings. Sorry.
270+the `purs` compiler to emit warnings. We beg your pardon.
258
271
259## Nix derivation
272## Nix derivation
260
273
default.nix−1
@@ -44,7 +44,6 @@ pkgs.stdenv.mkDerivation {
44 install-spago-style
44 install-spago-style
45 '';
45 '';
46 buildPhase = ''
46 buildPhase = ''
47− ls -la .spago/unsafe-coerce
48 build-spago-style "./src/**/*.purs"
47 build-spago-style "./src/**/*.purs"
49 '';
48 '';
50 installPhase = ''
49 installPhase = ''
nix/pkgs.nix−10
@@ -1,5 +1,4 @@
1# https://nixos.org/guides/towards-reproducibility-pinning-nixpkgs.html
1# https://nixos.org/guides/towards-reproducibility-pinning-nixpkgs.html
2−# { pkgs ? import <nixpkgs> { } }:
3{ pkgs ? import (builtins.fetchTarball {
2{ pkgs ? import (builtins.fetchTarball {
4 url = "https://github.com/NixOS/nixpkgs/archive/20.03.tar.gz";
3 url = "https://github.com/NixOS/nixpkgs/archive/20.03.tar.gz";
5 sha256 = "0182ys095dfx02vl2a20j1hz92dx3mfgz2a6fhn31bqlp1wa8hlq";
4 sha256 = "0182ys095dfx02vl2a20j1hz92dx3mfgz2a6fhn31bqlp1wa8hlq";
@@ -11,24 +10,15 @@ let
11 (builtins.fetchGit {
10 (builtins.fetchGit {
12 url = "https://github.com/justinwoo/easy-purescript-nix.git";
11 url = "https://github.com/justinwoo/easy-purescript-nix.git";
13 rev = "1ec689df0adf8e8ada7fcfcb513876307ea34226";
12 rev = "1ec689df0adf8e8ada7fcfcb513876307ea34226";
14− # (pkgs.fetchFromGitHub {
15− # owner = "justinwoo";
16− # repo = "easy-purescript-nix";
17− # rev = "1ec689df0adf8e8ada7fcfcb513876307ea34226";
18− # sha256 = "12hk2zbjkrq2i5fs6xb3x254lnhm9fzkcxph0a7ngxyzfykvf4hi";
19 }) { inherit pkgs; };
13 }) { inherit pkgs; };
20
14
21 spago2nix = import (builtins.fetchGit {
15 spago2nix = import (builtins.fetchGit {
22− #url = "git@github.com:justinwoo/spago2nix.git";
23− #rev = "22bd974ec44c98d2831c31f4f4402a9afa8aecea";
24− # url = "git@github.com:xc-jp/spago2nix.git";
25 url = "https://github.com/xc-jp/spago2nix.git";
16 url = "https://github.com/xc-jp/spago2nix.git";
26 ref = "ls-transitive";
17 ref = "ls-transitive";
27 rev = "1e65ec78def30b9c10bfc165cfb5f3b1c42bbd80";
18 rev = "1e65ec78def30b9c10bfc165cfb5f3b1c42bbd80";
28 }) { inherit pkgs; };
19 }) { inherit pkgs; };
29
20
30 node2nix = import (builtins.fetchGit {
21 node2nix = import (builtins.fetchGit {
31− # url = "git@github.com:svanderburg/node2nix.git";
32 url = "https://github.com/svanderburg/node2nix.git";
22 url = "https://github.com/svanderburg/node2nix.git";
33 rev = "c6cc7edc7c180ef01f23bc2aff31a1cbb5524b0d";
23 rev = "c6cc7edc7c180ef01f23bc2aff31a1cbb5524b0d";
34 }) { inherit pkgs; };
24 }) { inherit pkgs; };
src/Protobuf/Library.purs+1−1
@@ -9,7 +9,7 @@ module Protobuf.Library
9where
9where
10
10
11import Protobuf.Common (Bytes(..))
11import Protobuf.Common (Bytes(..))
12−import Protobuf.Runtime (label)
12+import Protobuf.Runtime (label, UnknownField(..))
13import Text.Parsing.Parser (ParserT, fail)
13import Text.Parsing.Parser (ParserT, fail)
14import Control.Monad.Except (ExceptT, runExceptT)
14import Control.Monad.Except (ExceptT, runExceptT)
15import Control.Monad (class Monad, (>>=), pure)
15import Control.Monad (class Monad, (>>=), pure)
src/Protobuf/Runtime.purs+2−2
@@ -4,7 +4,7 @@
4-- | See package README for explanation.
4-- | See package README for explanation.
5module Protobuf.Runtime
5module Protobuf.Runtime
6( parseMessage
6( parseMessage
7−, UnknownField
7+, UnknownField(..)
8, parseFieldUnknown
8, parseFieldUnknown
9, putFieldUnknown
9, putFieldUnknown
10, parseLenDel
10, parseLenDel
@@ -255,4 +255,4 @@ parseEnum = do
255-- | to the error `String` in the `ParseError`.
255-- | to the error `String` in the `ParseError`.
256label :: forall m s a. Monad m => String -> ParserT s m a -> ParserT s m a
256label :: forall m s a. Monad m => String -> ParserT s m a -> ParserT s m a
257label messagePrefix p = catchError p $ \ (ParseError message pos) ->
257label messagePrefix p = catchError p $ \ (ParseError message pos) ->
258− throwError $ ParseError (messagePrefix <> message) pos
258+ throwError $ ParseError (messagePrefix <> message) pos