Skip to content

Commit b5114c4

Browse files
authored
Merge pull request #5 from panapol-p/fix/repo-name
fix(core): change listener to diff
2 parents c96a033 + 0ac830b commit b5114c4

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# go-slice-struct-listener
22

3-
A Go library to listen slice of struct (when feed new data then output show id was added , id was updated or id was deleted)
3+
A Go library to find diff slice of struct (when feed new data then output show id was added , id was updated or id was deleted)
44
<hr>
55

66

@@ -12,11 +12,11 @@ package main
1212
import (
1313
"fmt"
1414

15-
listener "github.com/panapol-p/go-slice-struct-listener"
15+
diff "github.com/panapol-p/go-slice-struct-diff"
1616
)
1717

1818
type FeedData struct {
19-
ID string `listener:"id"`
19+
ID string `diff:"id"`
2020
Name string
2121
Score float32
2222
}
@@ -27,29 +27,29 @@ func main() {
2727
{ID: "2", Name: "Joe", Score: 92.50},
2828
}
2929

30-
l := listener.NewListener[FeedData]()
30+
d := diff.NewListener[FeedData]()
3131

3232
// set callback func if you need
33-
f := func(e []listener.Events[FeedData]) {
33+
f := func(e []diff.Events[FeedData]) {
3434
fmt.Println("[callback func]", "receive new event!!", e)
3535
}
36-
l.SetCallback(f)
36+
d.SetCallback(f)
3737

38-
events := l.AddNewValue(fs)
38+
events := d.AddNewValue(fs)
3939
fmt.Println(events) // [{1 added {1 Bob 98.5}} {2 added {2 Joe 92.5}}]
4040

4141
fs = []FeedData{
4242
{ID: "1", Name: "Bob", Score: 96.50},
4343
{ID: "2", Name: "Joe", Score: 92.50},
4444
{ID: "3", Name: "Micky", Score: 89.70},
4545
}
46-
events = l.AddNewValue(fs)
46+
events = d.AddNewValue(fs)
4747
fmt.Println(events) // [{1 updated {1 Bob 96.5}} {3 added {3 Micky 89.7}}]
4848

4949
fs = []FeedData{
5050
{ID: "1", Name: "Bob", Score: 96.50},
5151
}
52-
events = l.AddNewValue(fs)
52+
events = d.AddNewValue(fs)
5353
fmt.Println(events) // [{2 deleted { 0}} {3 deleted { 0}}]
5454
}
5555
```

listener.go renamed to diff.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package listener
1+
package diff
22

33
import (
44
"encoding/json"
@@ -7,7 +7,7 @@ import (
77
"sync"
88
)
99

10-
type Listener[T any] struct {
10+
type Diff[T any] struct {
1111
CurrentValue map[string]Value[T]
1212
NewValue map[string]Value[T]
1313
EventCallback func([]Events[T])
@@ -33,23 +33,23 @@ type Events[T any] struct {
3333
Data T
3434
}
3535

36-
func NewListener[T any]() *Listener[T] {
37-
return &Listener[T]{}
36+
func NewDiff[T any]() *Diff[T] {
37+
return &Diff[T]{}
3838
}
3939

40-
func (l *Listener[T]) SetCallback(f func([]Events[T])) {
40+
func (l *Diff[T]) SetCallback(f func([]Events[T])) {
4141
l.EventCallback = f
4242
}
4343

44-
func (l *Listener[T]) AddNewValue(a []T) []Events[T] {
44+
func (l *Diff[T]) AddNewValue(a []T) []Events[T] {
4545
l.mu.Lock()
4646
defer l.mu.Unlock()
4747

4848
l.NewValue = l.convertToMap(a)
4949
return l.compareMap()
5050
}
5151

52-
func (l *Listener[T]) convertToMap(s []T) map[string]Value[T] {
52+
func (l *Diff[T]) convertToMap(s []T) map[string]Value[T] {
5353
m := make(map[string]Value[T])
5454

5555
var uniqueField []string
@@ -59,8 +59,8 @@ func (l *Listener[T]) convertToMap(s []T) map[string]Value[T] {
5959
st := reflect.TypeOf(s[ii])
6060
for i := 0; i < st.NumField(); i++ {
6161
field := st.Field(i)
62-
if listener, ok := field.Tag.Lookup("listener"); ok {
63-
if listener == "id" {
62+
if Diff, ok := field.Tag.Lookup("Diff"); ok {
63+
if Diff == "id" {
6464
uniqueField = append(uniqueField, field.Name)
6565
}
6666
}
@@ -82,7 +82,7 @@ func (l *Listener[T]) convertToMap(s []T) map[string]Value[T] {
8282
return m
8383
}
8484

85-
func (l *Listener[T]) compareMap() []Events[T] {
85+
func (l *Diff[T]) compareMap() []Events[T] {
8686
//clear CurrentEvent
8787
l.CurrentEvent = []Events[T]{}
8888

listener_test.go renamed to diff_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package listener
1+
package diff
22

33
import (
44
"testing"
@@ -7,25 +7,25 @@ import (
77
)
88

99
type S struct {
10-
F0 string `listener:"id"`
10+
F0 string `Diff:"id"`
1111
F1 string
1212
F2 string
1313
}
1414

15-
func TestNewListener(t *testing.T) {
16-
l := NewListener[S]()
15+
func TestNewDiff(t *testing.T) {
16+
l := NewDiff[S]()
1717
assert.NotNil(t, l)
1818
}
1919

20-
func TestListener_SetCallback(t *testing.T) {
21-
l := NewListener[S]()
20+
func TestDiff_SetCallback(t *testing.T) {
21+
l := NewDiff[S]()
2222
f := func(e []Events[S]) {}
2323
l.SetCallback(f)
2424
assert.NotNil(t, l.EventCallback)
2525
}
2626

27-
func TestListener_AddNewValue(t *testing.T) {
28-
l := new(Listener[S])
27+
func TestDiff_AddNewValue(t *testing.T) {
28+
l := new(Diff[S])
2929

3030
s := []S{
3131
{F0: "1", F1: "test2", F2: ""},
@@ -44,8 +44,8 @@ func TestListener_AddNewValue(t *testing.T) {
4444
assert.Equal(t, expected, l.NewValue)
4545
}
4646

47-
func TestListener_convertToMap(t *testing.T) {
48-
l := new(Listener[S])
47+
func TestDiff_convertToMap(t *testing.T) {
48+
l := new(Diff[S])
4949

5050
s := []S{
5151
{F0: "1", F1: "test2", F2: ""},
@@ -64,8 +64,8 @@ func TestListener_convertToMap(t *testing.T) {
6464
assert.Equal(t, expected, actual)
6565
}
6666

67-
func TestListener_compareMap(t *testing.T) {
68-
l := new(Listener[S])
67+
func TestDiff_compareMap(t *testing.T) {
68+
l := new(Diff[S])
6969
l.CurrentValue = map[string]Value[S]{
7070
"2": {"{\"F0\":\"2\"}", S{F0: "2"}},
7171
"3": {"{\"F0\":\"3\"}", S{F0: "3"}},

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/panapol-p/go-slice-struct-listener
1+
module github.com/panapol-p/go-slice-struct-diff
22

33
go 1.19
44

0 commit comments

Comments
 (0)