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

Document prototool usage #85

Merged
merged 1 commit into from
Aug 15, 2019
Merged
Changes from all 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
74 changes: 73 additions & 1 deletion docs/weave-tutorial/03-codec.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,79 @@ As you can see above we define necessary fields that will be used by `handler` t

To compile protobuf files, you need to have the [protoc](https://github.com/google/protobuf#protocol-compiler-installation) binary installed and a language-specific translator (gogo-protobuf in this case). This can be a bit of a pain, especially the first time, so the default **weave-starter-kit** [Makefile](https://github.com/iov-one/weave-starter-kit/blob/master/Makefile) contains helpers for you.

After defining your state and messages run `make protoc`. This script will download `prototool` docker image that is for making protooling easier) and compile codec.proto file `codec.pb.go` file.
### Prototool

At IOV, we use [prototool](https://github.com/iov-one/prototool-docker) for linting, formating and generating protobuf files. It is highly convenient and easy to use. Generating `*.pb.go` files are easy as running `make protoc` with Makefile script. Configuration is easier, just edit `prototool.yaml` or `prototool.json`.

Here is the [prototool.yaml](https://github.com/iov-one/weave-starter-kit/blob/master/prototool.yaml) from **weave-starter-kit**:

```yaml
protoc:
version: 3.7.1 # Defines which version of protoc to use

# If not set, compile will fail if there are unused imports.
# Setting this will ignore unused imports.
allow_unused_imports: false

# Import parths
includes:
- .
- /usr/include
- spec
- spec/github.com/iov-one/weave

# excluded paths
excludes:
- .git
- spec

generate:
go_options:
import_path: github.com/iov-one/weave-starter-kit

plugins:
- name: gogofaster # Indicates gogofaster will be used.
type: gogo
output: .

lint:
rules:
remove:
- FILE_OPTIONS_REQUIRE_JAVA_MULTIPLE_FILES
- FILE_OPTIONS_REQUIRE_JAVA_OUTER_CLASSNAME
- FILE_OPTIONS_REQUIRE_JAVA_PACKAGE
- FILE_OPTIONS_REQUIRE_GO_PACKAGE
```

- `includes` field contains which import paths will be used when running protoc.

- `.` means that `import "x/custom/codec.go"` will work inside this app (recommended to avoid GOPATH issues)
- `spec` means that we can `import "github.com/iov-one/weave/coins/codec.proto"` and it will use the protobuf file from our spec directory (which must be kept up to date with the weave repo).
- `spec/github.com/iov-one/weave` is needed as if we import eg. `github.com/iov-one/weave/x/cash/codec.proto`, it imports `coins/codec.proto` using relative imports to it's project. When we then import this via spec, the relative import doesn't work without this second line.

- `excludes` field contains which paths to exclude when compiling protobuf. `spec` directory contains the weave codecs that will be used in modules, so no need to compile these.

```yaml
generate:
go_options:
import_path: github.com/iov-one/weave-starter-kit
```

- The base import path. This should be the go path of the prototool.yaml file. This is required if you have any go plugins.

```yaml
plugins:
- name: gogofaster # Indicates gogofaster will be used.
type: gogo
output: .
```

- This part tells protool to use `gogofaster` plugin and output is current directory.
You are welcome to use other codecs than `gogofaster`, you can also try the standard Go language protobuf compiler. What this mode goes is auto-generate static code for serialization and deserialization of the type. It performs the introspection one time to generate efficient code allowing us to avoid the use of reflection at runtime and get ~10x speed ups in the serialization/deserialization. I like this, but this may vary based on your preference or aversion of auto-generated code.

- `lint` field defines the linting rules. Extra rules could be added if desired.

After defining your state and messages run `make protoc`. This script will download `prototool` docker image and compile codec.proto file `codec.pb.go` file.
Now we have scaffold of our application thanks to auto-generated *codec.pb.go* file.

## Using Autogenerated Structs
Expand Down