You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add regex replace functionality to transformation filter extractors [Revised] (#309)
* Add regex replace functionality to transformation filter extractors (#301)
* initial extractor implementation of replace functionality
* minor changes, testing against mergeExtractorsToBody and extraction callback
* add changelog entry
* update API to use new mode selector
update transformation_filter proto
* minor updates to comments in transformation_filter.proto
* remove existing references to no-longer-existing replace_all setting
* update replacement_text_ to a std::optional<std::string>
* remove duplicate mode enum
* update comment indicating that subgroup should never exceed regex_result size
* add AttemptReplaceFromNoMatchNonNilSubgroup test
* prevent string reallocation
* remove unnecessary if block + variable in replaceAllValues
* clean up new tests
* inline replacement_text in inja_transformer_test.cc
* more test cleanup
* update function signatures, remove replaced_value_
* support dynamic metadata as extractor input
* update changelog location
* add API changes to go with 3175ca9
* revert support for dynamic metadata as an extractor input 3175ca9 and e2668be
* refactor calls to extract/replace
* rename replace to extractDestructive, add breaks to switch statement
* update data types to match updated function signatures in inja_transformer_test.cc
* respond to review comments
* update changelog location
* update changelog location
* separate destructive extractors and non-destructive extractors
* fix match_not_null edge case
* update inline documentation for new proto field
* add test demonstrating use of format specifiers
* update REPLACE_ALL mode to return input on no match
* return input on no match in single replace case
* update changelog
* empty commit to kick changelog bot after switching target branch
// if there are no matches, return the original input value
168
+
if (!std::regex_search(value.begin(), value.end(), regex_result, extract_regex_)) {
169
+
ENVOY_STREAM_LOG(debug, "replaceIndividualValue: extractor regex did not match input. Returning input", callbacks);
170
+
returnstd::string(value.begin(), value.end());
171
+
}
172
+
173
+
// if the subgroup specified is greater than the number of subgroups in the regex, return the original input value
174
+
if (group_ >= regex_result.size()) {
175
+
// this should never happen as we test this in the ctor.
176
+
ASSERT("no such group in the regex");
177
+
ENVOY_STREAM_LOG(debug, "replaceIndividualValue: invalid group specified for regex. Returning input", callbacks);
178
+
returnstd::string(value.begin(), value.end());
179
+
}
180
+
181
+
// if the regex doesn't match the entire input value, return the original input value
182
+
if (regex_result[0].length() != long(value.length())) {
183
+
ENVOY_STREAM_LOG(debug, "replaceIndividualValue: Regex did not match entire input value. This is not allowed in SINGLE_REPLACE mode. Returning input", callbacks);
184
+
returnstd::string(value.begin(), value.end());
185
+
}
186
+
187
+
// Create a new string with the maximum possible length after replacement
188
+
auto max_possible_length = value.length() + replacement_text_.value().length();
189
+
std::string replaced;
190
+
replaced.reserve(max_possible_length);
191
+
192
+
auto subgroup_start = regex_result[group_].first;
193
+
auto subgroup_end = regex_result[group_].second;
194
+
195
+
// Copy the initial part of the string until the match
196
+
replaced.assign(value.begin(), subgroup_start);
197
+
198
+
// Append the replacement text
199
+
replaced += replacement_text_.value();
200
+
201
+
// Append the remaining part of the string after the match
202
+
replaced.append(subgroup_end, value.end());
203
+
204
+
return replaced;
205
+
}
206
+
207
+
// Match a regex against the input value and replace all instances of the regex with the replacement_text_ value
0 commit comments