阶段性提交

This commit is contained in:
GoEdgeLab
2020-09-09 18:53:53 +08:00
parent ea05b2f236
commit 5a941ec9d7
115 changed files with 7910 additions and 744 deletions

View File

@@ -0,0 +1,20 @@
package configutils
import (
"reflect"
)
// 拷贝同类型struct指针对象中的字段
func CopyStructObject(destPtr, sourcePtr interface{}) {
value := reflect.ValueOf(destPtr)
value2 := reflect.ValueOf(sourcePtr)
countFields := value2.Elem().NumField()
for i := 0; i < countFields; i++ {
v := value2.Elem().Field(i)
if !v.IsValid() || !v.CanSet() {
continue
}
value.Elem().Field(i).Set(v)
}
}

View File

@@ -0,0 +1,28 @@
package configutils
import (
"github.com/iwind/TeaGo/logs"
"testing"
)
func TestCopyStructObject(t *testing.T) {
type Book struct {
Name string
Price int
Year int
Author string
press string
}
book1 := &Book{
Name: "Hello Golang",
Price: 100,
Year: 2020,
Author: "Liu",
press: "Beijing",
}
book2 := new(Book)
CopyStructObject(book2, book1)
logs.PrintAsJSON(book2, t)
logs.PrintAsJSON(book1, t)
}

View File

@@ -0,0 +1,59 @@
package configutils
import (
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/utils/string"
"strings"
)
// 从一组规则中匹配域名
// 支持的格式example.com, www.example.com, .example.com, *.example.com, ~(\d+).example.com
// 更多参考http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name
func MatchDomains(patterns []string, domain string) (isMatched bool) {
if len(patterns) == 0 {
return
}
for _, pattern := range patterns {
if matchDomain(pattern, domain) {
return true
}
}
return
}
// 匹配单个域名规则
func matchDomain(pattern string, domain string) (isMatched bool) {
if len(pattern) == 0 {
return
}
// 正则表达式
if pattern[0] == '~' {
reg, err := stringutil.RegexpCompile(strings.TrimSpace(pattern[1:]))
if err != nil {
logs.Error(err)
return false
}
return reg.MatchString(domain)
}
if pattern[0] == '.' {
return strings.HasSuffix(domain, pattern)
}
// 其他匹配
patternPieces := strings.Split(pattern, ".")
domainPieces := strings.Split(domain, ".")
if len(patternPieces) != len(domainPieces) {
return
}
isMatched = true
for index, patternPiece := range patternPieces {
if patternPiece == "" || patternPiece == "*" || patternPiece == domainPieces[index] {
continue
}
isMatched = false
break
}
return isMatched
}

View File

@@ -0,0 +1,79 @@
package configutils
import (
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestMatchDomain(t *testing.T) {
a := assert.NewAssertion(t)
{
ok := MatchDomains([]string{}, "example.com")
a.IsFalse(ok)
}
{
ok := MatchDomains([]string{"example.com"}, "example.com")
a.IsTrue(ok)
}
{
ok := MatchDomains([]string{"www.example.com"}, "example.com")
a.IsFalse(ok)
}
{
ok := MatchDomains([]string{".example.com"}, "www.example.com")
a.IsTrue(ok)
}
{
ok := MatchDomains([]string{".example.com"}, "a.www.example.com")
a.IsTrue(ok)
}
{
ok := MatchDomains([]string{".example.com"}, "a.www.example123.com")
a.IsFalse(ok)
}
{
ok := MatchDomains([]string{"*.example.com"}, "www.example.com")
a.IsTrue(ok)
}
{
ok := MatchDomains([]string{"*.*.com"}, "www.example.com")
a.IsTrue(ok)
}
{
ok := MatchDomains([]string{"www.*.com"}, "www.example.com")
a.IsTrue(ok)
}
{
ok := MatchDomains([]string{"gallery.*.com"}, "www.example.com")
a.IsFalse(ok)
}
{
ok := MatchDomains([]string{"~\\w+.example.com"}, "www.example.com")
a.IsTrue(ok)
}
{
ok := MatchDomains([]string{"~\\w+.example.com"}, "a.www.example.com")
a.IsTrue(ok)
}
{
ok := MatchDomains([]string{"~^\\d+.example.com$"}, "www.example.com")
a.IsFalse(ok)
}
{
ok := MatchDomains([]string{"~^\\d+.example.com$"}, "123.example.com")
a.IsTrue(ok)
}
}

View File

@@ -0,0 +1,11 @@
package configutils
import "github.com/iwind/TeaGo/logs"
// 记录错误
func LogError(arg ...interface{}) {
if len(arg) == 0 {
return
}
logs.Println(arg...)
}

View File

@@ -0,0 +1,25 @@
package configutils
import (
"regexp"
"strings"
)
var whitespaceReg = regexp.MustCompile(`\s+`)
// 关键词匹配
func MatchKeyword(source, keyword string) bool {
if len(keyword) == 0 {
return false
}
pieces := whitespaceReg.Split(keyword, -1)
source = strings.ToLower(source)
for _, piece := range pieces {
if strings.Index(source, strings.ToLower(piece)) > -1 {
return true
}
}
return false
}

View File

@@ -0,0 +1,13 @@
package configutils
import (
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestMatchKeyword(t *testing.T) {
a := assert.NewAssertion(t)
a.IsTrue(MatchKeyword("a b c", "a"))
a.IsFalse(MatchKeyword("a b c", ""))
a.IsTrue(MatchKeyword("abc", "BC"))
}

View File

@@ -0,0 +1,14 @@
package configutils
import (
"github.com/go-yaml/yaml"
"io/ioutil"
)
func UnmarshalYamlFile(file string, ptr interface{}) error {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
return yaml.Unmarshal(data, ptr)
}