Browse Source

normalize level name in registry

pull/1/head
Stephanie Gredell 7 months ago
parent
commit
c73da65a2c
  1. 24
      internals/level/level.go

24
internals/level/level.go

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"strings"
) )
type Level struct { type Level struct {
@ -29,7 +30,10 @@ type Level struct {
FailureEvents []FailureEvent `json:"failureEvents,omitempty"` FailureEvents []FailureEvent `json:"failureEvents,omitempty"`
ScoringWeights map[string]float64 `json:"scoringWeights,omitempty"` ScoringWeights map[string]float64 `json:"scoringWeights,omitempty"`
Hints []string `json:"hints,omitempty"` Hints []string `json:"hints,omitempty"`
InterviewerRequirements []string `json:"interviewerRequirements,omitempty"`
FunctionalRequirements []string `json:"functionalRequirements,omitempty"`
NonFunctionalRequirements []string `json:"nonFunctionalRequirements,omitempty"`
} }
type Difficulty string type Difficulty string
@ -66,15 +70,17 @@ func InitRegistry(levels []Level) {
Registry = make(map[string]map[string]Level) Registry = make(map[string]map[string]Level)
for _, lvl := range levels { for _, lvl := range levels {
// check if level already exists here // check if level already exists here
if _, ok := Registry[lvl.Name]; !ok { normalized := strings.ToLower(lvl.Name)
Registry[lvl.Name] = make(map[string]Level) if _, ok := Registry[normalized]; !ok {
Registry[normalized] = make(map[string]Level)
} }
// populate it // populate it
Registry[lvl.Name][string(lvl.Difficulty)] = lvl Registry[normalized][string(lvl.Difficulty)] = lvl
} }
} }
func GetLevel(name string, difficulty Difficulty) (*Level, error) { func GetLevel(name string, difficulty Difficulty) (*Level, error) {
name = strings.ToLower(name)
diffMap, ok := Registry[name] diffMap, ok := Registry[name]
if !ok { if !ok {
return nil, fmt.Errorf("level name %s not found", name) return nil, fmt.Errorf("level name %s not found", name)
@ -101,3 +107,13 @@ func (d *Difficulty) UnmarshalJSON(b []byte) error {
return fmt.Errorf("invalid difficulty: %q", s) return fmt.Errorf("invalid difficulty: %q", s)
} }
} }
func AllLevels() []Level {
var levels []Level
for _, diffMap := range Registry {
for _, lvl := range diffMap {
levels = append(levels, lvl)
}
}
return levels
}

Loading…
Cancel
Save