-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] 1. 增加了事件通知中详细的数据修改部分 2. 修正了preload的namespace也会有事件通知的问题
- Loading branch information
liuxingwang
committed
Jul 12, 2019
1 parent
0a72555
commit f37d9be
Showing
4 changed files
with
135 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package agollo | ||
|
||
type ChangeType string | ||
|
||
const ( | ||
ChangeTypeAdd ChangeType = "add" | ||
ChangeTypeUpdate ChangeType = "update" | ||
ChangeTypeDelete ChangeType = "delete" | ||
) | ||
|
||
type Change struct { | ||
Type ChangeType | ||
Key string | ||
Value interface{} | ||
} | ||
|
||
type Changes []Change | ||
|
||
// Len is part of sort.Interface. | ||
func (cs Changes) Len() int { | ||
return len(cs) | ||
} | ||
|
||
// Swap is part of sort.Interface. | ||
func (cs Changes) Swap(i, j int) { | ||
cs[i], cs[j] = cs[j], cs[i] | ||
} | ||
|
||
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter. | ||
func (cs Changes) Less(i, j int) bool { | ||
return cs[i].Key < cs[j].Key | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package agollo | ||
|
||
import "sort" | ||
|
||
type Configurations map[string]interface{} | ||
|
||
func (old Configurations) Different(new Configurations) Changes { | ||
var changes []Change | ||
for k, newValue := range new { | ||
oldValue, found := old[k] | ||
if found { | ||
if oldValue != newValue { | ||
changes = append(changes, Change{ | ||
Type: ChangeTypeUpdate, | ||
Key: k, | ||
Value: newValue, | ||
}) | ||
} | ||
} else { | ||
changes = append(changes, Change{ | ||
Type: ChangeTypeAdd, | ||
Key: k, | ||
Value: newValue, | ||
}) | ||
} | ||
} | ||
|
||
for k, oldValue := range old { | ||
_, found := new[k] | ||
if !found { | ||
changes = append(changes, Change{ | ||
Type: ChangeTypeDelete, | ||
Key: k, | ||
Value: oldValue, | ||
}) | ||
} | ||
} | ||
|
||
sort.Sort(Changes(changes)) | ||
|
||
return changes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package agollo | ||
|
||
import "testing" | ||
|
||
func TestConfigurationsDifferent(t *testing.T) { | ||
tests := []struct { | ||
old Configurations | ||
new Configurations | ||
changes []Change | ||
}{ | ||
{ | ||
Configurations{ | ||
"name": "foo", | ||
"age": 18, | ||
"balance": 101.2, | ||
}, | ||
|
||
Configurations{ | ||
"name": "foo", | ||
"age": 19, | ||
"height": 1.82, | ||
}, | ||
[]Change{ | ||
Change{ChangeTypeUpdate, "age", 19}, | ||
Change{ChangeTypeDelete, "balance", 101.2}, | ||
Change{ChangeTypeAdd, "height", 1.82}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
changes := test.old.Different(test.new) | ||
|
||
if len(changes) != len(test.changes) { | ||
t.Errorf("should be equal (expected=%v, actual=%v)", test.changes, len(changes)) | ||
} | ||
for i, actual := range changes { | ||
expected := test.changes[i] | ||
if actual.Type != expected.Type && | ||
actual.Key != expected.Key && | ||
actual.Value != expected.Value { | ||
t.Errorf("should be equal (expected=%v, actual=%v)", expected, actual) | ||
} | ||
} | ||
} | ||
} |