-
Notifications
You must be signed in to change notification settings - Fork 10
/
logger.go
71 lines (61 loc) · 1.63 KB
/
logger.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package publish
import (
"fmt"
"log"
"os"
"reflect"
"strings"
"github.com/jinzhu/gorm"
)
// LoggerInterface logger interface used to print publish logs
type LoggerInterface interface {
Print(...interface{})
}
// Logger default logger used to print publish logs
var Logger LoggerInterface
func init() {
Logger = log.New(os.Stdout, "\r\n", 0)
}
func stringify(object interface{}) string {
if obj, ok := object.(interface {
Stringify() string
}); ok {
return obj.Stringify()
}
scope := gorm.Scope{Value: object}
for _, column := range []string{"Description", "Name", "Title", "Code"} {
if field, ok := scope.FieldByName(column); ok {
return fmt.Sprintf("%v", field.Field.Interface())
}
}
if scope.PrimaryField() != nil {
if scope.PrimaryKeyZero() {
return ""
}
return fmt.Sprintf("%v#%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue())
}
return fmt.Sprint(reflect.Indirect(reflect.ValueOf(object)).Interface())
}
func stringifyPrimaryValues(primaryValues [][][]interface{}, columns ...string) string {
var values []string
for _, primaryValue := range primaryValues {
var primaryKeys []string
for _, value := range primaryValue {
if len(columns) == 0 {
primaryKeys = append(primaryKeys, fmt.Sprint(value[1]))
} else {
for _, column := range columns {
if column == fmt.Sprint(value[0]) {
primaryKeys = append(primaryKeys, fmt.Sprint(value[1]))
}
}
}
}
if len(primaryKeys) > 1 {
values = append(values, fmt.Sprintf("[%v]", strings.Join(primaryKeys, ", ")))
} else {
values = append(values, primaryKeys...)
}
}
return strings.Join(values, "; ")
}