Skip to content

Commit 1c971b2

Browse files
Misc doc changes (#144)
Besides other changes, this commit ensures the generated HTML doc for hexdocs.pm will become the main source doc for this Elixir library which leverage ExDoc features. Co-authored-by: Andrea Leopardi <an.leopardi@gmail.com>
1 parent e63e10b commit 1c971b2

File tree

8 files changed

+96
-70
lines changed

8 files changed

+96
-70
lines changed

.formatter.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
21
[
3-
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"],
2+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
43
locals_without_parens: [field: 2, field: 3, oneof: 2, extend: 4, extensions: 1],
54
export: [
65
locals_without_parens: [field: 2, field: 3, oneof: 2, extend: 4, extensions: 1]

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ erl_crash.dump
1919
# Also ignore archive artifacts (built via "mix archive.build").
2020
*.ez
2121

22-
protoc-gen-elixir
22+
# Ignore package tarball (built via "mix hex.build").
23+
protobuf-*.tar
2324

24-
# QuickCheck artifacts
25+
# Misc.
26+
protoc-gen-elixir
2527
.eqc-info
2628
*.eqc
27-
28-
.elixir_ls

README.md

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
# protobuf-elixir
22

3-
[![Hex.pm](https://img.shields.io/hexpm/v/protobuf.svg)](https://hex.pm/packages/protobuf)
4-
[![Build Status](https://travis-ci.org/tony612/protobuf-elixir.svg?branch=master)](https://travis-ci.org/tony612/protobuf-elixir)
3+
[![CI](https://github.com/elixir-protobuf/protobuf/actions/workflows/main.yml/badge.svg)](https://github.com/elixir-protobuf/protobuf/actions/workflows/main.yml)
54

6-
A pure Elixir implementation of [Google Protobuf](https://developers.google.com/protocol-buffers/)
5+
A pure Elixir implementation of [Google Protobuf](https://developers.google.com/protocol-buffers/).
76

87
## Why this instead of exprotobuf(gpb)?
98

109
It has some must-have and other cool features like:
1110

1211
1. A protoc [plugin](https://developers.google.com/protocol-buffers/docs/cpptutorial#compiling-your-protocol-buffers) to generate Elixir code just like what other official libs do, which is powerful and reliable.
13-
2. Generate **simple and explicit** code with the power of Macro. (see [test/support/test_msg.ex](https://github.com/tony612/protobuf-elixir/blob/master/test/support/test_msg.ex))
12+
2. Generate **simple and explicit** code with the power of Macro. See [test/support/test_msg.ex](https://github.com/tony612/protobuf-elixir/blob/master/test/support/test_msg.ex).
1413
3. Plugins support. Only [grpc](https://github.com/tony612/grpc-elixir) is supported now.
1514
4. Use **structs** for messages instead of Erlang records.
1615
5. Support Typespec in generated code.
1716

1817
## Installation
1918

20-
The package can be installed by adding `protobuf` to your list of dependencies in `mix.exs`:
19+
The package can be installed by adding `:protobuf` to your list of dependencies in `mix.exs`:
2120

2221
```elixir
2322
def deps do
@@ -51,40 +50,47 @@ end
5150

5251
### Generate Elixir code
5352

54-
1. Install `protoc`(cpp) [here](https://github.com/google/protobuf/blob/master/src/README.md) or `brew install protobuf` on MacOS.
55-
2. Install protoc plugin `protoc-gen-elixir` for Elixir . NOTE: You have to make sure `protoc-gen-elixir`(this name is important) is in your PATH.
56-
```
57-
$ mix escript.install hex protobuf
58-
```
59-
3. Generate Elixir code using protoc
60-
```
61-
$ protoc --elixir_out=./lib helloworld.proto
62-
```
63-
4. Files `helloworld.pb.ex` will be generated, like:
53+
1. Install `protoc`(cpp) [here](https://github.com/google/protobuf/blob/master/src/README.md) or
54+
`brew install protobuf` on MacOS.
6455

65-
```elixir
66-
defmodule Helloworld.HelloRequest do
67-
use Protobuf, syntax: :proto3
56+
2. Install protoc plugin `protoc-gen-elixir` for Elixir . NOTE: You have to
57+
make sure `protoc-gen-elixir`(this name is important) is in your PATH.
6858

69-
@type t :: %__MODULE__{
70-
name: String.t
71-
}
72-
defstruct [:name]
59+
```bash
60+
$ mix escript.install hex protobuf
61+
```
7362

74-
field :name, 1, type: :string
75-
end
63+
3. Generate Elixir code using protoc
7664

77-
defmodule Helloworld.HelloReply do
78-
use Protobuf, syntax: :proto3
65+
```bash
66+
$ protoc --elixir_out=./lib helloworld.proto
67+
```
7968

80-
@type t :: %__MODULE__{
81-
message: String.t
82-
}
83-
defstruct [:message]
69+
4. Files `helloworld.pb.ex` will be generated, like:
8470

85-
field :message, 1, type: :string
86-
end
87-
```
71+
```elixir
72+
defmodule Helloworld.HelloRequest do
73+
use Protobuf, syntax: :proto3
74+
75+
@type t :: %__MODULE__{
76+
name: String.t
77+
}
78+
defstruct [:name]
79+
80+
field :name, 1, type: :string
81+
end
82+
83+
defmodule Helloworld.HelloReply do
84+
use Protobuf, syntax: :proto3
85+
86+
@type t :: %__MODULE__{
87+
message: String.t
88+
}
89+
defstruct [:message]
90+
91+
field :message, 1, type: :string
92+
end
93+
```
8894

8995
### Encode and decode in your code
9096

@@ -118,32 +124,38 @@ $ protoc --elixir_out=plugins=grpc:./lib/ *.proto
118124
119125
### Tips for protoc
120126
121-
- Custom protoc-gen-elixir name or path using `--plugin`
122-
```
127+
Custom protoc-gen-elixir name or path using `--plugin`:
128+
129+
```bash
123130
$ protoc --elixir_out=./lib --plugin=./protoc-gen-elixir *.proto
124131
```
125-
- Pass `-I` argument if you import other protobuf files
126-
```
132+
133+
Pass `-I` argument if you import other protobuf files:
134+
135+
```bash
127136
$ protoc -I protos --elixir_out=./lib protos/hello.proto
128137
```
129138
130139
### Custom options
131140
132-
Since extensions(`Protobuf.Extension`) is supported now, some options are defined, like custom module_prefix.
141+
Since extensions(`Protobuf.Extension`) is supported now, some options are
142+
defined, like custom module_prefix.
133143
134-
1. Copy src/elixirpb.proto to your protos path
135-
2. Import elixirpb.proto and use the options
144+
1. Copy `src/elixirpb.proto` to your protos path.
136145
137-
```proto
138-
syntax = "proto2";
146+
2. Import `elixirpb.proto` and use the options.
139147
140-
package your.pkg;
148+
```proto
149+
syntax = "proto2";
141150
142-
import "elixirpb.proto";
151+
package your.pkg;
143152
144-
option (elixirpb.file).module_prefix = "Foo.Bar";
145-
```
146-
3. Generate code as before
153+
import "elixirpb.proto";
154+
155+
option (elixirpb.file).module_prefix = "Foo.Bar";
156+
```
157+
158+
3. Generate code as before
147159
148160
More options will be added in the future, see elixirpb.proto comments for details.
149161
@@ -163,7 +175,6 @@ mix test
163175
164176
<img src="https://user-images.githubusercontent.com/1253659/84641850-3f163d80-af2e-11ea-98a2-cfb854180222.png" height="80">
165177
166-
167178
## Acknowledgements
168179
169180
Many thanks to [gpb](https://github.com/tomas-abrahamsson/gpb) and

lib/protobuf.ex

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule Protobuf do
22
@moduledoc """
3-
`protoc` should always be used to generate code instead of wrting the code by hand.
3+
`protoc` should always be used to generate code instead of writing the code by hand.
44
55
By `use` this module, macros defined in `Protobuf.DSL` will be injected. Most of thee macros
66
are equal to definition in .proto files.
@@ -25,10 +25,10 @@ defmodule Protobuf do
2525
Except functions in "Callbacks", some other functions may be defined:
2626
2727
* Extension functions when your Protobuf message use extensions. See `Protobuf.Extension` for details.
28-
* put_extension(struct, extension_mod, field, value)
29-
* get_extension(struct, extension_mod, field, default \\ nil)
30-
"""
28+
* `put_extension(struct, extension_mod, field, value)`
29+
* `get_extension(struct, extension_mod, field, default \\ nil)`
3130
31+
"""
3232
defmacro __using__(opts) do
3333
quote location: :keep do
3434
import Protobuf.DSL, only: [field: 3, field: 2, oneof: 2, extend: 4, extensions: 1]
@@ -73,7 +73,9 @@ defmodule Protobuf do
7373
"""
7474
@callback new() :: struct
7575

76-
@doc "Build and update the struct with passed fields."
76+
@doc """
77+
Build and update the struct with passed fields.
78+
"""
7779
@callback new(Enum.t()) :: struct
7880

7981
@doc """
@@ -97,19 +99,21 @@ defmodule Protobuf do
9799
@callback decode(binary) :: struct
98100

99101
@doc """
100-
It's preferable to use message's `decode` function, like
102+
It's preferable to use message's `decode` function, like:
101103
102104
Foo.decode(bin)
105+
103106
"""
104107
@spec decode(binary, module) :: struct
105108
def decode(data, mod) do
106109
Protobuf.Decoder.decode(data, mod)
107110
end
108111

109112
@doc """
110-
It's preferable to use message's `encode` function, like
113+
It's preferable to use message's `encode` function, like:
111114
112115
Foo.encode(foo)
116+
113117
"""
114118
@spec encode(struct) :: binary
115119
def encode(struct) do

lib/protobuf/json.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ defmodule Protobuf.JSON do
7474
Currently, the `protoc` compiler won't check for field name collisions, this library either.
7575
So make sure your field names will be unique when serialized to JSON. For instance, this
7676
message definition will not encode correctly, it will emit just one of the two fields, and
77-
the problem might go unoticed:
77+
the problem might go unnoticed:
7878
7979
message CollidingFields {
8080
int32 f1 = 1 [json_name = "sameName"];
8181
float f2 = 2 [json_name = "sameName"];
8282
}
8383
8484
According to the specification, when duplicated JSON keys are found in maps, the library
85-
should raise a decoding error. It currently ignores duplicates and keeps the last occurence.
85+
should raise a decoding error. It currently ignores duplicates and keeps the last occurrence.
8686
"""
8787

8888
alias Protobuf.JSON.{Encode, EncodeError, Decode, DecodeError}

lib/protobuf/protoc/cli.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
defmodule Protobuf.Protoc.CLI do
22
@moduledoc """
3-
protoc plugin for generating Elixir code
3+
protoc plugin for generating Elixir code.
44
55
See `protoc -h` and protobuf-elixir for details.
6-
NOTICE: protoc-gen-elixir(this name is important) must be in $PATH
6+
7+
**NOTICE:** protoc-gen-elixir(this name is important) must be in `$PATH`.
78
89
## Examples
910

mix.exs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule Protobuf.Mixfile do
22
use Mix.Project
33

4+
@source_url "https://github.com/elixir-protobuf/protobuf"
45
@version "0.8.0-beta.1"
56

67
def project do
@@ -17,7 +18,8 @@ defmodule Protobuf.Mixfile do
1718
description: description(),
1819
package: package(),
1920
aliases: aliases(),
20-
preferred_cli_env: ["test.integration": :test]
21+
preferred_cli_env: ["test.integration": :test],
22+
docs: docs()
2123
]
2224
end
2325

@@ -36,7 +38,7 @@ defmodule Protobuf.Mixfile do
3638
{:jason, "~> 1.2", optional: true},
3739
{:dialyxir, "~> 1.0", only: [:dev, :test], runtime: false},
3840
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
39-
{:ex_doc, "~> 0.23", only: :dev, runtime: false},
41+
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
4042
{:stream_data, "~> 0.5.0", only: [:dev, :test]}
4143
]
4244
end
@@ -53,9 +55,18 @@ defmodule Protobuf.Mixfile do
5355
[
5456
maintainers: ["Bing Han"],
5557
licenses: ["MIT"],
56-
links: %{"GitHub" => "https://github.com/tony612/protobuf-elixir"},
5758
files:
58-
~w(mix.exs README.md lib/google lib/protobuf lib/*.ex src LICENSE priv/templates .formatter.exs)
59+
~w(mix.exs README.md lib/google lib/protobuf lib/*.ex src LICENSE priv/templates .formatter.exs),
60+
links: %{"GitHub" => @source_url}
61+
]
62+
end
63+
64+
defp docs do
65+
[
66+
extras: ["README.md"],
67+
main: "readme",
68+
source_url: @source_url,
69+
source_ref: "v#{@version}"
5970
]
6071
end
6172

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"file_system": {:hex, :file_system, "0.2.9", "545b9c9d502e8bfa71a5315fac2a923bd060fd9acb797fe6595f54b0f975fd32", [:mix], [], "hexpm", "3cf87a377fe1d93043adeec4889feacf594957226b4f19d5897096d6f61345d8"},
99
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
1010
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
11-
"makeup_elixir": {:hex, :makeup_elixir, "0.15.0", "98312c9f0d3730fde4049985a1105da5155bfe5c11e47bdc7406d88e01e4219b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "75ffa34ab1056b7e24844c90bfc62aaf6f3a37a15faa76b07bc5eba27e4a8b4a"},
11+
"makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"},
1212
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
1313
"stream_data": {:hex, :stream_data, "0.5.0", "b27641e58941685c75b353577dc602c9d2c12292dd84babf506c2033cd97893e", [:mix], [], "hexpm", "012bd2eec069ada4db3411f9115ccafa38540a3c78c4c0349f151fc761b9e271"},
1414
}

0 commit comments

Comments
 (0)