forked from zileyuan/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
120 lines (105 loc) · 2.26 KB
/
util.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package goflow
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
"github.com/bitly/go-simplejson"
"github.com/satori/go.uuid"
)
//生成UUID
func NewUUID() string {
u, _ := uuid.NewV4()
return u.String()
}
//字符串转整型
func StrToInt(value string) int {
if value == "" {
return 0
}
val, _ := strconv.Atoi(value)
return val
}
//整型转字符串
func IntToStr(value int) string {
return strconv.Itoa(value)
}
//装载XML文件
func LoadXML(xmlFile string) []byte {
content, err := ioutil.ReadFile(xmlFile)
if err != nil {
flowlog.Errorf("error to read xml file %v", err)
panic(fmt.Errorf("error to read xml file!"))
}
return content
}
//map转json
func MapToJson(v map[string]interface{}) string {
if v == nil {
return ""
}
js := simplejson.New()
js.Set("map", v)
ret, _ := js.Get("map").Encode()
return string(ret)
}
//json转map
func JsonToMap(v string) map[string]interface{} {
js, _ := simplejson.NewJson([]byte(v))
return js.MustMap()
}
//删除Slice中的元素
func StringsRemove(strings []string, start, end int) []string {
return append(strings[:start], strings[end:]...)
}
//删除Slice中的元素
func StringsRemoveAtIndex(strings []string, index int) []string {
return StringsRemove(strings, index, index+1)
}
//格式化时间字符串
func FormatTime(t time.Time, layout string) string {
if t.IsZero() {
return ""
} else {
return t.Format(layout)
}
}
func ProcessTime(args map[string]interface{}, timeParam string) time.Time {
if timeParam != "" {
var timeStr string
if timeInf, ok := args[timeParam]; ok {
timeStr = timeInf.(string)
} else {
timeStr = timeParam
}
the_time, err := time.Parse(STD_TIME_LAYOUT, timeStr)
if err == nil {
return the_time
}
}
return time.Time{}
}
//TaskType转换
func ProcessTaskType(taskType TASK_TYPE) TASK_ORDER {
if strings.ToUpper(string(taskType)) == string(TT_ASSIST) {
return TO_ASSIST
} else {
return TO_MAJOR
}
}
//PerformType转换
func ProcessPerformType(performType PERFORM_TYPE) PERFORM_ORDER {
if strings.ToUpper(string(performType)) == string(PT_ALL) {
return PO_ALL
} else {
return PO_ANY
}
}
//error处理
func PanicIf(err error, format string, v ...interface{}) {
if err != nil {
flowlog.Errorf(format+" [ %v ]", v, err)
panic(err)
}
}