Skip to content

Commit af13b45

Browse files
committed
clean up
1 parent d095e79 commit af13b45

File tree

7 files changed

+125
-66
lines changed

7 files changed

+125
-66
lines changed

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schema/DescriptorLoader.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33

44
import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;
5-
import com.squareup.protoparser.ProtoFile;
65

76
import java.io.ByteArrayInputStream;
87
import java.io.File;
98
import java.io.FileInputStream;
109
import java.io.IOException;
1110
import java.io.InputStream;
1211
import java.net.URL;
13-
import java.util.Map;
1412

1513
/**
1614
* Class used for loading protobuf descriptors (from .desc files
@@ -25,7 +23,6 @@ public class DescriptorLoader
2523
private final String DESCRIPTOR_PROTO = "/descriptor.proto";
2624
private ProtobufMapper descriptorMapper;
2725
private ProtobufSchema descriptorFileSchema;
28-
private Map<String, ProtoFile> protoFileMap;
2926

3027
/**
3128
* Standard loader instance that is usually used for loading descriptor file.

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schema/FileDescriptorSet.java

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public MessageElement buildMessageElement()
160160
String typeName = fullyQualifiedtypeName.substring(fullyQualifiedtypeName.indexOf(".", 2) + 1);
161161
dataType = DataType.NamedType.create(typeName);
162162
} else {
163-
dataType = FieldDescriptorProto.Type.getDataType(type);
163+
dataType = f.getDataType();
164164
}
165165

166166
// build field
@@ -219,13 +219,49 @@ public MessageElement buildMessageElement()
219219

220220
static class FieldDescriptorProto
221221
{
222+
static private Map<Type, DataType> scalarTypeMap = new HashMap();
223+
static private Map<Label, FieldElement.Label> labelMap = new HashMap();
224+
225+
static {
226+
scalarTypeMap.put(Type.TYPE_DOUBLE, DataType.ScalarType.DOUBLE);
227+
scalarTypeMap.put(Type.TYPE_FLOAT, DataType.ScalarType.FLOAT);
228+
scalarTypeMap.put(Type.TYPE_INT64, DataType.ScalarType.INT64);
229+
scalarTypeMap.put(Type.TYPE_UINT64, DataType.ScalarType.UINT64);
230+
scalarTypeMap.put(Type.TYPE_INT32, DataType.ScalarType.INT32);
231+
scalarTypeMap.put(Type.TYPE_FIXED64, DataType.ScalarType.FIXED64);
232+
scalarTypeMap.put(Type.TYPE_FIXED32, DataType.ScalarType.FIXED32);
233+
scalarTypeMap.put(Type.TYPE_BOOL, DataType.ScalarType.BOOL);
234+
scalarTypeMap.put(Type.TYPE_STRING, DataType.ScalarType.STRING);
235+
scalarTypeMap.put(Type.TYPE_BYTES, DataType.ScalarType.BYTES);
236+
scalarTypeMap.put(Type.TYPE_UINT32, DataType.ScalarType.UINT32);
237+
scalarTypeMap.put(Type.TYPE_SFIXED32, DataType.ScalarType.SFIXED32);
238+
scalarTypeMap.put(Type.TYPE_SFIXED64, DataType.ScalarType.SFIXED64);
239+
scalarTypeMap.put(Type.TYPE_SINT32, DataType.ScalarType.SINT32);
240+
scalarTypeMap.put(Type.TYPE_SINT64, DataType.ScalarType.SINT64);
241+
242+
labelMap.put(Label.LABEL_OPTIONAL, FieldElement.Label.OPTIONAL);
243+
labelMap.put(Label.LABEL_REQUIRED, FieldElement.Label.REQUIRED);
244+
labelMap.put(Label.LABEL_REPEATED, FieldElement.Label.REPEATED);
245+
}
246+
247+
public String name;
248+
public int number;
249+
public Label label;
250+
public Type type;
251+
public String type_name;
252+
public String extendee;
253+
public String default_value;
254+
public long oneof_index;
255+
public String json_name;
256+
public FieldOptions options;
257+
222258
enum Type
223259
{
224260
TYPE_DOUBLE,
225261
TYPE_FLOAT,
226262
TYPE_INT64,
227263
TYPE_UINT64,
228-
TYPE_LONG,
264+
TYPE_INT32,
229265
TYPE_FIXED64,
230266
TYPE_FIXED32,
231267
TYPE_BOOL,
@@ -238,35 +274,7 @@ enum Type
238274
TYPE_SFIXED32,
239275
TYPE_SFIXED64,
240276
TYPE_SINT32,
241-
TYPE_SINT64;
242-
243-
static private Map<Type, DataType> labelMap = new HashMap();
244-
245-
static {
246-
labelMap.put(TYPE_DOUBLE, DataType.ScalarType.DOUBLE);
247-
labelMap.put(TYPE_FLOAT, DataType.ScalarType.FLOAT);
248-
labelMap.put(TYPE_INT64, DataType.ScalarType.INT64);
249-
labelMap.put(TYPE_UINT64, DataType.ScalarType.UINT64);
250-
// labelMap.put(TYPE_LONG, DataType.ScalarType.LONG);
251-
labelMap.put(TYPE_FIXED64, DataType.ScalarType.FIXED64);
252-
labelMap.put(TYPE_FIXED32, DataType.ScalarType.FIXED32);
253-
labelMap.put(TYPE_BOOL, DataType.ScalarType.BOOL);
254-
labelMap.put(TYPE_STRING, DataType.ScalarType.STRING);
255-
// labelMap.put(TYPE_GROUP, DataType.ScalarType.GROUP);
256-
// labelMap.put(TYPE_MESSAGE, DataType.ScalarType.MESSAGE);
257-
labelMap.put(TYPE_BYTES, DataType.ScalarType.BYTES);
258-
labelMap.put(TYPE_UINT32, DataType.ScalarType.UINT32);
259-
// labelMap.put(TYPE_ENUM, DataType.ScalarType.ENUM);
260-
labelMap.put(TYPE_SFIXED32, DataType.ScalarType.SFIXED32);
261-
labelMap.put(TYPE_SFIXED64, DataType.ScalarType.SFIXED64);
262-
labelMap.put(TYPE_SINT32, DataType.ScalarType.SINT32);
263-
labelMap.put(TYPE_SINT64, DataType.ScalarType.SINT64);
264-
}
265-
266-
static public DataType getDataType(Type type)
267-
{
268-
return labelMap.get(type);
269-
}
277+
TYPE_SINT64
270278
}
271279

272280
enum Label
@@ -276,26 +284,11 @@ enum Label
276284
LABEL_REPEATED
277285
}
278286

279-
static private Map<Label, FieldElement.Label> labelMap = new HashMap();
280-
281-
static {
282-
labelMap.put(Label.LABEL_OPTIONAL, FieldElement.Label.OPTIONAL);
283-
labelMap.put(Label.LABEL_REQUIRED, FieldElement.Label.REQUIRED);
284-
labelMap.put(Label.LABEL_REPEATED, FieldElement.Label.REPEATED);
285-
287+
public DataType getDataType()
288+
{
289+
return scalarTypeMap.get(type);
286290
}
287291

288-
public String name;
289-
public int number;
290-
public Label label;
291-
public Type type;
292-
public String type_name;
293-
public String extendee;
294-
public String default_value;
295-
public long oneof_index;
296-
public String json_name;
297-
public FieldOptions options;
298-
299292
public FieldElement.Label getLabel()
300293
{
301294
return labelMap.get(label);
Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,76 @@
11
package com.fasterxml.jackson.dataformat.protobuf.schema;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.ObjectWriter;
5+
import com.fasterxml.jackson.dataformat.protobuf.ProtobufFactory;
36
import com.fasterxml.jackson.dataformat.protobuf.ProtobufTestBase;
47

58
import java.io.InputStream;
9+
import java.io.StringReader;
610

711
public class DescriptorLoaderTest extends ProtobufTestBase
812
{
13+
14+
private final ObjectMapper MAPPER = new ObjectMapper(new ProtobufFactory());
15+
16+
static class Main
17+
{
18+
public Other o;
19+
20+
protected Main() { }
21+
22+
public Main(Other o)
23+
{
24+
this.o = o;
25+
}
26+
}
27+
28+
29+
static class Other
30+
{
31+
public int f;
32+
33+
protected Other() { }
34+
35+
public Other(int f)
36+
{
37+
this.f = f;
38+
}
39+
}
40+
41+
static String mergedProto =
42+
"syntax = \"proto2\";\n"
43+
+ "package mypackage;\n"
44+
+ "\n"
45+
+ "message Main {\n"
46+
+ " required Other o = 1;\n"
47+
+ "}\n"
48+
+ "\n"
49+
+ "message Other {\n"
50+
+ " required int32 f = 1;\n"
51+
+ "}\n";
52+
953
public void testParsing() throws Exception
1054
{
55+
// create PB binary from known .proto schema
56+
ProtobufSchema schema = ProtobufSchemaLoader.std.load(new StringReader(mergedProto));
57+
58+
final ObjectWriter w = MAPPER.writerFor(Main.class).with(schema);
59+
Other o = new Other(123);
60+
Main m = new Main(o);
61+
byte[] bytes = w.writeValueAsBytes(m);
62+
assertNotNull(bytes);
63+
64+
// Deserialize the bytes using the descriptor
65+
// load main.desc descriptor file. This file was created by protoc - o main.desc main.proto other.proto
1166
InputStream in = this.getClass().getResourceAsStream("/main.desc");
1267
FileDescriptorSet fds = DescriptorLoader.std.load(in);
13-
ProtobufSchema nps = fds.forType("main1");
14-
assertNotNull(nps);
68+
ProtobufSchema schema2 = fds.forType("Main");
69+
70+
Main t = MAPPER.readerFor(Main.class).with(schema2).readValue(bytes);
71+
assertNotNull(t);
72+
assertEquals(123, t.o.f);
73+
1574
}
1675
}
1776

protobuf/src/test/resources/main.desc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
P
2+
/
3+
other.proto mypackage"
4+
Other
5+
f (Rf
6+
L
37

4-
main.proto mypackage other.proto"*
5-
main1!
6-
f1 ( 2.mypackage.other1Rf1
7-
2
8-
other.proto mypackage"
9-
other1
10-
f1 (Rf1
8+
main.proto mypackage other.proto"&
9+
Main
10+
o ( 2.mypackage.OtherRo

protobuf/src/test/resources/main.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package mypackage;
33

44
import "other.proto";
55

6-
message main1 {
7-
required other1 f1 = 1;
6+
message Main {
7+
required Other o = 1;
88
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
syntax = "proto2";
2+
package mypackage;
3+
4+
message Main {
5+
required Other o = 1;
6+
}
7+
8+
message Other {
9+
required int32 f = 1;
10+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
syntax = "proto2";
22
package mypackage;
33

4-
message other1 {
5-
required uint32 f1 = 1;
4+
message Other {
5+
required int32 f = 1;
66
}

0 commit comments

Comments
 (0)