forked from opencontainers/runtime-spec
-
Notifications
You must be signed in to change notification settings - Fork 0
proto: Add example.cpp to get working JSON serialization #3
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
Open
wking
wants to merge
7
commits into
vbatts:protobuf3
Choose a base branch
from
wking:protobuf3-cpp
base: protobuf3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
932c50d
proto: Port *.proto to proto3
wking 60a186f
proto: Add example.cpp to get working JSON serialization
wking 2e87acb
proto: Make the C++ example the default
wking 9371318
proto: Drop LinuxSpec (and work around 'linux' sensitivity)
wking 83aff07
proto: Drop LinuxRuntimeSpec (and work around 'linux' sensitivity)
wking a7129a6
proto: Flesh out config.json
wking 5f422ee
proto: Flesh out runtime.json (and pass it through example_cpp)
wking 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 hidden or 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
This file contains hidden or 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,35 @@ | ||
| { | ||
| "version": "0.1.0", | ||
| "platform": { | ||
| "os": "linux", | ||
| "arch": "amd64" | ||
| }, | ||
| "process": { | ||
| "terminal": true, | ||
| "user": { | ||
| "@type": "type.googleapis.com/oci.LinuxUser", | ||
| "uid": 1, | ||
| "gid": 1, | ||
| "additionalGids": [ | ||
| 5, | ||
| 6 | ||
| ] | ||
| }, | ||
| "args": [ | ||
| "sh" | ||
| ], | ||
| "env": [ | ||
| "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", | ||
| "TERM=linux" | ||
| ], | ||
| "cwd": "/root" | ||
| }, | ||
| "hostname": "mrsdalloway", | ||
| "linuxx": { | ||
| "capabilities": [ | ||
| "CAP_AUDIT_WRITE", | ||
| "CAP_KILL", | ||
| "CAP_NET_BIND_SERVICE" | ||
| ] | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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,104 @@ | ||
| // See README.txt for information and build instructions. | ||
|
|
||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
| #include <google/protobuf/descriptor.h> | ||
| #include <google/protobuf/util/json_util.h> | ||
| #include <google/protobuf/util/type_resolver.h> | ||
| #include <google/protobuf/util/type_resolver_util.h> | ||
|
|
||
| #include "config.pb.h" | ||
| #include "runtime_config.pb.h" | ||
|
|
||
| using namespace std; | ||
|
|
||
| static const char kTypeUrlPrefix[] = "type.googleapis.com"; | ||
|
|
||
| static string GetTypeUrl(const google::protobuf::Descriptor* message) { | ||
| return string(kTypeUrlPrefix) + "/" + message->full_name(); | ||
| } | ||
|
|
||
| static bool ReadMessage(string path, google::protobuf::Message *message) { | ||
| string binary; | ||
| ifstream input(path.c_str(), ios::in | ios::binary); | ||
| if (!input) { | ||
| cout << path << ": File not found." << endl; | ||
| return false; | ||
| } | ||
| string json( (istreambuf_iterator<char>(input)), | ||
| istreambuf_iterator<char>() ); | ||
| google::protobuf::scoped_ptr<google::protobuf::util::TypeResolver> resolver; | ||
| resolver.reset(google::protobuf::util::NewTypeResolverForDescriptorPool( | ||
| kTypeUrlPrefix, | ||
| google::protobuf::DescriptorPool::generated_pool())); | ||
| GOOGLE_CHECK_OK(google::protobuf::util::JsonToBinaryString( | ||
| resolver.get(), | ||
| GetTypeUrl(message->GetDescriptor()), | ||
| json, | ||
| &binary)); | ||
| return message->ParseFromString(binary); | ||
| } | ||
|
|
||
| static bool WriteMessage(const google::protobuf::Message& message) { | ||
| string json; | ||
| google::protobuf::util::JsonOptions options; | ||
| google::protobuf::scoped_ptr<google::protobuf::util::TypeResolver> resolver; | ||
| resolver.reset(google::protobuf::util::NewTypeResolverForDescriptorPool( | ||
| kTypeUrlPrefix, | ||
| google::protobuf::DescriptorPool::generated_pool())); | ||
| options.add_whitespace = true; | ||
| GOOGLE_CHECK_OK(google::protobuf::util::BinaryToJsonString( | ||
| resolver.get(), | ||
| GetTypeUrl(message.GetDescriptor()), | ||
| message.SerializeAsString(), | ||
| &json, | ||
| options)); | ||
| cout << json; | ||
| return true; | ||
| } | ||
|
|
||
| // Main function: Reads the config from a file and writes it to stdout. | ||
| int main(int argc, char* argv[]) { | ||
| // Verify that the version of the library that we linked against is | ||
| // compatible with the version of the headers we compiled against. | ||
| GOOGLE_PROTOBUF_VERIFY_VERSION; | ||
|
|
||
| oci::Spec config; | ||
| oci::RuntimeSpec runtime; | ||
|
|
||
| if (!ReadMessage("config.json", &config)) { | ||
| cerr << "config.json: Failed to load." << endl; | ||
| return -1; | ||
| } | ||
|
|
||
| /* | ||
| oci::LinuxUser user; | ||
| user.set_uid(1); | ||
| user.set_gid(1); | ||
| user.add_additional_gids(5); | ||
| user.add_additional_gids(6); | ||
| config.mutable_process()->mutable_user()->PackFrom(user); | ||
| */ | ||
|
|
||
| if (!WriteMessage(config)) { | ||
| cerr << "config.json: Failed to write to stdout." << endl; | ||
| return -1; | ||
| } | ||
|
|
||
| if (!ReadMessage("runtime.json", &runtime)) { | ||
| cerr << "runtime.json: Failed to load." << endl; | ||
| return -1; | ||
| } | ||
|
|
||
| if (!WriteMessage(runtime)) { | ||
| cerr << "runtime.json: Failed to write to stdout." << endl; | ||
| return -1; | ||
| } | ||
|
|
||
| // Optional: Delete all global objects allocated by libprotobuf. | ||
| google::protobuf::ShutdownProtobufLibrary(); | ||
|
|
||
| return 0; | ||
| } |
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.
Is type added by the json generation? Or is this something we have in the spec now? (I don't see it)
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.
On Wed, Sep 30, 2015 at 10:29:33AM -0700, Timothy Chen wrote:
It's added by protobuf so it can figure out which message type to use
for deserializing Any fields. See 1.