Skip to content

Commit

Permalink
Code refactory to use protobuf message Attributes. (#232)
Browse files Browse the repository at this point in the history
* Refactory to use protobuf Attributes.

* add new files.

* Change to use pointer for AttributesBuilder.

* Change Builder not to return *this

* change to const string& for key

* change time and duration to be const &
  • Loading branch information
qiwzhang committed Oct 27, 2017
1 parent 03acea2 commit 58d9265
Show file tree
Hide file tree
Showing 28 changed files with 647 additions and 885 deletions.
22 changes: 5 additions & 17 deletions mixerclient/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ genrule(
cc_library(
name = "mixer_client_lib",
srcs = [
"src/attribute.cc",
"src/attribute_converter.cc",
"src/attribute_converter.h",
"src/attribute_compressor.cc",
"src/attribute_compressor.h",
"src/check_cache.cc",
"src/check_cache.h",
"src/client_impl.cc",
Expand All @@ -62,7 +61,7 @@ cc_library(
"utils/status_test_util.h",
],
hdrs = [
"include/attribute.h",
"include/attributes_builder.h",
"include/client.h",
"include/options.h",
"include/timer.h",
Expand Down Expand Up @@ -102,20 +101,9 @@ cc_test(
)

cc_test(
name = "attribute_test",
name = "attribute_compressor_test",
size = "small",
srcs = ["src/attribute_test.cc"],
linkstatic = 1,
deps = [
":mixer_client_lib",
"//external:googletest_main",
],
)

cc_test(
name = "attribute_converter_test",
size = "small",
srcs = ["src/attribute_converter_test.cc"],
srcs = ["src/attribute_compressor_test.cc"],
linkstatic = 1,
deps = [
":mixer_client_lib",
Expand Down
88 changes: 0 additions & 88 deletions mixerclient/include/attribute.h

This file was deleted.

95 changes: 95 additions & 0 deletions mixerclient/include/attributes_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* 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 MIXERCLIENT_ATTRIBUTES_BUILDER_H
#define MIXERCLIENT_ATTRIBUTES_BUILDER_H

#include <chrono>
#include <map>
#include <string>

#include "mixer/v1/attributes.pb.h"

namespace istio {
namespace mixer_client {

// Builder class to add attribute to protobuf Attributes.
// Its usage:
// builder(attribute).Add("key", value)
// .Add("key2", value2);
class AttributesBuilder {
public:
AttributesBuilder(::istio::mixer::v1::Attributes* attributes)
: attributes_(attributes) {}

void AddString(const std::string& key, const std::string& str) {
(*attributes_->mutable_attributes())[key].set_string_value(str);
}

void AddBytes(const std::string& key, const std::string& bytes) {
(*attributes_->mutable_attributes())[key].set_bytes_value(bytes);
}

void AddInt64(const std::string& key, int64_t value) {
(*attributes_->mutable_attributes())[key].set_int64_value(value);
}

void AddDouble(const std::string& key, double value) {
(*attributes_->mutable_attributes())[key].set_double_value(value);
}

void AddBool(const std::string& key, bool value) {
(*attributes_->mutable_attributes())[key].set_bool_value(value);
}

void AddTimestamp(
const std::string& key,
const std::chrono::time_point<std::chrono::system_clock>& value) {
auto time_stamp =
(*attributes_->mutable_attributes())[key].mutable_timestamp_value();
long long nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(
value.time_since_epoch())
.count();
time_stamp->set_seconds(nanos / 1000000000);
time_stamp->set_nanos(nanos % 1000000000);
}

void AddDuration(const std::string& key,
const std::chrono::nanoseconds& value) {
auto duration =
(*attributes_->mutable_attributes())[key].mutable_duration_value();
duration->set_seconds(value.count() / 1000000000);
duration->set_nanos(value.count() % 1000000000);
}

void AddStringMap(const std::string& key,
const std::map<std::string, std::string>& string_map) {
auto entries = (*attributes_->mutable_attributes())[key]
.mutable_string_map_value()
->mutable_entries();
entries->clear();
for (const auto& map_it : string_map) {
(*entries)[map_it.first] = map_it.second;
}
}

private:
::istio::mixer::v1::Attributes* attributes_;
};

} // namespace mixer_client
} // namespace istio

#endif // MIXERCLIENT_ATTRIBUTES_BUILDER_H
5 changes: 2 additions & 3 deletions mixerclient/include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#ifndef MIXERCLIENT_CLIENT_H
#define MIXERCLIENT_CLIENT_H

#include "attribute.h"
#include "google/protobuf/stubs/status.h"
#include "mixer/v1/service.pb.h"
#include "options.h"
Expand Down Expand Up @@ -94,11 +93,11 @@ class MixerClient {
// The response data from mixer will be consumed by mixer client.

// A check call.
virtual CancelFunc Check(const Attributes& attributes,
virtual CancelFunc Check(const ::istio::mixer::v1::Attributes& attributes,
TransportCheckFunc transport, DoneFunc on_done) = 0;

// A report call.
virtual void Report(const Attributes& attributes) = 0;
virtual void Report(const ::istio::mixer::v1::Attributes& attributes) = 0;
};

// Creates a MixerClient object.
Expand Down
Loading

0 comments on commit 58d9265

Please sign in to comment.