forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support attribute context update. (envoyproxy#31)
* Support attribute context update. * Not to set attribute_context. * Fix format.
- Loading branch information
Showing
7 changed files
with
233 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* Copyright 2017 Google Inc. 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_ATTRIBUTE_H | ||
#define MIXERCLIENT_ATTRIBUTE_H | ||
|
||
#include <chrono> | ||
#include <functional> | ||
#include <map> | ||
#include <memory> | ||
#include <string> | ||
|
||
namespace istio { | ||
namespace mixer_client { | ||
|
||
// A structure to represent a bag of attributes with | ||
// different types. | ||
struct Attributes { | ||
// A structure to hold different types of value. | ||
struct Value { | ||
// Data type | ||
enum ValueType { | ||
STRING, | ||
INT64, | ||
DOUBLE, | ||
BOOL, | ||
TIME, | ||
BYTES, | ||
DURATION, | ||
STRING_MAP | ||
} type; | ||
|
||
// Data value | ||
union { | ||
int64_t int64_v; | ||
double double_v; | ||
bool bool_v; | ||
} value; | ||
// Move types with constructor outside of union. | ||
// It is not easy for union to support them. | ||
std::string str_v; // for both STRING and BYTES | ||
std::chrono::time_point<std::chrono::system_clock> time_v; | ||
std::chrono::nanoseconds duration_nanos_v; | ||
std::map<std::string, std::string> string_map_v; | ||
|
||
// compare operator | ||
bool operator==(const Value& v) const; | ||
}; | ||
|
||
std::map<std::string, Value> attributes; | ||
|
||
// Generates a string for logging or debugging. | ||
std::string DebugString() const; | ||
}; | ||
|
||
} // namespace mixer_client | ||
} // namespace istio | ||
|
||
#endif // MIXERCLIENT_ATTRIBUTE_H |
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,96 @@ | ||
/* Copyright 2017 Google Inc. 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 "include/attribute.h" | ||
#include <sstream> | ||
|
||
namespace istio { | ||
namespace mixer_client { | ||
|
||
bool Attributes::Value::operator==(const Attributes::Value &v) const { | ||
if (type != v.type) { | ||
return false; | ||
} | ||
switch (type) { | ||
case Attributes::Value::ValueType::STRING: | ||
case Attributes::Value::ValueType::BYTES: | ||
return str_v == v.str_v; | ||
break; | ||
case Attributes::Value::ValueType::INT64: | ||
return value.int64_v == v.value.int64_v; | ||
break; | ||
case Attributes::Value::ValueType::DOUBLE: | ||
return value.double_v == v.value.double_v; | ||
break; | ||
case Attributes::Value::ValueType::BOOL: | ||
return value.bool_v == v.value.bool_v; | ||
break; | ||
case Attributes::Value::ValueType::TIME: | ||
return time_v == v.time_v; | ||
break; | ||
case Attributes::Value::ValueType::DURATION: | ||
return duration_nanos_v == v.duration_nanos_v; | ||
break; | ||
case Attributes::Value::ValueType::STRING_MAP: | ||
return string_map_v == v.string_map_v; | ||
break; | ||
} | ||
return false; | ||
} | ||
|
||
std::string Attributes::DebugString() const { | ||
std::stringstream ss; | ||
for (const auto &it : attributes) { | ||
ss << it.first << ": "; | ||
switch (it.second.type) { | ||
case Attributes::Value::ValueType::STRING: | ||
ss << "(STRING): " << it.second.str_v; | ||
break; | ||
case Attributes::Value::ValueType::BYTES: | ||
ss << "(BYTES): " << it.second.str_v; | ||
break; | ||
case Attributes::Value::ValueType::INT64: | ||
ss << "(INT64): " << it.second.value.int64_v; | ||
break; | ||
case Attributes::Value::ValueType::DOUBLE: | ||
ss << "(DOUBLE): " << it.second.value.double_v; | ||
break; | ||
case Attributes::Value::ValueType::BOOL: | ||
ss << "(BOOL): " << it.second.value.bool_v; | ||
break; | ||
case Attributes::Value::ValueType::TIME: | ||
ss << "(TIME ms): " | ||
<< std::chrono::duration_cast<std::chrono::microseconds>( | ||
it.second.time_v.time_since_epoch()) | ||
.count(); | ||
break; | ||
case Attributes::Value::ValueType::DURATION: | ||
ss << "(DURATION nanos): " << it.second.duration_nanos_v.count(); | ||
break; | ||
case Attributes::Value::ValueType::STRING_MAP: | ||
ss << "(STRING MAP):"; | ||
for (const auto &map_it : it.second.string_map_v) { | ||
ss << std::endl; | ||
ss << " " << map_it.first << ": " << map_it.second; | ||
} | ||
break; | ||
} | ||
ss << std::endl; | ||
} | ||
return ss.str(); | ||
} | ||
|
||
} // 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
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