summaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorflu0r1ne <flu0r1ne@flu0r1ne.net>2021-09-05 20:37:19 -0500
committerflu0r1ne <flu0r1ne@flu0r1ne.net>2021-09-05 20:37:19 -0500
commitf5b60238e05b124eb40f805eb4a0bbfc0b043da5 (patch)
treef461bff108f5ddafc4078aa7394d7bf2a6309cc9 /config.go
parent8f22bd4f5b4eb6996c524bcb6948d36cef0ac822 (diff)
parentfd66fb134967067ed7e1c3182325f646b73c730b (diff)
downloaddeb-planr-f5b60238e05b124eb40f805eb4a0bbfc0b043da5.tar.xz
deb-planr-f5b60238e05b124eb40f805eb4a0bbfc0b043da5.zip
Merge branch 'upstream' into ppa
Merge v0.1.0
Diffstat (limited to 'config.go')
-rw-r--r--config.go202
1 files changed, 27 insertions, 175 deletions
diff --git a/config.go b/config.go
index bc0fa6a..d7cd3e4 100644
--- a/config.go
+++ b/config.go
@@ -1,199 +1,51 @@
package planr
import (
- "log"
"github.com/BurntSushi/toml"
+ "log"
+ "path"
+ "strings"
)
-
-/*
- TODO: Every property defined within the defaults currently
- has to implement the "inherit" method to conditionally inherit a
- property in relation to a parent. (Ostensibly so that test cases
- can override default configuration.) This is pedantic because
- most properties will end up writing boilerplate amounting to:
-
- parent.inherit()
- ...
- if config.property == nil {
- config.property = config.parent.property
- }
-
- This library provides copying behavior between structs
- with common properties using reflection. It seems like
- a slight abuse of reflection... But, it could be
- retro-fitted to implement this behavior if a "onlyCopyZeroFields"
- option was provided.
-
- > "github.com/jinzhu/copier"
-*/
-
-// Inheritable configuration can inherit properties defined in a
-// defaults file. This happens on a per-directory basis so multiple
-// tests can share common configuration.
-//
-// The parent will always be of the same type as the child and an
-// assertion is required to define the proper behavior.
-type InheritableConfig interface {
- Inherit(parent interface{})
-}
-
-// A parser function takes a blob of TOML and decodes it into
-// configuration relevant to an adapter
-type TomlParser func (toml.Primitive) (InheritableConfig, error)
-
-// The name under which an adapter registers corresponds
-// to a table under the super-table adapters. All corresponding
-// TOML will be passed to the ParseConfig method or ParseDefaultConfig
-// for parsing. The ParseConfig file parses options in test case files.
-// The ParseDefaultConfig is parsed by `defaults.toml` files and can
-// be used to establish default configuration that will be inherited
-// by all units in a common directory (collection)
-type AdapterConfig struct {
- Name string
- ParseConfig TomlParser
- ParseDefaultConfig TomlParser
-}
-
-// Program-wide configuration which is recognized
-// in defaults.toml
-type Defaults struct {
- Points *float32
- Adapter *string
-
- /*
- The TOML library only parses exported fields.
- The Adapters field is an intermediate mapping
- individual adapters to their locally defined
- configuration. After they individually process
- the configuration, it is mapped to the adapters_
- field.
-
- See: decodeAdapters()
- */
- Adapters *map[string] toml.Primitive
- adapters_ map[string] InheritableConfig
-
- /*
- The configs_ field is necessary to property
- implement the Inherit method using a common
- interface.
- */
- configs_ *[]AdapterConfig
-}
-
-// Program-wide testcase config
-type TestCaseConfig struct {
- Defaults
- Title *string
- Description *string
-}
-
-func (c TestCaseConfig) ensureSatisfied(name string) {
- if (c.Adapter == nil) {
- log.Fatalf("Adapter must be provided for testcase %s", name)
- }
+type Config struct {
+ Version string
}
-// The default configuration must be able in inherit from
-// other defaults further up the tree
-//
-// This provides multiple levels of configurability
-func (child *Defaults) Inherit(p interface{}) {
- parent := p.(Defaults)
-
- // Inherit properties which haven't been configured
- if child.Points == nil {
- child.Points = parent.Points;
- }
-
- if child.Adapter == nil {
- child.Adapter = parent.Adapter;
- }
+const PLANR_CONFIG_FILE = "config.toml"
- // Call the inherit method as defined by the adapters
- // If an adapter is undefined, inherit the parent configuration
- //
- // _configs represents all adapters (registered to a runner)
- for _, adapter := range *child.configs_ {
- parent_adapter, parent_exists := parent.adapters_[adapter.Name]
- child_adapter, child_exists := child.adapters_[adapter.Name]
+// TODO: REMOVE
+const STRICTLY_REQUIRE_CONFIG = false
- if parent_exists {
- if child_exists {
- child_adapter.Inherit(parent_adapter)
- } else {
- child.adapters_[adapter.Name] = parent_adapter
- }
- }
- }
-}
+func DecodeConfig(configDir string) *Config {
+ cfg := new(Config)
-// Parses the intermediate adapters Adapters containing TOML primitives
-// according to methods registered with the runner
-// Once parsed, they are stored alongside the registered name to determine
-// which adapter will receive the configuration
-func (defaults *Defaults) decodeAdapters(
- adapters []AdapterConfig,
- asDefault bool,
-) error {
- defaults.configs_ = &adapters
- defaults.adapters_ = make(map[string]InheritableConfig)
+ configFile := path.Join(configDir, PLANR_CONFIG_FILE)
- if defaults.Adapters != nil {
- for _, config := range adapters {
- primitive, exists := (*defaults.Adapters)[config.Name]
-
- if exists {
- var parsed InheritableConfig
- var err error
- if asDefault {
- parsed, err = config.ParseDefaultConfig(primitive)
- } else {
- parsed, err = config.ParseConfig(primitive)
- }
+ if _, err := toml.DecodeFile(configFile, cfg); err != nil {
+ cfg = nil
- if err != nil {
- return err
- }
-
- defaults.adapters_[config.Name] = parsed
- }
+ // TODO: handle missing config
+ if STRICTLY_REQUIRE_CONFIG {
+ log.Fatalf("Could not decode global configuration %s: %v", configFile, err)
}
}
- return nil
+ return cfg
}
-// Decode defaults.toml
-func DecodeDefaults(path string, adapterCfg []AdapterConfig) (Defaults, error) {
- defaults := Defaults { }
-
-
-
- if _, err := toml.DecodeFile(path, &defaults); err != nil {
- return defaults, err
+func (cfg Config) IncompatibleWithVersion() bool {
+ if strings.Count(cfg.Version, ".") != 2 {
+ log.Fatalf("Version %s is not semantic", cfg.Version)
}
- if err := defaults.decodeAdapters(adapterCfg, true); err != nil {
- return defaults, err
- }
-
-
- return defaults, nil
-}
-
-// Decode an individual unit
-func DecodeConfig(path string, adapterCfg []AdapterConfig) (TestCaseConfig, error) {
- config := TestCaseConfig { }
-
- if _, err := toml.DecodeFile(path, &config); err != nil {
- return config, nil
- }
+ cfgbits := strings.SplitN(cfg.Version, ".", 2)
+ bits := strings.SplitN(VERSION, ".", 2)
- if err := config.decodeAdapters(adapterCfg, false); err != nil {
- return config, err
+ // major version change
+ if cfgbits[0] != bits[0] {
+ return true
}
- return config, nil
+ // Config newer, possible feature additions
+ return cfgbits[1] > bits[1]
}