1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-22 18:01:15 +01:00

Follow go style guide re declaring empty slices

This commit is contained in:
Oliver Steele 2017-09-02 14:09:04 -04:00
parent 4a6665f30c
commit 433480804a
6 changed files with 23 additions and 24 deletions

View File

@ -14,7 +14,7 @@ var benchmark = app.Command("benchmark", "Repeat build for ten seconds. Implies
// a separate benchmark runner that invokes a new gojekyll process each time.
func benchmarkCommand() (err error) {
startTime := time.Now()
samples := []float64{}
var samples []float64
for i := 0; time.Since(startTime) < 10*time.Second; i++ {
sampleStart := time.Now()
site, err := loadSite(*source, options)

View File

@ -12,7 +12,7 @@ var dynamicRoutes = routes.Flag("dynamic", "Only show routes to non-static files
func routesCommand(site *site.Site) error {
logger.label("Routes:", "")
urls := []string{}
var urls []string
for u, p := range site.Routes {
if !(*dynamicRoutes && p.IsStatic()) {
urls = append(urls, u)

View File

@ -27,7 +27,7 @@ func AddJekyllFilters(e *liquid.Engine, c *config.Config) {
e.RegisterFilter("array_to_sentence_string", arrayToSentenceStringFilter)
// TODO doc neither Liquid nor Jekyll docs this, but it appears to be present
e.RegisterFilter("filter", func(values []map[string]interface{}, key string) []interface{} {
result := []interface{}{}
var result []interface{}
for _, value := range values {
if _, ok := value[key]; ok {
result = append(result, value)
@ -200,7 +200,7 @@ func groupByExpFilter(array []map[string]interface{}, name string, expr expressi
groups[key] = []interface{}{item}
}
}
result := []map[string]interface{}{}
var result []map[string]interface{}
for k, v := range groups {
result = append(result, map[string]interface{}{"name": k, "items": v})
}
@ -227,7 +227,7 @@ func groupByFilter(array []map[string]interface{}, property string) []map[string
}
}
}
result := []map[string]interface{}{}
var result []map[string]interface{}
for k, v := range groups {
result = append(result, map[string]interface{}{"name": k, "items": v})
}
@ -252,7 +252,7 @@ func whereExpFilter(array []interface{}, name string, expr expressions.Closure)
if rt.Kind() != reflect.Array && rt.Kind() != reflect.Slice {
return nil, nil
}
result := []interface{}{}
var result []interface{}
for i := 0; i < rt.Len(); i++ {
item := rt.Index(i).Interface()
value, err := expr.Bind(name, item).Evaluate()
@ -271,7 +271,7 @@ func whereFilter(array []map[string]interface{}, key string, value interface{})
if rt.Kind() != reflect.Array && rt.Kind() != reflect.Slice {
return nil
}
result := []interface{}{}
var result []interface{}
for i := 0; i < rt.Len(); i++ {
item := rt.Index(i)
if item.Kind() == reflect.Map && item.Type().Key().Kind() == reflect.String {

View File

@ -41,7 +41,7 @@ var smartifyReplacementPattern *regexp.Regexp
func init() {
smartifyReplacements = map[string]string{}
disjuncts := []string{}
var disjuncts []string
regexQuoter := regexp.MustCompile(`[\(\)\.]`)
escape := func(s string) string {
return regexQuoter.ReplaceAllString(s, `\$0`)

View File

@ -49,20 +49,19 @@ func (fm FrontMatter) String(k string, defaultValue string) string {
//
// This is the format for page categories and tags.
func (fm FrontMatter) SortedStringArray(key string) []string {
out := []string{}
field := fm[key]
switch value := field.(type) {
var result []string
switch v := fm[key].(type) {
case string:
out = strings.Fields(value)
result = strings.Fields(v)
case []interface{}:
if c, e := evaluator.Convert(value, reflect.TypeOf(out)); e == nil {
out = c.([]string)
if c, e := evaluator.Convert(v, reflect.TypeOf(result)); e == nil {
result = c.([]string)
}
case []string:
out = value
result = v
}
sort.Strings(out)
return out
sort.Strings(result)
return result
}
// Merge creates a new FrontMatter that merges its arguments,

View File

@ -18,12 +18,12 @@ func TestFileHasFrontMatter(t *testing.T) {
}
func TestFrontMatter_SortedStringArray(t *testing.T) {
sortedStringValue := func(value interface{}) []string {
fm := map[string]interface{}{"categories": value}
return FrontMatter(fm).SortedStringArray("categories")
sorted := func(v interface{}) []string {
fm := FrontMatter{"categories": v}
return fm.SortedStringArray("categories")
}
require.Equal(t, []string{"a", "b"}, sortedStringValue("b a"))
require.Equal(t, []string{"a", "b"}, sortedStringValue([]interface{}{"b", "a"}))
require.Equal(t, []string{"a", "b"}, sortedStringValue([]string{"b", "a"}))
require.Equal(t, []string{}, sortedStringValue(3))
require.Equal(t, []string{"a", "b"}, sorted("b a"))
require.Equal(t, []string{"a", "b"}, sorted([]interface{}{"b", "a"}))
require.Equal(t, []string{"a", "b"}, sorted([]string{"b", "a"}))
require.Len(t, sorted(3), 0)
}