@@ -14,11 +14,10 @@ use wdl::{
14
14
types:: Type ,
15
15
} ,
16
16
ast:: {
17
- AstToken , Diagnostic , Document as AstDocument , SyntaxKind , Visitor ,
18
17
v1:: {
19
18
self ,
20
- Expr :: { self , Literal } ,
21
- } ,
19
+ Expr :: { self , Literal } , InputSection ,
20
+ } , AstToken , Diagnostic , Document as AstDocument , SupportedVersion , SyntaxKind , VisitReason , Visitor
22
21
} ,
23
22
cli:: analyze,
24
23
doc,
@@ -48,8 +47,46 @@ pub struct InputsArgs {
48
47
// pub override_expressions: bool,
49
48
}
50
49
50
+ type InputDefaultMap = IndexMap < String , bool > ; // input_name -> has_default
51
+
51
52
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
+ }
53
90
}
54
91
55
92
pub async fn generate_inputs ( args : InputsArgs ) -> Result < ( ) > {
@@ -77,7 +114,7 @@ pub async fn generate_inputs(args: InputsArgs) -> Result<()> {
77
114
78
115
println ! ( "document: {:?}" , document) ;
79
116
80
- let mut template = serde_json:: Map :: new ( ) ;
117
+ let mut template = serde_json:: Map :: new ( ) ;
81
118
82
119
// Collect inputs and their parent information
83
120
let inputs_with_parents = collect_inputs_with_parents ( & args, document) ?;
@@ -128,28 +165,13 @@ fn type_to_json(ty: &Type) -> Value {
128
165
}
129
166
130
167
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
142
174
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
- }
153
175
154
176
false
155
177
}
0 commit comments