Skip to content

Commit

Permalink
feat: add keybeauty and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bos-hieu committed Aug 19, 2024
1 parent 49b8d82 commit a0497a4
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/testing.yml
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 ./...
63 changes: 63 additions & 0 deletions keybeauty/keybeauty
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()))
}

72 changes: 72 additions & 0 deletions keybeauty/keybeauty_test.go
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

View workflow job for this annotation

GitHub Actions / ubuntu-latest @ Go 1.20

undefined: Beauty

Check failure on line 54 in keybeauty/keybeauty_test.go

View workflow job for this annotation

GitHub Actions / ubuntu-latest @ Go 1.21

undefined: Beauty

Check failure on line 54 in keybeauty/keybeauty_test.go

View workflow job for this annotation

GitHub Actions / ubuntu-latest @ Go 1.22

undefined: Beauty
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.

0 comments on commit a0497a4

Please sign in to comment.