Skip to content

Commit

Permalink
internal/config: fix CR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
seilagamo committed Feb 11, 2025
1 parent 13ffca9 commit 0a760ae
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 44 deletions.
5 changes: 2 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ func Parse(path string) (Config, error) {
if err != nil {
return Config{}, fmt.Errorf("build dag: %w", err)
}
graph.Resolve()
cfg := graph.cfg
cfg := graph.Resolve()

if err = cfg.validate(); err != nil {
return Config{}, fmt.Errorf("validate config: %w", err)
}

return *cfg, nil
return cfg, nil
}

// Decode decodes from a slice of bytes to a [Config] structure.
Expand Down
40 changes: 18 additions & 22 deletions internal/config/configgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"fmt"
"io"
"log/slog"
"os"

hdag "github.com/heimdalr/dag"

"github.com/adevinta/lava/internal/config/dag"
"github.com/adevinta/lava/internal/urlutil"
Expand All @@ -19,7 +16,6 @@ import (
type ConfigGraph struct {
dag *dag.DAG
configs map[string]Config
cfg *Config
}

// NewConfigGraph creates a new [ConfigGraph] that represents the whole configuration.
Expand Down Expand Up @@ -88,25 +84,25 @@ func (cg *ConfigGraph) Config(url string) (Config, error) {
return Config{}, fmt.Errorf("could not find config for %s", url)
}

// Visit executes the merge when a vertex is visited.
func (cg *ConfigGraph) Visit(v hdag.Vertexer) {
_, cfg := v.Vertex()
config, err := cg.Config(cfg.(string))
if err != nil {
os.Exit(1)
}
if cg.cfg == nil {
cg.cfg = &config
} else {
mergedConfig, err := merge(config, *cg.cfg)
// Resolve walks the dag and merge the configuration.
func (cg *ConfigGraph) Resolve() Config {
var cfg *Config
cg.dag.DFSWalk(func(vertexID string, vertex interface{}) {
vexCfg, err := cg.Config(vertex.(string))
if err != nil {
os.Exit(1)
panic(err)
}
cg.cfg = &mergedConfig
}
}

// Resolve walks the dag and merge the configuration.
func (cg *ConfigGraph) Resolve() {
cg.dag.Dag.DFSWalk(cg)
if cfg == nil {
cfg = &vexCfg
return
}

vexCfg, err = merge(vexCfg, *cfg)
if err != nil {
panic(err)
}
cfg = &vexCfg
})
return *cfg
}
23 changes: 11 additions & 12 deletions internal/config/configgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestNewConfigGraph(t *testing.T) {
"testdata/include/no_includes.yaml": {
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"commonchecktypes.json",
"checktypes_no_includes.json",
},
Targets: []Target{
{
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestNewConfigGraph(t *testing.T) {
"testdata/include/no_includes.yaml": {
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"commonchecktypes.json",
"checktypes_no_includes.json",
},
Targets: []Target{
{
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestNewConfigGraph(t *testing.T) {
"testdata/include/no_includes.yaml": {
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"commonchecktypes.json",
"checktypes_no_includes.json",
},
Targets: []Target{
{
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestNewConfigGraph(t *testing.T) {
"testdata/include/no_includes.yaml": {
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"commonchecktypes.json",
"checktypes_no_includes.json",
},
Targets: []Target{
{
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestConfigGraph_Resolve(t *testing.T) {
want: Config{
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"commonchecktypes.json",
"checktypes_no_includes.json",
},
Targets: []Target{
{
Expand All @@ -234,7 +234,7 @@ func TestConfigGraph_Resolve(t *testing.T) {
Includes: []string{"testdata/include/no_includes.yaml"},
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"commonchecktypes.json",
"checktypes_no_includes.json",
"checktypes.json",
},
Targets: []Target{
Expand All @@ -260,7 +260,7 @@ func TestConfigGraph_Resolve(t *testing.T) {
},
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"commonchecktypes.json",
"checktypes_no_includes.json",
"checktypes.json",
},
Targets: []Target{
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestConfigGraph_Resolve(t *testing.T) {
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"checktypes.json",
"commonchecktypes.json",
"checktypes_no_includes.json",
"checktypes.json",
"checktypes.json",
},
Expand Down Expand Up @@ -324,12 +324,11 @@ func TestConfigGraph_Resolve(t *testing.T) {
if err != nil {
t.Errorf("build dag: %v", err)
}
graph.Resolve()
got := graph.cfg
got := graph.Resolve()
if (err != nil) != tt.wantErr {
t.Errorf("unexpected error: want: %v, got: %v", tt.wantErr, err)
t.Errorf("unexpected error: %v", err)
}
if diff := cmp.Diff(tt.want, *got); diff != "" {
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("configs mismatch (-want +got):\n%v", diff)
}
})
Expand Down
18 changes: 14 additions & 4 deletions internal/config/dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ var (

// DAG represents a Direct Acyclic Graph object.
type DAG struct {
Dag *hdag.DAG
dag *hdag.DAG
vertices map[string]string
}

// New returns a Directed Acyclic Graph object.
func New() *DAG {
return &DAG{
Dag: hdag.NewDAG(),
dag: hdag.NewDAG(),
vertices: make(map[string]string),
}
}

// AddVertex adds the vertex x to the DAG. AddVertex returns an error if s is
// nil or s is already part of the graph.
func (d *DAG) AddVertex(s string) (string, error) {
id, err := d.Dag.AddVertex(s)
id, err := d.dag.AddVertex(s)
if err != nil {
if errors.As(err, &hdag.VertexDuplicateError{}) {
return id, fmt.Errorf("%w: %s", ErrDuplicatedVertex, s)
Expand All @@ -66,7 +66,7 @@ func (d *DAG) AddEdge(from, to string) error {
if toID == "" {
return fmt.Errorf("%w: %s", ErrUnknownVertex, to)
}
err := d.Dag.AddEdge(fromID, toID)
err := d.dag.AddEdge(fromID, toID)
if err != nil {
if errors.As(err, &hdag.EdgeDuplicateError{}) {
return fmt.Errorf("%w: from %s to %s", ErrDuplicatedEdge, from, to)
Expand Down Expand Up @@ -94,3 +94,13 @@ func (d *DAG) getVertexID(s string) string {
}
return ""
}

type WalkFunc func(vertexID string, vertex interface{})

func (fn WalkFunc) Visit(v hdag.Vertexer) {
fn(v.Vertex())
}

func (d *DAG) DFSWalk(fn WalkFunc) {
d.dag.DFSWalk(fn)
}
2 changes: 1 addition & 1 deletion internal/config/testdata/include/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ targets:
- identifier: example.com
type: DomainName
report:
severity: critical
severity: critical
2 changes: 1 addition & 1 deletion internal/config/testdata/include/common_a.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ targets:
- identifier: example.com
type: DomainName
report:
severity: medium
severity: medium
2 changes: 1 addition & 1 deletion internal/config/testdata/include/no_includes.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
lava: v1.0.0
checktypes:
- commonchecktypes.json
- checktypes_no_includes.json
targets:
- identifier: example.com
type: DomainName

0 comments on commit 0a760ae

Please sign in to comment.