diff --git a/.github/workflows/golang-lint.yml b/.github/workflows/go-lint.yml similarity index 93% rename from .github/workflows/golang-lint.yml rename to .github/workflows/go-lint.yml index b091ab18..faa14770 100644 --- a/.github/workflows/golang-lint.yml +++ b/.github/workflows/go-lint.yml @@ -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: @@ -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 diff --git a/.github/workflows/golang-test.yml b/.github/workflows/go-test.yml similarity index 93% rename from .github/workflows/golang-test.yml rename to .github/workflows/go-test.yml index c65a779b..bb3197ed 100644 --- a/.github/workflows/golang-test.yml +++ b/.github/workflows/go-test.yml @@ -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: @@ -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 diff --git a/.golangci.yml b/.golangci.yml index dade5bd1..11296807 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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" diff --git a/archive/zip/add.go b/archive/zip/add.go index dd531e8b..b57beb0e 100644 --- a/archive/zip/add.go +++ b/archive/zip/add.go @@ -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. diff --git a/archive/zip/zip.go b/archive/zip/zip.go index 02c5434b..b20cd3ae 100644 --- a/archive/zip/zip.go +++ b/archive/zip/zip.go @@ -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 @@ -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 } } @@ -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 { @@ -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) { diff --git a/cache/cache.go b/cache/cache.go index 3e584d9b..86b553d1 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -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() diff --git a/database/sql/queryer.go b/database/sql/queryer.go index 2a96de55..c882ded7 100644 --- a/database/sql/queryer.go +++ b/database/sql/queryer.go @@ -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 @@ -47,7 +50,7 @@ func newDB(db sqlQueryerContext, opts ...NewDBOption) *queryerContext { } for _, opt := range opts { - opt(qc) + opt.apply(qc) } return qc diff --git a/discard/discard.go b/discard/discard.go index f268889e..f74ed962 100644 --- a/discard/discard.go +++ b/discard/discard.go @@ -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 } diff --git a/must/must.go b/must/must.go index c626d2e6..9db8480f 100644 --- a/must/must.go +++ b/must/must.go @@ -6,7 +6,7 @@ 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) } @@ -14,7 +14,7 @@ func One[T any](v T, err error) T { 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) } @@ -22,7 +22,7 @@ func Two[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2) { 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) } @@ -30,7 +30,7 @@ func Three[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3) { 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) } @@ -38,7 +38,7 @@ func Four[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3 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) } diff --git a/net/http/header.go b/net/http/header.go index 28645d86..201151f4 100644 --- a/net/http/header.go +++ b/net/http/header.go @@ -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) diff --git a/net/http/realip.go b/net/http/realip.go index 835570af..10b928a1 100644 --- a/net/http/realip.go +++ b/net/http/realip.go @@ -88,23 +88,32 @@ func ContextWithXRealIP(parent context.Context, xRealIP string) context.Context return context.WithValue(parent, keyXRealIP, xRealIP) } -type xRealIPHandlerConfig struct { - //nolint:revive,stylecheck - set_real_ip_from []*net.IPNet - //nolint:revive,stylecheck - real_ip_header string - //nolint:revive,stylecheck - real_ip_recursive bool - - clientIPAddressHeader string -} +type ( + xRealIPHandlerConfig struct { + //nolint:revive,stylecheck + set_real_ip_from []*net.IPNet + //nolint:revive,stylecheck + real_ip_header string + //nolint:revive,stylecheck + real_ip_recursive bool + + clientIPAddressHeader string + } -type XRealIPHandlerOption interface { - apply(*xRealIPHandlerConfig) + NewXRealIPHandlerOption interface { + apply(*xRealIPHandlerConfig) + } + + newXRealIPHandlerOptionClientIPAddressHeader string +) + +func (f newXRealIPHandlerOptionClientIPAddressHeader) apply(cfg *xRealIPHandlerConfig) { + cfg.clientIPAddressHeader = string(f) } -type xRealIPHandlerOption func(h *xRealIPHandlerConfig) -func (f xRealIPHandlerOption) apply(h *xRealIPHandlerConfig) { f(h) } +func WithNewXRealIPHandlerOptionClientIPAddressHeader(header string) NewXRealIPHandlerOption { //nolint:ireturn + return newXRealIPHandlerOptionClientIPAddressHeader(header) +} // NewXRealIPHandler returns realip handler that appends X-Real-IP header. // If set_real_ip_from is X-Forwarded-For and it has below values: @@ -124,7 +133,7 @@ func (f xRealIPHandlerOption) apply(h *xRealIPHandlerConfig) { f(h) } // ) // //nolint:revive,stylecheck -func NewXRealIPHandler(set_real_ip_from []*net.IPNet, real_ip_header string, real_ip_recursive bool, opts ...XRealIPHandlerOption) func(next http.Handler) http.Handler { +func NewXRealIPHandler(set_real_ip_from []*net.IPNet, real_ip_header string, real_ip_recursive bool, opts ...NewXRealIPHandlerOption) func(next http.Handler) http.Handler { c := &xRealIPHandlerConfig{ set_real_ip_from: set_real_ip_from, real_ip_header: real_ip_header, @@ -147,12 +156,6 @@ func NewXRealIPHandler(set_real_ip_from []*net.IPNet, real_ip_header string, rea } } -func WithClientIPAddressHeader(header string) XRealIPHandlerOption { - return xRealIPHandlerOption(func(h *xRealIPHandlerConfig) { - h.clientIPAddressHeader = header - }) -} - func RemoteIP(r *http.Request) string { ip, _, _ := net.SplitHostPort(r.RemoteAddr) return ip diff --git a/net/http/realip_test.go b/net/http/realip_test.go index 5b72bdb2..b1c300c5 100644 --- a/net/http/realip_test.go +++ b/net/http/realip_test.go @@ -37,7 +37,7 @@ func TestNewXRealIPHandler(t *testing.T) { httpz.DefaultSetRealIPFrom(), httpz.HeaderXForwardedFor, true, - httpz.WithClientIPAddressHeader(header), + httpz.WithNewXRealIPHandlerOptionClientIPAddressHeader(header), ) r := httptest.NewRequest(http.MethodPost, "http://util.go/net/httpz", bytes.NewBufferString("test_request_body")) diff --git a/slices/slices.go b/slices/slices.go index 787b5b09..673274d7 100644 --- a/slices/slices.go +++ b/slices/slices.go @@ -111,7 +111,7 @@ func Select[Source, Selected any](s []Source, generator func(index int, source S return gen } -func First[T any](s []T) T { +func First[T any](s []T) T { //nolint:ireturn if len(s) == 0 { var zero T return zero @@ -119,7 +119,7 @@ func First[T any](s []T) T { return s[0] } -func Last[T any](s []T) T { +func Last[T any](s []T) T { //nolint:ireturn if len(s) == 0 { var zero T return zero