Skip to content

Commit

Permalink
refactor(hashing): Change the file path for Hasher (#30)
Browse files Browse the repository at this point in the history
* refactor(hashing): Change the file path for `Hasher`

* refactor(hashing): Change the file path for `Hasher`

* refactor(hashing): Change the file path for `Hasher`
  • Loading branch information
flc1125 authored Nov 14, 2023
1 parent 2f307fe commit 13c3411
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 53 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# CHANGELOG

## v1.1.0 `Doing`
## `TODO`

- [ ] 功能:引入事件驱动
- [ ] 功能:完善缓存库

## v1.0.10 `Doing`
## v1.1.0 - 2023-11-14

- refactor(hashing): Change the file path for `Hasher` by @flc1125 #30
- build(deps): bump github.com/redis/go-redis/v9 from 9.2.1 to 9.3.0 by @dependabot #27
- feat(gorm): Added `LoggerAdapter` by @flc1125 in #28
- build(deps): bump google.golang.org/grpc from 1.58.0 to 1.58.3 by @dependabot #29

14 changes: 0 additions & 14 deletions contract/hashing/hasher.go

This file was deleted.

44 changes: 9 additions & 35 deletions hashing/hasher.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,14 @@
package hashing

import (
"strconv"
type Hasher interface {
// Make a hash value from the given value.
Make(value string) (string, error)

"github.com/go-packagist/go-kratos-components/contract/hashing"
)
// MustMake a hash value from the given value.
// If an error occurs, it will panic.
MustMake(value string) string

type Hash uint

const (
MD5 Hash = 1 + iota
maxHash
)

var hashes = make([]func() hashing.Hasher, maxHash)

func (h Hash) Available() bool {
return h < maxHash && hashes[h] != nil
}

func (h Hash) New() hashing.Hasher {
if h > 0 && h < maxHash {
f := hashes[h]
if f != nil {
return f()
}
}

panic("hashing: requested hash function #" + strconv.Itoa(int(h)) + " is unavailable")
}

func Register(h Hash, f func() hashing.Hasher) {
if h > 0 && h < maxHash {
hashes[h] = f
return
}

panic("hashing: invalid hash")
// Check the given value matches the given hashed value.
// If you Make() is error, it will return false.
Check(value, hashedValue string) bool
}
38 changes: 38 additions & 0 deletions hashing/hashing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package hashing

import (
"strconv"
)

type Hash uint

const (
MD5 Hash = 1 + iota
maxHash
)

var hashes = make([]func() Hasher, maxHash)

func (h Hash) Available() bool {
return h < maxHash && hashes[h] != nil
}

func (h Hash) New() Hasher {
if h > 0 && h < maxHash {
f := hashes[h]
if f != nil {
return f()
}
}

panic("hashing: requested hash function #" + strconv.Itoa(int(h)) + " is unavailable")
}

func Register(h Hash, f func() Hasher) {
if h > 0 && h < maxHash {
hashes[h] = f
return
}

panic("hashing: invalid hash")
}
3 changes: 1 addition & 2 deletions hashing/md5/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"crypto/md5"
"fmt"

hashingContract "github.com/go-packagist/go-kratos-components/contract/hashing"
"github.com/go-packagist/go-kratos-components/hashing"
)

type hasher struct{}

var global *hasher

func New() hashingContract.Hasher {
func New() hashing.Hasher {
if global == nil {
global = &hasher{}
}
Expand Down

0 comments on commit 13c3411

Please sign in to comment.