From 2af7117891cdb67758219a7075f39c05dc02f3f5 Mon Sep 17 00:00:00 2001 From: Furkan Sahin Date: Thu, 2 Sep 2021 03:14:47 -0500 Subject: Make adapters and internals complient with new directory structure --- config.go | 33 --------------------------------- 1 file changed, 33 deletions(-) (limited to 'config.go') diff --git a/config.go b/config.go index bc0fa6a..887bbb0 100644 --- a/config.go +++ b/config.go @@ -1,11 +1,8 @@ package planr import ( - "log" "github.com/BurntSushi/toml" ) - - /* TODO: Every property defined within the defaults currently has to implement the "inherit" method to conditionally inherit a @@ -38,22 +35,6 @@ 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 @@ -82,18 +63,6 @@ type Defaults struct { 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) - } -} // The default configuration must be able in inherit from // other defaults further up the tree @@ -169,8 +138,6 @@ func (defaults *Defaults) decodeAdapters( func DecodeDefaults(path string, adapterCfg []AdapterConfig) (Defaults, error) { defaults := Defaults { } - - if _, err := toml.DecodeFile(path, &defaults); err != nil { return defaults, err } -- cgit v1.2.3 From 2f78806caea83272f0875627e46e314ebd48da73 Mon Sep 17 00:00:00 2001 From: Furkan Sahin 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 (limited to 'config.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 From db947b801555913179c5e700e8b526166e3582ca Mon Sep 17 00:00:00 2001 From: Furkan Sahin Date: Sun, 5 Sep 2021 02:41:21 -0500 Subject: Add config w/ version information --- cmd/planr/main.go | 6 ++++-- cmd/planr/sub/build.go | 4 ++-- cmd/planr/sub/common.go | 15 +++++++++++++++ cmd/planr/sub/evaluate.go | 4 +++- config.go | 31 ++++++++++++++++++++++--------- fs.go | 4 ++-- rubric_config.go | 4 ++-- 7 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 cmd/planr/sub/common.go (limited to 'config.go') diff --git a/cmd/planr/main.go b/cmd/planr/main.go index 779ee58..9db30de 100644 --- a/cmd/planr/main.go +++ b/cmd/planr/main.go @@ -79,6 +79,8 @@ func main() { runner := getConfiguredRunner() + cfg := planr.DecodeConfig(runner.ConfigDir()) + subcommand := flag.Arg(0) subargs := flag.Args()[1:] @@ -86,9 +88,9 @@ func main() { case "version": fmt.Printf("%s\n", planr.VERSION) case "build": - sub.Build(runner, subargs) + sub.Build(runner, subargs, cfg) case "evaluate", "eval": - sub.Evaluate(runner, subargs) + sub.Evaluate(runner, subargs, cfg) case "clean": sub.Clean(runner, subargs) case "config": diff --git a/cmd/planr/sub/build.go b/cmd/planr/sub/build.go index caf7bde..2617a68 100644 --- a/cmd/planr/sub/build.go +++ b/cmd/planr/sub/build.go @@ -4,8 +4,8 @@ import ( "golang.furkistan.com/planr" ) - -func Build(runner planr.Runner, params []string) { +func Build(runner planr.Runner, params []string, cfg planr.Config) { + dieIncompatibleVersion(cfg) tcs := runner.CollectCases() runner.Build(tcs) } diff --git a/cmd/planr/sub/common.go b/cmd/planr/sub/common.go new file mode 100644 index 0000000..7e896c1 --- /dev/null +++ b/cmd/planr/sub/common.go @@ -0,0 +1,15 @@ +package sub + +import ( + "golang.furkistan.com/planr" + "os" + "fmt" +) + +func dieIncompatibleVersion(cfg planr.Config) { + if cfg.IncompatibleWithVersion() { + fmt.Fprintf(os.Stderr, "This version of PlanR (%v) is incompatible with config version %s\n", planr.VERSION, cfg.Version) + fmt.Fprintf(os.Stderr, "Please upgrade to version %s or greater\n", cfg.Version) + os.Exit(1) + } +} diff --git a/cmd/planr/sub/evaluate.go b/cmd/planr/sub/evaluate.go index fe864ad..5719b10 100644 --- a/cmd/planr/sub/evaluate.go +++ b/cmd/planr/sub/evaluate.go @@ -4,7 +4,9 @@ import ( "golang.furkistan.com/planr" ) -func Evaluate(runner planr.Runner, params []string) { +func Evaluate(runner planr.Runner, params []string, cfg planr.Config) { + dieIncompatibleVersion(cfg) + tcs := runner.CollectCases() trs := runner.Evaluate(tcs) diff --git a/config.go b/config.go index d2d32e9..88495e3 100644 --- a/config.go +++ b/config.go @@ -4,19 +4,19 @@ import ( "github.com/BurntSushi/toml" "log" "path" + "strings" ) -type planrConfig struct { - Version string - Project_title string +type Config struct { + Version string } -const PLANR_CONFIG = "config.toml" +const PLANR_CONFIG_FILE = "config.toml" -func decodeConfig(configDir string) planrConfig { - cfg := planrConfig { } +func DecodeConfig(configDir string) Config { + cfg := Config { } - configFile := path.Join(configDir, PLANR_CONFIG) + configFile := path.Join(configDir, PLANR_CONFIG_FILE) if _, err := toml.DecodeFile(configFile, &cfg); err != nil { // TODO: handle missing config @@ -26,6 +26,19 @@ func decodeConfig(configDir string) planrConfig { return cfg } -func (cfg planrConfig) isIncompatibleWithVersion() bool { - return cfg.Version > VERSION +func (cfg Config) IncompatibleWithVersion() bool { + if strings.Count(cfg.Version, ".") != 2 { + log.Fatalf("Version %s is not semantic", cfg.Version) + } + + cfgbits := strings.SplitN(cfg.Version, ".", 2) + bits := strings.SplitN(VERSION, ".", 2) + + // major version change + if cfgbits[0] != bits[0] { + return true + } + + // Config newer, possible feature additions + return cfgbits[1] > bits[1] } diff --git a/fs.go b/fs.go index a42ff2c..04a3522 100644 --- a/fs.go +++ b/fs.go @@ -99,7 +99,7 @@ func collectFromDir( // Process defaults for this directory if a defaults.toml is found defaultsPath := path.Join(dir, DEFAULTS) if info, err := os.Stat(defaultsPath); err == nil && !info.IsDir() { - d, err := DecodeDefaults(defaultsPath, cfgs) + d, err := DecodeRubricDefaults(defaultsPath, cfgs) if err != nil { log.Fatalf("Error encounter in %s: %v\n", defaultsPath, err); @@ -135,7 +135,7 @@ func collectFromDir( } // Decode a unit - config, err := DecodeConfig(child, cfgs) + config, err := DecodeRubricConfig(child, cfgs) if err != nil { log.Fatalf("Error encountered in %s: %v", child, config) diff --git a/rubric_config.go b/rubric_config.go index 887bbb0..322e58a 100644 --- a/rubric_config.go +++ b/rubric_config.go @@ -135,7 +135,7 @@ func (defaults *Defaults) decodeAdapters( } // Decode defaults.toml -func DecodeDefaults(path string, adapterCfg []AdapterConfig) (Defaults, error) { +func DecodeRubricDefaults(path string, adapterCfg []AdapterConfig) (Defaults, error) { defaults := Defaults { } if _, err := toml.DecodeFile(path, &defaults); err != nil { @@ -151,7 +151,7 @@ func DecodeDefaults(path string, adapterCfg []AdapterConfig) (Defaults, error) { } // Decode an individual unit -func DecodeConfig(path string, adapterCfg []AdapterConfig) (TestCaseConfig, error) { +func DecodeRubricConfig(path string, adapterCfg []AdapterConfig) (TestCaseConfig, error) { config := TestCaseConfig { } if _, err := toml.DecodeFile(path, &config); err != nil { -- cgit v1.2.3 From b1cd5692b9c1139298d82ad610dad6657feb2590 Mon Sep 17 00:00:00 2001 From: Furkan Sahin Date: Sun, 5 Sep 2021 18:57:59 -0500 Subject: Do not throw fatal error when missing config, this behavior will be enabled in future releases --- cmd/planr/sub/build.go | 2 +- cmd/planr/sub/common.go | 4 ++-- cmd/planr/sub/evaluate.go | 2 +- config.go | 15 +++++++++++---- 4 files changed, 15 insertions(+), 8 deletions(-) (limited to 'config.go') diff --git a/cmd/planr/sub/build.go b/cmd/planr/sub/build.go index 2617a68..25bc46b 100644 --- a/cmd/planr/sub/build.go +++ b/cmd/planr/sub/build.go @@ -4,7 +4,7 @@ import ( "golang.furkistan.com/planr" ) -func Build(runner planr.Runner, params []string, cfg planr.Config) { +func Build(runner planr.Runner, params []string, cfg * planr.Config) { dieIncompatibleVersion(cfg) tcs := runner.CollectCases() runner.Build(tcs) diff --git a/cmd/planr/sub/common.go b/cmd/planr/sub/common.go index 7e896c1..ec67356 100644 --- a/cmd/planr/sub/common.go +++ b/cmd/planr/sub/common.go @@ -6,8 +6,8 @@ import ( "fmt" ) -func dieIncompatibleVersion(cfg planr.Config) { - if cfg.IncompatibleWithVersion() { +func dieIncompatibleVersion(cfg *planr.Config) { + if cfg != nil && cfg.IncompatibleWithVersion() { fmt.Fprintf(os.Stderr, "This version of PlanR (%v) is incompatible with config version %s\n", planr.VERSION, cfg.Version) fmt.Fprintf(os.Stderr, "Please upgrade to version %s or greater\n", cfg.Version) os.Exit(1) diff --git a/cmd/planr/sub/evaluate.go b/cmd/planr/sub/evaluate.go index dac42d0..79d2b23 100644 --- a/cmd/planr/sub/evaluate.go +++ b/cmd/planr/sub/evaluate.go @@ -34,7 +34,7 @@ func jsonPrint(results gradingResults) { fmt.Println(string(res)) } -func Evaluate(runner planr.Runner, params []string, cfg planr.Config) { +func Evaluate(runner planr.Runner, params []string, cfg *planr.Config) { f := flag.NewFlagSet("evaluate", flag.ExitOnError) jsonOutput := f.Bool("json", false, "print json output") diff --git a/config.go b/config.go index 88495e3..d7cd3e4 100644 --- a/config.go +++ b/config.go @@ -13,14 +13,21 @@ type Config struct { const PLANR_CONFIG_FILE = "config.toml" -func DecodeConfig(configDir string) Config { - cfg := Config { } +// TODO: REMOVE +const STRICTLY_REQUIRE_CONFIG = false + +func DecodeConfig(configDir string) *Config { + cfg := new(Config) configFile := path.Join(configDir, PLANR_CONFIG_FILE) - if _, err := toml.DecodeFile(configFile, &cfg); err != nil { + if _, err := toml.DecodeFile(configFile, cfg); err != nil { + cfg = nil + // TODO: handle missing config - log.Fatalf("Could not decode global configuration %s: %v", configFile, err) + if STRICTLY_REQUIRE_CONFIG { + log.Fatalf("Could not decode global configuration %s: %v", configFile, err) + } } return cfg -- cgit v1.2.3