Skip to content

Commit

Permalink
gen: same alias/language name lookup as in linguis
Browse files Browse the repository at this point in the history
Includes generated code, to keep commits atomic.

Consits of:
 - code generator for alias produces new API
 - retrofiting all clients to a new API
 - generated code data/aliases.go

Signed-off-by: Alexander Bezzubov <bzz@apache.org>
  • Loading branch information
bzz committed Jan 9, 2019
1 parent 0b627a9 commit 82cb3c5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
5 changes: 2 additions & 3 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,8 @@ func GetLanguageType(language string) (langType Type) {
// GetLanguageByAlias returns either the language related to the given alias and ok set to true
// or Otherlanguage and ok set to false if the alias is not recognized.
func GetLanguageByAlias(alias string) (lang string, ok bool) {
a := strings.Split(alias, `,`)[0]
a = strings.ToLower(a)
lang, ok = data.LanguagesByAlias[a]
a := strings.SplitN(alias, `,`, 2)[0]
lang, ok = data.LanguagesByAlias(a)
if !ok {
lang = OtherLanguage
}
Expand Down
28 changes: 26 additions & 2 deletions data/alias.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion data/heuristics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ func (h *Heuristics) Match(data []byte) []string {
var matchedLangs []string
for _, matcher := range *h {
if matcher.Match(data) {
for _, lang := range matcher.(Rule).GetLanguages() {
for _, langOrAlias := range matcher.(Rule).GetLanguages() {
lang, ok := LanguagesByAlias(langOrAlias)
if !ok { // should never happen
// language name/alias in heuristics.yml is not consistent with languages.yml
// but we do not surface any error on the API
continue
}
matchedLangs = append(matchedLangs, lang)
}
break
Expand Down
31 changes: 28 additions & 3 deletions internal/code-generator/assets/alias.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
package data

// LanguagesByAlias keeps alias for different languages and use the name of the languages as an alias too.
import "strings"

// LanguagesByAliasMap keeps alias for different languages and use the name of the languages as an alias too.
// All the keys (alias or not) are written in lower case and the whitespaces has been replaced by underscores.
var LanguagesByAlias = map[string]string{
var LanguagesByAliasMap = map[string]string{
{{range $alias, $language := . -}}
"{{ $alias }}": {{ printf "%q" $language -}},
"{{ $alias }}": {{ printf "%q" $language -}},
{{end -}}
}

// LanguagesByAlias looks up the language name by it's alias or name.
// It mirrors the logic of github linguist and is needed e.g for heuristcs.yml
// that mixes names and aliases in a language field (see XPM example).
func LanguagesByAlias(langOrAlias string) (lang string, ok bool) {
k := ConvertToAliasKey(langOrAlias)
lang, ok = LanguagesByAliasMap[k]
return
}


// ConvertToAliasKey converts language name to a key in LanguagesByAliasMap.
// Following
// - internal.code-generator.generator.convertToAliasKey()
// - GetLanguageByAlias()
// conventions.
// It is here to avoid dependency on "generate" and "enry" packages.
func ConvertToAliasKey(langName string) string {
ak := strings.SplitN(langName, `,`, 2)[0]
ak = strings.Replace(ak, ` `, `_`, -1)
ak = strings.ToLower(ak)
return ak
}

0 comments on commit 82cb3c5

Please sign in to comment.