Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["common.go"],
importpath = "go-common/app/admin/main/aegis/model/common",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["common_test.go"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)

View File

@@ -0,0 +1,203 @@
package common
import (
"database/sql/driver"
"fmt"
"regexp"
"strconv"
"time"
)
// TimeFormat time format
var TimeFormat = "2006-01-02 15:04:05"
//GrayField gray config for each business
type GrayField struct {
Name string
Value string
}
// Pager .
type Pager struct {
Total int `json:"total" reflect:"ignore"`
Pn int `form:"pn" default:"1" json:"pn" reflect:"ignore"`
Ps int `form:"ps" default:"20" json:"ps" reflect:"ignore"`
}
// BaseOptions 公共参数
type BaseOptions struct {
BusinessID int64 `form:"business_id" json:"business_id"`
NetID int64 `form:"net_id" json:"net_id"`
FlowID int64 `form:"flow_id" json:"flow_id"`
UID int64 `form:"uid" json:"uid" submit:"int"`
OID string `form:"oid" json:"oid" submit:"string"`
RID int64 `form:"rid" json:"rid"`
Role int8 `form:"role" json:"role"`
Debug int8 `form:"debug" json:"debug"`
Uname string `form:"uname" json:"uname" submit:"string"`
}
// FormatTime .
type FormatTime string
// Scan .
func (f *FormatTime) Scan(src interface{}) (err error) {
switch sc := src.(type) {
case time.Time:
*f = FormatTime(sc.Format("2006-01-02 15:04:05"))
case string:
*f = FormatTime(sc)
}
return
}
// WaitTime 计算等待时长
func WaitTime(ctime time.Time) string {
wt := time.Since(ctime)
h := int(wt.Hours())
m := int(wt.Minutes()) % 60
s := int(wt.Seconds()) % 60
return fmt.Sprintf("%.2d:%.2d:%.2d", h, m, s)
}
//ParseWaitTime 。
func ParseWaitTime(ut int64) string {
h := ut / 3600
m := ut % 3600 / 60
s := ut % 60
return fmt.Sprintf("%.2d:%.2d:%.2d", h, m, s)
}
// Group .
type Group struct {
ID int64 `json:"group_id"`
Name string `json:"group_name"`
Note string `json:"group_note"`
Tag string `json:"group_tag"`
FontColor string `json:"font_color"`
BgColor string `json:"bg_color"`
}
// IntTime .
type IntTime int64
// Scan scan time.
func (jt *IntTime) Scan(src interface{}) (err error) {
switch sc := src.(type) {
case time.Time:
*jt = IntTime(sc.Unix())
case string:
var i int64
i, err = strconv.ParseInt(sc, 10, 64)
*jt = IntTime(i)
}
return
}
// Value get time value.
func (jt IntTime) Value() (driver.Value, error) {
return time.Unix(int64(jt), 0), nil
}
// Time get time.
func (jt IntTime) Time() time.Time {
return time.Unix(int64(jt), 0)
}
// UnmarshalJSON implement Unmarshaler
func (jt *IntTime) UnmarshalJSON(data []byte) error {
if data == nil || len(data) <= 1 {
*jt = 0
return nil
}
if data[0] != '"' {
// 1.直接判断数字
sti, err := strconv.Atoi(string(data))
if err == nil {
*jt = IntTime(sti)
}
return nil
}
str := string(data[1 : len(data)-1])
// 2.标准格式判断
st, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
if err == nil {
*jt = IntTime(st.Unix())
return nil
}
*jt = IntTime(0)
return nil
}
// FilterName .
func FilterName(s string) (res string) {
exp := "[^a-zA-Z0-9_]+"
reg, err := regexp.Compile(exp)
if err != nil {
res = s
return
}
res = reg.ReplaceAllString(s, "")
return
}
// FilterChname .
func FilterChname(s string) (res string) {
exp := "[^0-9_\u4e00-\u9fa5]+"
reg, err := regexp.Compile(exp)
if err != nil {
res = s
return
}
res = reg.ReplaceAllString(s, "")
return
}
// FilterBusinessName .
func FilterBusinessName(s string) (res string) {
exp := "[^a-zA-Z\u4e00-\u9fa5]+"
reg, err := regexp.Compile(exp)
if err != nil {
res = s
return
}
res = reg.ReplaceAllString(s, "")
return
}
//Unique remove duplicated value from slice
func Unique(ids []int64, gthan0 bool) (res []int64) {
res = []int64{}
mm := map[int64]int64{}
for _, id := range ids {
if mm[id] == id || (gthan0 && id <= 0) {
continue
}
res = append(res, id)
mm[id] = id
}
return
}
//CopyMap copy src to dest
func CopyMap(src, dest map[int64][]int64, gthan0 bool) (res map[int64][]int64) {
if dest == nil {
dest = map[int64][]int64{}
}
for k, v := range src {
dest[k] = append(dest[k], v...)
dest[k] = Unique(dest[k], gthan0)
}
res = dest
return
}

