-
-
Notifications
You must be signed in to change notification settings - Fork 138
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
add descriptor loader #87
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
75ec330
add descriptor reader
knoguchi c618360
use POJOs instead of JsonNode for the descriptor PB
knoguchi d095e79
fix merge error
knoguchi af13b45
clean up
knoguchi e74892a
fix int32 type. keep the POJO in the order of descriptor.proto
knoguchi f58164a
fix comment
knoguchi 968194e
fix access to ctype field
knoguchi b9e001c
add complete test
knoguchi 8f123f8
POOJO inner classes got to be static
knoguchi 0f33b62
remove the test that might be broken
knoguchi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...obuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schema/DescriptorLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.fasterxml.jackson.dataformat.protobuf.schema; | ||
|
||
|
||
import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
|
||
/** | ||
* Class used for loading protobuf descriptors (from .desc files | ||
* or equivalent sources), to construct FileDescriptorSet. | ||
*/ | ||
public class DescriptorLoader | ||
{ | ||
private final String DESCRIPTOR_PROTO = "/descriptor.proto"; | ||
private ProtobufMapper descriptorMapper; | ||
private ProtobufSchema descriptorFileSchema; | ||
|
||
/** | ||
* Standard loader instance that is usually used for loading descriptor file. | ||
*/ | ||
public final static DescriptorLoader std = new DescriptorLoader(); | ||
|
||
public DescriptorLoader() {} | ||
|
||
|
||
/** | ||
* Public API | ||
*/ | ||
|
||
public FileDescriptorSet load(URL url) throws IOException | ||
{ | ||
return _loadFileDescriptorSet(url.openStream()); | ||
} | ||
|
||
public FileDescriptorSet load(File f) throws IOException | ||
{ | ||
return _loadFileDescriptorSet(new FileInputStream(f)); | ||
} | ||
|
||
public FileDescriptorSet load(InputStream in) throws IOException | ||
{ | ||
return _loadFileDescriptorSet(in); | ||
} | ||
|
||
public FileDescriptorSet fromBytes(byte[] descriptorBytes) throws IOException | ||
{ | ||
return _loadFileDescriptorSet(new ByteArrayInputStream(descriptorBytes)); | ||
} | ||
|
||
protected FileDescriptorSet _loadFileDescriptorSet(InputStream in) throws IOException | ||
{ | ||
try { | ||
if (descriptorMapper == null) { | ||
createDescriptorMapper(); | ||
} | ||
return descriptorMapper.readerFor(FileDescriptorSet.class) | ||
.with(descriptorFileSchema) | ||
.readValue(in); | ||
} | ||
finally { | ||
try { | ||
in.close(); | ||
} | ||
catch (IOException e) { | ||
} | ||
} | ||
} | ||
|
||
private void createDescriptorMapper() throws IOException | ||
{ | ||
// read Descriptor Proto | ||
descriptorMapper = new ProtobufMapper(); | ||
InputStream in = getClass().getResourceAsStream(DESCRIPTOR_PROTO); | ||
descriptorFileSchema = ProtobufSchemaLoader.std.load(in, "FileDescriptorSet"); | ||
in.close(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why catch? Shouldn't it just be propagated as-is?