github.com/coveo/gotemplate@v2.7.7+incompatible/template/func_category.go (about)

     1  package template
     2  
     3  import (
     4  	"sort"
     5  )
     6  
     7  // FuncCategory represents a group of functions of the same group.
     8  type FuncCategory struct {
     9  	name      string
    10  	functions []string
    11  }
    12  
    13  // Name returns the name related to the entry.
    14  func (fc FuncCategory) Name() string { return fc.name }
    15  
    16  // Functions returns the list of functions associated with the category.
    17  func (fc FuncCategory) Functions() []string { return fc.functions }
    18  
    19  func (t Template) getCategories() []FuncCategory {
    20  	categories := make(map[string][]string)
    21  	for name := range t.functions {
    22  		fi := t.functions[name]
    23  		if fi.alias != nil {
    24  			fi = *fi.alias
    25  		}
    26  		categories[fi.group] = append(categories[fi.group], name)
    27  	}
    28  
    29  	categoryList := make([]string, 0, len(categories))
    30  	for key := range categories {
    31  		categoryList = append(categoryList, key)
    32  	}
    33  	sort.Strings(categoryList)
    34  
    35  	result := make([]FuncCategory, len(categoryList))
    36  	for i := range categoryList {
    37  		sort.Strings(categories[categoryList[i]])
    38  		result[i] = FuncCategory{
    39  			name:      categoryList[i],
    40  			functions: categories[categoryList[i]],
    41  		}
    42  	}
    43  	return result
    44  }