Skip to content

Commit

Permalink
BREAKING CHANGE: fix functional options for test (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Aug 27, 2023
2 parents dc5cbab + f96c570 commit 107128e
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: golang-lint
# ~~~~~~~~~~~
# https://github.com/kunitsucom/util.go/workflows/golang-lint/badge.svg
# ~~~~~~~~~~~
name: go-lint
# ~~~~~~~
# https://github.com/kunitsucom/util.go/workflows/go-lint/badge.svg
# ~~~~~~~

on:
push:
Expand Down Expand Up @@ -40,7 +40,7 @@ defaults:
shell: bash

jobs:
golang-lint: # NOTE: for Branch protection rule `Status checks that are required.`
go-lint: # NOTE: for Branch protection rule `Status checks that are required.`
runs-on: ubuntu-latest # ref. https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
steps:
- uses: actions/checkout@v3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: golang-test
# ~~~~~~~~~~~
# https://github.com/kunitsucom/util.go/workflows/golang-test/badge.svg
# ~~~~~~~~~~~
name: go-test
# ~~~~~~~
# https://github.com/kunitsucom/util.go/workflows/go-test/badge.svg
# ~~~~~~~

on:
push:
Expand Down Expand Up @@ -40,7 +40,7 @@ defaults:
shell: bash

jobs:
golang-test: # NOTE: for Branch protection rule `Status checks that are required.`
go-test: # NOTE: for Branch protection rule `Status checks that are required.`
runs-on: ubuntu-latest # ref. https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
steps:
- uses: actions/checkout@v3
Expand Down
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ linters:
- ifshort # for readability
- interfacer # deprecated https://github.com/mvdan/interfacer
- interfacebloat # unnecessary
- ireturn # TODO: enable
- lll # unnecessary
- maligned # deprecated https://github.com/mdempsky/maligned
- nlreturn # ignore "return with no blank line before"
Expand Down
17 changes: 9 additions & 8 deletions archive/zip/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import (
)

type (
AddFileToZipOption interface{ apply(*addFileToZipConfig) }
addFileToZipOptionFunc func(*addFileToZipConfig)
)
AddFileToZipOption interface{ apply(*addFileToZipConfig) }

func (f addFileToZipOptionFunc) apply(cfg *addFileToZipConfig) { f(cfg) }
addFileToZipDecompressionBombLimit int64
)

type addFileToZipConfig struct {
decompressionBombLimit int64
}

