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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(all): add options mechanism, fix #3 * fix(all): fix errors for correct work, add fields to lazy-update params * fix(callbacks): fix getLastRecord method
- Loading branch information
Showing
4 changed files
with
154 additions
and
31 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
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,13 @@ | ||
package loggable | ||
|
||
type options struct { | ||
lazyUpdate bool | ||
lazyUpdateFields []string | ||
} | ||
|
||
func LazyUpdateOption(fields ...string) func(options *options) { | ||
return func(options *options) { | ||
options.lazyUpdate = true | ||
options.lazyUpdateFields = fields | ||
} | ||
} |
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,93 @@ | ||
package loggable | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
func isEqual(item1, item2 interface{}, except ...string) bool { | ||
except = StringMap(except, ToSnakeCase) | ||
m1, m2 := somethingToMapStringInterface(item1), somethingToMapStringInterface(item2) | ||
if len(m1) != len(m2) { | ||
return false | ||
} | ||
for k, v := range m1 { | ||
if isInStringSlice(ToSnakeCase(k), except) { | ||
continue | ||
} | ||
v2, ok := m2[k] | ||
if !ok || !reflect.DeepEqual(v, v2) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func somethingToMapStringInterface(item interface{}) map[string]interface{} { | ||
if item == nil { | ||
return nil | ||
} | ||
switch raw := item.(type) { | ||
case JSONB: | ||
return somethingToMapStringInterface([]byte(raw)) | ||
case string: | ||
return somethingToMapStringInterface([]byte(raw)) | ||
case []byte: | ||
var m map[string]interface{} | ||
err := json.Unmarshal(raw, &m) | ||
if err != nil { | ||
return nil | ||
} | ||
return m | ||
default: | ||
data, err := json.Marshal(item) | ||
if err != nil { | ||
return nil | ||
} | ||
return somethingToMapStringInterface(data) | ||
} | ||
return nil | ||
} | ||
|
||
func isInStringSlice(what string, where []string) bool { | ||
for i := range where { | ||
if what == where[i] { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
var ToSnakeCase = toSomeCase("_") | ||
|
||
func toSomeCase(sep string) func(string) string { | ||
return func(s string) string { | ||
for i := range s { | ||
if unicode.IsUpper(rune(s[i])) { | ||
if i != 0 { | ||
s = strings.Join([]string{s[:i], ToLowerFirst(s[i:])}, sep) | ||
} else { | ||
s = ToLowerFirst(s) | ||
} | ||
} | ||
} | ||
return s | ||
} | ||
} | ||
|
||
func ToLowerFirst(s string) string { | ||
if len(s) == 0 { | ||
return "" | ||
} | ||
return strings.ToLower(string(s[0])) + s[1:] | ||
} | ||
|
||
func StringMap(strs []string, fn func(string) string) []string { | ||
res := make([]string, len(strs)) | ||
for i := range strs { | ||
res[i] = fn(strs[i]) | ||
} | ||
return res | ||
} |