Skip to content

Commit 44390bf

Browse files
committed
Updates
1 parent ddccaac commit 44390bf

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

pkg/provider/parser/block.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package parser
33
import "github.com/mutablelogic/go-server/pkg/provider/meta"
44

55
const (
6-
variable = "variable"
7-
comment = "//"
6+
kwVariable = "variable"
7+
kwDescription = "description"
8+
kwDefault = "default"
9+
kwComment = "//"
810
)
911

1012
type Resource struct {

pkg/provider/parser/parser.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

189189
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
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

196226
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
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
}

pkg/provider/parser/parser_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Test_Parser_002(t *testing.T) {
2929
t.FailNow()
3030
}
3131

32-
err = parser.Parse("testdata/log.json")
32+
err = parser.Parse("jsonparser/testdata/log.json")
3333
assert.NoError(err)
3434
}
3535

@@ -42,6 +42,6 @@ func Test_Parser_003(t *testing.T) {
4242
t.FailNow()
4343
}
4444

45-
err = parser.Parse("testdata/httpserver.json")
45+
err = parser.Parse("jsonparser/testdata/httpserver.json")
4646
assert.NoError(err)
4747
}

0 commit comments

Comments
 (0)