Skip to content

Commit

Permalink
fix(plugin): export read-only table fields to the plugin API
Browse files Browse the repository at this point in the history
Signed-off-by: Grzegorz Nosek <[email protected]>
  • Loading branch information
gnosek committed Dec 12, 2024
1 parent e1d5323 commit c18f4e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions falco_plugin/src/plugin/exported_tables/field/readonly.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::internals::tables::export::DynamicFieldValue;
use crate::plugin::exported_tables::field_value::traits::FieldValue;
use crate::plugin::exported_tables::field_value::traits::{seal, StaticField};
use crate::plugin::exported_tables::metadata::HasMetadata;
Expand Down Expand Up @@ -52,3 +53,11 @@ impl<T: StaticField> StaticField for Readonly<T> {
const TYPE_ID: FieldTypeId = T::TYPE_ID;
const READONLY: bool = true;
}

impl<T: TryFrom<DynamicFieldValue>> TryFrom<DynamicFieldValue> for Readonly<T> {
type Error = T::Error;

fn try_from(value: DynamicFieldValue) -> Result<Self, Self::Error> {
Ok(Self(T::try_from(value)?))
}
}
6 changes: 6 additions & 0 deletions falco_plugin_tests/tests/parse_table_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type RemainingEntryTable = export::Table<u64, RemainingCounter>;
#[derive(export::Entry)]
struct RemainingCounter {
remaining: export::Public<u64>,
readonly: export::Readonly<u64>,
}

// same table, but imported
Expand All @@ -33,6 +34,7 @@ type RemainingCounterImport = import::Entry<Arc<RemainingCounterImportMetadata>>
#[entry_type(RemainingCounterImport)]
struct RemainingCounterImportMetadata {
remaining: import::Field<u64, RemainingCounterImport>,
readonly: import::Field<u64, RemainingCounterImport>,
}

struct DummyPlugin {
Expand Down Expand Up @@ -145,6 +147,10 @@ impl ParsePlugin for DummyPlugin {
let w = &parse_input.writer;
let entry = self.remaining_table_import.create_entry(w)?;
entry.set_remaining(w, &remaining)?;
anyhow::ensure!(
entry.set_readonly(w, &1).is_err(),
"setting a read-only field succeeded"
);
let _ = self
.remaining_table_import
.insert(r, w, &event_num, entry)?;
Expand Down

0 comments on commit c18f4e5

Please sign in to comment.