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

fix: precedence in case of multiple matching language mappings (fix #172)

This commit is contained in:
Ferdinand Mütsch 2021-04-13 23:39:31 +02:00
parent f8e1453754
commit 20dd4cf0ab
3 changed files with 14 additions and 4 deletions

View File

@ -34,10 +34,11 @@ func (h *Heartbeat) Valid() bool {
} }
func (h *Heartbeat) Augment(languageMappings map[string]string) { func (h *Heartbeat) Augment(languageMappings map[string]string) {
maxPrec := -1 // precision / mapping complexity -> more concrete ones shall take precedence
for ending, value := range languageMappings { for ending, value := range languageMappings {
if strings.HasSuffix(h.Entity, "."+ending) { if ok, prec := strings.HasSuffix(h.Entity, "."+ending), strings.Count(ending, "."); ok && prec > maxPrec {
h.Language = value h.Language = value
return maxPrec = prec
} }
} }
} }

View File

@ -28,22 +28,28 @@ func TestHeartbeat_Augment(t *testing.T) {
testMappings := map[string]string{ testMappings := map[string]string{
"py": "Python3", "py": "Python3",
"foo": "Foo Script", "foo": "Foo Script",
"php": "PHP 8",
"blade.php": "Blade", "blade.php": "Blade",
} }
sut1, sut2 := &Heartbeat{ sut1, sut2, sut3 := &Heartbeat{
Entity: "~/dev/file.py", Entity: "~/dev/file.py",
Language: "Python", Language: "Python",
}, &Heartbeat{ }, &Heartbeat{
Entity: "~/dev/file.blade.php", Entity: "~/dev/file.blade.php",
Language: "unknown", Language: "unknown",
}, &Heartbeat{
Entity: "~/dev/file.php",
Language: "PHP",
} }
sut1.Augment(testMappings) sut1.Augment(testMappings)
sut2.Augment(testMappings) sut2.Augment(testMappings)
sut3.Augment(testMappings)
assert.Equal(t, "Python3", sut1.Language) assert.Equal(t, "Python3", sut1.Language)
assert.Equal(t, "Blade", sut2.Language) assert.Equal(t, "Blade", sut2.Language)
assert.Equal(t, "PHP 8", sut3.Language)
} }
func TestHeartbeat_GetKey(t *testing.T) { func TestHeartbeat_GetKey(t *testing.T) {

View File

@ -17,7 +17,10 @@ LANGUAGES = {
'Go': 'go', 'Go': 'go',
'Java': 'java', 'Java': 'java',
'JavaScript': 'js', 'JavaScript': 'js',
'Python': 'py' 'Python': 'py',
# https://github.com/muety/wakapi/issues/172
'PHP': 'php',
'Blade': 'blade.php'
} }