View File

@@ -0,0 +1,97 @@
package common
import (
"github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
func TestFormatTime_Scan(t *testing.T) {
convey.Convey("FormatTime_Scan", t, func(ctx convey.C) {
a := FormatTime("2018-01-01 01:00:00")
b := time.Time{}
err := a.Scan(b)
ctx.Convey("FormatTime_Scan", func(ctx convey.C) {
convey.So(err, convey.ShouldBeNil)
})
})
}
func TestWaitTime(t *testing.T) {
convey.Convey("WaitTime", t, func(ctx convey.C) {
b := time.Time{}
WaitTime(b)
})
}
func TestParseWaitTime(t *testing.T) {
convey.Convey("ParseWaitTime", t, func(ctx convey.C) {
b := time.Time{}.Unix()
ParseWaitTime(b)
})
}
var s = IntTime(time.Now().Unix())
func TestIntTime_Scan(t *testing.T) {
convey.Convey("IntTime_Scan", t, func(ctx convey.C) {
b := time.Time{}
err := s.Scan(&b)
ctx.Convey("IntTime_Scan", func(ctx convey.C) {
convey.So(err, convey.ShouldBeNil)
})
})
}
func TestIntTime_Value(t *testing.T) {
convey.Convey("IntTime_Value", t, func(ctx convey.C) {
s.Value()
})
}
func TestIntTime_UnmarshalJSON(t *testing.T) {
convey.Convey("IntTime_UnmarshalJSON", t, func(ctx convey.C) {
err := s.UnmarshalJSON(nil)
ctx.Convey("IntTime_UnmarshalJSON", func(ctx convey.C) {
convey.So(err, convey.ShouldBeNil)
})
})
}
func TestFilterName(t *testing.T) {
convey.Convey("FilterName", t, func(ctx convey.C) {
a := FilterName("hahah一个")
convey.So(a, convey.ShouldEqual, "hahah")
})
}
func TestFilterChname(t *testing.T) {
convey.Convey("FilterChname", t, func(ctx convey.C) {
a := FilterChname("hahah一个")
convey.So(a, convey.ShouldEqual, "一个")
})
}
func TestFilterBusinessName(t *testing.T) {
convey.Convey("FilterBusinessName", t, func(ctx convey.C) {
a := FilterBusinessName("hahah一个")
convey.So(a, convey.ShouldEqual, "hahah一个")
})
}
func TestUnique(t *testing.T) {
convey.Convey("Unique", t, func(ctx convey.C) {
b := []int64{1, -1, 0, 1, 1}
a := Unique(b, true)
convey.So(len(a), convey.ShouldEqual, 1)
convey.So(a[0], convey.ShouldEqual, 1)
})
}
func TestCopyMap(t *testing.T) {
convey.Convey("CopyMap", t, func(ctx convey.C) {
b := map[int64][]int64{}
a := CopyMap(map[int64][]int64{1: []int64{1}}, b, true)
convey.So(len(a), convey.ShouldEqual, 1)
})
}