From cbf44205e02bf98c56511c2f9b18cc48776d5b87 Mon Sep 17 00:00:00 2001 From: Manuel Carmona Date: Wed, 3 May 2017 10:42:31 +0200 Subject: [PATCH] fixed GetLanguageType to return Unknown when language is not found in languagesType map --- internal/code-generator/assets/type.go.tmpl | 6 +++- .../generator/test_files/languages.test.tmpl | 11 -------- .../generator/test_files/type.gold | 6 +++- .../generator/test_files/type.test.go.tmpl | 28 ------------------- type.go | 6 +++- type_test.go | 3 ++ 6 files changed, 18 insertions(+), 42 deletions(-) delete mode 100644 internal/code-generator/generator/test_files/languages.test.tmpl delete mode 100644 internal/code-generator/generator/test_files/type.test.go.tmpl diff --git a/internal/code-generator/assets/type.go.tmpl b/internal/code-generator/assets/type.go.tmpl index de7592b..5d44d5e 100644 --- a/internal/code-generator/assets/type.go.tmpl +++ b/internal/code-generator/assets/type.go.tmpl @@ -17,7 +17,11 @@ const ( // GetLanguageType returns the given language's type. func GetLanguageType(language string) (langType Type) { - langType, _ = languagesType[language] + langType, ok := languagesType[language] + if !ok { + langType = Unknown + } + return langType } diff --git a/internal/code-generator/generator/test_files/languages.test.tmpl b/internal/code-generator/generator/test_files/languages.test.tmpl deleted file mode 100644 index 70a9641..0000000 --- a/internal/code-generator/generator/test_files/languages.test.tmpl +++ /dev/null @@ -1,11 +0,0 @@ -package slinguist - -// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/simple-linguist.v1/internal/code-generator -// THIS FILE SHOULD NOT BE EDITED BY HAND -// Extracted from github/linguist commit: {{ getCommit }} - -var languagesByExtension = map[string][]string{ - {{range $extension, $languages := . -}} - "{{ $extension }}": { {{- $languages | formatStringSlice -}} }, - {{end -}} -} diff --git a/internal/code-generator/generator/test_files/type.gold b/internal/code-generator/generator/test_files/type.gold index a54d81c..ebf69ea 100644 --- a/internal/code-generator/generator/test_files/type.gold +++ b/internal/code-generator/generator/test_files/type.gold @@ -17,7 +17,11 @@ const ( // GetLanguageType returns the given language's type. func GetLanguageType(language string) (langType Type) { - langType, _ = languagesType[language] + langType, ok := languagesType[language] + if !ok { + langType = Unknown + } + return langType } diff --git a/internal/code-generator/generator/test_files/type.test.go.tmpl b/internal/code-generator/generator/test_files/type.test.go.tmpl deleted file mode 100644 index de7592b..0000000 --- a/internal/code-generator/generator/test_files/type.test.go.tmpl +++ /dev/null @@ -1,28 +0,0 @@ -package slinguist - -// CODE GENERATED AUTOMATICALLY WITH gopkg.in/src-d/simple-linguist.v1/internal/code-generator -// THIS FILE SHOULD NOT BE EDITED BY HAND -// Extracted from github/linguist commit: {{ getCommit }} - -type Type int - -const ( - // Language's type. Either data, programming, markup, prose, or unknown. - Unknown Type = iota - Data - Programming - Markup - Prose -) - -// GetLanguageType returns the given language's type. -func GetLanguageType(language string) (langType Type) { - langType, _ = languagesType[language] - return langType -} - -var languagesType = map[string]Type{ - {{range $language, $type := . -}} - "{{ $language }}": {{ $type -}}, - {{end -}} -} diff --git a/type.go b/type.go index 4c715d2..6ab3462 100644 --- a/type.go +++ b/type.go @@ -17,7 +17,11 @@ const ( // GetLanguageType returns the given language's type. func GetLanguageType(language string) (langType Type) { - langType, _ = languagesType[language] + langType, ok := languagesType[language] + if !ok { + langType = Unknown + } + return langType } diff --git a/type_test.go b/type_test.go index 094a5a7..5607e34 100644 --- a/type_test.go +++ b/type_test.go @@ -29,4 +29,7 @@ func (s *TSuite) TestGetLanguageType(c *C) { langType = GetLanguageType("Textile") c.Assert(langType, Equals, Prose) + + langType = GetLanguageType("Whatever") + c.Assert(langType, Equals, Unknown) }