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,44 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["tool.go"],
importpath = "go-common/app/interface/main/creative/dao/tool",
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 = [
"dao_test.go",
"tool_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,31 @@
package tool
import (
"flag"
"os"
"testing"
"go-common/app/interface/main/creative/conf"
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.creative")
flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
flag.Set("tree_id", "2305")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/creative.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,123 @@
package tool
import (
"crypto/md5"
"encoding/hex"
"fmt"
"math/rand"
"net/url"
"strings"
)
// Sign fn
func Sign(params url.Values) (query string, err error) {
if len(params) == 0 {
return
}
if params.Get("appkey") == "" {
err = fmt.Errorf("utils http get must have parameter appkey")
return
}
if params.Get("appsecret") == "" {
err = fmt.Errorf("utils http get must have parameter appsecret")
return
}
if params.Get("sign") != "" {
err = fmt.Errorf("utils http get must have not parameter sign")
return
}
// sign
secret := params.Get("appsecret")
params.Del("appsecret")
tmp := params.Encode()
if strings.IndexByte(tmp, '+') > -1 {
tmp = strings.Replace(tmp, "+", "%20", -1)
}
mh := md5.Sum([]byte(tmp + secret))
params.Set("sign", hex.EncodeToString(mh[:]))
query = params.Encode()
return
}
//DeDuplicationSlice for del repeat element
func DeDuplicationSlice(a []int64) (b []int64) {
if len(a) == 0 {
return
}
isHas := make(map[int64]bool)
b = make([]int64, 0)
for _, v := range a {
if ok := isHas[v]; !ok {
isHas[v] = true
b = append(b, v)
}
}
return
}
//ContainAll all element of a contain in the b.
func ContainAll(a []int64, b []int64) bool {
isHas := make(map[int64]bool)
for _, k := range b {
isHas[k] = true
}
for _, v := range a {
if !isHas[v] {
return false
}
}
return true
}
//ContainAtLeastOne fn
func ContainAtLeastOne(a []int64, b []int64) bool {
if len(a) == 0 {
return true
}
isHas := make(map[int64]bool)
for _, k := range b {
isHas[k] = true
}
for _, v := range a {
if isHas[v] {
return true
}
}
return false
}
//ElementInSlice fn
func ElementInSlice(a int64, b []int64) bool {
if len(b) == 0 {
return false
}
for _, v := range b {
if a == v {
return true
}
}
return false
}
//RandomSliceKeys for get random keys from slice by rand.
func RandomSliceKeys(start int, end int, count int, seed int64) []int {
if end < start || (end-start) < count {
return nil
}
nums := make([]int, 0)
r := rand.New(rand.NewSource(seed))
for len(nums) < count {
num := r.Intn((end - start)) + start
exist := false
for _, v := range nums {
if v == num {
exist = true
break
}
}
if !exist {
nums = append(nums, num)
}
}
return nums
}

View File

@@ -0,0 +1,89 @@
package tool
import (
"fmt"
"net/url"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestToolSign(t *testing.T) {
var (
params url.Values
)
convey.Convey("Sign", t, func(ctx convey.C) {
query, err := Sign(params)
ctx.Convey("Then err should be nil.query should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(query, convey.ShouldNotBeNil)
})
})
}
func TestToolDeDuplicationSlice(t *testing.T) {
var (
a = []int64{1, 2}
)
convey.Convey("DeDuplicationSlice", t, func(ctx convey.C) {
b := DeDuplicationSlice(a)
ctx.Convey("Then b should not be nil.", func(ctx convey.C) {
ctx.So(b, convey.ShouldNotBeNil)
})
})
}
func TestToolContainAll(t *testing.T) {
var (
a = []int64{1}
b = []int64{2}
)
convey.Convey("ContainAll", t, func(ctx convey.C) {
p1 := ContainAll(a, b)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestToolContainAtLeastOne(t *testing.T) {
var (
a = []int64{}
b = []int64{}
)
convey.Convey("ContainAtLeastOne", t, func(ctx convey.C) {
p1 := ContainAtLeastOne(a, b)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestToolElementInSlice(t *testing.T) {
var (
a = int64(1)
b = []int64{1, 2}
)
convey.Convey("ElementInSlice", t, func(ctx convey.C) {
p1 := ElementInSlice(a, b)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestToolRandomSliceKeys(t *testing.T) {
var (
start = 0
end = 100
count = 100
seed = int64(100)
)
convey.Convey("RandomSliceKeys", t, func(ctx convey.C) {
p1 := RandomSliceKeys(start, end, count, seed)
fmt.Println(p1)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(len(p1), convey.ShouldBeGreaterThan, 0)
})
})
}