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

feat(metagen): add file upload support for Rust client #893

Merged
merged 18 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exclude = [
"tests/runtimes/wasm_reflected/rust",
"tests/runtimes/wasm_wire/rust",
"tests/metagen/typegraphs/sample/rs",
"tests/metagen/typegraphs/sample/rs_upload",
"src/pyrt_wit_wire",
]

Expand Down
1 change: 1 addition & 0 deletions src/common/src/typegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod types;
pub mod utils;
pub mod validator;
pub mod visitor;
pub mod visitor2;

pub use types::*;

Expand Down
2 changes: 1 addition & 1 deletion src/common/src/typegraph/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn validate_typegraph(tg: &Typegraph) -> Vec<ValidatorError> {
let context = ValidatorContext { typegraph: tg };
let validator = Validator::default();

errors.extend(tg.traverse_types(validator, &context, Layer).unwrap());
errors.extend(tg.traverse_types(validator, &context, Layer, 0).unwrap());
errors
}

Expand Down
3 changes: 2 additions & 1 deletion src/common/src/typegraph/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl Typegraph {
visitor: V,
context: &'a V::Context,
layer: L,
root_type_idx: u32,
) -> Option<V::Return>
where
V: TypeVisitor<'a> + Sized + 'a,
Expand All @@ -71,7 +72,7 @@ impl Typegraph {
layer,
};
traversal
.visit_type(0, context)
.visit_type(root_type_idx, context)
.or_else(|| traversal.visitor.take_result())
}
}
Expand Down
249 changes: 249 additions & 0 deletions src/common/src/typegraph/visitor2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
use std::{cell::RefCell, rc::Rc};

use crate::typegraph::{
visitor::{Edge, PathSegment},
TypeNode, Typegraph,
};

pub struct VisitorContext<'tg> {
pub tg: &'tg Typegraph,
pub current_node: CurrentNode<'tg>,
}

pub struct NearestFn {
pub path_index: usize,
pub type_idx: u32,
pub is_input: bool,
}

impl<'a> CurrentNode<'a> {
pub fn nearest_function(&self) -> Option<NearestFn> {
for (i, segment) in self.path.borrow().iter().enumerate().rev() {
match segment.edge {
Edge::FunctionInput => {
return Some(NearestFn {
path_index: i,
type_idx: segment.from,
is_input: true,
})
}
Edge::FunctionOutput => {
return Some(NearestFn {
path_index: i,
type_idx: segment.from,
is_input: false,
})
}
_ => continue,
}
}
None
}
}

pub enum VisitNext {
/// continue traversal, with the eventual child nodes
Children,
/// continue traversal, but do not visit the children
Siblings,
Stop,
}

pub fn traverse_types<'tg, 'path, A, V, E>(
tg: &'tg Typegraph,
root_type_idx: u32,
accumulator: A,
visit_fn: V,
) -> Result<A, E>
where
V: Fn(VisitorContext<'tg>, &mut A) -> Result<VisitNext, E>,
{
let path = Rc::new(RefCell::new(Vec::new()));
let output = traverse_types_with_path(tg, root_type_idx, &path, accumulator, &visit_fn)?;
Ok(output.accumulator)
}

struct TraverseOutput<A> {
accumulator: A,
stop: bool,
}

type SharedPath = Rc<RefCell<Vec<PathSegment>>>;

#[derive(Debug)]
pub struct CurrentNode<'tg> {
pub type_node: &'tg TypeNode,
pub type_idx: u32,
pub path: SharedPath,
}

