-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Run Testing | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
# Label of the container job | ||
runner-job: | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
go: ["1.20", 1.21, 1.22] | ||
name: ${{ matrix.os }} @ Go ${{ matrix.go }} | ||
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- name: Set up Go ${{ matrix.go }} | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: ${{ matrix.go }} | ||
|
||
- name: Checkout Code | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ github.ref }} | ||
|
||
- name: Run Tests | ||
run: | | ||
go test -v -covermode=atomic -coverprofile=coverage.txt ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package keybeauty | ||
|
||
import ( | ||
"bytes" | ||
"crypto/md5" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type key struct { | ||
sb strings.Builder | ||
} | ||
|
||
// Beauty beauty key to format <prefix_uuid_md5ValueOfParams> | ||
// Example: Beauty("aa").WithUUID(123).WithParams("code") | ||
func Beauty(prefix string) *key { | ||
k := &key{} | ||
k.sb.WriteString(prefix) | ||
return k | ||
} | ||
|
||
func (k *key) String() string { | ||
return k.sb.String() | ||
} | ||
|
||
// WithUUID concat uuid to key | ||
func (k *key) WithUUID(uuid any) *key { | ||
k.sb.WriteRune(concatStr) | ||
|
||
uuidStr := fmt.Sprint(uuid) | ||
k.sb.WriteString(uuidStr) | ||
return k | ||
} | ||
|
||
// WithParams concat params to key | ||
func (k *key) WithParams(params ...interface{}) *key { | ||
for _, item := range params { | ||
k.sb.WriteRune(concatStr) | ||
k.sb.WriteString(fmt.Sprintf("%v", item)) | ||
} | ||
|
||
return k | ||
} | ||
|
||
// WithParamsHash md5 params and concat to key | ||
func (k *key) WithParamsHash(params ...interface{}) *key{ | ||
k.sb.WriteRune(concatStr) | ||
k.sb.WriteString(md5Params(params...)) | ||
return k | ||
} | ||
|
||
func md5Params(params ...interface{}) string { | ||
if len(params) == 0 { | ||
return "" | ||
} | ||
|
||
var buffer bytes.Buffer | ||
for _, v := range params { | ||
buffer.WriteString(fmt.Sprintf("%v", v)) | ||
} | ||
return fmt.Sprintf("%x", md5.Sum(buffer.Bytes())) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package keybeauty | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestBeauty(t *testing.T) { | ||
type args struct { | ||
prefix string | ||
uuid int | ||
params []interface{} | ||
paramsHash []interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "No uuid", | ||
args: args{ | ||
prefix: "ahihi", | ||
}, | ||
want: "ahihi", | ||
}, | ||
{ | ||
name: "With uuid", | ||
args: args{ | ||
prefix: "ahihi", | ||
uuid: 12, | ||
}, | ||
want: "ahihi_12", | ||
}, | ||
{ | ||
name: "With params", | ||
args: args{ | ||
prefix: "ahihi", | ||
params: []interface{}{12}, | ||
}, | ||
want: "ahihi_12", | ||
}, | ||
{ | ||
name: "With params hash", | ||
args: args{ | ||
prefix: "ahihi", | ||
paramsHash: []interface{}{12}, | ||
}, | ||
want: "ahihi_c20ad4d76fe97759aa27a0c99bff6710", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := Beauty(tt.args.prefix) | ||
Check failure on line 54 in keybeauty/keybeauty_test.go GitHub Actions / ubuntu-latest @ Go 1.20
Check failure on line 54 in keybeauty/keybeauty_test.go GitHub Actions / ubuntu-latest @ Go 1.21
|
||
if tt.args.uuid > 0 { | ||
got = got.WithUUID(tt.args.uuid) | ||
} | ||
|
||
if len(tt.args.params) > 0 { | ||
got = got.WithParams(tt.args.params...) | ||
} | ||
|
||
if len(tt.args.paramsHash) > 0 { | ||
got = got.WithParamsHash(tt.args.paramsHash...) | ||
} | ||
|
||
if !reflect.DeepEqual(got.String(), tt.want) { | ||
t.Errorf("Beauty() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.