From e5a1002e2e1494b2f4f55e22790e6eb51ec8a3ef Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 14 Mar 2025 11:51:55 -0500 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=9B=A0=20Add=20serve-docs=20task?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Taskfile.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Taskfile.yml b/Taskfile.yml index 025ff4b..0fb25c9 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -83,4 +83,9 @@ tasks: deps: [coverage-report] cmds: - ip addr list | grep inet - - php -S 0.0.0.0:3265 -t build + - php -S 0.0.0.0:3434 -t build + + serve-docs: + desc: Serve the current docs + cmds: + - godoc -http=0.0.0.0:3434 -play From 9e84cfe5f95fca239ffb8d7a622ec427fa63941a Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 14 Mar 2025 11:59:32 -0500 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=9B=A0=20Add=20complexity=20calc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Taskfile.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Taskfile.yml b/Taskfile.yml index 0fb25c9..35792f6 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -43,6 +43,26 @@ tasks: cmds: - staticcheck ./... + cog-complex: + desc: Calculate cognitive complexity + sources: + - '**/*.go' + cmds: + - gocognit -over 5 . + + cyc-complex: + desc: Calculate cyclomatic complexity + sources: + - '**/*.go' + cmds: + - gocyclo -over 5 . + + complex: + desc: Calculate complexities + deps: + - cog-complex + - cyc-complex + vuln: desc: Check for vulnerabilities sources: @@ -56,6 +76,7 @@ tasks: - vet - critic - staticcheck + - complex - vuln test: From dd531c1f737633ebaa80473b6ea33f01de7fa368 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 14 Mar 2025 13:47:44 -0500 Subject: [PATCH 3/5] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Fix=20go=20vet=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- make.go | 2 +- make_test.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/make.go b/make.go index c2c222c..538c7e0 100644 --- a/make.go +++ b/make.go @@ -12,7 +12,7 @@ func Make(conf Config) (string, error) { } if conf.prefix != "" { - conf.prefix = conf.prefix + conf.separator + conf.prefix += conf.separator } if conf.original != "" { conf.original = conf.separator + conf.original diff --git a/make_test.go b/make_test.go index 28096ce..ffdecee 100644 --- a/make_test.go +++ b/make_test.go @@ -36,7 +36,9 @@ func TestMake(t *testing.T) { for _, testcase := range testcases { t.Run(testcase.name, func(sub *testing.T) { - opts := append(testcase.opts, genOpt) + opts := testcase.opts + opts = append(opts, genOpt) + conf := NewConfig(opts...) st, err := Make(conf) assert.NoError(t, err) From 8888ee3855a29428625ba6011fb16d58afedb9ee Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 14 Mar 2025 14:45:55 -0500 Subject: [PATCH 4/5] =?UTF-8?q?=E2=9C=A8=20Add=20Hash=20generator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Taskfile.yml | 3 ++- generators.go | 45 +++++++++++++++++++++++++++++++++++++ generators_examples_test.go | 30 +++++++++++++++++++++++++ generators_test.go | 21 +++++++++++++++++ hashtype_string.go | 26 +++++++++++++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 hashtype_string.go diff --git a/Taskfile.yml b/Taskfile.yml index 35792f6..474a888 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -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: diff --git a/generators.go b/generators.go index 4b98284..8685662 100644 --- a/generators.go +++ b/generators.go @@ -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 + } +} diff --git a/generators_examples_test.go b/generators_examples_test.go index 0275877..11cfae2 100644 --- a/generators_examples_test.go +++ b/generators_examples_test.go @@ -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 +} diff --git a/generators_test.go b/generators_test.go index fb28ce7..c6b3bbe 100644 --- a/generators_test.go +++ b/generators_test.go @@ -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) +} diff --git a/hashtype_string.go b/hashtype_string.go new file mode 100644 index 0000000..16aa752 --- /dev/null +++ b/hashtype_string.go @@ -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]] +} From e25c1001a52701cdf496c3245cd2d0ba484f484b Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 14 Mar 2025 15:14:23 -0500 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=93=9D=20Update=20CHANGELOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1d7c98..c597e7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,24 @@ # Changelog +### [0.2.1] - 2025-03-14 + +#### Features + +- Add Hash Generator + +#### Dev Tooling + +- Added a task to serve docs +- Added tasts to check code complexity + +#### Miscellaneous + +- Fixed some `go vet` complaints + ### [0.2.0] - 2025-03-14 +#### Features + - Add `IncrementalFormat`* Generators - Add `Slug`* Generators - Add `WithOriginalSlug`* Options