SchemaValue.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2017 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "SchemaValue.hpp"
  14. #include "EnvironmentValue.hpp"
  15. SchemaValue::SchemaValue(const std::string &name, bool isDefined) :
  16. m_name(name), m_displayName(name)
  17. {
  18. bitMask.m_required = 0;
  19. bitMask.m_readOnly = 0;
  20. bitMask.m_hidden = 0;
  21. bitMask.m_deprecated = 0;
  22. bitMask.m_isUnique = 0;
  23. bitMask.m_isDefined = isDefined;
  24. }
  25. bool SchemaValue::isValueValid(const std::string &value, const EnvironmentValue *pEnvValue) const
  26. {
  27. bool isValid = true; // assume valid
  28. //
  29. // Check the type
  30. isValid = m_pType->isValueValid(value);
  31. //
  32. // Keyed ?, then value must NOT be in the current list.
  33. if (isValid && isUniqueValue() && pEnvValue != nullptr)
  34. {
  35. bool found = false;
  36. std::vector<std::string> allValues;
  37. pEnvValue->getAllValuesForSiblings(allValues);
  38. for (auto it = allValues.begin(); it != allValues.end() && !found; ++it)
  39. found = *it == value;
  40. if (found)
  41. {
  42. return false;
  43. }
  44. }
  45. //
  46. // Keyref ?, then the value must be from another set
  47. if (isValid && isFromUniqueValueSet() && pEnvValue != nullptr)
  48. {
  49. bool found = false;
  50. std::vector<std::string> allValues;
  51. getAllKeyRefValues(allValues);
  52. for (auto it = allValues.begin(); it != allValues.end() && !found; ++it)
  53. found = *it == value;
  54. isValid = found;
  55. }
  56. return isValid;
  57. }
  58. void SchemaValue::validate(Status &status, const std::string &id, const EnvironmentValue *pEnvValue) const
  59. {
  60. std::string curValue = pEnvValue->getValue();
  61. bool isValid;
  62. if (!m_pType->isValueValid(curValue))
  63. {
  64. if (pEnvValue)
  65. {
  66. std::string msg;
  67. if (pEnvValue->wasForced())
  68. msg = "Value was forced to an invalid value (" + curValue + ").";
  69. else
  70. msg = "Value is invalid (" + curValue + ").";
  71. msg += "Valid value (" + m_pType->getLimitString() + ")";
  72. status.addMsg(pEnvValue->wasForced() ? statusMsg::warning : statusMsg::error, pEnvValue->getNodeId(), pEnvValue->getName(), "", msg);
  73. }
  74. isValid = false;
  75. }
  76. // get currentvalue from pEnvValue
  77. // for keyed, make sure all values are unique
  78. // call pType with value to see if good
  79. // call pType->limits->toString(value) if bad to get message about whats bad
  80. // add to status
  81. }
  82. void SchemaValue::resetEnvironment()
  83. {
  84. m_envValues.clear();
  85. }
  86. // replicates the new value throughout the environment
  87. void SchemaValue::mirrorValueToEnvironment(const std::string &oldValue, const std::string &newValue)
  88. {
  89. for (auto mirrorCfgIt = m_mirrorToSchemaValues.begin(); mirrorCfgIt != m_mirrorToSchemaValues.end(); ++mirrorCfgIt)
  90. {
  91. (*mirrorCfgIt)->setMirroredEnvironmentValues(oldValue, newValue);
  92. }
  93. }
  94. // Worker method for replicating a mirrored value to the environment values for this config value
  95. void SchemaValue::setMirroredEnvironmentValues(const std::string &oldValue, const std::string &newValue)
  96. {
  97. for (auto envIt = m_envValues.begin(); envIt != m_envValues.end(); ++envIt)
  98. {
  99. std::shared_ptr<EnvironmentValue> pEnvValue = (*envIt).lock();
  100. if (pEnvValue && pEnvValue->getValue() == oldValue)
  101. {
  102. pEnvValue->setValue(newValue, nullptr, true);
  103. }
  104. }
  105. }
  106. void SchemaValue::getAllEnvironmentValues(std::vector<std::string> &values) const
  107. {
  108. for (auto it = m_envValues.begin(); it != m_envValues.end(); ++it)
  109. {
  110. values.push_back((*it).lock()->getValue());
  111. }
  112. }
  113. void SchemaValue::getAllowedValues(std::vector<AllowedValue> &allowedValues, const EnvironmentValue *pEnvValue) const
  114. {
  115. //
  116. // Either the type is enumerated, or there is a keyref.
  117. if (m_pType->isEnumerated())
  118. {
  119. allowedValues = m_pType->getEnumeratedValues();
  120. }
  121. else if (isFromUniqueValueSet() && pEnvValue != nullptr)
  122. {
  123. std::vector<std::string> refValues;
  124. getAllKeyRefValues(refValues);
  125. for (auto it = refValues.begin(); it != refValues.end(); ++it)
  126. {
  127. allowedValues.push_back({ *it, "" });
  128. }
  129. }
  130. }
  131. void SchemaValue::getAllKeyRefValues(std::vector<std::string> &keyRefValues) const
  132. {
  133. std::vector<std::weak_ptr<SchemaValue>> refCfgValues = getUniqueValueSetRefs();
  134. for (auto refCfgValueIt = refCfgValues.begin(); refCfgValueIt != refCfgValues.end(); ++refCfgValueIt)
  135. {
  136. std::shared_ptr<SchemaValue> pRefCfgValue = (*refCfgValueIt).lock();
  137. std::vector<std::string> allValues;
  138. pRefCfgValue->getAllEnvironmentValues(allValues);
  139. keyRefValues.insert(keyRefValues.end(), allValues.begin(), allValues.end());
  140. }
  141. }