@@ -98,12 +98,12 @@ func (p *parser) jsonParse(root ast.Node) error {
98
98
}
99
99
100
100
// Ignore comments
101
- if name == comment {
101
+ if name == kwComment {
102
102
continue
103
103
}
104
104
105
105
// Create variable
106
- if name == variable && len (ident .Children ()) > 0 {
106
+ if name == kwVariable && len (ident .Children ()) > 0 {
107
107
if err := p .jsonVariablesParse (ident .Children ()[0 ]); err != nil {
108
108
return httpresponse .ErrBadRequest .Withf ("failed to parse %q: %s" , name , err )
109
109
}
@@ -187,15 +187,66 @@ func (p *parser) jsonPluginsParse(meta *meta.Meta, root ast.Node) error {
187
187
}
188
188
189
189
func jsonVariableParse (name string , root ast.Node ) (* Variable , error ) {
190
+ if root .Type () != ast .Dict {
191
+ return nil , httpresponse .ErrBadRequest .Withf ("expected object, got %s" , root .Type ())
192
+ }
193
+
194
+ // tree should contain "description" and "default" only
190
195
v := new (Variable )
191
196
v .Name = name
192
- fmt .Println ("jsonVariableParse" , name , root )
197
+ for _ , ident := range root .Children () {
198
+ if ident .Type () != ast .Ident {
199
+ return nil , httpresponse .ErrBadRequest .Withf ("expected identifier, got %s" , ident .Type ())
200
+ } else if len (ident .Children ()) == 0 {
201
+ continue
202
+ }
203
+
204
+ name , ok := ident .Value ().(string )
205
+ if ! ok || name == "" {
206
+ return nil , httpresponse .ErrBadRequest .Withf ("expected identifier, got %q" , ident .Value ())
207
+ }
208
+ value := ident .Children ()[0 ]
209
+
210
+ switch name {
211
+ case kwDescription :
212
+ if value .Type () != ast .String {
213
+ return nil , httpresponse .ErrBadRequest .Withf ("expected string, got %s" , value .Type ())
214
+ } else {
215
+ v .Description = value .Value ().(string )
216
+ }
217
+ case kwDefault :
218
+ v .Default = value .Value ()
219
+ default :
220
+ return nil , httpresponse .ErrBadRequest .Withf ("expected description or default, got %q" , ident .Value ())
221
+ }
222
+ }
193
223
return v , nil
194
224
}
195
225
196
226
func jsonResourceParse (meta * meta.Meta , root ast.Node ) (* Resource , error ) {
227
+ if root .Type () != ast .Dict {
228
+ return nil , httpresponse .ErrBadRequest .Withf ("expected object, got %s" , root .Type ())
229
+ }
230
+
231
+ // tree should contain key/value pairs and objects which may be nested
197
232
r := new (Resource )
198
233
r .Meta = meta
199
- fmt .Println ("jsonResourceParse" , r , root )
234
+
235
+ for _ , ident := range root .Children () {
236
+ if ident .Type () != ast .Ident {
237
+ return nil , httpresponse .ErrBadRequest .Withf ("expected identifier, got %s" , ident .Type ())
238
+ } else if len (ident .Children ()) == 0 {
239
+ continue
240
+ }
241
+
242
+ name , ok := ident .Value ().(string )
243
+ if ! ok || name == "" {
244
+ return nil , httpresponse .ErrBadRequest .Withf ("expected identifier, got %q" , ident .Value ())
245
+ }
246
+ value := ident .Children ()[0 ]
247
+
248
+ fmt .Println ("name:" , name , "value:" , value )
249
+
250
+ }
200
251
return r , nil
201
252
}
0 commit comments