diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-03-24 10:11:16 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-03-24 16:14:51 +0100 |
commit | b5f39d23b86f9cb83c51da9fe4abb4c19c01c3b7 (patch) | |
tree | cf23180dc07698391cf47c2fe525755417729020 /tpl | |
parent | 3011f36c27ecde309325e6c75ca377f4f87fa97a (diff) |
all: Apply staticcheck recommendations
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/collections/reflect_helpers.go | 2 | ||||
-rw-r--r-- | tpl/data/data.go | 14 | ||||
-rw-r--r-- | tpl/data/resources.go | 10 | ||||
-rw-r--r-- | tpl/data/resources_test.go | 10 | ||||
-rw-r--r-- | tpl/math/math.go | 4 | ||||
-rw-r--r-- | tpl/os/os.go | 4 | ||||
-rw-r--r-- | tpl/partials/partials.go | 2 | ||||
-rw-r--r-- | tpl/template.go | 4 | ||||
-rw-r--r-- | tpl/tplimpl/embedded/generate/generate.go | 3 | ||||
-rw-r--r-- | tpl/transform/remarshal.go | 3 |
10 files changed, 31 insertions, 25 deletions
diff --git a/tpl/collections/reflect_helpers.go b/tpl/collections/reflect_helpers.go index fca65481f..69425fcb0 100644 --- a/tpl/collections/reflect_helpers.go +++ b/tpl/collections/reflect_helpers.go @@ -38,7 +38,7 @@ func numberToFloat(v reflect.Value) (float64, error) { case kind == reflect.Interface: return numberToFloat(v.Elem()) default: - return 0, fmt.Errorf("Invalid kind %s in numberToFloat", kind) + return 0, fmt.Errorf("invalid kind %s in numberToFloat", kind) } } diff --git a/tpl/data/data.go b/tpl/data/data.go index 81fde9d70..15f039294 100644 --- a/tpl/data/data.go +++ b/tpl/data/data.go @@ -58,18 +58,18 @@ func (ns *Namespace) GetCSV(sep string, urlParts ...string) (d [][]string, err e url := strings.Join(urlParts, "") cache := ns.cacheGetCSV - unmarshal := func(b []byte) (error, bool) { + unmarshal := func(b []byte) (bool, error) { if !bytes.Contains(b, []byte(sep)) { - return _errors.Errorf("cannot find separator %s in CSV for %s", sep, url), false + return false, _errors.Errorf("cannot find separator %s in CSV for %s", sep, url) } if d, err = parseCSV(b, sep); err != nil { err = _errors.Wrapf(err, "failed to parse CSV file %s", url) - return err, true + return true, err } - return nil, false + return false, nil } var req *http.Request @@ -103,12 +103,12 @@ func (ns *Namespace) GetJSON(urlParts ...string) (interface{}, error) { return nil, _errors.Wrapf(err, "Failed to create request for getJSON resource %s", url) } - unmarshal := func(b []byte) (error, bool) { + unmarshal := func(b []byte) (bool, error) { err := json.Unmarshal(b, &v) if err != nil { - return err, true + return true, err } - return nil, false + return false, nil } req.Header.Add("Accept", "application/json") diff --git a/tpl/data/resources.go b/tpl/data/resources.go index 8b246a662..7de440ca6 100644 --- a/tpl/data/resources.go +++ b/tpl/data/resources.go @@ -34,7 +34,7 @@ var ( ) // getRemote loads the content of a remote file. This method is thread safe. -func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (error, bool), req *http.Request) error { +func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (bool, error), req *http.Request) error { url := req.URL.String() id := helpers.MD5String(url) var handled bool @@ -63,7 +63,7 @@ func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (e } res.Body.Close() - err, retry = unmarshal(b) + retry, err = unmarshal(b) if err == nil { // Return it so it can be cached. @@ -85,7 +85,7 @@ func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (e if !handled { // This is cached content and should be correct. - err, _ = unmarshal(b) + _, err = unmarshal(b) } return err @@ -104,14 +104,14 @@ func getLocal(url string, fs afero.Fs, cfg config.Provider) ([]byte, error) { // getResource loads the content of a local or remote file and returns its content and the // cache ID used, if relevant. -func (ns *Namespace) getResource(cache *filecache.Cache, unmarshal func(b []byte) (error, bool), req *http.Request) error { +func (ns *Namespace) getResource(cache *filecache.Cache, unmarshal func(b []byte) (bool, error), req *http.Request) error { switch req.URL.Scheme { case "": b, err := getLocal(req.URL.String(), ns.deps.Fs.Source, ns.deps.Cfg) if err != nil { return err } - err, _ = unmarshal(b) + _, err = unmarshal(b) return err default: return ns.getRemote(cache, unmarshal, req) diff --git a/tpl/data/resources_test.go b/tpl/data/resources_test.go index d1091b23f..a42232f94 100644 --- a/tpl/data/resources_test.go +++ b/tpl/data/resources_test.go @@ -114,9 +114,9 @@ func TestScpGetRemote(t *testing.T) { ns.client = cl var c []byte - f := func(b []byte) (error, bool) { + f := func(b []byte) (bool, error) { c = b - return nil, false + return false, nil } err = ns.getRemote(cache, f, req) @@ -158,15 +158,15 @@ func TestScpGetRemoteParallel(t *testing.T) { defer wg.Done() for j := 0; j < 10; j++ { var c []byte - f := func(b []byte) (error, bool) { + f := func(b []byte) (bool, error) { c = b - return nil, false + return false, nil } err := ns.getRemote(ns.cacheGetJSON, f, req) assert.NoError(t, err) if string(content) != string(c) { - t.Fatalf("expected\n%q\ngot\n%q", content, c) + t.Errorf("expected\n%q\ngot\n%q", content, c) } time.Sleep(23 * time.Millisecond) diff --git a/tpl/math/math.go b/tpl/math/math.go index 0bc813dce..08be42b47 100644 --- a/tpl/math/math.go +++ b/tpl/math/math.go @@ -78,11 +78,11 @@ func (ns *Namespace) Mod(a, b interface{}) (int64, error) { bi, errb := cast.ToInt64E(b) if erra != nil || errb != nil { - return 0, errors.New("Modulo operator can't be used with non integer value") + return 0, errors.New("modulo operator can't be used with non integer value") } if bi == 0 { - return 0, errors.New("The number can't be divided by zero at modulo operation") + return 0, errors.New("the number can't be divided by zero at modulo operation") } return ai % bi, nil diff --git a/tpl/os/os.go b/tpl/os/os.go index cc4a42d5d..2dab5c490 100644 --- a/tpl/os/os.go +++ b/tpl/os/os.go @@ -73,7 +73,7 @@ func readFile(fs afero.Fs, filename string) (string, error) { if info, err := fs.Stat(filename); err == nil { if info.Size() > 1000000 { - return "", fmt.Errorf("File %q is too big", filename) + return "", fmt.Errorf("file %q is too big", filename) } } else { return "", err @@ -108,7 +108,7 @@ func (ns *Namespace) ReadDir(i interface{}) ([]_os.FileInfo, error) { list, err := afero.ReadDir(ns.deps.Fs.WorkingDir, path) if err != nil { - return nil, fmt.Errorf("Failed to read Directory %s with error message %s", path, err) + return nil, fmt.Errorf("failed to read directory %q: %s", path, err) } return list, nil diff --git a/tpl/partials/partials.go b/tpl/partials/partials.go index ece16276d..1e8a84954 100644 --- a/tpl/partials/partials.go +++ b/tpl/partials/partials.go @@ -107,7 +107,7 @@ func (ns *Namespace) Include(name string, contextList ...interface{}) (interface } - return "", fmt.Errorf("Partial %q not found", name) + return "", fmt.Errorf("partial %q not found", name) } // IncludeCached executes and caches partial templates. An optional variant diff --git a/tpl/template.go b/tpl/template.go index 07152166a..01f79c407 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -152,7 +152,7 @@ func (t *TemplateAdapter) Execute(w io.Writer, data interface{}) (execErr error) // Panics in templates are a little bit too common (nil pointers etc.) // See https://github.com/gohugoio/hugo/issues/5327 if r := recover(); r != nil { - execErr = t.addFileContext(t.Name(), fmt.Errorf(`panic in Execute: %s. See "https://github.com/gohugoio/hugo/issues/5327" for the reason why we cannot provide a better error message for this.`, r)) + execErr = t.addFileContext(t.Name(), fmt.Errorf(`panic in Execute: %s. See "https://github.com/gohugoio/hugo/issues/5327" for the reason why we cannot provide a better error message for this`, r)) } }() @@ -174,7 +174,7 @@ func (t *TemplateAdapter) TemplateInfo() Info { // The identifiers may be truncated in the log, e.g. // "executing "main" at <$scaled.SRelPermalin...>: can't evaluate field SRelPermalink in type *resource.Image" -var identifiersRe = regexp.MustCompile("at \\<(.*?)(\\.{3})?\\>:") +var identifiersRe = regexp.MustCompile(`at \<(.*?)(\.{3})?\>:`) func (t *TemplateAdapter) extractIdentifiers(line string) []string { m := identifiersRe.FindAllStringSubmatch(line, -1) diff --git a/tpl/tplimpl/embedded/generate/generate.go b/tpl/tplimpl/embedded/generate/generate.go index a48e00756..df4de4799 100644 --- a/tpl/tplimpl/embedded/generate/generate.go +++ b/tpl/tplimpl/embedded/generate/generate.go @@ -39,6 +39,9 @@ func main() { var nameValues []string err = filepath.Walk(temlatePath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } if info.IsDir() { return nil diff --git a/tpl/transform/remarshal.go b/tpl/transform/remarshal.go index 62d826b43..182bd21d6 100644 --- a/tpl/transform/remarshal.go +++ b/tpl/transform/remarshal.go @@ -41,6 +41,9 @@ func (ns *Namespace) Remarshal(format string, data interface{}) (string, error) } meta, err := metadecoders.Default.UnmarshalToMap([]byte(from), fromFormat) + if err != nil { + return "", err + } var result bytes.Buffer if err := parser.InterfaceToConfig(meta, mark, &result); err != nil { |