Skip to content

Commit

Permalink
Plugins: UI Fix binding & Extend Example Plugins
Browse files Browse the repository at this point in the history
* Fix binding on text input control.
* Remove saving configuration on Destroy because it's called after
  session request is sent.
* Extend string plugin with prefix field to demonstrate configurations
  functionality
* Add more debug logs
  • Loading branch information
AmmarAbouZor committed Dec 20, 2024
1 parent e121414 commit 215ba58
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 36 deletions.
55 changes: 45 additions & 10 deletions application/apps/indexer/plugins/string_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use plugins_api::{
};

const LOSSY_ID: &str = "lossy";
const PREFIX_ID: &str = "prefix";

/// Simple struct that converts the given bytes into UTF-8 Strings line by line.
///
/// This parser will ignore invalid characters if [`Self::lossy`] is set to true, otherwise
/// it'll return a parsing error.
pub struct StringTokenizer {
lossy: bool,
prefix: Option<String>,
}

impl StringTokenizer {
Expand All @@ -33,7 +35,7 @@ impl StringTokenizer {
let end_idx = memchr(b'\n', data).unwrap_or_else(|| data.len() - 1);
let slice = &data[..end_idx];

let line = if self.lossy {
let mut line = if self.lossy {
String::from_utf8_lossy(slice).to_string()
} else {
std::str::from_utf8(slice)
Expand All @@ -42,6 +44,10 @@ impl StringTokenizer {
ParseError::Parse(format!("Converting to UTF-8 failed. Error {err}"))
})?
};

if let Some(prefix) = self.prefix.as_ref() {
line = format!("{prefix} {line}");
}
let msg = ParsedMessage::Line(line);
let yld = ParseYield::Message(msg);

Expand All @@ -56,12 +62,20 @@ impl Parser for StringTokenizer {
}

fn get_config_schemas() -> Vec<ConfigSchemaItem> {
vec![ConfigSchemaItem::new(
LOSSY_ID,
"Parse Lossy",
Some("Parse UTF-8 including invalid characters"),
ConfigSchemaType::Boolean,
)]
vec![
ConfigSchemaItem::new(
LOSSY_ID,
"Parse Lossy",
Some("Parse UTF-8 including invalid characters"),
ConfigSchemaType::Boolean,
),
ConfigSchemaItem::new(
PREFIX_ID,
"Custom Prefix",
Some("Specify custom prefix for each line"),
ConfigSchemaType::Text,
),
]
}

fn get_render_options() -> RenderOptions {
Expand All @@ -76,7 +90,7 @@ impl Parser for StringTokenizer {
where
Self: Sized,
{
// Demonstrates log functionality
// *** Demonstrates log functionality ***
log::debug!(
"Plugin initialize called with the general settings: {:?}",
general_configs
Expand All @@ -87,6 +101,7 @@ impl Parser for StringTokenizer {
plugins_configs
);

// *** Configurations validation ***
let lossy_config_item = plugins_configs
.iter()
.find(|item| item.id == LOSSY_ID)
Expand All @@ -107,8 +122,28 @@ impl Parser for StringTokenizer {
}
};

// Plugin initialization.
Ok(Self { lossy })
let prefix_config_item = plugins_configs
.iter()
.find(|item| item.id == PREFIX_ID)
.ok_or_else(|| {
InitError::Config(format!(
"No configuration value for id '{PREFIX_ID}' is provided"
))
})?;

let prefix = match &prefix_config_item.value {
ConfigValue::Text(txt) if txt.is_empty() => None,
ConfigValue::Text(txt) => Some(txt.to_owned()),
invalid => {
let err_msg = format!(
"Invalid config value for '{PREFIX_ID}' is provided. Value: {invalid:?}"
);
return Err(InitError::Config(err_msg));
}
};

// *** Plugin initialization ***
Ok(Self { lossy, prefix })
}

fn parse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ async fn export<S: ByteSource>(
println!("------------------------------------------------------");
println!("------------- WASM parser used -----------------");
println!("------------------------------------------------------");
println!("DEBUG: Plugin Path: {}", settings.plugin_path.display());
let parser = PluginsParser::initialize(
&settings.plugin_path,
&settings.general_settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async fn run_source_intern<S: ByteSource>(
println!("------------------------------------------------------");
println!("------------- WASM parser used -----------------");
println!("------------------------------------------------------");
println!("DEBUG: Plugin Path: {}", settings.plugin_path.display());
let parser = PluginsParser::initialize(
&settings.plugin_path,
&settings.general_settings,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
Component,
ChangeDetectorRef,
Input,
SimpleChange,
AfterContentInit,
OnDestroy,
} from '@angular/core';
import { Component, ChangeDetectorRef, Input, SimpleChange, AfterContentInit } from '@angular/core';
import { ChangesDetector } from '@ui/env/extentions/changes';
import { ConfigSchema } from '@platform/types/plugins';
import { State } from '../../state';
Expand All @@ -15,7 +8,7 @@ import { State } from '../../state';
templateUrl: './template.html',
styleUrls: ['./styles.less'],
})
export class ConfigSchemaBool extends ChangesDetector implements AfterContentInit, OnDestroy {
export class ConfigSchemaBool extends ChangesDetector implements AfterContentInit {
@Input() public config!: ConfigSchema;
@Input() public state!: State;

Expand All @@ -25,11 +18,6 @@ export class ConfigSchemaBool extends ChangesDetector implements AfterContentIni
super(cdRef);
}

//TODO AAZ: Check if all this saving is needed (Init + OnChange + OnDestroy)
ngOnDestroy(): void {
this.state.saveConfig(this.config.id, { Boolean: this.value });
}

ngAfterContentInit(): void {
this.value = false;
this.state.saveConfig(this.config.id, { Boolean: this.value });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
ChangeDetectorRef,
Input,
AfterContentInit,
OnDestroy,
OnChanges,
SimpleChanges,
} from '@angular/core';
Expand All @@ -16,10 +15,7 @@ import { State } from '../../state';
templateUrl: './template.html',
styleUrls: ['./styles.less'],
})
export class ConfigSchemaString
extends ChangesDetector
implements AfterContentInit, OnChanges, OnDestroy
{
export class ConfigSchemaString extends ChangesDetector implements AfterContentInit, OnChanges {
@Input() public config!: ConfigSchema;
@Input() public state!: State;

Expand All @@ -35,16 +31,13 @@ export class ConfigSchemaString
}
}

ngOnDestroy(): void {
this.state.saveConfig(this.config.id, { Text: this.value });
}

ngAfterContentInit(): void {
this.value = '';
this.state.saveConfig(this.config.id, { Text: this.value });
}

public ngOnInputChange(event: string): void {
this.state.saveConfig(this.config.id, { Text: event });
public ngOnInputChange(event: Event): void {
const target = event.target as HTMLInputElement;
this.state.saveConfig(this.config.id, { Text: target?.value ?? '' });
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<mat-form-field class="material-mofication-normal">
<mat-label>{{config.title}}</mat-label>
<input matInput [attr.placeholder]="config.title" [(ngModel)]="value" />
<input
matInput
[attr.placeholder]="config.title"
[(ngModel)]="value"
(input)="ngOnInputChange($event)"
/>
<mat-hint>{{config.description}}</mat-hint>
</mat-form-field>
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
position: relative;
display: block;
}

.itemspadding {
padding-bottom: 20px;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<app-tabs-config-schema-entry
*ngFor="let config of state.schemas"
class="itemspadding"
[config]="config"
[state]="state"
></app-tabs-config-schema-entry>

0 comments on commit 215ba58

Please sign in to comment.