Skip to content

Commit

Permalink
Merge pull request #4 from symflower/register-pattern
Browse files Browse the repository at this point in the history
Use convenience functions to register languages and models
  • Loading branch information
zimmski authored Mar 30, 2024
2 parents b0c59b4 + 43909f9 commit 883e027
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
3 changes: 1 addition & 2 deletions language/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
type LanguageGolang struct{}

func init() {
l := &LanguageGolang{}
Languages[l.ID()] = l
Register(&LanguageGolang{})
}

var _ Language = (*LanguageGolang)(nil)
Expand Down
14 changes: 14 additions & 0 deletions language/language.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package language

import (
pkgerrors "github.com/pkg/errors"
)

// Language defines a language to evaluate a repository.
type Language interface {
// ID returns the unique ID of this language.
Expand All @@ -14,3 +18,13 @@ type Language interface {

// Languages holds a register of all languages.
var Languages = map[string]Language{}

// Register adds a language to the common language list.
func Register(language Language) {
id := language.ID()
if _, ok := Languages[id]; ok {
panic(pkgerrors.WithMessage(pkgerrors.New("language was already registered"), id))
}

Languages[id] = language
}
14 changes: 14 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package model

import (
pkgerrors "github.com/pkg/errors"
)

// Model defines a model that can be queried for generations.
type Model interface {
// ID returns the unique ID of this model.
Expand All @@ -11,3 +15,13 @@ type Model interface {

// Models holds a register of all models.
var Models = map[string]Model{}

// Register adds a model to the common model list.
func Register(model Model) {
id := model.ID()
if _, ok := Models[id]; ok {
panic(pkgerrors.WithMessage(pkgerrors.New("model was already registered"), id))
}

Models[id] = model
}
3 changes: 1 addition & 2 deletions model/symflower.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
type ModelSymflower struct{}

func init() {
m := &ModelSymflower{}
Models[m.ID()] = m
Register(&ModelSymflower{})
}

var _ Model = (*ModelSymflower)(nil)
Expand Down

0 comments on commit 883e027

Please sign in to comment.