You've already forked go-digest
mirror of
https://github.com/opencontainers/go-digest.git
synced 2025-07-30 00:21:10 +03:00
address some linting issues
- rename "digester" vars that shadowed package-level digester type - rename "hexdigestbytes" to be properly camelCase - use "errors.Is()" instead of straight comparing errors Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
18
algorithm.go
18
algorithm.go
@ -156,8 +156,8 @@ func RegisterAlgorithm(algorithm Algorithm, implementation CryptoHash) bool {
|
|||||||
|
|
||||||
// hexDigestRegex can be used to generate a regex for RegisterAlgorithm.
|
// hexDigestRegex can be used to generate a regex for RegisterAlgorithm.
|
||||||
func hexDigestRegex(cryptoHash CryptoHash) *regexp.Regexp {
|
func hexDigestRegex(cryptoHash CryptoHash) *regexp.Regexp {
|
||||||
hexdigestbytes := cryptoHash.Size() * 2
|
hexDigestBytes := cryptoHash.Size() * 2
|
||||||
return regexp.MustCompile(fmt.Sprintf("^[a-f0-9]{%d}$", hexdigestbytes))
|
return regexp.MustCompile(fmt.Sprintf("^[a-f0-9]{%d}$", hexDigestBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Available returns true if the digest type is available for use. If this
|
// Available returns true if the digest type is available for use. If this
|
||||||
@ -254,20 +254,18 @@ func (a Algorithm) Encode(d []byte) string {
|
|||||||
|
|
||||||
// FromReader returns the digest of the reader using the algorithm.
|
// FromReader returns the digest of the reader using the algorithm.
|
||||||
func (a Algorithm) FromReader(rd io.Reader) (Digest, error) {
|
func (a Algorithm) FromReader(rd io.Reader) (Digest, error) {
|
||||||
digester := a.Digester()
|
d := a.Digester()
|
||||||
|
if _, err := io.Copy(d.Hash(), rd); err != nil {
|
||||||
if _, err := io.Copy(digester.Hash(), rd); err != nil {
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return digester.Digest(), nil
|
return d.Digest(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromBytes digests the input and returns a Digest.
|
// FromBytes digests the input and returns a Digest.
|
||||||
func (a Algorithm) FromBytes(p []byte) Digest {
|
func (a Algorithm) FromBytes(p []byte) Digest {
|
||||||
digester := a.Digester()
|
d := a.Digester()
|
||||||
|
if _, err := d.Hash().Write(p); err != nil {
|
||||||
if _, err := digester.Hash().Write(p); err != nil {
|
|
||||||
// Writes to a Hash should never fail. None of the existing
|
// Writes to a Hash should never fail. None of the existing
|
||||||
// hash implementations in the stdlib or hashes vendored
|
// hash implementations in the stdlib or hashes vendored
|
||||||
// here can return errors from Write. Having a panic in this
|
// here can return errors from Write. Having a panic in this
|
||||||
@ -276,7 +274,7 @@ func (a Algorithm) FromBytes(p []byte) Digest {
|
|||||||
panic("write to hash function returned error: " + err.Error())
|
panic("write to hash function returned error: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return digester.Digest()
|
return d.Digest()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromString digests the string input and returns a Digest.
|
// FromString digests the string input and returns a Digest.
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"crypto"
|
"crypto"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
@ -56,7 +57,7 @@ func TestFlagInterface(t *testing.T) {
|
|||||||
} {
|
} {
|
||||||
t.Run(testcase.Name, func(t *testing.T) {
|
t.Run(testcase.Name, func(t *testing.T) {
|
||||||
alg = Canonical
|
alg = Canonical
|
||||||
if err := flagSet.Parse(testcase.Args); err != testcase.Err {
|
if err := flagSet.Parse(testcase.Args); !errors.Is(err, testcase.Err) {
|
||||||
if testcase.Err == nil {
|
if testcase.Err == nil {
|
||||||
t.Fatal("unexpected error", err)
|
t.Fatal("unexpected error", err)
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ func (dst *Set) Lookup(d string) (digest.Digest, error) {
|
|||||||
hex string
|
hex string
|
||||||
)
|
)
|
||||||
dgst, err := digest.Parse(d)
|
dgst, err := digest.Parse(d)
|
||||||
if err == digest.ErrDigestInvalidFormat {
|
if errors.Is(err, digest.ErrDigestInvalidFormat) {
|
||||||
hex = d
|
hex = d
|
||||||
searchFunc = func(i int) bool {
|
searchFunc = func(i int) bool {
|
||||||
return dst.entries[i].val >= d
|
return dst.entries[i].val >= d
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
package testdigest
|
package testdigest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
pkgdigest "github.com/opencontainers/go-digest"
|
pkgdigest "github.com/opencontainers/go-digest"
|
||||||
@ -38,7 +39,7 @@ type TestCase struct {
|
|||||||
|
|
||||||
func RunTestCase(t *testing.T, testcase TestCase) {
|
func RunTestCase(t *testing.T, testcase TestCase) {
|
||||||
digest, err := pkgdigest.Parse(testcase.Input)
|
digest, err := pkgdigest.Parse(testcase.Input)
|
||||||
if err != testcase.Err {
|
if errors.Is(err, testcase.Err) {
|
||||||
t.Fatalf("error differed from expected while parsing %q: %v != %v", testcase.Input, err, testcase.Err)
|
t.Fatalf("error differed from expected while parsing %q: %v != %v", testcase.Input, err, testcase.Err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user