1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: allow custom mappings with dot (resolve #144)

This commit is contained in:
Tobias Dillig 2021-03-24 19:25:36 +01:00
parent 9fc3c65efe
commit caf87de887

View File

@ -4,16 +4,10 @@ import (
"fmt" "fmt"
"github.com/emvi/logbuch" "github.com/emvi/logbuch"
"github.com/mitchellh/hashstructure/v2" "github.com/mitchellh/hashstructure/v2"
"regexp" "strings"
"time" "time"
) )
var languageRegex *regexp.Regexp
func init() {
languageRegex = regexp.MustCompile(`^.+\.(.+)$`)
}
type Heartbeat struct { type Heartbeat struct {
ID uint `gorm:"primary_key" hash:"ignore"` ID uint `gorm:"primary_key" hash:"ignore"`
User *User `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" hash:"ignore"` User *User `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" hash:"ignore"`
@ -40,15 +34,12 @@ func (h *Heartbeat) Valid() bool {
} }
func (h *Heartbeat) Augment(languageMappings map[string]string) { func (h *Heartbeat) Augment(languageMappings map[string]string) {
groups := languageRegex.FindAllStringSubmatch(h.Entity, -1) for ending, value := range languageMappings {
if len(groups) == 0 || len(groups[0]) != 2 { if strings.HasSuffix(h.Entity, "."+ending) {
return h.Language = value
return
}
} }
ending := groups[0][1]
if _, ok := languageMappings[ending]; !ok {
return
}
h.Language, _ = languageMappings[ending]
} }
func (h *Heartbeat) GetKey(t uint8) (key string) { func (h *Heartbeat) GetKey(t uint8) (key string) {