-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new(libsinsp): add len() filter transformer #2131
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,15 +63,16 @@ | |
return true; | ||
} | ||
|
||
bool sinsp_filter_transformer::transform_type(ppm_param_type& t) const { | ||
bool sinsp_filter_transformer::transform_type(ppm_param_type& t, uint32_t& flags) const { | ||
bool is_list = flags & EPF_IS_LIST; | ||
switch(m_type) { | ||
case FTR_TOUPPER: { | ||
switch(t) { | ||
case PT_CHARBUF: | ||
case PT_FSPATH: | ||
case PT_FSRELPATH: | ||
// for TOUPPER, the transformed type is the same as the input type | ||
return true; | ||
return !is_list; | ||
default: | ||
return false; | ||
} | ||
|
@@ -82,7 +83,7 @@ | |
case PT_FSPATH: | ||
case PT_FSRELPATH: | ||
// for TOLOWER, the transformed type is the same as the input type | ||
return true; | ||
return !is_list; | ||
default: | ||
return false; | ||
} | ||
|
@@ -92,7 +93,7 @@ | |
case PT_CHARBUF: | ||
case PT_BYTEBUF: | ||
// for BASE64, the transformed type is the same as the input type | ||
return true; | ||
return !is_list; | ||
default: | ||
return false; | ||
} | ||
|
@@ -107,6 +108,23 @@ | |
case PT_FSPATH: | ||
case PT_FSRELPATH: | ||
// for BASENAME, the transformed type is the same as the input type | ||
return !is_list; | ||
default: | ||
return false; | ||
} | ||
} | ||
case FTR_LEN: { | ||
if(is_list) { | ||
t = PT_UINT64; | ||
flags = flags & ~EPF_IS_LIST; | ||
return true; | ||
} | ||
switch(t) { | ||
case PT_CHARBUF: | ||
case PT_BYTEBUF: | ||
case PT_FSPATH: | ||
case PT_FSRELPATH: | ||
t = PT_UINT64; | ||
return true; | ||
default: | ||
return false; | ||
|
@@ -119,8 +137,11 @@ | |
} | ||
|
||
bool sinsp_filter_transformer::transform_values(std::vector<extract_value_t>& vec, | ||
ppm_param_type& t) { | ||
if(!transform_type(t)) { | ||
ppm_param_type& t, | ||
uint32_t& flags) { | ||
bool is_list = flags & EPF_IS_LIST; | ||
ppm_param_type original_type = t; | ||
if(!transform_type(t, flags)) { | ||
throw_type_incompatibility_err(t, filter_transformer_type_str(m_type)); | ||
} | ||
|
||
|
@@ -180,6 +201,50 @@ | |
return true; | ||
}); | ||
} | ||
case FTR_LEN: { | ||
assert((void("len() type must be PT_UINT64"), t == PT_UINT64)); | ||
if(is_list) { | ||
uint64_t len = static_cast<uint64_t>(vec.size()); | ||
auto stored_val = store_scalar(len); | ||
vec.clear(); | ||
vec.push_back(stored_val); | ||
return true; | ||
} | ||
|
||
// not a list: could be string or buffer | ||
bool is_string = false; | ||
switch(original_type) { | ||
case PT_CHARBUF: | ||
case PT_FSPATH: | ||
case PT_FSRELPATH: | ||
is_string = true; | ||
break; | ||
case PT_BYTEBUF: | ||
is_string = false; | ||
break; | ||
default: | ||
return false; | ||
} | ||
|
||
for(std::size_t i = 0; i < vec.size(); i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question -- is there really a scenario where the field is not a list-type and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I honestly do not know, but tests were using it like this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you can't think of a corner case (such as plugins) I'll remove the loop as it simplifies things. |
||
uint64_t len; | ||
if(vec[i].ptr == nullptr) { | ||
vec[i] = store_scalar(0); | ||
continue; | ||
} | ||
|
||
if(is_string) { | ||
len = static_cast<uint64_t>( | ||
strnlen(reinterpret_cast<const char*>(vec[i].ptr), vec[i].len)); | ||
vec[i] = store_scalar(len); | ||
continue; | ||
} | ||
|
||
len = static_cast<uint64_t>(vec[i].len); | ||
vec[i] = store_scalar(len); | ||
} | ||
return true; | ||
} | ||
default: | ||
throw_unsupported_err(m_type); | ||
return false; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this potentially a breaking change? It might be that we supported
tolower
(and similars) with list-type fields without noticing. Maybe I'm wrong, just checking in.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, although I never tried it. I would suggest to test and document this behavior if we want it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I have reverted this change and I'll add a couple of tests.