-
Notifications
You must be signed in to change notification settings - Fork 7
new(plugin): actually allow listing tables, improve fields/tables ergonomics #83
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6e0ad35
new(plugin): actually allow listing tables
gnosek e8e0373
new(plugin): add a more ergonomic wrapper for fields list
gnosek f158432
cleanup(plugin): move list_tables impl to tables::import
gnosek 9dbccec
new(plugin): add a more ergonomic wrapper for tables list
gnosek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| use crate::tables::FieldTypeId; | ||
| use num_traits::FromPrimitive; | ||
| use std::fmt::Debug; | ||
|
|
||
| /// Information about a table field | ||
| #[repr(transparent)] | ||
| pub struct FieldInfo(falco_plugin_api::ss_plugin_table_fieldinfo); | ||
|
|
||
| impl Debug for FieldInfo { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("FieldInfo") | ||
| .field("name", &self.name()) | ||
| .field("read_only", &self.read_only()) | ||
| .field("field_type", &self.field_type()) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| impl FieldInfo { | ||
| /// Returns the name of the field | ||
| /// | ||
| /// If the field name cannot be represented as UTF-8, returns "<invalid field name>" | ||
| pub fn name(&self) -> &str { | ||
| unsafe { std::ffi::CStr::from_ptr(self.0.name) } | ||
| .to_str() | ||
| .unwrap_or("<invalid field name>") | ||
| } | ||
|
gnosek marked this conversation as resolved.
|
||
|
|
||
| /// Returns true if the current field is read-only | ||
| pub fn read_only(&self) -> bool { | ||
| self.0.read_only != 0 | ||
| } | ||
|
|
||
| /// Returns the type of the field | ||
| /// | ||
| /// If the type cannot be represented as a [`FieldTypeId`], returns an error | ||
| /// with the raw field type value. | ||
| pub fn field_type(&self) -> Result<FieldTypeId, u32> { | ||
| match FieldTypeId::from_u32(self.0.field_type) { | ||
| Some(field_type) => Ok(field_type), | ||
| None => Err(self.0.field_type), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| use crate::tables::FieldTypeId; | ||
| use num_traits::FromPrimitive; | ||
| use std::fmt::Debug; | ||
|
|
||
| /// Information about a table | ||
| #[repr(transparent)] | ||
| pub struct TableInfo(falco_plugin_api::ss_plugin_table_info); | ||
|
|
||
| impl Debug for TableInfo { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("TableInfo") | ||
| .field("name", &self.name()) | ||
| .field("key_type", &self.key_type()) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| impl TableInfo { | ||
| /// Returns the name of the table | ||
| /// | ||
| /// If the table name cannot be represented as UTF-8, returns "<invalid table name>" | ||
| pub fn name(&self) -> &str { | ||
| unsafe { std::ffi::CStr::from_ptr(self.0.name) } | ||
| .to_str() | ||
| .unwrap_or("<invalid table name>") | ||
|
gnosek marked this conversation as resolved.
|
||
| } | ||
|
gnosek marked this conversation as resolved.
|
||
|
|
||
| /// Returns the type of the table's key | ||
| /// | ||
| /// If the type cannot be represented as a [`FieldTypeId`], returns an error | ||
| /// with the raw field type value. | ||
| pub fn key_type(&self) -> Result<FieldTypeId, u32> { | ||
| match FieldTypeId::from_u32(self.0.key_type) { | ||
| Some(field_type) => Ok(field_type), | ||
| None => Err(self.0.key_type), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| use anyhow::Error; | ||
| use falco_plugin::base::Plugin; | ||
| use falco_plugin::event::events::Event; | ||
| use falco_plugin::event::{EventInput, PluginEvent}; | ||
| use falco_plugin::parse::{ParseInput, ParsePlugin}; | ||
| use falco_plugin::static_plugin; | ||
| use falco_plugin::tables::{FieldTypeId, TablesInput}; | ||
| use falco_plugin_tests::plugin_collection::events::countdown::Countdown; | ||
| use falco_plugin_tests::plugin_collection::parse::remaining_into_table_direct::PARSE_REMAINING_INTO_TABLE_DIRECT_PLUGIN_API; | ||
| use falco_plugin_tests::plugin_collection::source::countdown::COUNTDOWN_PLUGIN_API; | ||
| use falco_plugin_tests::plugin_collection::tables::remaining_import::RemainingCounterImportTable; | ||
| use falco_plugin_tests::{init_plugin, instantiate_tests, TestDriver}; | ||
| use std::ffi::CStr; | ||
|
|
||
| struct ListFieldsPlugin { | ||
| #[allow(unused)] | ||
| remaining_table_import: RemainingCounterImportTable, | ||
| } | ||
|
|
||
| impl Plugin for ListFieldsPlugin { | ||
| const NAME: &'static CStr = c"dummy"; | ||
| const PLUGIN_VERSION: &'static CStr = c"0.0.0"; | ||
| const DESCRIPTION: &'static CStr = c"test plugin"; | ||
| const CONTACT: &'static CStr = c"rust@localdomain.pl"; | ||
| type ConfigType = (); | ||
|
|
||
| fn new(input: Option<&TablesInput>, _config: Self::ConfigType) -> Result<Self, Error> { | ||
| let input = input.ok_or_else(|| anyhow::anyhow!("did not get table input"))?; | ||
|
|
||
| let remaining_table_import: RemainingCounterImportTable = input.get_table(c"remaining")?; | ||
|
|
||
| let fields = remaining_table_import.list_fields(input); | ||
| let mut num_fields = 0; | ||
| for field in fields { | ||
| match field.name() { | ||
| "remaining" => { | ||
| anyhow::ensure!(field.read_only() == false); | ||
| anyhow::ensure!(field.field_type() == Ok(FieldTypeId::U64)); | ||
| num_fields += 1; | ||
| } | ||
| "readonly" => { | ||
| anyhow::ensure!(field.read_only() == true); | ||
| anyhow::ensure!(field.field_type() == Ok(FieldTypeId::U64)); | ||
| num_fields += 1; | ||
| } | ||
| "countdown" => { | ||
| anyhow::ensure!(field.read_only() == true); | ||
| anyhow::ensure!(field.field_type() == Ok(FieldTypeId::Table)); | ||
| num_fields += 1; | ||
| } | ||
| name => panic!("unknown field: {name}"), | ||
| } | ||
| } | ||
| anyhow::ensure!(num_fields == 3); | ||
|
|
||
|
gnosek marked this conversation as resolved.
|
||
| Ok(Self { | ||
| remaining_table_import, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl ParsePlugin for ListFieldsPlugin { | ||
| type Event<'a> = Event<PluginEvent<Countdown<'a>>>; | ||
|
|
||
| fn parse_event( | ||
| &mut self, | ||
| _event: &EventInput<Self::Event<'_>>, | ||
| _parse_input: &ParseInput, | ||
| ) -> anyhow::Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| static_plugin!(pub LIST_FIELDS_API = ListFieldsPlugin); | ||
|
|
||
| fn test_list_fields<D: TestDriver>() { | ||
| let (mut driver, _plugin) = init_plugin::<D>( | ||
| &COUNTDOWN_PLUGIN_API, | ||
| cr#"{"remaining": 4, "batch_size": 4}"#, | ||
| ) | ||
| .unwrap(); | ||
| driver | ||
| .register_plugin(&PARSE_REMAINING_INTO_TABLE_DIRECT_PLUGIN_API, c"") | ||
| .unwrap(); | ||
| driver.register_plugin(&LIST_FIELDS_API, c"").unwrap(); | ||
| } | ||
|
|
||
| instantiate_tests!(test_list_fields); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.