Skip to content
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

fix(plugin): export read-only table fields to the plugin API #27

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading