-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move protobuf conversion code to utils. (#58)
* Move protobuf conversation code to utils. * fix format
- Loading branch information
Showing
10 changed files
with
118 additions
and
82 deletions.
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
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
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
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
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
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
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
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
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,56 @@ | ||
/* Copyright 2017 Istio Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "protobuf.h" | ||
|
||
using namespace std::chrono; | ||
|
||
using ::google::protobuf::Duration; | ||
using ::google::protobuf::Timestamp; | ||
|
||
using ::google::protobuf::util::Status; | ||
using ::google::protobuf::util::error::Code; | ||
|
||
namespace istio { | ||
namespace mixer_client { | ||
|
||
// Convert timestamp from time_point to Timestamp | ||
Timestamp CreateTimestamp(system_clock::time_point tp) { | ||
Timestamp time_stamp; | ||
long long nanos = duration_cast<nanoseconds>(tp.time_since_epoch()).count(); | ||
|
||
time_stamp.set_seconds(nanos / 1000000000); | ||
time_stamp.set_nanos(nanos % 1000000000); | ||
return time_stamp; | ||
} | ||
|
||
Duration CreateDuration(nanoseconds value) { | ||
Duration duration; | ||
duration.set_seconds(value.count() / 1000000000); | ||
duration.set_nanos(value.count() % 1000000000); | ||
return duration; | ||
} | ||
|
||
milliseconds ToMilliseonds(const Duration& duration) { | ||
return duration_cast<milliseconds>(seconds(duration.seconds())) + | ||
duration_cast<milliseconds>(nanoseconds(duration.nanos())); | ||
} | ||
|
||
Status ConvertRpcStatus(const ::google::rpc::Status& status) { | ||
return Status(static_cast<Code>(status.code()), status.message()); | ||
} | ||
|
||
} // namespace mixer_client | ||
} // namespace istio |
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,44 @@ | ||
/* Copyright 2017 Istio Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef MIXER_CLIENT_UTILS_PROTOBUF_H_ | ||
#define MIXER_CLIENT_UTILS_PROTOBUF_H_ | ||
|
||
#include "google/protobuf/stubs/status.h" | ||
#include "google/protobuf/timestamp.pb.h" | ||
#include "mixer/v1/service.grpc.pb.h" | ||
|
||
namespace istio { | ||
namespace mixer_client { | ||
|
||
// Convert system_clock time to protobuf timestamp | ||
::google::protobuf::Timestamp CreateTimestamp( | ||
std::chrono::system_clock::time_point tp); | ||
|
||
// Convert from chrono duration to protobuf duration. | ||
::google::protobuf::Duration CreateDuration(std::chrono::nanoseconds value); | ||
|
||
// Convert from prtoobuf duration to chrono duration. | ||
std::chrono::milliseconds ToMilliseonds( | ||
const ::google::protobuf::Duration& duration); | ||
|
||
// Convert from grpc status to protobuf status. | ||
::google::protobuf::util::Status ConvertRpcStatus( | ||
const ::google::rpc::Status& status); | ||
|
||
} // namespace mixer_client | ||
} // namespace istio | ||
|
||
#endif // MIXER_CLIENT_CLIENT_UTILS_PROTOBUF_H_ |