Skip to content

Commit 506f330

Browse files
committed
Some updates
1 parent 49d2d7e commit 506f330

File tree

6 files changed

+70
-57
lines changed

6 files changed

+70
-57
lines changed

pkg/provider/ast/array.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (r *arrayNode) Value(ctx *Context) (any, error) {
7171
var err error
7272
result := make([]any, len(r.C))
7373
for i, child := range r.C {
74-
ctx.push(fmt.Sprintf("[%d]", i))
74+
ctx.push(fmt.Sprintf("[%d]", i), false)
7575
value, err_ := child.Value(ctx)
7676
ctx.pop()
7777
if err != nil {

pkg/provider/ast/context.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@ func (ctx *Context) SetLabel(label types.Label) {
4040
}
4141

4242
// Return the current path
43-
func (ctx *Context) Path() string {
44-
return "/" + strings.Join(ctx.path, "/")
43+
func (ctx *Context) Path() types.Label {
44+
return types.Label(strings.Join(ctx.path, ""))
4545
}
4646

4747
/////////////////////////////////////////////////////////////////////
4848
// PRIVATE METHODS
4949

50-
func (ctx *Context) push(path string) {
51-
ctx.path = append(ctx.path, path)
50+
func (ctx *Context) push(path string, sep bool) {
51+
if sep {
52+
ctx.path = append(ctx.path, types.LabelSeparator+path)
53+
} else {
54+
ctx.path = append(ctx.path, path)
55+
}
5256
}
5357

5458
func (ctx *Context) pop() {

pkg/provider/ast/map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (r *mapNode) Value(ctx *Context) (any, error) {
7171
for _, child := range r.C {
7272
key := child.Key()
7373

74-
ctx.push(key)
74+
ctx.push(key, true)
7575
value, err := child.Children()[0].Value(ctx)
7676
ctx.pop()
7777
if err != nil {

pkg/provider/dep/dep.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ import (
1111

1212
type Node any
1313

14-
type node struct {
15-
Node
16-
edges []Node
17-
}
18-
1914
type dep struct {
2015
// Node depends on several other nodes
2116
edges map[Node][]Node
@@ -24,31 +19,12 @@ type dep struct {
2419
///////////////////////////////////////////////////////////////////////////////
2520
// LIFECYCLE
2621

27-
func NewDep() *dep {
22+
func NewGraph() *dep {
2823
dep := new(dep)
2924
dep.edges = make(map[Node][]Node)
3025
return dep
3126
}
3227

33-
///////////////////////////////////////////////////////////////////////////////
34-
// STRINGIFY
35-
36-
func (d *dep) String() string {
37-
str := "dep{\n"
38-
for a, b := range d.edges {
39-
str += fmt.Sprint(" ", a) + " -> ["
40-
for i, c := range b {
41-
if i > 0 {
42-
str += ", "
43-
}
44-
str += fmt.Sprint(c)
45-
}
46-
str += "]\n"
47-
}
48-
str += "}"
49-
return str
50-
}
51-
5228
///////////////////////////////////////////////////////////////////////////////
5329
// PUBLIC METHODS
5430

pkg/provider/dep/dep_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func NewNode(name string) *node {
2020
}
2121

2222
func Test_dep_00(t *testing.T) {
23-
d := dep.NewDep()
23+
d := dep.NewGraph()
2424
if d == nil {
2525
t.Fatalf("Expected a new dep, got nil")
2626
}
@@ -43,7 +43,7 @@ func Test_dep_00(t *testing.T) {
4343

4444
func Test_dep_01(t *testing.T) {
4545
assert := assert.New(t)
46-
d := dep.NewDep()
46+
d := dep.NewGraph()
4747
if !assert.NotNil(d) {
4848
t.SkipNow()
4949
}
@@ -57,9 +57,8 @@ func Test_dep_01(t *testing.T) {
5757
d.AddNode(a, b, c)
5858
d.AddNode(root, a, b, c)
5959
d.AddNode(b, root)
60+
d.AddNode(c, root)
6061

6162
_, err := d.Resolve(root)
62-
if !assert.NoError(err) {
63-
t.SkipNow()
64-
}
63+
assert.Error(err)
6564
}

pkg/provider/parser.go

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
// Packages
99
"github.com/mutablelogic/go-server"
1010
"github.com/mutablelogic/go-server/pkg/provider/ast"
11+
"github.com/mutablelogic/go-server/pkg/provider/dep"
1112
"github.com/mutablelogic/go-server/pkg/provider/json"
1213
"github.com/mutablelogic/go-server/pkg/types"
1314

@@ -64,13 +65,14 @@ func (p *Parser) ParseJSON(r io.Reader) error {
6465
continue
6566
}
6667

68+
// Handle var block
6769
label := config.Key()
6870
if label == labelVar {
69-
fmt.Println("TODO", label)
71+
fmt.Println("TODO", label, config)
7072
continue
7173
}
7274

73-
// Parse a task block
75+
// Handle task block
7476
if label, err := types.ParseLabel(label); err != nil {
7577
result = errors.Join(result, err)
7678
} else if _, exists := p.configs[label]; exists {
@@ -86,39 +88,71 @@ func (p *Parser) ParseJSON(r io.Reader) error {
8688
return result
8789
}
8890

91+
type node struct {
92+
label types.Label
93+
path types.Label
94+
}
95+
8996
// Bind values
9097
func (p *Parser) Bind() error {
91-
var result error
98+
// Create a dependency graph and a root node
99+
dep := dep.NewGraph()
100+
root := &node{}
92101

93-
// Evaluate each set instruction, creating a dependency graph
102+
// Evaluate each value, and create dependencies
94103
ctx := ast.NewContext(func(ctx *ast.Context, value any) (any, error) {
95-
switch value := value.(type) {
96-
case string:
97-
var expanded bool
98-
value = Expand(value, func(key string) string {
99-
expanded = true
100-
return "(ref:" + key + ")"
101-
})
102-
if expanded {
103-
fmt.Printf("EVAL %s.%s => %q (%T)\n", ctx.Label(), ctx.Path(), value, value)
104-
} else {
105-
fmt.Printf("SET %s.%s => %q (%T)\n", ctx.Label(), ctx.Path(), value, value)
106-
}
107-
default:
108-
fmt.Printf("SET %s.%s => %q (%T)\n", ctx.Label(), ctx.Path(), value, value)
104+
if node, others, err := eval(ctx, value); err != nil {
105+
return nil, err
106+
} else {
107+
dep.AddNode(root, node)
108+
dep.AddNode(node, others...)
109109
}
110110
return nil, nil
111111
})
112112

113-
// Process each configuration in order to discover the dependencies
113+
// Traverse the nodes to create a dependency graph
114+
var result error
114115
for label, config := range p.configs {
115116
ctx.SetLabel(label)
116-
_, err := config.Value(ctx)
117-
if err != nil {
117+
if _, err := config.Value(ctx); err != nil {
118118
result = errors.Join(result, err)
119119
}
120120
}
121+
if result != nil {
122+
return result
123+
}
124+
125+
// Resolve the dependency graph
126+
resolved, err := dep.Resolve(root)
127+
if err != nil {
128+
return err
129+
}
130+
131+
// Print out the resolved nodes
132+
for _, n := range resolved {
133+
fmt.Println(n.(*node).label, n.(*node).path, "=>")
134+
}
121135

122136
// Return any errors
123137
return result
124138
}
139+
140+
////////////////////////////////////////////////////////////////////////////////
141+
// PRIVATE METHODS
142+
143+
// evaluate value
144+
func eval(ctx *ast.Context, value any) (dep.Node, []dep.Node, error) {
145+
label := ctx.Label()
146+
path := ctx.Path()
147+
switch value := value.(type) {
148+
case string:
149+
result := []dep.Node{}
150+
Expand(value, func(label string) string {
151+
fmt.Println("label", label)
152+
return ""
153+
})
154+
return &node{label, path}, result, nil
155+
default:
156+
return &node{label, path}, nil, nil
157+
}
158+
}

0 commit comments

Comments
 (0)