#include "ConfigPropertyEvaluator.h" #include "lib/utils/windows_wrapper.h" #include "lib/utils/bts_assert.h" #include "lib/expression/ExpressionEvaluator.h" #include "ConfigurationState.h" #include #include namespace bts { namespace config { namespace internal { using namespace bts::expression; using namespace boost; using expression::Value; namespace proto = google::protobuf; class ConfigListRuleEvaluatorShim : public expression::IPropertyEvaluator { expression::IPropertyEvaluator& _eval; public: ConfigListRuleEvaluatorShim(expression::IPropertyEvaluator& eval) : _eval(eval) { } virtual ~ConfigListRuleEvaluatorShim() {} virtual bool get_value(const std::string& property, Value* out_value) { return _eval.get_value(property, out_value); } }; ConfigPropertyEvaluator::ConfigPropertyEvaluator(ConfigurationState& config) : _config(config), _rule_engine() { std::shared_ptr ptr(new ConfigListRuleEvaluatorShim(*this)); _rule_engine.reset(new expression::ExpressionEvaluator(ptr)); } ConfigPropertyEvaluator::~ConfigPropertyEvaluator() { } void get_field_scalar(const proto::Message& message, const proto::FieldDescriptor* field_desc, Value& out_value) { const proto::Reflection* mirror = message.GetReflection(); switch(field_desc->cpp_type()) { case proto::FieldDescriptor::CPPTYPE_INT32: out_value = Value((double) mirror->GetInt32(message, field_desc)); break; case proto::FieldDescriptor::CPPTYPE_INT64: out_value = Value((double) mirror->GetInt64(message, field_desc)); break; case proto::FieldDescriptor::CPPTYPE_UINT32: out_value = Value((double) mirror->GetUInt32(message, field_desc)); break; case proto::FieldDescriptor::CPPTYPE_UINT64: out_value = Value((double) mirror->GetUInt64(message, field_desc)); break; case proto::FieldDescriptor::CPPTYPE_DOUBLE: out_value = Value(mirror->GetDouble(message, field_desc)); break; case proto::FieldDescriptor::CPPTYPE_FLOAT: out_value = Value((double) mirror->GetFloat(message, field_desc)); break; case proto::FieldDescriptor::CPPTYPE_BOOL: out_value = Value(mirror->GetBool(message, field_desc)); break; case proto::FieldDescriptor::CPPTYPE_STRING: out_value = Value(mirror->GetString(message, field_desc)); break; case proto::FieldDescriptor::CPPTYPE_ENUM: out_value = Value(mirror->GetEnum(message, field_desc)->name()); break; case proto::FieldDescriptor::CPPTYPE_MESSAGE: out_value = Value((void*)&mirror->GetMessage(message, field_desc)); break; default: throw std::invalid_argument("Unsupported field type in Config rule"); } } void get_field_vector(const proto::Message& message, const proto::FieldDescriptor* field_desc, Value& out_value) { const proto::Reflection* mirror = message.GetReflection(); size_t field_size = mirror->FieldSize(message, field_desc); std::vector values; switch(field_desc->cpp_type()) { case proto::FieldDescriptor::CPPTYPE_INT32: for(size_t index = 0; index < field_size; index++) { values.push_back( Value((double) mirror->GetRepeatedInt32(message, field_desc, index))); } break; case proto::FieldDescriptor::CPPTYPE_INT64: for(size_t index = 0; index < field_size; index++) { values.push_back( Value((double) mirror->GetRepeatedInt64(message, field_desc, index))); } break; case proto::FieldDescriptor::CPPTYPE_UINT32: for(size_t index = 0; index < field_size; index++) { values.push_back( Value((double) mirror->GetRepeatedUInt32(message, field_desc, index))); } break; case proto::FieldDescriptor::CPPTYPE_UINT64: for(size_t index = 0; index < field_size; index++) { values.push_back( Value((double) mirror->GetRepeatedUInt64(message, field_desc, index))); } break; case proto::FieldDescriptor::CPPTYPE_DOUBLE: for(size_t index = 0; index < field_size; index++) { values.push_back( Value((double) mirror->GetRepeatedDouble(message, field_desc, index))); } break; case proto::FieldDescriptor::CPPTYPE_FLOAT: for(size_t index = 0; index < field_size; index++) { values.push_back( Value((double) mirror->GetRepeatedFloat(message, field_desc, index))); } break; case proto::FieldDescriptor::CPPTYPE_BOOL: for(size_t index = 0; index < field_size; index++) { values.push_back( Value(mirror->GetRepeatedBool(message, field_desc, index))); } break; case proto::FieldDescriptor::CPPTYPE_STRING: for(size_t index = 0; index < field_size; index++) { values.push_back( Value(mirror->GetRepeatedString(message, field_desc, index))); } break; case proto::FieldDescriptor::CPPTYPE_ENUM: for (size_t index = 0; index < field_size; index++) { values.push_back( Value(mirror->GetRepeatedEnum(message, field_desc, index)->name())); } break; case proto::FieldDescriptor::CPPTYPE_MESSAGE: for (size_t index = 0; index < field_size; index++) { values.push_back( Value((void*)&mirror->GetRepeatedMessage(message, field_desc, index))); } break; default: throw std::invalid_argument("Unsupported field type in Config rule"); } out_value = Value(values); } const proto::Message* lookup_key(std::shared_ptr evaluator, const proto::Message* message, const proto::FieldDescriptor* field, const std::string& search_key) { bts_assert(message); bts_assert(field); bts_assert(!search_key.empty()); bts_assert(field->is_repeated()); bts_assert(field->type() == proto::FieldDescriptor::TYPE_MESSAGE); const proto::Reflection* mirror = message->GetReflection(); int size = mirror->FieldSize(*message, field); const proto::FieldDescriptor* key_field = NULL; const proto::FieldDescriptor* rule_field = NULL; const proto::Reflection* key_reflection = NULL; for (int x = 0; x < size; x++) { // Iterate through the field instances const proto::Message& cur = mirror->GetRepeatedMessage(*message, field, x); if (x == 0) { // Grab the reflection and field descriptor for the repeated type key_reflection = cur.GetReflection(); bts_assert(key_reflection); // Use the reflection object to look for a field named "key" or "name" key_field = cur.GetDescriptor()->FindFieldByLowercaseName("key"); if (key_field == NULL) { key_field = cur.GetDescriptor()->FindFieldByLowercaseName("name"); if (key_field == NULL) { // If there is no key abandon ship break; } } if (key_field->type() != proto::FieldDescriptor::TYPE_STRING) { // To keep things simple the key must be a string break; } rule_field = cur.GetDescriptor()->FindFieldByLowercaseName("rule"); } std::string key; key = key_reflection->GetStringReference(cur, key_field, &key); if (key == search_key) { if(rule_field != NULL) { std::string rule; rule = key_reflection->GetStringReference(cur, rule_field, &rule); if(rule != "" && !evaluator->eval(rule)) return cur.New(); } return &cur; } } return NULL; } bool ConfigPropertyEvaluator::get_value(const std::string& property, Value* out_value) { bts_assert(out_value); if (property[0] == '$') { *out_value = _config.get_macro(property); return true; } // 1) Get the proto::Message* from config tokenizer > tokens(property, char_separator(".[]")); // Note: Split arbitrarily on [], enforcing matching brackets is done in the parser tokenizer >::iterator iter = tokens.begin (), end = tokens.end (); // The first token is the key name const proto::Message* key = _config.try_get_message_or_default(*iter); if (!key) return false; // 2) Drill down using reflection to find the named property do { ++iter; if (iter == end) { // We're asking for a complete Message *out_value = Value((void*)key); return true; } const proto::Descriptor* message_desc = key->GetDescriptor(); std::string field_name = *iter; const proto::FieldDescriptor* field_desc = message_desc->FindFieldByName(field_name); if (field_desc) { if (field_desc->is_repeated()) { ++iter; if (field_desc->type() == proto::FieldDescriptor::TYPE_MESSAGE && iter != end) { // Treat the next token as a key into the list key = lookup_key(_rule_engine, key, field_desc, *iter); if (key == NULL) return false; } else { get_field_vector(*key, field_desc, *out_value); return (iter == end); } } else { if (field_desc->type() != proto::FieldDescriptor::TYPE_MESSAGE) { get_field_scalar(*key, field_desc, *out_value); return (++iter == tokens.end()); } else { // continue iterating const proto::Reflection* mirror = key->GetReflection(); key = &mirror->GetMessage(*key, field_desc); } } } else { break; } } while(key && iter != end); return false; } }}}//namespace