func AddFileToZipWithDecompressionBombLimit(decompressionBombLimit int64) AddFileToZipOption {
return addFileToZipOptionFunc(func(cfg *addFileToZipConfig) {
cfg.decompressionBombLimit = decompressionBombLimit
})
func (f addFileToZipDecompressionBombLimit) apply(cfg *addFileToZipConfig) {
cfg.decompressionBombLimit = int64(f)
}

func WithAddFileToZipDecompressionBombLimit(decompressionBombLimit int64) AddFileToZipOption { //nolint:ireturn
return addFileToZipDecompressionBombLimit(decompressionBombLimit)
}

// AddFileToZip is a function to add a file to an existing zip file.
Expand Down
67 changes: 37 additions & 30 deletions archive/zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,30 @@ import (

var ErrDoNotUnzipAFileAtRiskOfZipSlip = errors.New("zipz: do not unzip a file at risk of zip slip")

type zipDirConfig struct {
walkHandler func(path string, info os.FileInfo, err error) error
pathInZipHandler func(path string) string
}

type ZipDirOption interface{ apply(*zipDirConfig) }
type (
zipDirConfig struct {
walkFunc func(path string, info os.FileInfo, err error) error
pathInZipHandlerFunc func(path string) string
}

type zipDirOptionFunc func(*zipDirConfig)
ZipDirOption interface{ apply(*zipDirConfig) }

func (f zipDirOptionFunc) apply(cfg *zipDirConfig) { f(cfg) }
zipDirOptionWalkFunc struct {
f func(path string, info os.FileInfo, err error) error
}
zipDirOptionPathInZipHandlerFunc struct {
f func(path string) string
}
)

func ZipDirWithWalkHandler(f func(path string, info os.FileInfo, err error) error) ZipDirOption {
return zipDirOptionFunc(func(cfg *zipDirConfig) {
cfg.walkHandler = f
})
func (f zipDirOptionWalkFunc) apply(cfg *zipDirConfig) { cfg.walkFunc = f.f }
func WithZipDirOptionWalkFunc(f func(path string, info os.FileInfo, err error) error) ZipDirOption { //nolint:ireturn
return zipDirOptionWalkFunc{f}
}

func ZipDirWithPathInZipHandler(f func(path string) string) ZipDirOption {
return zipDirOptionFunc(func(cfg *zipDirConfig) {
cfg.pathInZipHandler = f
})
func (f zipDirOptionPathInZipHandlerFunc) apply(cfg *zipDirConfig) { cfg.pathInZipHandlerFunc = f.f }
func WithZipDirOptionPathInZipHandlerFunc(f func(path string) string) ZipDirOption { //nolint:ireturn
return zipDirOptionPathInZipHandlerFunc{f}
}

//nolint:cyclop
Expand All @@ -48,8 +51,8 @@ func ZipDir(dst io.Writer, srcDir string, opts ...ZipDirOption) error {
defer zipWriter.Close()

if err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if cfg.walkHandler != nil {
if err := cfg.walkHandler(path, info, err); err != nil {
if cfg.walkFunc != nil {
if err := cfg.walkFunc(path, info, err); err != nil {
return err
}
}
Expand All @@ -67,8 +70,8 @@ func ZipDir(dst io.Writer, srcDir string, opts ...ZipDirOption) error {
defer file.Close()

pathInZip := cleaned
if cfg.pathInZipHandler != nil {
pathInZip = cfg.pathInZipHandler(pathInZip)
if cfg.pathInZipHandlerFunc != nil {
pathInZip = cfg.pathInZipHandlerFunc(pathInZip)
}
f, err := zipWriter.Create(pathInZip)
if err != nil {
Expand All @@ -87,20 +90,24 @@ func ZipDir(dst io.Writer, srcDir string, opts ...ZipDirOption) error {
return nil
}

type unzipFileConfig struct {
unzipFileFileInZipHandler func(zipfile *zip.File, dstDir string) error
}
type (
unzipFileConfig struct {
unzipFileFileInZipHandler func(zipfile *zip.File, dstDir string) error
}

type UnzipFileOption interface{ apply(*unzipFileConfig) }
UnzipFileOption interface{ apply(*unzipFileConfig) }

type unzipFileOptionFunc func(*unzipFileConfig)
unzipFileOptionFileInZipHandler struct {
f func(zipfile *zip.File, dstDir string) error
}
)

func (f unzipFileOptionFunc) apply(cfg *unzipFileConfig) { f(cfg) }
func (f unzipFileOptionFileInZipHandler) apply(cfg *unzipFileConfig) {
cfg.unzipFileFileInZipHandler = f.f
}

func UnzipFileWithFileInZipHandler(f func(zipfile *zip.File, dstDir string) error) UnzipFileOption {
return unzipFileOptionFunc(func(cfg *unzipFileConfig) {
cfg.unzipFileFileInZipHandler = f
})
func WithUnzipFileOptionFileInZipHandler(f func(zipfile *zip.File, dstDir string) error) UnzipFileOption { //nolint:ireturn
return unzipFileOptionFileInZipHandler{f}
}

func UnzipFile(srcZipFilePath, dstDir string, opts ...UnzipFileOption) (paths []string, err error) {
Expand Down
6 changes: 3 additions & 3 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ func (s *Store[T]) StopRefresher() {

// GetOrSet gets cache value T, or set the value T that returns getValue.
// If getValue does not return err, cache the value T.
func (s *Store[T]) GetOrSet(key string, getValue func() (T, error)) (T, error) {
func (s *Store[T]) GetOrSet(key string, getValue func() (T, error)) (T, error) { //nolint:ireturn
return s.GetOrSetWithTTL(key, getValue, s.defaultTTL)
}

// GetOrSet gets cache value T, or set the value T that returns getValue with TTL.
// If getValue does not return err, cache the value T.
func (s *Store[T]) GetOrSetWithTTL(key string, getValue func() (T, error), ttl time.Duration) (T, error) {
func (s *Store[T]) GetOrSetWithTTL(key string, getValue func() (T, error), ttl time.Duration) (T, error) { //nolint:ireturn
return s.getOrSet(key, getValue, ttl, time.Now())
}

func (s *Store[T]) getOrSet(key string, getValue func() (T, error), ttl time.Duration, now time.Time) (T, error) {
func (s *Store[T]) getOrSet(key string, getValue func() (T, error), ttl time.Duration, now time.Time) (T, error) { //nolint:ireturn
s.mu.Lock()
defer s.mu.Unlock()

Expand Down
23 changes: 13 additions & 10 deletions database/sql/queryer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ type QueryerContext interface {
QueryRowContext(ctx context.Context, dst interface{}, query string, args ...interface{}) error
}

type queryerContext struct {
sqlQueryer sqlQueryerContext
type (
queryerContext struct {
sqlQueryer sqlQueryerContext
// Options
structTag string
}

structTag string
}
NewDBOption interface{ apply(*queryerContext) }

type NewDBOption func(qc *queryerContext)
newDBOptionStructTag string
)

func WithNewDBOptionStructTag(structTag string) NewDBOption {
return func(qc *queryerContext) {
qc.structTag = structTag
}
func (f newDBOptionStructTag) apply(qc *queryerContext) { qc.structTag = string(f) }
func WithNewDBOptionStructTag(structTag string) NewDBOption { //nolint:ireturn
return newDBOptionStructTag(structTag)
}

func NewDB(db sqlQueryerContext, opts ...NewDBOption) QueryerContext { //nolint:ireturn
Expand All @@ -47,7 +50,7 @@ func newDB(db sqlQueryerContext, opts ...NewDBOption) *queryerContext {
}

for _, opt := range opts {
opt(qc)
opt.apply(qc)
}

return qc
Expand Down
10 changes: 5 additions & 5 deletions discard/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ func Discard(discard any) {
_ = discard
}

func One[T any](v1 T, _ any) T {
func One[T any](v1 T, _ any) T { //nolint:ireturn
return v1
}

func Two[T1, T2 any](v1 T1, v2 T2, _ any) (T1, T2) {
func Two[T1, T2 any](v1 T1, v2 T2, _ any) (T1, T2) { //nolint:ireturn
return v1, v2
}

func Three[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, _ any) (T1, T2, T3) {
func Three[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, _ any) (T1, T2, T3) { //nolint:ireturn
return v1, v2, v3
}

func Four[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, _ any) (T1, T2, T3, T4) {
func Four[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, _ any) (T1, T2, T3, T4) { //nolint:ireturn
return v1, v2, v3, v4
}

func Five[T1, T2, T3, T4, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, _ any) (T1, T2, T3, T4, T5) {
func Five[T1, T2, T3, T4, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, _ any) (T1, T2, T3, T4, T5) { //nolint:ireturn
return v1, v2, v3, v4, v5
}
10 changes: 5 additions & 5 deletions must/must.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ func Must(err error) {
}
}

func One[T any](v T, err error) T {
func One[T any](v T, err error) T { //nolint:ireturn
if err != nil {
panic(err)
}

return v
}

func Two[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2) {
func Two[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2) { //nolint:ireturn
if err != nil {
panic(err)
}

return v1, v2
}

func Three[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3) {
func Three[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3) { //nolint:ireturn
if err != nil {
panic(err)
}

return v1, v2, v3
}

func Four[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4) {
func Four[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4) { //nolint:ireturn
if err != nil {
panic(err)
}

return v1, v2, v3, v4
}

func Five[T1, T2, T3, T4, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) (T1, T2, T3, T4, T5) {
func Five[T1, T2, T3, T4, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) (T1, T2, T3, T4, T5) { //nolint:ireturn
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions net/http/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ type headerBuilder struct {
header http.Header
}

func NewHeaderBuilder() HeaderBuilder {
func NewHeaderBuilder() HeaderBuilder { //nolint:ireturn
return &headerBuilder{
header: make(http.Header),
}
}

func (h *headerBuilder) Add(key, value string) HeaderBuilder {
func (h *headerBuilder) Add(key, value string) HeaderBuilder { //nolint:ireturn
h.header.Add(key, value)
return h
}

func (h *headerBuilder) Set(key, value string) HeaderBuilder {
func (h *headerBuilder) Set(key, value string) HeaderBuilder { //nolint:ireturn
h.header.Set(key, value)
return h
}

func (h *headerBuilder) Merge(header http.Header) HeaderBuilder {
func (h *headerBuilder) Merge(header http.Header) HeaderBuilder { //nolint:ireturn
for key, values := range header {
for _, value := range values {
h.Add(key, value)
Expand Down
Loading

0 comments on commit 107128e

Please sign in to comment.