Skip to content

Commit 66e9b59

Browse files
committed
feat: InputVisitor to track default values in input sections
1 parent 4d3659e commit 66e9b59

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

src/commands/inputs.rs

+48-26
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ use wdl::{
1414
types::Type,
1515
},
1616
ast::{
17-
AstToken, Diagnostic, Document as AstDocument, SyntaxKind, Visitor,
1817
v1::{
1918
self,
20-
Expr::{self, Literal},
21-
},
19+
Expr::{self, Literal}, InputSection,
20+
}, AstToken, Diagnostic, Document as AstDocument, SupportedVersion, SyntaxKind, VisitReason, Visitor
2221
},
2322
cli::analyze,
2423
doc,
@@ -48,8 +47,46 @@ pub struct InputsArgs {
4847
// pub override_expressions: bool,
4948
}
5049

50+
type InputDefaultMap = IndexMap<String, bool>; // input_name -> has_default
51+
5152
struct InputVisitor {
52-
inputs: IndexMap<String, Option<Expr>>, // input_name -> default expression (if any)
53+
inputs: InputDefaultMap,
54+
}
55+
56+
impl Visitor for InputVisitor {
57+
type State = ();
58+
59+
fn document(
60+
&mut self,
61+
state: &mut Self::State,
62+
reason: VisitReason,
63+
doc: &AstDocument,
64+
version: SupportedVersion,
65+
) {
66+
if reason == VisitReason::Enter {
67+
self.inputs.clear(); // Reset the list when starting a new document
68+
println!("visiting document {:?}", version);
69+
} else if reason == VisitReason::Exit {
70+
println!("Exiting document {:?}", version);
71+
}
72+
}
73+
74+
fn input_section(
75+
&mut self,
76+
_state: &mut Self::State,
77+
reason: VisitReason,
78+
section: &InputSection,
79+
) {
80+
if reason == VisitReason::Enter {
81+
// Iterate over all declarations in the input section
82+
for decl in section.declarations() {
83+
let name = decl.name().as_str().to_string();
84+
// Check if the declaration has an expression (i.e., a default value)
85+
let has_default: bool = decl.expr().is_some();
86+
self.inputs.insert(name, has_default);
87+
}
88+
}
89+
}
5390
}
5491

5592
pub async fn generate_inputs(args: InputsArgs) -> Result<()> {
@@ -77,7 +114,7 @@ pub async fn generate_inputs(args: InputsArgs) -> Result<()> {
77114

78115
println!("document: {:?}", document);
79116

80-
let mut template = serde_json::Map::new();
117+
let mut template = serde_json::Map::new();
81118

82119
// Collect inputs and their parent information
83120
let inputs_with_parents = collect_inputs_with_parents(&args, document)?;
@@ -128,28 +165,13 @@ fn type_to_json(ty: &Type) -> Value {
128165
}
129166

130167
fn has_default(document: &Document, parent_name: &str, input_name: &str) -> bool {
131-
// Check workflow inputs
132-
if let Some(workflow) = document.workflow() {
133-
let input_section: &IndexMap<String, Input> = workflow.inputs();
134-
for input in input_section {
135-
println!("workflow input: {:?}", input);
136-
if input.0.as_str() == input_name {
137-
println!("workflow input: {:?}", input.1.ty());
138-
return !input.1.ty().is_none();
139-
}
140-
}
141-
}
168+
// default value rules
169+
// 1. Int? X (optional)
170+
// 2. Int X = 5 (has a default value)
171+
172+
// parse the WDL document into a SyntaxTree
173+
// use a Visitor to collect input decl and their default expressions
142174

143-
// Check task inputs
144-
for task in document.tasks() {
145-
let input_section: &IndexMap<String, Input> = task.inputs();
146-
for input in input_section {
147-
println!("task input: {:?}", input);
148-
if input.0.as_str() == input_name {
149-
return !input.1.ty().is_none();
150-
}
151-
}
152-
}
153175

154176
false
155177
}

0 commit comments

Comments
 (0)