Skip to content

Commit

Permalink
Refactor interface to any (#123)
Browse files Browse the repository at this point in the history
* refactor(maps): Refactor interface to any

Signed-off-by: Flc゛ <[email protected]>

* refactor(debug): Refactor interface to any

Signed-off-by: Flc゛ <[email protected]>

* refactor(codec): Refactor interface to any

Signed-off-by: Flc゛ <[email protected]>

* refactor(event): Refactor interface to any

Signed-off-by: Flc゛ <[email protected]>

---------

Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 authored Mar 4, 2024
1 parent ea8de18 commit d5a8290
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package codec

type Codec interface {
// Marshal the given data into bytes.
Marshal(data interface{}) ([]byte, error)
Marshal(data any) ([]byte, error)

// Unmarshal the given bytes into dest.
Unmarshal(src []byte, dest interface{}) error
Unmarshal(src []byte, dest any) error
}
4 changes: 2 additions & 2 deletions codec/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ var Codec codec.Codec = &jsonCodec{}

type jsonCodec struct{}

func (j *jsonCodec) Marshal(data interface{}) ([]byte, error) {
func (j *jsonCodec) Marshal(data any) ([]byte, error) {
return json.Marshal(data)
}

func (j *jsonCodec) Unmarshal(src []byte, dest interface{}) error {
func (j *jsonCodec) Unmarshal(src []byte, dest any) error {
return json.Unmarshal(src, dest)
}
2 changes: 1 addition & 1 deletion codec/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestJSON(t *testing.T) {
assert.Equal(t, bytes1, bytes2)

// unmarshal
dest1, dest2 := make(map[string]interface{}), make(map[string]interface{})
dest1, dest2 := make(map[string]any), make(map[string]any)
assert.NoError(t, json.Unmarshal(bytes1, &dest1))
assert.NoError(t, j1.Unmarshal(bytes1, &dest2))

Expand Down
2 changes: 1 addition & 1 deletion crontab/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (s *Server) Stop(_ context.Context) error {
return nil
}

func (s *Server) log(v ...interface{}) {
func (s *Server) log(v ...any) {
if s.debug {
log.Println(v...)
}
Expand Down
6 changes: 3 additions & 3 deletions debug/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
// Dump dumps the given values.
// It is a wrapper for spew.Dump(https://github.com/davecgh/go-spew)
// This is useful for debugging.
func Dump(v ...interface{}) {
func Dump(v ...any) {
spew.Dump(v...)
}

// Sdump the given values and returns the result.
// It's a wrapper for spew.Sdump(https://github.com/davecgh/go-spew)
// This is useful for debugging.
func Sdump(v ...interface{}) string {
func Sdump(v ...any) string {
return spew.Sdump(v...)
}

// Fdump the given values to the given writer.
// It's a wrapper for spew.Fdump(https://github.com/davecgh/go-spew)
// This is useful for debugging.
func Fdump(w io.Writer, v ...interface{}) {
func Fdump(w io.Writer, v ...any) {
spew.Fdump(w, v...)
}
10 changes: 5 additions & 5 deletions event/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func (e Event) String() string {

type Listener interface {
Listen() []Event
Handle(event Event, data interface{})
Handle(event Event, data any)
}

type RecoveryHandler func(err interface{}, listener Listener, event Event, data interface{})
type RecoveryHandler func(err any, listener Listener, event Event, data any)

type Dispatcher struct {
listeners map[Event][]Listener
Expand All @@ -38,7 +38,7 @@ func WithRecovery(handler RecoveryHandler) Option {
}
}

var DefaultRecovery RecoveryHandler = func(err interface{}, listener Listener, event Event, data interface{}) {
var DefaultRecovery RecoveryHandler = func(err any, listener Listener, event Event, data any) {
log.Errorf("[Event] handler panic listener: %v, event: %s, data: %v, err: %v", listener, event, data, err)
}

Expand Down Expand Up @@ -69,7 +69,7 @@ func (d *Dispatcher) AddListener(listener ...Listener) {
}
}

func (d *Dispatcher) Dispatch(event Event, data interface{}) {
func (d *Dispatcher) Dispatch(event Event, data any) {
d.rw.RLock()
defer d.rw.RUnlock()

Expand All @@ -85,7 +85,7 @@ func (d *Dispatcher) Dispatch(event Event, data interface{}) {
}
}

func (d *Dispatcher) handle(listener Listener, event Event, data interface{}) {
func (d *Dispatcher) handle(listener Listener, event Event, data any) {
if d.recovery != nil {
defer func() {
if err := recover(); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions event/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

type result struct {
event Event
data interface{}
err interface{}
data any
err any
}

var recv = make(chan result, 1)
Expand All @@ -31,7 +31,7 @@ func (l *testListener) Listen() []Event {
}
}

func (l *testListener) Handle(event Event, data interface{}) {
func (l *testListener) Handle(event Event, data any) {
if s, ok := data.(string); ok {
recv <- result{
event: event,
Expand All @@ -50,7 +50,7 @@ func (l *test2Listener) Listen() []Event {
}
}

func (l *test2Listener) Handle(event Event, data interface{}) {
func (l *test2Listener) Handle(event Event, data any) {
recv <- result{
event: event,
data: data,
Expand All @@ -60,7 +60,7 @@ func (l *test2Listener) Handle(event Event, data interface{}) {
func TestDispatcher(t *testing.T) {
var (
d = NewDispatcher(
WithRecovery(func(err interface{}, _ Listener, event Event, data interface{}) {
WithRecovery(func(err any, _ Listener, event Event, data any) {
recv <- result{
event: event,
data: data,
Expand Down
20 changes: 10 additions & 10 deletions maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"fmt"
)

type M map[string]interface{}
type M map[string]any

func (m M) Maps() map[string]interface{} {
func (m M) Maps() map[string]any {
return m
}

func (m M) All() map[string]interface{} {
func (m M) All() map[string]any {
return m.Maps()
}

Expand All @@ -34,20 +34,20 @@ func (m M) Has(k string) bool {
return ok
}

func (m M) Get(k string) (interface{}, bool) {
func (m M) Get(k string) (any, bool) {
v, ok := m[k]
return v, ok
}

func (m M) GetX(k string) interface{} {
func (m M) GetX(k string) any {
if v, ok := m.Get(k); ok {
return v
}

panic(fmt.Sprintf("maps: key %s not exists", k))
}

func (m M) Set(k string, v interface{}) M {
func (m M) Set(k string, v any) M {
m[k] = v
return m
}
Expand All @@ -65,8 +65,8 @@ func (m M) Keys() []string {
return keys
}

func (m M) Values() []interface{} {
values := make([]interface{}, 0, len(m))
func (m M) Values() []any {
values := make([]any, 0, len(m))
for _, v := range m {
values = append(values, v)
}
Expand All @@ -91,7 +91,7 @@ func (m M) Unless(guard bool, fn func(maps M) M) M {
return m
}

func (m M) Map(fn func(k string, v interface{}) (string, interface{})) M {
func (m M) Map(fn func(k string, v any) (string, any)) M {
n := make(M, len(m))
for k, v := range m {
k, v := fn(k, v)
Expand All @@ -100,7 +100,7 @@ func (m M) Map(fn func(k string, v interface{}) (string, interface{})) M {
return n
}

func (m M) Each(fn func(k string, v interface{})) {
func (m M) Each(fn func(k string, v any)) {
for k, v := range m {
fn(k, v)
}
Expand Down

0 comments on commit d5a8290

Please sign in to comment.