This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathoptions.go
57 lines (50 loc) · 2.06 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package loggable
import (
"reflect"
)
// Option is a generic options pattern.
type Option func(options *options)
type options struct {
lazyUpdate bool
lazyUpdateFields []string
metaTypes map[string]reflect.Type
objectTypes map[string]reflect.Type
computeDiff bool
}
// Option ComputeDiff allows you also write differences between objects on update operations.
// ComputeDiff not reads records from db, it used only as cache on plugin side.
// So it does not track changes outside plugin.
func ComputeDiff() Option {
return func(options *options) {
options.computeDiff = true
}
}
// Option LazyUpdate allows you to skip update operations when nothing was changed.
// Parameter 'fields' is list of sql field names that should be ignored on updates.
func LazyUpdate(fields ...string) Option {
return func(options *options) {
options.lazyUpdate = true
options.lazyUpdateFields = fields
}
}
// RegObjectType maps object to type name, that is used in field Type of ChangeLog struct.
// On read change log operations, if plugin finds registered object type, by its name from db,
// it unmarshal field RawObject to Object field via json.Unmarshal.
//
// To access decoded object, e.g. `ReallyFunnyClient`, use type casting: `changeLog.Object.(ReallyFunnyClient)`.
func RegObjectType(objectType string, objectStruct interface{}) Option {
return func(options *options) {
options.objectTypes[objectType] = reflect.Indirect(reflect.ValueOf(objectStruct)).Type()
}
}
// RegMetaType works like RegObjectType, but for field RawMeta.
// RegMetaType maps object to type name, that is used in field Type of ChangeLog struct.
// On read change log operations, if plugin finds registered object type, by its name from db,
// it unmarshal field RawMeta to Meta field via json.Unmarshal.
//
// To access decoded object, e.g. `MyClientMeta`, use type casting: `changeLog.Meta.(MyClientMeta)`.
func RegMetaType(objectType string, metaType interface{}) Option {
return func(options *options) {
options.metaTypes[objectType] = reflect.Indirect(reflect.ValueOf(metaType)).Type()
}
}