diff options
author | Julien Dessaux | 2020-12-26 12:54:42 +0100 |
---|---|---|
committer | Julien Dessaux | 2020-12-26 12:54:42 +0100 |
commit | bbea9342330ff020b8e3094c849db88719915132 (patch) | |
tree | bee0f9b036947e2b03a10192d0927dfe5e0f6711 /config/menu.go | |
parent | Began implementing config validation (diff) | |
download | shell-game-launcher-bbea9342330ff020b8e3094c849db88719915132.tar.gz shell-game-launcher-bbea9342330ff020b8e3094c849db88719915132.tar.bz2 shell-game-launcher-bbea9342330ff020b8e3094c849db88719915132.zip |
Continued implementing config validation
Diffstat (limited to '')
-rw-r--r-- | config/menu.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/config/menu.go b/config/menu.go index 160afa3..004c7fe 100644 --- a/config/menu.go +++ b/config/menu.go @@ -2,6 +2,7 @@ package config import ( "regexp" + "strings" "github.com/pkg/errors" ) @@ -61,6 +62,52 @@ func (m *Menu) validate(name string) error { return nil } +func (m *Menu) validateConsistency(c *Config) error { + // Necessary menus + if _, ok := c.Menus["anonymous"]; !ok { + return errors.New("No anonymous menu declared") + } + if _, ok := c.Menus["logged_in"]; !ok { + return errors.New("No logged_in menu declared") + } + // Validate actions + menus := make(map[string]bool) + menus["anonymous"] = true + menus["logged_in"] = true + playable := make(map[string]bool) + for k, v := range c.Menus { + for _, e := range v.MenuEntries { + tokens := strings.Split(e.Action, " ") + switch tokens[0] { + case "menu": + if _, ok := c.Menus[tokens[1]]; ok { + menus[tokens[1]] = true + } else { + return errors.New("menu action " + tokens[1] + " in menu " + k + " does not exist") + } + case "play": + if _, ok := c.Games[tokens[1]]; ok { + playable[tokens[1]] = true + } else { + return errors.New("play action " + tokens[1] + " in menu " + k + " does not exist") + } + } + } + } + // Check for unreachables + for k, _ := range c.Menus { + if _, ok := menus[k]; !ok { + return errors.New("unreachable menu : " + k) + } + } + for k, _ := range c.Games { + if _, ok := playable[k]; !ok { + return errors.New("unplayable game : " + k) + } + } + return nil +} + func (m *MenuEntry) validate() error { // Key if ok := reValidKey.MatchString(m.Key); !ok { |