From ff3c1b99f691640a386fd452fa3b3e77769b9138 Mon Sep 17 00:00:00 2001 From: Flu0r1ne Date: Fri, 3 Sep 2021 19:53:06 -0500 Subject: Add project-wide configuration file --- config.go | 165 +++++------------------------------------------------- rubric_config.go | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ version.go | 3 + 3 files changed, 184 insertions(+), 150 deletions(-) create mode 100644 rubric_config.go create mode 100644 version.go diff --git a/config.go b/config.go index 887bbb0..d2d32e9 100644 --- a/config.go +++ b/config.go @@ -2,165 +2,30 @@ package planr import ( "github.com/BurntSushi/toml" + "log" + "path" ) -/* - 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{}) -} - - -// 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 -} - - -// 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; - } - - // 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] - - if parent_exists { - if child_exists { - child_adapter.Inherit(parent_adapter) - } else { - child.adapters_[adapter.Name] = parent_adapter - } - } - } +type planrConfig struct { + Version string + Project_title string } -// 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) - - 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 != nil { - return err - } - - defaults.adapters_[config.Name] = parsed - } - } - } - - return nil -} +const PLANR_CONFIG = "config.toml" -// Decode defaults.toml -func DecodeDefaults(path string, adapterCfg []AdapterConfig) (Defaults, error) { - defaults := Defaults { } +func decodeConfig(configDir string) planrConfig { + cfg := planrConfig { } - if _, err := toml.DecodeFile(path, &defaults); err != nil { - return defaults, err - } + configFile := path.Join(configDir, PLANR_CONFIG) - if err := defaults.decodeAdapters(adapterCfg, true); err != nil { - return defaults, err + if _, err := toml.DecodeFile(configFile, &cfg); err != nil { + // TODO: handle missing config + log.Fatalf("Could not decode global configuration %s: %v", configFile, err) } - - return defaults, nil + return cfg } -// 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 - } - - if err := config.decodeAdapters(adapterCfg, false); err != nil { - return config, err - } - - return config, nil +func (cfg planrConfig) isIncompatibleWithVersion() bool { + return cfg.Version > VERSION } diff --git a/rubric_config.go b/rubric_config.go new file mode 100644 index 0000000..887bbb0 --- /dev/null +++ b/rubric_config.go @@ -0,0 +1,166 @@ +package planr + +import ( + "github.com/BurntSushi/toml" +) +/* + 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{}) +} + + +// 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 +} + + +// 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; + } + + // 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] + + if parent_exists { + if child_exists { + child_adapter.Inherit(parent_adapter) + } else { + child.adapters_[adapter.Name] = parent_adapter + } + } + } +} + +// 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) + + 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 != nil { + return err + } + + defaults.adapters_[config.Name] = parsed + } + } + } + + return nil +} + +// Decode defaults.toml +func DecodeDefaults(path string, adapterCfg []AdapterConfig) (Defaults, error) { + defaults := Defaults { } + + if _, err := toml.DecodeFile(path, &defaults); err != nil { + return defaults, err + } + + 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 + } + + if err := config.decodeAdapters(adapterCfg, false); err != nil { + return config, err + } + + return config, nil +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..51a9233 --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package planr + +const VERSION = "0.0.3" -- cgit v1.2.3