@@ -98,12 +98,12 @@ func (p *parser) jsonParse(root ast.Node) error {
9898 }
9999
100100 // Ignore comments
101- if name == comment {
101+ if name == kwComment {
102102 continue
103103 }
104104
105105 // Create variable
106- if name == variable && len (ident .Children ()) > 0 {
106+ if name == kwVariable && len (ident .Children ()) > 0 {
107107 if err := p .jsonVariablesParse (ident .Children ()[0 ]); err != nil {
108108 return httpresponse .ErrBadRequest .Withf ("failed to parse %q: %s" , name , err )
109109 }
@@ -187,15 +187,66 @@ func (p *parser) jsonPluginsParse(meta *meta.Meta, root ast.Node) error {
187187}
188188
189189func 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
190195 v := new (Variable )
191196 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+ }
193223 return v , nil
194224}
195225
196226func 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
197232 r := new (Resource )
198233 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+ }
200251 return r , nil
201252}
0 commit comments