| 152 | }; |
| 153 | |
| 154 | TSReturnCode |
| 155 | scalar_node_handler(const TSYAMLRecCfgFieldData *cfg, void *data) |
| 156 | { |
| 157 | TSOverridableConfigKey name; |
| 158 | TSRecordDataType expected_type; |
| 159 | std::string text; |
| 160 | |
| 161 | auto &ctx = *static_cast<Context *>(data); |
| 162 | YAML::Node value = *reinterpret_cast<YAML::Node *>(cfg->value_node); |
| 163 | |
| 164 | if (TSHttpTxnConfigFind(cfg->record_name, strlen(cfg->record_name), &name, &expected_type) != TS_SUCCESS) { |
| 165 | TSError("[%s] '%s' is not a configuration variable or cannot be overridden", PLUGIN_NAME, cfg->record_name); |
| 166 | return TS_ERROR; |
| 167 | } |
| 168 | |
| 169 | RemapConfigs::Item *item = &ctx.items[*ctx.current]; |
| 170 | |
| 171 | auto type = try_deduce_type(value); |
| 172 | Dbg(dbg_ctl, "### deduced type %d for %s", type, cfg->record_name); |
| 173 | // If we detected a type but it's different from the one registered with the in ATS, then we ignore it. |
| 174 | if (type != TS_RECORDDATATYPE_NULL && expected_type != type) { |
| 175 | TSError("%s", swoc::bwprint(text, "[{}] '{}' variable type mismatch, expected {}, got {}", PLUGIN_NAME, cfg->record_name, |
| 176 | static_cast<int>(expected_type), static_cast<int>(type)) |
| 177 | .c_str()); |
| 178 | return TS_ERROR; // Ignore the field |
| 179 | } |
| 180 | |
| 181 | // If no type set or the type did match, then we assume it's safe to use the |
| 182 | // expected type. |
| 183 | try { |
| 184 | switch (expected_type) { |
| 185 | case TS_RECORDDATATYPE_INT: |
| 186 | item->_data.rec_int = value.as<int64_t>(); |
| 187 | break; |
| 188 | case TS_RECORDDATATYPE_STRING: { |
| 189 | std::string str = value.as<std::string>(); |
| 190 | if (value.IsNull() || str == "NULL") { |
| 191 | item->_data.rec_string = nullptr; |
| 192 | item->_data_len = 0; |
| 193 | } else { |
| 194 | item->_data.rec_string = TSstrdup(str.c_str()); |
| 195 | item->_data_len = str.size(); |
| 196 | } |
| 197 | } break; |
| 198 | case TS_RECORDDATATYPE_FLOAT: |
| 199 | item->_data.rec_float = value.as<float>(); |
| 200 | break; |
| 201 | default: |
| 202 | TSError("[%s] field %s: type(%d) not support (unheard of)", PLUGIN_NAME, cfg->field_name, expected_type); |
| 203 | return TS_ERROR; |
| 204 | |
| 205 | ; |
| 206 | } |
| 207 | } catch (YAML::BadConversion const &e) { |
| 208 | TSError("%s", swoc::bwprint(text, "[{}] We couldn't convert the passed field({}) value({}) to the expected type {}. {}", |
| 209 | PLUGIN_NAME, cfg->field_name, value.as<std::string>(), static_cast<int>(expected_type), e.what()) |
| 210 | .c_str()); |
| 211 | return TS_ERROR; |
nothing calls this directly
no test coverage detected