feat(sdk): support override paths in get_state/set_state functions (#7473)

Co-authored-by: hugocasa <hugo@casademont.ch>
This commit is contained in:
Bryan
2026-01-07 11:38:25 +08:00
committed by GitHub
parent e9810e7795
commit 9f19d91596
4 changed files with 69 additions and 15 deletions

View File

@@ -145,12 +145,22 @@ func GetStatePath() string {
return value return value
} }
func GetState() (interface{}, error) { func getStatePathFromOpt(pathOpt ...string) string {
return GetResource(GetStatePath()) if len(pathOpt) > 0 {
path := pathOpt[0]
if len(path) > 0 {
return path
}
}
return GetStatePath()
} }
func SetState(state interface{}) error { func GetState(pathOpt ...string) (interface{}, error) {
err := SetResource(GetStatePath(), state) return GetResource(getStatePathFromOpt(pathOpt...))
}
func SetState(state interface{}, pathOpt ...string) error {
err := SetResource(getStatePathFromOpt(pathOpt...), state)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -707,13 +707,25 @@ class Windmill:
params=params if params else None, params=params if params else None,
).json() ).json()
def set_state(self, value: Any): def set_state(self, value: Any, path: str | None = None) -> None:
"""Set the workflow state. """Set the workflow state.
Args: Args:
value: State value to set value: State value to set
path: Optional state resource path override.
""" """
self.set_resource(value, path=self.state_path, resource_type="state") self.set_resource(value, path=path or self.state_path, resource_type="state")
def get_state(self, path: str | None = None) -> Any:
"""Get the workflow state.
Args:
path: Optional state resource path override.
Returns:
State value or None if not set
"""
return self.get_resource(path=path or self.state_path, none_if_undefined=True)
def set_progress(self, value: int, job_id: Optional[str] = None): def set_progress(self, value: int, job_id: Optional[str] = None):
"""Set job progress percentage (0-99). """Set job progress percentage (0-99).
@@ -1725,12 +1737,11 @@ def whoami() -> dict:
@init_global_client @init_global_client
@deprecate("Windmill().state") def get_state(path: str | None = None) -> Any:
def get_state() -> Any:
""" """
Get the state Get the state
""" """
return _client.state return _client.get_state(path=path)
@init_global_client @init_global_client
@@ -1781,11 +1792,11 @@ def list_resources(
@init_global_client @init_global_client
def set_state(value: Any) -> None: def set_state(value: Any, path: str | None = None) -> None:
""" """
Set the state Set the state
""" """
return _client.set_state(value) return _client.set_state(value, path=path)
@init_global_client @init_global_client

View File

@@ -479,6 +479,17 @@ impl Windmill {
ret!(self.get_resource_inner(&get_state_path()?)); ret!(self.get_resource_inner(&get_state_path()?));
} }
/// Retrieves and deserializes the typed state value from a custom resource path.
///
/// This is the same as [`Self::get_state`] but allows overriding the underlying state
/// resource path. Useful when you want to read/write a different state resource.
pub fn get_state_at<'a, T: serde::de::DeserializeOwned>(
&'a self,
path: &'a str,
) -> MaybeFuture<'a, Result<T, SdkError>> {
ret!(self.get_resource_inner(path));
}
/// Retrieves the current state value for a script's execution context. /// Retrieves the current state value for a script's execution context.
/// ///
/// States persist data between runs of the same script by the same trigger (schedule or user). /// States persist data between runs of the same script by the same trigger (schedule or user).
@@ -522,6 +533,14 @@ impl Windmill {
ret!(self.get_resource_any_inner(&get_state_path()?)); ret!(self.get_resource_any_inner(&get_state_path()?));
} }
/// Retrieves the untyped state value from a custom resource path.
///
/// This is the same as [`Self::get_state_any`] but allows overriding the underlying state
/// resource path.
pub fn get_state_any_at<'a>(&'a self, path: &'a str) -> MaybeFuture<'a, Result<Value, SdkError>> {
ret!(self.get_resource_any_inner(path));
}
/// Updates or clears the script's persistent state. /// Updates or clears the script's persistent state.
/// ///
/// # Arguments /// # Arguments
@@ -549,6 +568,18 @@ impl Windmill {
ret!(self.set_resource_inner(value, &get_state_path()?, "state")); ret!(self.set_resource_inner(value, &get_state_path()?, "state"));
} }
/// Updates or clears a state resource at a custom path.
///
/// This is the same as [`Self::set_state`] but allows overriding the target state resource
/// path.
pub fn set_state_at<'a>(
&'a self,
value: Option<Value>,
path: &'a str,
) -> MaybeFuture<'a, Result<(), SdkError>> {
ret!(self.set_resource_inner(value, path, "state"));
}
/// Executes a script synchronously and waits for its completion. /// Executes a script synchronously and waits for its completion.
/// ///
/// This is a blocking version of `run_script_async` that handles the entire script execution /// This is a blocking version of `run_script_async` that handles the entire script execution

View File

@@ -589,9 +589,10 @@ export async function setInternalState(state: any): Promise<void> {
/** /**
* Set the state * Set the state
* @param state state to set * @param state state to set
* @param path Optional state resource path override. Defaults to `getStatePath()`.
*/ */
export async function setState(state: any): Promise<void> { export async function setState(state: any, path?: string): Promise<void> {
await setResource(state, undefined, "state"); await setResource(state, path ?? getStatePath(), "state");
} }
/** /**
@@ -706,9 +707,10 @@ export async function getInternalState(): Promise<any> {
/** /**
* Get the state shared across executions * Get the state shared across executions
* @param path Optional state resource path override. Defaults to `getStatePath()`.
*/ */
export async function getState(): Promise<any> { export async function getState(path?: string): Promise<any> {
return await getResource(getStatePath(), true); return await getResource(path ?? getStatePath(), true);
} }
/** /**