Skip to content

Commit

Permalink
cli: Add sqlite.Config to general configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspcr committed Jan 25, 2025
1 parent 65dd32d commit 8b53aaf
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 55 deletions.
60 changes: 5 additions & 55 deletions cmd/decli/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package config

import (
"encoding/json"

"github.com/nicholaspcr/GoDE/cmd/decli/internal/state/sqlite"
"github.com/nicholaspcr/GoDE/internal/log"
"gopkg.in/yaml.v3"
)

type (
// Config is a set of values that are necessary to execute an Differential
// Evolutionary algorithm.
Config struct {
Local LocalConfig `json:"local" yaml:"local"`
Log LogConfig `json:"log" yaml:"log"`
Server ServerConfig `json:"server" yaml:"server"`
Local LocalConfig `json:"local" yaml:"local"`
Log LogConfig `json:"log" yaml:"log"`
Server ServerConfig `json:"server" yaml:"server"`
Sqlite sqlite.Config `json:"sqlite" yaml:"sqlite"`
}

LocalConfig struct {
Expand Down Expand Up @@ -55,52 +54,3 @@ type (
P float64 `json:"p" yaml:"p"`
}
)

// Default configuration of the decli binary.
func Default() *Config {
return &Config{
Local: LocalConfig{
PopulationSize: 50,
Generations: 100,
Executions: 1,
Dimensions: Dimensions{
Size: 7,
Floors: []float64{0, 0, 0, 0, 0, 0, 0},
Ceils: []float64{1, 1, 1, 1, 1, 1, 1},
},
Constants: Constants{
M: int(3),
CR: float64(0.9),
F: float64(0.5),
P: float64(0.2),
},
Problem: "dtlz1",
Variant: "rand1",
},
Log: LogConfig{
Config: log.DefaultConfig(),
},
Server: ServerConfig{
GRPCAddr: "localhost:3030",
HTTPAddr: "http://localhost:8081",
},
}
}

// StringifyJSON returns a string with the JSON object of the configuration.
func (cfg *Config) StringifyJSON() (string, error) {
b, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}

// StringifyYAML returns a string block with the yaml configuration contents.
func (cfg *Config) StringifyYAML() (string, error) {
b, err := yaml.Marshal(cfg)
if err != nil {
return "", err
}
return string(b), nil
}
41 changes: 41 additions & 0 deletions cmd/decli/internal/config/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package config

import (
"github.com/nicholaspcr/GoDE/cmd/decli/internal/state/sqlite"
"github.com/nicholaspcr/GoDE/internal/log"
)

// Default configuration of the decli binary.
func Default() *Config {
return &Config{
Local: LocalConfig{
PopulationSize: 50,
Generations: 100,
Executions: 1,
Dimensions: Dimensions{
Size: 7,
Floors: []float64{0, 0, 0, 0, 0, 0, 0},
Ceils: []float64{1, 1, 1, 1, 1, 1, 1},
},
Constants: Constants{
M: int(3),
CR: float64(0.9),
F: float64(0.5),
P: float64(0.2),
},
Problem: "dtlz1",
Variant: "rand1",
},
Log: LogConfig{
Config: log.DefaultConfig(),
},
Server: ServerConfig{
GRPCAddr: "localhost:3030",
HTTPAddr: "http://localhost:8081",
},
Sqlite: sqlite.Config{
Provider: "file",
Filepath: ".dev/cli/state.yaml",
},
}
}
25 changes: 25 additions & 0 deletions cmd/decli/internal/config/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"encoding/json"

"gopkg.in/yaml.v3"
)

// StringifyJSON returns a string with the JSON object of the configuration.
func (cfg *Config) StringifyJSON() (string, error) {
b, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}

// StringifyYAML returns a string block with the yaml configuration contents.
func (cfg *Config) StringifyYAML() (string, error) {
b, err := yaml.Marshal(cfg)
if err != nil {
return "", err
}
return string(b), nil
}

0 comments on commit 8b53aaf

Please sign in to comment.