Skip to content

Commit b33927b

Browse files
authored
Initial implementation of firebase-encoders-proto (#2206)
* Initial implementation of firebase-encoders-proto * Fix presubmit failures. * Add api.txt file. * Add float support.
1 parent 8cb30c1 commit b33927b

20 files changed

+1368
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Signature format: 2.0
2+
package com.google.firebase.encoders.proto {
3+
4+
public interface ProtoEnum {
5+
method public int getNumber();
6+
}
7+
8+
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.CLASS) public @interface Protobuf {
9+
method public abstract com.google.firebase.encoders.proto.Protobuf.IntEncoding intEncoding() default com.google.firebase.encoders.proto.Protobuf.IntEncoding.DEFAULT;
10+
method public abstract int tag();
11+
}
12+
13+
public enum Protobuf.IntEncoding {
14+
enum_constant public static final com.google.firebase.encoders.proto.Protobuf.IntEncoding DEFAULT;
15+
enum_constant public static final com.google.firebase.encoders.proto.Protobuf.IntEncoding FIXED;
16+
enum_constant public static final com.google.firebase.encoders.proto.Protobuf.IntEncoding SIGNED;
17+
}
18+
19+
public class ProtobufEncoder {
20+
method public static com.google.firebase.encoders.proto.ProtobufEncoder.Builder builder();
21+
method public void encode(@NonNull Object, @NonNull OutputStream);
22+
method @NonNull public byte[] encode(@NonNull Object);
23+
}
24+
25+
public static final class ProtobufEncoder.Builder implements com.google.firebase.encoders.config.EncoderConfig<com.google.firebase.encoders.proto.ProtobufEncoder.Builder> {
26+
ctor public ProtobufEncoder.Builder();
27+
method public com.google.firebase.encoders.proto.ProtobufEncoder build();
28+
method @NonNull public com.google.firebase.encoders.proto.ProtobufEncoder.Builder configureWith(@NonNull com.google.firebase.encoders.config.Configurator);
29+
method @NonNull public <U> com.google.firebase.encoders.proto.ProtobufEncoder.Builder registerEncoder(@NonNull Class<U>, @NonNull com.google.firebase.encoders.ObjectEncoder<? super U>);
30+
method @NonNull public <U> com.google.firebase.encoders.proto.ProtobufEncoder.Builder registerEncoder(@NonNull Class<U>, @NonNull com.google.firebase.encoders.ValueEncoder<? super U>);
31+
method @NonNull public com.google.firebase.encoders.proto.ProtobufEncoder.Builder registerFallbackEncoder(@NonNull com.google.firebase.encoders.ObjectEncoder<Object>);
32+
}
33+
34+
}
35+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
plugins {
16+
id 'firebase-java-library'
17+
id 'com.google.protobuf'
18+
}
19+
20+
firebaseLibrary {
21+
publishSources = true
22+
publishJavadoc = false
23+
}
24+
25+
java {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
30+
protobuf {
31+
protoc {
32+
artifact = "com.google.protobuf:protoc:$protocVersion"
33+
}
34+
}
35+
36+
37+
dependencies {
38+
implementation 'androidx.annotation:annotation:1.1.0'
39+
implementation project(':encoders:firebase-encoders')
40+
41+
annotationProcessor project(':encoders:firebase-encoders-processor')
42+
43+
testAnnotationProcessor project(':encoders:firebase-encoders-processor')
44+
45+
testImplementation 'com.google.guava:guava:30.0-jre'
46+
testImplementation 'junit:junit:4.13.1'
47+
testImplementation 'com.google.protobuf:protobuf-java-util:3.11.0'
48+
testImplementation 'com.google.truth.extensions:truth-proto-extension:1.0'
49+
testImplementation "com.google.truth:truth:$googleTruthVersion"
50+
51+
}
52+
53+
tasks.withType(JavaCompile) {
54+
options.compilerArgs << "-Werror"
55+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
version=0.1.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.encoders.proto;
16+
17+
import androidx.annotation.NonNull;
18+
import java.io.OutputStream;
19+
20+
/** OutputStream that only keeps track of the number of bytes written into it. */
21+
final class LengthCountingOutputStream extends OutputStream {
22+
private long length = 0;
23+
24+
@Override
25+
public void write(int b) {
26+
length++;
27+
}
28+
29+
@Override
30+
public void write(byte[] b) {
31+
length += b.length;
32+
}
33+
34+
@Override
35+
public void write(@NonNull byte[] b, int off, int len) {
36+
if ((off < 0)
37+
|| (off > b.length)
38+
|| (len < 0)
39+
|| ((off + len) > b.length)
40+
|| ((off + len) < 0)) {
41+
throw new IndexOutOfBoundsException();
42+
}
43+
length += len;
44+
}
45+
46+
long getLength() {
47+
return length;
48+
}
49+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.encoders.proto;
16+
17+
/**
18+
* Base interface to be implemented by Enums that want to have control over their representation.
19+
*/
20+
public interface ProtoEnum {
21+
22+
/** Numeric representation of the Enum. */
23+
int getNumber();
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.encoders.proto;
16+
17+
import com.google.firebase.encoders.annotations.ExtraProperty;
18+
19+
/**
20+
* Protocol buffer field configuration that propagates into {@link
21+
* com.google.firebase.encoders.FieldDescriptor}s
22+
*/
23+
@ExtraProperty
24+
public @interface Protobuf {
25+
/** Field tag */
26+
int tag();
27+
28+
/** Specifies numeric field encoding. */
29+
IntEncoding intEncoding() default IntEncoding.DEFAULT;
30+
31+
enum IntEncoding {
32+
DEFAULT,
33+
SIGNED,
34+
FIXED
35+
}
36+
}

0 commit comments

Comments
 (0)