Skip to content

Commit

Permalink
feat: rewrite interface{} to any (#1419)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <gaius.qi@gmail.com>
  • Loading branch information
gaius-qi committed Jun 30, 2022
1 parent 6955e7c commit 726b19f
Show file tree
Hide file tree
Showing 89 changed files with 521 additions and 521 deletions.
10 changes: 5 additions & 5 deletions client/clientutil/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (r *RateLimit) UnmarshalYAML(node *yaml.Node) error {
return r.unmarshal(yaml.Unmarshal, []byte(node.Value))
}

func (r *RateLimit) unmarshal(unmarshal func(in []byte, out interface{}) (err error), b []byte) error {
var v interface{}
func (r *RateLimit) unmarshal(unmarshal func(in []byte, out any) (err error), b []byte) error {
var v any
if err := unmarshal(b, &v); err != nil {
return err
}
Expand Down Expand Up @@ -73,15 +73,15 @@ type Duration struct {
}

func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
var v any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
return d.unmarshal(v)
}

func (d *Duration) UnmarshalYAML(node *yaml.Node) error {
var v interface{}
var v any
switch node.Kind {
case yaml.ScalarNode:
switch node.Tag {
Expand All @@ -106,7 +106,7 @@ func (d *Duration) UnmarshalYAML(node *yaml.Node) error {
return d.unmarshal(v)
}

func (d *Duration) unmarshal(v interface{}) error {
func (d *Duration) unmarshal(v any) error {
switch value := v.(type) {
case float64:
d.Duration = time.Duration(value)
Expand Down
2 changes: 1 addition & 1 deletion client/config/dynconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func newManagerClient(client managerclient.Client, hostOption HostOption) intern
}
}

func (mc *managerClient) Get() (interface{}, error) {
func (mc *managerClient) Get() (any, error) {
listSchedulersResp, err := mc.ListSchedulers(&manager.ListSchedulersRequest{
SourceType: manager.SourceType_PEER_SOURCE,
HostName: mc.hostOption.Hostname,
Expand Down
44 changes: 22 additions & 22 deletions client/config/peerhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ type ProxyOption struct {
}

func (p *ProxyOption) UnmarshalJSON(b []byte) error {
var v interface{}
var v any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
Expand All @@ -278,7 +278,7 @@ func (p *ProxyOption) UnmarshalJSON(b []byte) error {
return err
}
return nil
case map[string]interface{}:
case map[string]any:
if err := p.unmarshal(json.Unmarshal, b); err != nil {
return err
}
Expand All @@ -305,11 +305,11 @@ func (p *ProxyOption) UnmarshalYAML(node *yaml.Node) error {
}
return nil
case yaml.MappingNode:
var m = make(map[string]interface{})
var m = make(map[string]any)
for i := 0; i < len(node.Content); i += 2 {
var (
key string
value interface{}
value any
)
if err := node.Content[i].Decode(&key); err != nil {
return err
Expand All @@ -334,7 +334,7 @@ func (p *ProxyOption) UnmarshalYAML(node *yaml.Node) error {
}
}

func (p *ProxyOption) unmarshal(unmarshal func(in []byte, out interface{}) (err error), b []byte) error {
func (p *ProxyOption) unmarshal(unmarshal func(in []byte, out any) (err error), b []byte) error {
pt := struct {
ListenOption `mapstructure:",squash" yaml:",inline"`
BasicAuth *BasicAuth `mapstructure:"basicAuth" yaml:"basicAuth"`
Expand Down Expand Up @@ -412,18 +412,18 @@ type TCPListenPortRange struct {
}

func (t *TCPListenPortRange) UnmarshalJSON(b []byte) error {
var v interface{}
var v any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
return t.unmarshal(v)
}

func (t *TCPListenPortRange) UnmarshalYAML(node *yaml.Node) error {
var v interface{}
var v any
switch node.Kind {
case yaml.MappingNode:
var m = make(map[string]interface{})
var m = make(map[string]any)
for i := 0; i < len(node.Content); i += 2 {
var (
key string
Expand All @@ -448,15 +448,15 @@ func (t *TCPListenPortRange) UnmarshalYAML(node *yaml.Node) error {
return t.unmarshal(v)
}

func (t *TCPListenPortRange) unmarshal(v interface{}) error {
func (t *TCPListenPortRange) unmarshal(v any) error {
switch value := v.(type) {
case int:
t.Start = value
return nil
case float64:
t.Start = int(value)
return nil
case map[string]interface{}:
case map[string]any:
if s, ok := value["start"]; ok {
switch start := s.(type) {
case float64:
Expand Down Expand Up @@ -638,11 +638,11 @@ type URL struct {

// UnmarshalJSON implements json.Unmarshaler.
func (u *URL) UnmarshalJSON(b []byte) error {
return u.unmarshal(func(v interface{}) error { return json.Unmarshal(b, v) })
return u.unmarshal(func(v any) error { return json.Unmarshal(b, v) })
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (u *URL) UnmarshalYAML(unmarshal func(any) error) error {
return u.unmarshal(unmarshal)
}

Expand All @@ -652,11 +652,11 @@ func (u *URL) MarshalJSON() ([]byte, error) {
}

// MarshalYAML implements yaml.Marshaller to print the url.
func (u *URL) MarshalYAML() (interface{}, error) {
func (u *URL) MarshalYAML() (any, error) {
return u.String(), nil
}

func (u *URL) unmarshal(unmarshal func(interface{}) error) error {
func (u *URL) unmarshal(unmarshal func(any) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
Expand All @@ -680,11 +680,11 @@ type CertPool struct {

// UnmarshalJSON implements json.Unmarshaler.
func (cp *CertPool) UnmarshalJSON(b []byte) error {
return cp.unmarshal(func(v interface{}) error { return json.Unmarshal(b, v) })
return cp.unmarshal(func(v any) error { return json.Unmarshal(b, v) })
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (cp *CertPool) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (cp *CertPool) UnmarshalYAML(unmarshal func(any) error) error {
return cp.unmarshal(unmarshal)
}

Expand All @@ -694,11 +694,11 @@ func (cp *CertPool) MarshalJSON() ([]byte, error) {
}

// MarshalYAML implements yaml.Marshaller to print the cert pool.
func (cp *CertPool) MarshalYAML() (interface{}, error) {
func (cp *CertPool) MarshalYAML() (any, error) {
return cp.Files, nil
}

func (cp *CertPool) unmarshal(unmarshal func(interface{}) error) error {
func (cp *CertPool) unmarshal(unmarshal func(any) error) error {
if err := unmarshal(&cp.Files); err != nil {
return err
}
Expand Down Expand Up @@ -776,16 +776,16 @@ func NewRegexp(exp string) (*Regexp, error) {
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (r *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (r *Regexp) UnmarshalYAML(unmarshal func(any) error) error {
return r.unmarshal(unmarshal)
}

// UnmarshalJSON implements json.Unmarshaler.
func (r *Regexp) UnmarshalJSON(b []byte) error {
return r.unmarshal(func(v interface{}) error { return json.Unmarshal(b, v) })
return r.unmarshal(func(v any) error { return json.Unmarshal(b, v) })
}

func (r *Regexp) unmarshal(unmarshal func(interface{}) error) error {
func (r *Regexp) unmarshal(unmarshal func(any) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
Expand All @@ -803,7 +803,7 @@ func (r *Regexp) MarshalJSON() ([]byte, error) {
}

// MarshalYAML implements yaml.Marshaller to print the regexp.
func (r *Regexp) MarshalYAML() (interface{}, error) {
func (r *Regexp) MarshalYAML() (any, error) {
return r.String(), nil
}

Expand Down
2 changes: 1 addition & 1 deletion client/config/peerhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Test_AllUnmarshalYAML(t *testing.T) {
assert := testifyassert.New(t)
var cases = []struct {
text string
target interface{}
target any
}{
{
text: `
Expand Down
2 changes: 1 addition & 1 deletion client/daemon/peer/peertask_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ func (ts *testSpec) runConductorTest(assert *testifyassert.Assertions, require *
assert.True(success, "task should success")

for i := 0; i < 3; i++ {
ptm.runningPeerTasks.Range(func(key, value interface{}) bool {
ptm.runningPeerTasks.Range(func(key, value any) bool {
noRunningTask = false
return false
})
Expand Down
2 changes: 1 addition & 1 deletion client/daemon/peer/peertask_piecetask_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (poller *pieceTaskPoller) getPieceTasksByPeer(
count int
ptc = poller.peerTaskConductor
)
p, _, err := retry.Run(ptc.ctx, func() (interface{}, bool, error) {
p, _, err := retry.Run(ptc.ctx, func() (any, bool, error) {
// GetPieceTasks must be fast, so short time out is okay
ctx, cancel := context.WithTimeout(ptc.ctx, 4*time.Second)
defer cancel()
Expand Down
6 changes: 3 additions & 3 deletions client/daemon/proxy/proxy_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ func getEnv(w http.ResponseWriter, r *http.Request) {
// ensureStringKey recursively ensures all maps in the given interface are string,
// to make the result marshalable by json. This is meant to be used with viper
// settings, so only maps and slices are handled.
func ensureStringKey(obj interface{}) interface{} {
func ensureStringKey(obj any) any {
rt, rv := reflect.TypeOf(obj), reflect.ValueOf(obj)
switch rt.Kind() {
case reflect.Map:
res := make(map[string]interface{})
res := make(map[string]any)
for _, k := range rv.MapKeys() {
res[fmt.Sprintf("%v", k.Interface())] = ensureStringKey(rv.MapIndex(k).Interface())
}
return res
case reflect.Slice:
res := make([]interface{}, rv.Len())
res := make([]any, rv.Len())
for i := 0; i < rv.Len(); i++ {
res[i] = ensureStringKey(rv.Index(i).Interface())
}
Expand Down
6 changes: 3 additions & 3 deletions client/daemon/storage/storage_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ func (s *storageManager) TryGC() (bool, error) {
// FIXME gc subtask
var markedTasks []PeerTaskMetadata
var totalNotMarkedSize int64
s.tasks.Range(func(key, task interface{}) bool {
s.tasks.Range(func(key, task any) bool {
if task.(Reclaimer).CanReclaim() {
task.(Reclaimer).MarkReclaim()
markedTasks = append(markedTasks, key.(PeerTaskMetadata))
Expand Down Expand Up @@ -804,7 +804,7 @@ func (s *storageManager) TryGC() (bool, error) {
}
logger.Infof("quota threshold reached, start gc oldest task, size: %d bytes", bytesExceed)
var tasks []*localTaskStore
s.tasks.Range(func(key, val interface{}) bool {
s.tasks.Range(func(key, val any) bool {
// skip reclaimed task
task, ok := val.(*localTaskStore)
if !ok { // skip subtask
Expand Down Expand Up @@ -911,7 +911,7 @@ func (s *storageManager) CleanUp() {
}

func (s *storageManager) forceGC() (bool, error) {
s.tasks.Range(func(key, task interface{}) bool {
s.tasks.Range(func(key, task any) bool {
meta := key.(PeerTaskMetadata)
err := s.deleteTask(meta)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/dependency/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import (

// InitCobra initializes flags binding and common sub cmds.
// config is a pointer to configuration struct.
func InitCobra(cmd *cobra.Command, useConfigFile bool, config interface{}) {
func InitCobra(cmd *cobra.Command, useConfigFile bool, config any) {
rootName := cmd.Root().Name()
cobra.OnInitialize(func() { initConfig(useConfigFile, rootName, config) })

Expand Down Expand Up @@ -155,7 +155,7 @@ func SetupQuitSignalHandler(handler func()) {
}

// initConfig reads in config file and ENV variables if set.
func initConfig(useConfigFile bool, name string, config interface{}) {
func initConfig(useConfigFile bool, name string, config any) {
// Use config file and read once.
if useConfigFile {
cfgFile := viper.GetString("config")
Expand Down Expand Up @@ -187,7 +187,7 @@ func initConfig(useConfigFile bool, name string, config interface{}) {
}

func initDecoderConfig(dc *mapstructure.DecoderConfig) {
dc.DecodeHook = mapstructure.ComposeDecodeHookFunc(func(from, to reflect.Type, v interface{}) (interface{}, error) {
dc.DecodeHook = mapstructure.ComposeDecodeHookFunc(func(from, to reflect.Type, v any) (any, error) {
switch to {
case reflect.TypeOf(unit.B),
reflect.TypeOf(dfnet.NetAddr{}),
Expand Down
2 changes: 1 addition & 1 deletion internal/dferrors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func New(code base.Code, msg string) *DfError {
}
}

func Newf(code base.Code, format string, a ...interface{}) *DfError {
func Newf(code base.Code, format string, a ...any) *DfError {
return &DfError{
Code: code,
Message: fmt.Sprintf(format, a...),
Expand Down
Loading

0 comments on commit 726b19f

Please sign in to comment.