fn traverse_types_with_path<'tg, A, V, E>(
tg: &'tg Typegraph,
type_idx: u32,
path: &Rc<RefCell<Vec<PathSegment>>>,
mut accumulator: A,
visit_fn: &V,
) -> Result<TraverseOutput<A>, E>
where
V: Fn(VisitorContext<'tg>, &mut A) -> Result<VisitNext, E>,
{
let type_node = &tg.types[type_idx as usize];
// TODO check for cycles

// visit current
{
let current_node = CurrentNode {
type_node,
type_idx,
path: path.clone(),
};
let cx = VisitorContext { tg, current_node };
match visit_fn(cx, &mut accumulator)? {
VisitNext::Stop => {
return Ok(TraverseOutput {
accumulator,
stop: true,
})
}
VisitNext::Siblings => {
return Ok(TraverseOutput {
accumulator,
stop: false,
})
}
VisitNext::Children => (),
}
}

let push = {
let path = path.clone();
move |edge: Edge| {
path.borrow_mut().push(PathSegment {
from: type_idx,
edge,
});
}
};

let pop = {
let path = path.clone();
move || {
path.borrow_mut().pop();
}
};

// visit children
match type_node {
TypeNode::Boolean { .. }
| TypeNode::Integer { .. }
| TypeNode::Float { .. }
| TypeNode::String { .. }
| TypeNode::File { .. }
| TypeNode::Any { .. } => Ok(TraverseOutput {
accumulator,
stop: false,
}),

TypeNode::Optional { data, .. } => {
let item_type_idx = data.item;
push(Edge::OptionalItem);
let output = traverse_types_with_path(tg, item_type_idx, path, accumulator, visit_fn)?;
pop();
Ok(output)
}

TypeNode::List { data, .. } => {
let item_type_idx = data.items;
push(Edge::ArrayItem);
let output = traverse_types_with_path(tg, item_type_idx, path, accumulator, visit_fn)?;
pop();
Ok(output)
}

TypeNode::Object { data, .. } => {
let mut accumulator = Some(accumulator);
for (key, prop_idx) in data.properties.iter() {
push(Edge::ObjectProp(key.clone()));
let output = traverse_types_with_path(
tg,
*prop_idx,
path,
accumulator.take().unwrap(),
visit_fn,
)?;
pop();
if output.stop {
return Ok(output);
}
accumulator = Some(output.accumulator);
}
Ok(TraverseOutput {
accumulator: accumulator.unwrap(),
stop: false,
})
}

TypeNode::Union { data, .. } => {
let mut accumulator = Some(accumulator);
for (v, &item_type_idx) in data.any_of.iter().enumerate() {
push(Edge::UnionVariant(v));
let output = traverse_types_with_path(
tg,
item_type_idx,
path,
accumulator.take().unwrap(),
visit_fn,
)?;
pop();
if output.stop {
return Ok(output);
}
accumulator = Some(output.accumulator);
}
Ok(TraverseOutput {
accumulator: accumulator.unwrap(),
stop: false,
})
}

TypeNode::Either { data, .. } => {
let mut accumulator = Some(accumulator);
for (v, &item_type_idx) in data.one_of.iter().enumerate() {
push(Edge::EitherVariant(v));
let output = traverse_types_with_path(
tg,
item_type_idx,
path,
accumulator.take().unwrap(),
visit_fn,
)?;
pop();
if output.stop {
return Ok(output);
}
accumulator = Some(output.accumulator);
}
Ok(TraverseOutput {
accumulator: accumulator.unwrap(),
stop: false,
})
}

TypeNode::Function { data, .. } => {
let input_type_idx = data.input;
push(Edge::FunctionInput);
let output = traverse_types_with_path(tg, input_type_idx, path, accumulator, visit_fn)?;
pop();
if output.stop {
return Ok(output);
}

let output_type_idx = data.output;
push(Edge::FunctionOutput);
let output =
traverse_types_with_path(tg, output_type_idx, path, output.accumulator, visit_fn)?;
pop();
Ok(output)
}
}
}
5 changes: 4 additions & 1 deletion src/meta-cli/src/deploy/actors/task_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,12 @@ impl<A: TaskAction + 'static> TaskManagerInit<A> {
) -> Option<Addr<WatcherActor<A>>> {
match &self.task_source {
TaskSource::Static(paths) => {
let config = self.action_generator.get_shared_config();
for path in paths {
let relative_path = pathdiff::diff_paths(path, &config.working_dir)
.unwrap_or_else(|| path.clone());
addr.do_send(AddTask {
task_ref: task_generator.generate(path.clone().into(), 0),
task_ref: task_generator.generate(relative_path.into(), 0),
reason: TaskReason::User,
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/meta-cli/src/deploy/actors/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl<A: TaskAction + 'static> Handler<File> for WatcherActor<A> {

RetryManager::clear_counter(&path);
self.task_manager.do_send(task_manager::message::AddTask {
task_ref: self.task_generator.generate(path.into(), 0),
task_ref: self.task_generator.generate(rel_path.into(), 0),
reason: TaskReason::DependencyChanged(dependency_path),
});
}
Expand Down
Loading
Loading