Skip to content

Commit

Permalink
add: go generate
Browse files Browse the repository at this point in the history
  • Loading branch information
moocss committed Nov 18, 2023
1 parent 5b9d488 commit 9f912e1
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 11 deletions.
4 changes: 3 additions & 1 deletion basic-layout/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
vendor/
vendor
/Godeps/
/scripts/docker/mysql/data
/scripts/docker/mysql/data
go.work
go.work.sum
8 changes: 1 addition & 7 deletions basic-layout/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,10 @@ check:
# 生成 mock 文件
.PHONY: mock
mock:
@mockgen -source=./internal/service/user.go -package=svcmocks -destination=./internal/service/mocks/user.mock.go
@mockgen -source=./internal/repository/user.go -package=repomocks -destination=./internal/repository/mocks/user.mock.go
@mockgen -source=./internal/repository/dao/user.go -package=daomocks -destination=./internal/repository/dao/mocks/user.mock.go
@mockgen -source=./internal/repo/cache/code.go -package=cachemocks -destination=./internal/repo/cache/mocks/code.mock.go
@mockgen -package=redismocks -destination=./internal/repo/cache/redismocks/cmd.mock.go github.com/redis/go-redis/v9 Cmdable

@go generate -tags=wireinject ./...
@$(MAKE) tidy
@echo "go mock finished"


# 单元测试
.PHONY: test
@go test -race ./..
Expand Down
3 changes: 2 additions & 1 deletion basic-layout/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ require (
github.com/redis/go-redis/v9 v9.3.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
go.uber.org/automaxprocs v1.5.3
go.uber.org/mock v0.3.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gorm.io/driver/mysql v1.5.2
)
Expand Down Expand Up @@ -67,7 +69,6 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
Expand Down
2 changes: 2 additions & 0 deletions basic-layout/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
Expand Down
79 changes: 79 additions & 0 deletions basic-layout/internal/core/repo/cache/mocks/user.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions basic-layout/internal/core/repo/cache/user.go
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
package cache

import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/redis/go-redis/v9"

"basic-layout/internal/core/domain/entity"
)

//go:generate mockgen -source=./user.go -package=cachemocks -destination=mocks/user.mock.go UserCache
type UserCache interface {
Delete(ctx context.Context, id uint64) error
Get(ctx context.Context, id uint64) (entity.User, error)
Set(ctx context.Context, u entity.User) error
}

type RedisUserCache struct {
cmd redis.Cmdable
// 过期时间
expiration time.Duration
}

func NewRedisUserCache(cmd redis.Cmdable) UserCache {
return &RedisUserCache{
cmd: cmd,
expiration: time.Minute * 15,
}
}
func (cache *RedisUserCache) Delete(ctx context.Context, id uint64) error {
return cache.cmd.Del(ctx, cache.key(id)).Err()
}

func (cache *RedisUserCache) Get(ctx context.Context, id uint64) (entity.User, error) {
key := cache.key(id)
data, err := cache.cmd.Get(ctx, key).Result()
if err != nil {
return entity.User{}, err
}
// 反序列化回来
var u entity.User
err = json.Unmarshal([]byte(data), &u)
return u, err
}

func (cache *RedisUserCache) Set(ctx context.Context, u entity.User) error {
data, err := json.Marshal(u)
if err != nil {
return err
}
key := cache.key(u.ID)
return cache.cmd.Set(ctx, key, data, cache.expiration).Err()
}

func (cache *RedisUserCache) key(id uint64) string {
return fmt.Sprintf("user:info:%d", id)
}
51 changes: 51 additions & 0 deletions basic-layout/internal/core/repo/dao/mocks/user.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions basic-layout/internal/core/repo/dao/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
var ErrDataNotFound = gorm.ErrRecordNotFound

// UserDAO ... 数据访问层相关接口定义, 使用 DB 风格的命名
//
//go:generate mockgen -source=./user.go -package=daomocks -destination=mocks/user.mock.go UserDAO
type UserDAO interface {
FindByID(ctx context.Context, id uint64) (*model.User, error)
}
Expand Down
51 changes: 51 additions & 0 deletions basic-layout/internal/core/repo/mocks/user.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions basic-layout/internal/core/repo/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"basic-layout/pkg/logger"
)

//go:generate mockgen -source=./user.go -package=repomocks -destination=mocks/user.mock.go UserRepository
type UserRepository interface {
FindByID(ctx context.Context, id uint64) (*entity.User, error)
}
Expand Down
51 changes: 51 additions & 0 deletions basic-layout/internal/core/svc/mocks/user.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions basic-layout/internal/core/svc/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"basic-layout/pkg/logger"
)

//go:generate mockgen -source=./user.go -package=svcmocks -destination=mocks/user.mock.go UserService
type UserService interface {
GetByID(ctx context.Context, id uint64) (*entity.User, error)
}
Expand Down
4 changes: 3 additions & 1 deletion gaea-layout/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
vendor/
vendor
/Godeps/
/scripts/docker/mysql/data
/scripts/docker/mysql/data
go.work
go.work.sum
4 changes: 3 additions & 1 deletion gaia-layout/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
vendor/
vendor
/Godeps/
/scripts/docker/mysql/data
/scripts/docker/mysql/data
go.work
go.work.sum
Loading

0 comments on commit 9f912e1

Please sign in to comment.