Skip to content

Commit 7c1afeb

Browse files
authored
Create README.md (#2750)
1 parent a1e350a commit 7c1afeb

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

encoders/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Firebase Encoders
2+
3+
This project provides libraries and code generation infrastructure that allows
4+
encoding java classes into various target serialization formats(currently
5+
supported: **json** and **proto**).
6+
7+
The project consists of multiple parts:
8+
9+
* `firebase_encoders` - Core API and Annotations library.
10+
* `processor` - Java plugin that automatically generates encoders for
11+
`@Encodable` annotated POJOs.
12+
* `firebase_encoders_json` - JSON serialization support.
13+
* `firebase_encoders_proto` - Protobuf serialization support.
14+
* `protoc_gen` - Protobuf compiler plugin that generates encoder-compliant
15+
classes. Can be used with `firebase_encoders_proto` and
16+
`firebase_encoders_json`.
17+
* `reflective` - Can be used to encode any given class via Java
18+
reflection(**not recommented**).
19+
20+
### Protobuf gettings started
21+
22+
##### Step1. Place proto files into **src/main/proto/**
23+
24+
*src/main/proto/my.proto*
25+
```proto
26+
syntax = "proto3";
27+
28+
package com.example.my;
29+
30+
import "google/protobuf/timestamp.proto";
31+
32+
message SimpleProto {
33+
int32 value = 1;
34+
.google.protobuf.Timestamp time = 2;
35+
}
36+
```
37+
38+
39+
##### Step2. Add the following configurations into gradle module build file.
40+
41+
*example.gradle*
42+
```gradle
43+
plugins {
44+
id "java-library"
45+
id 'com.google.protobuf'
46+
}
47+
48+
// add a dependency on the protoc plugin's fat jar to make it available to protobuf below.
49+
configurations.create("protobuild")
50+
dependencies {
51+
protobuild project(path: ":encoders:protoc-gen-firebase-encoders", configuration: "shadow")
52+
}
53+
54+
protobuf {
55+
protoc {
56+
artifact = "com.google.protobuf:protoc:$protocVersion"
57+
}
58+
plugins {
59+
firebaseEncoders {
60+
path = configurations.protobuild.asPath
61+
}
62+
}
63+
generateProtoTasks {
64+
all().each { task ->
65+
task.dependsOn configurations.protobuild
66+
task.inputs.file 'code-gen-cfg.textproto'
67+
task.plugins {
68+
firebaseEncoders {
69+
option file('code-gen-cfg.textproto').path
70+
}
71+
}
72+
// In most cases you don't need the full Java output
73+
task.builtins {
74+
remove java
75+
}
76+
}
77+
}
78+
}
79+
80+
dependencies {
81+
implementation project(":encoders:firebase-encoders")
82+
implementation project(":encoders:firebase-encoders-proto")
83+
annotationProcessor project(":encoders:firebase-encoders-processor")
84+
}
85+
```
86+
87+
##### Step3. Create a code-gen-cfg.textproto file at the module root folder(same location as the gradle module build file).
88+
89+
*code-gen-cfg.textproto*
90+
91+
Note:
92+
- The filename must be the same as the filename determined in the gradle build file.
93+
- Only need to specify the "root" proto object, anything it references will automatically be included.
94+
```textproto
95+
# code_gen_cfg.textproto
96+
# proto-file: src/main/proto/my.proto
97+
# proto-message: SimpleProto
98+
99+
# all types will be vendored in this package
100+
vendor_package: "com.google"
101+
102+
# marks a type as a "root" message
103+
include: "com.example.my.SimpleProto"
104+
```
105+
106+
With the above configuration here's a list of classes that will be generated:
107+
108+
```
109+
com.google.com.example.my.SimpleProto
110+
com.google.com.example.my.SimpleProto$Builder
111+
com.google.google.protobuf.Timestamp
112+
com.google.google.protobuf.Timestamp$Builder
113+
```
114+
115+
Only `root` classes are "encodable" meaning that they have the following
116+
methods:
117+
118+
```java
119+
public class SimpleProto {
120+
public void writeTo(OutputStream output) throws IOException;
121+
public byte[] toByteArray();
122+
}
123+
```

0 commit comments

Comments
 (0)