Add an option to allow jsonpath/xpath to return as array (#297)

Co-authored-by: Gregory Schier <gschier1990@gmail.com>
This commit is contained in:
Gregor Majcen
2025-11-13 14:57:11 +01:00
committed by GitHub
parent a4c4663011
commit 593a7ab7e5
34 changed files with 800 additions and 338 deletions

View File

@@ -32,7 +32,7 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
event: &InternalEvent,
plugin_handle: &PluginHandle,
) -> Result<Option<InternalEventPayload>> {
// debug!("Got event to app {event:?}");
// log::debug!("Got event to app {event:?}");
let plugin_context = event.context.to_owned();
match event.clone().payload {
InternalEventPayload::CopyTextRequest(req) => {

View File

@@ -3,11 +3,11 @@ use crate::error::Error::ModelNotFound;
use crate::error::Result;
use crate::models::{AnyModel, UpsertModelInfo};
use crate::util::{ModelChangeEvent, ModelPayload, UpdateSource};
use log::{error, warn};
use log::error;
use rusqlite::OptionalExtension;
use sea_query::{
Alias, Asterisk, Expr, Func, IntoColumnRef, IntoIden, IntoTableRef, OnConflict, Query,
ReturningClause, SimpleExpr, SqliteQueryBuilder,
Asterisk, Expr, Func, IntoColumnRef, IntoIden, IntoTableRef, OnConflict, Query, SimpleExpr,
SqliteQueryBuilder,
};
use sea_query_rusqlite::RusqliteBinder;
use std::fmt::Debug;

View File

@@ -1,7 +1,6 @@
use crate::commands::*;
use crate::migrate::migrate_db;
use crate::query_manager::QueryManager;
use crate::util::ModelChangeEvent;
use log::error;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;

View File

@@ -30,7 +30,7 @@ export type CallHttpRequestActionArgs = { httpRequest: HttpRequest, };
export type CallHttpRequestActionRequest = { index: number, pluginRefId: string, args: CallHttpRequestActionArgs, };
export type CallTemplateFunctionArgs = { purpose: RenderPurpose, values: { [key in string]?: JsonValue }, };
export type CallTemplateFunctionArgs = { purpose: RenderPurpose, values: { [key in string]?: JsonPrimitive }, };
export type CallTemplateFunctionRequest = { name: string, args: CallTemplateFunctionArgs, };
@@ -74,7 +74,7 @@ export type FindHttpResponsesRequest = { requestId: string, limit?: number, };
export type FindHttpResponsesResponse = { httpResponses: Array<HttpResponse>, };
export type FormInput = { "type": "text" } & FormInputText | { "type": "editor" } & FormInputEditor | { "type": "select" } & FormInputSelect | { "type": "checkbox" } & FormInputCheckbox | { "type": "file" } & FormInputFile | { "type": "http_request" } & FormInputHttpRequest | { "type": "accordion" } & FormInputAccordion | { "type": "banner" } & FormInputBanner | { "type": "markdown" } & FormInputMarkdown;
export type FormInput = { "type": "text" } & FormInputText | { "type": "editor" } & FormInputEditor | { "type": "select" } & FormInputSelect | { "type": "checkbox" } & FormInputCheckbox | { "type": "file" } & FormInputFile | { "type": "http_request" } & FormInputHttpRequest | { "type": "accordion" } & FormInputAccordion | { "type": "h_stack" } & FormInputHStack | { "type": "banner" } & FormInputBanner | { "type": "markdown" } & FormInputMarkdown;
export type FormInputAccordion = { label: string, inputs?: Array<FormInput>, hidden?: boolean, };
@@ -224,6 +224,8 @@ defaultValue?: string, disabled?: boolean,
*/
description?: string, };
export type FormInputHStack = { inputs?: Array<FormInput>, };
export type FormInputHttpRequest = {
/**
* The name of the input. The value will be stored at this object attribute in the resulting data

View File

@@ -658,6 +658,18 @@ pub enum JsonPrimitive {
Null,
}
impl From<serde_json::Value> for JsonPrimitive {
fn from(value: serde_json::Value) -> Self {
match value {
serde_json::Value::Null => JsonPrimitive::Null,
serde_json::Value::Bool(b) => JsonPrimitive::Boolean(b),
serde_json::Value::Number(n) => JsonPrimitive::Number(n.as_f64().unwrap()),
serde_json::Value::String(s) => JsonPrimitive::String(s),
v => panic!("Unsupported JSON primitive type {:?}", v),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
@@ -733,6 +745,7 @@ pub enum FormInput {
File(FormInputFile),
HttpRequest(FormInputHttpRequest),
Accordion(FormInputAccordion),
HStack(FormInputHStack),
Banner(FormInputBanner),
Markdown(FormInputMarkdown),
}
@@ -895,7 +908,7 @@ pub struct FormInputFile {
#[ts(optional)]
pub directory: Option<bool>,
// Default file path for selection dialog
// Default file path for the selection dialog
#[ts(optional)]
pub default_path: Option<String>,
@@ -953,6 +966,14 @@ pub struct FormInputAccordion {
pub hidden: Option<bool>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct FormInputHStack {
#[ts(optional)]
pub inputs: Option<Vec<FormInput>>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
@@ -1015,7 +1036,7 @@ pub struct CallTemplateFunctionResponse {
#[ts(export, export_to = "gen_events.ts")]
pub struct CallTemplateFunctionArgs {
pub purpose: RenderPurpose,
pub values: HashMap<String, serde_json::Value>,
pub values: HashMap<String, JsonPrimitive>,
}
#[derive(Debug, Clone, Serialize, Deserialize, TS)]

View File

@@ -786,7 +786,7 @@ impl PluginManager {
&self,
plugin_context: &PluginContext,
fn_name: &str,
values: HashMap<String, serde_json::Value>,
values: HashMap<String, JsonPrimitive>,
purpose: RenderPurpose,
) -> TemplateResult<String> {
let req = CallTemplateFunctionRequest {

View File

@@ -1,4 +1,4 @@
use crate::events::{PluginContext, RenderPurpose};
use crate::events::{JsonPrimitive, PluginContext, RenderPurpose};
use crate::manager::PluginManager;
use crate::native_template_functions::{
template_function_keychain_run, template_function_secure_run,
@@ -42,12 +42,17 @@ impl<R: Runtime> TemplateCallback for PluginTemplateCallback<R> {
return template_function_keychain_run(args);
}
let mut primitive_args = HashMap::new();
for (key, value) in args {
primitive_args.insert(key, JsonPrimitive::from(value));
}
let plugin_manager = &*self.app_handle.state::<PluginManager>();
let resp = plugin_manager
.call_template_function(
&self.plugin_context,
fn_name,
args,
primitive_args,
self.render_purpose.to_owned(),
)
.await?;