✨ Add Hash generator
This commit is contained in:
parent
dd531c1f73
commit
8888ee3855
5 changed files with 124 additions and 1 deletions
|
|
@ -26,6 +26,7 @@ tasks:
|
|||
desc: Vet go code
|
||||
sources:
|
||||
- '**/*.go'
|
||||
deps: [gen]
|
||||
cmds:
|
||||
- go vet ./...
|
||||
|
||||
|
|
@ -81,7 +82,7 @@ tasks:
|
|||
|
||||
test:
|
||||
desc: Run unit tests
|
||||
deps: [fmt, vet]
|
||||
deps: [fmt, vet, gen]
|
||||
sources:
|
||||
- '**/*.go'
|
||||
generates:
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
package nomino
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
|
@ -185,3 +189,44 @@ func SlugWithLang(lang string) Generator {
|
|||
return slug.MakeLang(name, lang), err
|
||||
}
|
||||
}
|
||||
|
||||
// HashingFunc is a function that generates a hash.Hash
|
||||
type HashingFunc func() hash.Hash
|
||||
|
||||
//go:generate stringer -type=HashType
|
||||
|
||||
// HashType represents a particular hashing algorithm
|
||||
type HashType uint8
|
||||
|
||||
const (
|
||||
MD5 HashType = iota + 1
|
||||
SHA1
|
||||
SHA256
|
||||
)
|
||||
|
||||
// ErrInvalidHashType is returned by the Hash generator when an invalid HashType is passed
|
||||
var ErrInvalidHashType = errors.New("invalid hash type")
|
||||
|
||||
var hashMap = map[HashType]HashingFunc{
|
||||
MD5: md5.New,
|
||||
SHA1: sha1.New,
|
||||
SHA256: sha256.New,
|
||||
}
|
||||
|
||||
// Hash generates a name from a hash of the filename.
|
||||
// When this is used, the original filename will be removed from the final filename.
|
||||
func Hash(t HashType) Generator {
|
||||
f, ok := hashMap[t]
|
||||
return func(c *Config) (string, error) {
|
||||
if !ok {
|
||||
return "", fmt.Errorf("%w: %s", ErrInvalidHashType, t)
|
||||
}
|
||||
name, err := getOriginal(c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
hs := f()
|
||||
hs.Write([]byte(name))
|
||||
return fmt.Sprintf("%x", hs.Sum(nil)), nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -161,3 +161,33 @@ func ExampleSlugWithLang() {
|
|||
|
||||
// Output: diese-und-dass.txt
|
||||
}
|
||||
|
||||
func ExampleHash_mD5() {
|
||||
conf := NewConfig(
|
||||
WithOriginal("foobar"),
|
||||
WithGenerator(Hash(MD5)),
|
||||
)
|
||||
str, _ := Make(conf)
|
||||
fmt.Println(str)
|
||||
// Output: 3858f62230ac3c915f300c664312c63f.txt
|
||||
}
|
||||
|
||||
func ExampleHash_sHA1() {
|
||||
conf := NewConfig(
|
||||
WithOriginal("foobar"),
|
||||
WithGenerator(Hash(SHA1)),
|
||||
)
|
||||
str, _ := Make(conf)
|
||||
fmt.Println(str)
|
||||
// Output: 8843d7f92416211de9ebb963ff4ce28125932878.txt
|
||||
}
|
||||
|
||||
func ExampleHash_sHA256() {
|
||||
conf := NewConfig(
|
||||
WithOriginal("foobar"),
|
||||
WithGenerator(Hash(SHA256)),
|
||||
)
|
||||
str, _ := Make(conf)
|
||||
fmt.Println(str)
|
||||
// Output: c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2.txt
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package nomino
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -120,3 +121,23 @@ func TestSlugRemovesOriginal(t *testing.T) {
|
|||
assert.Equal(t, "hello-world", st)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestHashBadHash(t *testing.T) {
|
||||
conf := NewConfig(WithOriginal("foobar"), WithGenerator(Hash(0)))
|
||||
st, err := conf.generator(&conf)
|
||||
assert.Equal(t, "", st)
|
||||
assert.ErrorIs(t, err, ErrInvalidHashType)
|
||||
assert.ErrorContains(t, err, "invalid hash type: HashType(0)")
|
||||
}
|
||||
|
||||
func TestHashMissingOriginal(t *testing.T) {
|
||||
conf := NewConfig(WithGenerator(Hash(MD5)))
|
||||
st, err := conf.generator(&conf)
|
||||
assert.Equal(t, "", st)
|
||||
assert.ErrorIs(t, err, ErrMissingOriginal)
|
||||
}
|
||||
|
||||
func TestHashTypeStringer(t *testing.T) {
|
||||
s := fmt.Sprintf("%s", MD5)
|
||||
assert.Equal(t, "MD5", s)
|
||||
}
|
||||
|
|
|
|||
26
hashtype_string.go
Normal file
26
hashtype_string.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// Code generated by "stringer -type=HashType"; DO NOT EDIT.
|
||||
|
||||
package nomino
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[MD5-1]
|
||||
_ = x[SHA1-2]
|
||||
_ = x[SHA256-3]
|
||||
}
|
||||
|
||||
const _HashType_name = "MD5SHA1SHA256"
|
||||
|
||||
var _HashType_index = [...]uint8{0, 3, 7, 13}
|
||||
|
||||
func (i HashType) String() string {
|
||||
i -= 1
|
||||
if i >= HashType(len(_HashType_index)-1) {
|
||||
return "HashType(" + strconv.FormatInt(int64(i+1), 10) + ")"
|
||||
}
|
||||
return _HashType_name[_HashType_index[i]:_HashType_index[i+1]]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue