Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deepsource: RVV-B0011 exported function returning value of unexported type #1848

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ import (
"github.com/vdaas/vald/internal/errors"
)

// Cache represent the cache interface to store cache.
type Cache interface {
Start(context.Context)
Get(string) (interface{}, bool)
Set(string, interface{})
Delete(string)
GetAndDelete(string) (interface{}, bool)
}

type cache struct {
cacher cacher.Type
expireDur time.Duration
Expand All @@ -43,7 +34,7 @@ type cache struct {
}

// New returns the Cache instance or error.
func New(opts ...Option) (cc Cache, err error) {
func New(opts ...Option) (cc cacher.Cache, err error) {
vankichi marked this conversation as resolved.
Show resolved Hide resolved
c := new(cache)
for _, opt := range append(defaultOptions, opts...) {
opt(c)
Expand Down
11 changes: 6 additions & 5 deletions internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"reflect"
"testing"

"github.com/vdaas/vald/internal/cache/cacher"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/test/goleak"
)
Expand All @@ -35,18 +36,18 @@ func TestNew(t *testing.T) {
opts []Option
}
type want struct {
wantCc Cache
wantCc cacher.Cache
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, Cache, error) error
checkFunc func(want, cacher.Cache, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, gotCc Cache, err error) error {
defaultCheckFunc := func(w want, gotCc cacher.Cache, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
Expand All @@ -61,7 +62,7 @@ func TestNew(t *testing.T) {
args: args{
opts: []Option{WithType("gache")},
},
checkFunc: func(w want, got Cache, err error) error {
checkFunc: func(w want, got cacher.Cache, err error) error {
if err != nil {
return err
}
Expand All @@ -86,7 +87,7 @@ func TestNew(t *testing.T) {
args: args{
opts: []Option{WithType("")},
},
checkFunc: func(w want, got Cache, err error) error {
checkFunc: func(w want, got cacher.Cache, err error) error {
if err != nil {
return err
}
Expand Down
15 changes: 14 additions & 1 deletion internal/cache/cacher/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,20 @@
// Package cacher provides implementation of cache type definition
package cacher

import "github.com/vdaas/vald/internal/strings"
import (
"context"

"github.com/vdaas/vald/internal/strings"
)

// Cache represent the cache interface to store cache.
type Cache interface {
Start(context.Context)
Get(string) (interface{}, bool)
Set(string, interface{})
Delete(string)
GetAndDelete(string) (interface{}, bool)
}

// Type represents the cacher type. Currently it support GACHE only.
type Type uint8
Expand Down
5 changes: 3 additions & 2 deletions internal/cache/gache/gache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/kpango/gache"
"github.com/vdaas/vald/internal/cache/cacher"
)

type cache struct {
Expand All @@ -32,8 +33,8 @@ type cache struct {
}

// New loads a cache model and returns a new cache struct.
func New(opts ...Option) (c *cache) {
c = new(cache)
func New(opts ...Option) (cacher.Cache) {
vankichi marked this conversation as resolved.
Show resolved Hide resolved
c := new(cache)
for _, opt := range append(defaultOptions(), opts...) {
opt(c)
}
Expand Down
26 changes: 11 additions & 15 deletions internal/cache/gache/gache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/kpango/gache"
"github.com/vdaas/vald/internal/cache/cacher"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/test/goleak"
)
Expand All @@ -39,29 +39,25 @@ func TestNew(t *testing.T) {
opts []Option
}
type want struct {
wantC *cache
wantC cacher.Cache
}
type test struct {
name string
args args
want want
checkFunc func(want, *cache) error
checkFunc func(want, cacher.Cache) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, gotC *cache) error {
opts := []cmp.Option{
cmp.AllowUnexported(*w.wantC),
cmp.AllowUnexported(*gotC),
cmp.Comparer(func(want, got gache.Gache) bool {
return want != nil && got != nil
}),
cmp.Comparer(func(want, got func(context.Context, string)) bool {
return reflect.ValueOf(want).Pointer() == reflect.ValueOf(got).Pointer()
}),
defaultCheckFunc := func(w want, got cacher.Cache) error {
vankichi marked this conversation as resolved.
Show resolved Hide resolved
wc := reflect.ValueOf(w.wantC.(*cache))
gc := reflect.ValueOf(got.(*cache))
flag := false
for i := 0; i < reflect.Indirect(gc).NumField(); i++ {
flag = reflect.DeepEqual(reflect.Indirect(gc).Field(i), reflect.Indirect(wc).Field(i))
}
if diff := cmp.Diff(w.wantC, gotC, opts...); diff != "" {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotC, w.wantC)
if flag {
return errors.Errorf("got: \"%#v\",\n\t\t\twant: \"%#v\"", got, w.wantC)
}
return nil
}
Expand Down
11 changes: 6 additions & 5 deletions internal/config/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package config

import "github.com/vdaas/vald/internal/strings"

type compressAlgorithm uint8
// CompressAlgorithm is an enum for compress algorithm.
type CompressAlgorithm uint8

const (
// GOB represents gob algorithm.
GOB compressAlgorithm = 1 + iota
GOB CompressAlgorithm = 1 + iota
// GZIP represents gzip algorithm.
GZIP
// LZ4 represents lz4 algorithm.
Expand All @@ -33,7 +34,7 @@ const (
)

// String returns compress algorithm.
func (ca compressAlgorithm) String() string {
func (ca CompressAlgorithm) String() string {
switch ca {
case GOB:
return "gob"
Expand All @@ -47,8 +48,8 @@ func (ca compressAlgorithm) String() string {
return "unknown"
}

// CompressAlgorithm returns compressAlgorithm converted from string.
func CompressAlgorithm(ca string) compressAlgorithm {
// AToCompressAlgorithm returns CompressAlgorithm converted from string.
func AToCompressAlgorithm(ca string) CompressAlgorithm {
switch strings.ToLower(ca) {
case "gob":
return GOB
Expand Down
14 changes: 7 additions & 7 deletions internal/config/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_compressAlgorithm_String(t *testing.T) {
}
type test struct {
name string
ca compressAlgorithm
ca CompressAlgorithm
want want
checkFunc func(want, string) error
beforeFunc func()
Expand Down Expand Up @@ -80,7 +80,7 @@ func Test_compressAlgorithm_String(t *testing.T) {
},
{
name: "return unknown when compressAlgorithm is 100",
ca: compressAlgorithm(100),
ca: CompressAlgorithm(100),
want: want{
want: "unknown",
},
Expand Down Expand Up @@ -110,22 +110,22 @@ func Test_compressAlgorithm_String(t *testing.T) {
}
}

func TestCompressAlgorithm(t *testing.T) {
func TestAToCompressAlgorithm(t *testing.T) {
type args struct {
ca string
}
type want struct {
want compressAlgorithm
want CompressAlgorithm
}
type test struct {
name string
args args
want want
checkFunc func(want, compressAlgorithm) error
checkFunc func(want, CompressAlgorithm) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got compressAlgorithm) error {
defaultCheckFunc := func(w want, got CompressAlgorithm) error {
if !reflect.DeepEqual(got, w.want) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestCompressAlgorithm(t *testing.T) {
checkFunc = defaultCheckFunc
}

got := CompressAlgorithm(test.args.ca)
got := AToCompressAlgorithm(test.args.ca)
if err := checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/log/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/log/format"
"github.com/vdaas/vald/internal/log/level"
log "github.com/vdaas/vald/internal/log/logger"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand All @@ -43,7 +44,7 @@ type logger struct {
}

// New returns a new logger instance.
func New(opts ...Option) (*logger, error) {
func New(opts ...Option) (log.Logger, error) {
vankichi marked this conversation as resolved.
Show resolved Hide resolved
l := new(logger)
for _, opt := range append(defaultOpts, opts...) {
opt(l)
Expand Down
9 changes: 5 additions & 4 deletions internal/log/zap/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/log/format"
"github.com/vdaas/vald/internal/log/level"
log "github.com/vdaas/vald/internal/log/logger"
"github.com/vdaas/vald/internal/test/goleak"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -37,11 +38,11 @@ func TestNew(t *testing.T) {
name string
args args
want want
checkFunc func(want, *logger, error) error
checkFunc func(want, log.Logger, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got *logger, err error) error {
defaultCheckFunc := func(w want, got log.Logger, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
Expand All @@ -58,7 +59,7 @@ func TestNew(t *testing.T) {
name: "return new logger instance correctly",
args: args{
opts: []Option{
func(l *logger) {
func(*logger) {
called = true
},
},
Expand All @@ -67,7 +68,7 @@ func TestNew(t *testing.T) {
want: &logger{},
err: nil,
},
checkFunc: func(w want, got *logger, err error) error {
checkFunc: func(w want, got log.Logger, err error) error {
if !called {
return errors.New("Option function is not applied")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/net/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/vdaas/vald/internal/cache"
"github.com/vdaas/vald/internal/cache/cacher"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/log"
"github.com/vdaas/vald/internal/net/control"
Expand All @@ -43,7 +44,7 @@ type Dialer interface {
}

type dialer struct {
dnsCache cache.Cache
dnsCache cacher.Cache
enableDNSCache bool
dnsCachedOnce sync.Once
tlsConfig *tls.Config
Expand Down
7 changes: 4 additions & 3 deletions internal/net/dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/vdaas/vald/internal/cache"
"github.com/vdaas/vald/internal/cache/cacher"
"github.com/vdaas/vald/internal/cache/gache"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/io"
Expand Down Expand Up @@ -268,7 +269,7 @@ func TestNewDialer(t *testing.T) {
if diff := cmp.Diff(*want, *got,
cmpopts.IgnoreFields(*want, "dialer", "der", "addrs", "dnsCachedOnce", "dnsCache", "ctrl", "tmu"),
cmp.AllowUnexported(*want),
cmp.Comparer(func(x, y cache.Cache) bool {
cmp.Comparer(func(x, y cacher.Cache) bool {
if x == nil && y == nil {
return true
}
Expand Down Expand Up @@ -528,7 +529,7 @@ func Test_dialer_lookup(t *testing.T) {
addr: "addr",
},
opts: []DialerOption{
WithDNSCache(func() cache.Cache {
WithDNSCache(func() cacher.Cache {
g := gache.New()
g.Set("addr", &dialerCache{
ips: []string{"999.999.999.999"},
Expand Down Expand Up @@ -1806,7 +1807,7 @@ func Test_dialer_lookupIPAddrs(t *testing.T) {
host string
}
type fields struct {
dnsCache cache.Cache
dnsCache cacher.Cache
enableDNSCache bool
dnsCachedOnce sync.Once
tlsConfig *tls.Config
Expand Down
4 changes: 2 additions & 2 deletions internal/net/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"crypto/tls"
"time"

"github.com/vdaas/vald/internal/cache"
"github.com/vdaas/vald/internal/cache/cacher"
"github.com/vdaas/vald/internal/net/control"
"github.com/vdaas/vald/internal/timeutil"
)
Expand All @@ -38,7 +38,7 @@ var defaultDialerOptions = []DialerOption{
}

// WithDNSCache returns the functional option to set the cache.
func WithDNSCache(c cache.Cache) DialerOption {
func WithDNSCache(c cacher.Cache) DialerOption {
return func(d *dialer) {
d.dnsCache = c
if d.dnsCache != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/net/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"
"time"

"github.com/vdaas/vald/internal/cache"
"github.com/vdaas/vald/internal/cache/cacher"
"github.com/vdaas/vald/internal/cache/gache"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/net/control"
Expand All @@ -33,7 +33,7 @@ func TestWithDNSCache(t *testing.T) {
t.Parallel()
type T = dialer
type args struct {
c cache.Cache
c cacher.Cache
}
type want struct {
obj *T
Expand Down
Loading