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_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["dsn_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)
go_library(
name = "go_default_library",
srcs = ["dsn.go"],
importpath = "go-common/app/infra/databus/dsn",
tags = ["automanaged"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,70 @@
package dsn
import (
"errors"
"strings"
)
var (
errInvalidDSN = errors.New("invalid dsn params")
)
// DSN is a configuration parsed from a DSN string
// key:secret@group/topic=?&role=?
type DSN struct {
Key string // app key
Secret string // app secret
Group string // kafka group
Topic string // kafka topic
Role string // pub or sub
Color string // env color
}
// ParseDSN parse databus info.
func ParseDSN(s string) (*DSN, error) {
if strings.Count(s, "@") != 1 || strings.Count(s, "/") != 1 || strings.Count(s, ":") != 1 {
return nil, errInvalidDSN
}
var (
c = &DSN{}
params string
)
i := len(s) - 1
var j, k int
for j = i; j >= 0; j-- {
// found key:passwd
if s[j] == '@' {
for k = 0; k < j; k++ {
if s[k] == ':' {
c.Secret = s[k+1 : j]
break
}
}
c.Key = s[:k]
break
}
}
// group
for k = j + 1; k < i; k++ {
if s[k] == '/' {
break
}
}
c.Group = s[j+1 : k]
params = s[k+1:]
for _, v := range strings.Split(params, "&") {
param := strings.SplitN(v, "=", 2)
if len(param) != 2 {
continue
}
switch value := param[1]; strings.ToLower(param[0]) {
case "topic":
c.Topic = value
case "role":
c.Role = value
case "color":
c.Color = value
}
}
return c, nil
}

View File

@@ -0,0 +1,29 @@
package dsn
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestParseDSN(t *testing.T) {
Convey("test parsedsn", t, func() {
var (
s = "key:secret@group/topic=1&role=2&color=red"
t = &DSN{
Key: "key",
Secret: "secret",
Group: "group",
Topic: "1",
Role: "2",
Color: "red",
}
)
d, err := ParseDSN(s)
So(err, ShouldBeNil)
So(d, ShouldResemble, t)
s = "key:secret@group/top:ic=1&role=2"
_, err = ParseDSN(s)
So(err, ShouldNotBeNil)
})
}