Compare commits

...

7 commits

Author SHA1 Message Date
90f8d22b23 🔀 Merge branch 'release/0.2.1' into stable 2025-03-14 15:14:43 -05:00
e25c1001a5 📝 Update CHANGELOG 2025-03-14 15:14:23 -05:00
8888ee3855 Add Hash generator 2025-03-14 15:01:39 -05:00
dd531c1f73 ♻️ Fix go vet error 2025-03-14 13:47:44 -05:00
9e84cfe5f9 🛠 Add complexity calc 2025-03-14 11:59:32 -05:00
e5a1002e2e 🛠 Add serve-docs task 2025-03-14 11:51:55 -05:00
3546ab52d1 🔀 Merge tag 'v0.2.0' into develop
🔖 Slugs, and more
2025-03-14 11:26:48 -05:00
8 changed files with 172 additions and 4 deletions

View file

@ -1,7 +1,24 @@
# Changelog # 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 ### [0.2.0] - 2025-03-14
#### Features
- Add `IncrementalFormat`* Generators - Add `IncrementalFormat`* Generators
- Add `Slug`* Generators - Add `Slug`* Generators
- Add `WithOriginalSlug`* Options - Add `WithOriginalSlug`* Options

View file

@ -26,6 +26,7 @@ tasks:
desc: Vet go code desc: Vet go code
sources: sources:
- '**/*.go' - '**/*.go'
deps: [gen]
cmds: cmds:
- go vet ./... - go vet ./...
@ -43,6 +44,26 @@ tasks:
cmds: cmds:
- staticcheck ./... - 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: vuln:
desc: Check for vulnerabilities desc: Check for vulnerabilities
sources: sources:
@ -56,11 +77,12 @@ tasks:
- vet - vet
- critic - critic
- staticcheck - staticcheck
- complex
- vuln - vuln
test: test:
desc: Run unit tests desc: Run unit tests
deps: [fmt, vet] deps: [fmt, vet, gen]
sources: sources:
- '**/*.go' - '**/*.go'
generates: generates:
@ -83,4 +105,9 @@ tasks:
deps: [coverage-report] deps: [coverage-report]
cmds: cmds:
- ip addr list | grep inet - 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

View file

@ -1,8 +1,12 @@
package nomino package nomino
import ( import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"hash"
"strconv" "strconv"
"time" "time"
@ -185,3 +189,44 @@ func SlugWithLang(lang string) Generator {
return slug.MakeLang(name, lang), err 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
}
}

View file

@ -161,3 +161,33 @@ func ExampleSlugWithLang() {
// Output: diese-und-dass.txt // 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
}

View file

@ -2,6 +2,7 @@ package nomino
import ( import (
"errors" "errors"
"fmt"
"testing" "testing"
"time" "time"
@ -120,3 +121,23 @@ func TestSlugRemovesOriginal(t *testing.T) {
assert.Equal(t, "hello-world", st) assert.Equal(t, "hello-world", st)
assert.NoError(t, err) 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
View 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]]
}

View file

@ -12,7 +12,7 @@ func Make(conf Config) (string, error) {
} }
if conf.prefix != "" { if conf.prefix != "" {
conf.prefix = conf.prefix + conf.separator conf.prefix += conf.separator
} }
if conf.original != "" { if conf.original != "" {
conf.original = conf.separator + conf.original conf.original = conf.separator + conf.original

View file

@ -36,7 +36,9 @@ func TestMake(t *testing.T) {
for _, testcase := range testcases { for _, testcase := range testcases {
t.Run(testcase.name, func(sub *testing.T) { t.Run(testcase.name, func(sub *testing.T) {
opts := append(testcase.opts, genOpt) opts := testcase.opts
opts = append(opts, genOpt)
conf := NewConfig(opts...) conf := NewConfig(opts...)
st, err := Make(conf) st, err := Make(conf)
assert.NoError(t, err) assert.NoError(t, err)