mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-05-26 08:35:18 +08:00
实现HTTP部分功能
This commit is contained in:
166
pkg/configutils/charsets.go
Normal file
166
pkg/configutils/charsets.go
Normal file
@@ -0,0 +1,166 @@
|
||||
package configutils
|
||||
|
||||
import "github.com/iwind/TeaGo/maps"
|
||||
|
||||
var UsualCharsets = []maps.Map{
|
||||
{"charset": "utf-8", "name": "Universal Alphabet (UTF-8)"},
|
||||
{"charset": "unicode", "name": "Unicode"},
|
||||
{"name": "Chinese Simplified (GB2312)", "charset": "gb2312"},
|
||||
{"charset": "big5", "name": "Chinese Traditional (Big5)"},
|
||||
{"charset": "iso-8859-1", "name": "Western Alphabet"},
|
||||
{"charset": "euc-kr", "name": "Korean (EUC)"},
|
||||
{"charset": "shift-jis", "name": "Japanese (Shift-JIS)"},
|
||||
{"charset": "us-ascii", "name": "US-ASCII"},
|
||||
}
|
||||
|
||||
// 数据来自 https://webcheatsheet.com/html/character_sets_list.php
|
||||
var BasicCharsets = []maps.Map{
|
||||
{"name": "Chinese Traditional (Big5)", "charset": "big5"},
|
||||
{"charset": "euc-kr", "name": "Korean (EUC)"},
|
||||
{"charset": "iso-8859-1", "name": "Western Alphabet"},
|
||||
{"charset": "iso-8859-2", "name": "Central European Alphabet (ISO)"},
|
||||
{"charset": "iso-8859-3", "name": "Latin 3 Alphabet (ISO)"},
|
||||
{"charset": "iso-8859-4", "name": "Baltic Alphabet (ISO)"},
|
||||
{"charset": "iso-8859-5", "name": "Cyrillic Alphabet (ISO)"},
|
||||
{"charset": "iso-8859-6", "name": "Arabic Alphabet (ISO)"},
|
||||
{"charset": "iso-8859-7", "name": "Greek Alphabet (ISO)"},
|
||||
{"charset": "iso-8859-8", "name": "Hebrew Alphabet (ISO)"},
|
||||
{"charset": "koi8-r", "name": "Cyrillic Alphabet (KOI8-R)"},
|
||||
{"charset": "shift-jis", "name": "Japanese (Shift-JIS)"},
|
||||
{"name": "Japanese (EUC)", "charset": "x-euc"},
|
||||
{"charset": "utf-8", "name": "Universal Alphabet (UTF-8)"},
|
||||
{"charset": "windows-1250", "name": "Central European Alphabet (Windows)"},
|
||||
{"charset": "windows-1251", "name": "Cyrillic Alphabet (Windows)"},
|
||||
{"charset": "windows-1252", "name": "Western Alphabet (Windows)"},
|
||||
{"charset": "windows-1253", "name": "Greek Alphabet (Windows)"},
|
||||
{"charset": "windows-1254", "name": "Turkish Alphabet"},
|
||||
{"charset": "windows-1255", "name": "Hebrew Alphabet (Windows)"},
|
||||
{"charset": "windows-1256", "name": "Arabic Alphabet (Windows)"},
|
||||
{"charset": "windows-1257", "name": "Baltic Alphabet (Windows)"},
|
||||
{"charset": "windows-1258", "name": "Vietnamese Alphabet (Windows)"},
|
||||
{"charset": "windows-874", "name": "Thai (Windows)"},
|
||||
}
|
||||
|
||||
var AllCharsets = []maps.Map{
|
||||
{"charset": "ASMO-708", "name": "Arabic (ASMO 708)"},
|
||||
{"charset": "DOS-720", "name": "Arabic (DOS)"},
|
||||
{"charset": "iso-8859-6", "name": "Arabic (ISO)"},
|
||||
{"charset": "x-mac-arabic", "name": "Arabic (Mac)"},
|
||||
{"charset": "windows-1256", "name": "Arabic (Windows)"},
|
||||
{"charset": "ibm775", "name": "Baltic (DOS)"},
|
||||
{"charset": "iso-8859-4", "name": "Baltic (ISO)"},
|
||||
{"charset": "windows-1257", "name": "Baltic (Windows)"},
|
||||
{"charset": "ibm852", "name": "Central European (DOS)"},
|
||||
{"name": "Central European (ISO)", "charset": "iso-8859-2"},
|
||||
{"charset": "x-mac-ce", "name": "Central European (Mac)"},
|
||||
{"charset": "windows-1250", "name": "Central European (Windows)"},
|
||||
{"name": "Chinese Simplified (EUC)", "charset": "EUC-CN"},
|
||||
{"name": "Chinese Simplified (GB2312)", "charset": "gb2312"},
|
||||
{"charset": "hz-gb-2312", "name": "Chinese Simplified (HZ)"},
|
||||
{"charset": "x-mac-chinesesimp", "name": "Chinese Simplified (Mac)"},
|
||||
{"charset": "big5", "name": "Chinese Traditional (Big5)"},
|
||||
{"name": "Chinese Traditional (CNS)", "charset": "x-Chinese-CNS"},
|
||||
{"charset": "x-Chinese-Eten", "name": "Chinese Traditional (Eten)"},
|
||||
{"charset": "x-mac-chinesetrad", "name": "Chinese Traditional (Mac)"},
|
||||
{"charset": "950", "name": "Chinese Traditional (Mac)"},
|
||||
{"charset": "cp866", "name": "Cyrillic (DOS)"},
|
||||
{"charset": "iso-8859-5", "name": "Cyrillic (ISO)"},
|
||||
{"charset": "koi8-r", "name": "Cyrillic (KOI8-R)"},
|
||||
{"charset": "koi8-u", "name": "Cyrillic (KOI8-U)"},
|
||||
{"charset": "x-mac-cyrillic", "name": "Cyrillic (Mac)"},
|
||||
{"charset": "windows-1251", "name": "Cyrillic (Windows)"},
|
||||
{"charset": "x-Europa", "name": "Europa"},
|
||||
{"charset": "x-IA5-German", "name": "German (IA5)"},
|
||||
{"charset": "ibm737", "name": "Greek (DOS)"},
|
||||
{"name": "Greek (ISO)", "charset": "iso-8859-7"},
|
||||
{"name": "Greek (Mac)", "charset": "x-mac-greek"},
|
||||
{"charset": "windows-1253", "name": "Greek (Windows)"},
|
||||
{"charset": " ", "name": "Greek (Windows)"},
|
||||
{"charset": "ibm869", "name": "Greek, Modern (DOS)"},
|
||||
{"name": "Hebrew (DOS)", "charset": "DOS-862"},
|
||||
{"charset": "iso-8859-8-i", "name": "Hebrew (ISO-Logical)"},
|
||||
{"charset": "iso-8859-8", "name": "Hebrew (ISO-Visual)"},
|
||||
{"charset": "x-mac-hebrew", "name": "Hebrew (Mac)"},
|
||||
{"charset": "windows-1255", "name": "Hebrew (Windows)"},
|
||||
{"charset": "x-EBCDIC-Arabic", "name": "IBM EBCDIC (Arabic)"},
|
||||
{"charset": "x-EBCDIC-CyrillicRussian", "name": "IBM EBCDIC (Cyrillic Russian)"},
|
||||
{"charset": "x-EBCDIC-CyrillicSerbianBulgarian", "name": "IBM EBCDIC (Cyrillic Serbian-Bulgarian)"},
|
||||
{"charset": "x-EBCDIC-DenmarkNorway", "name": "IBM EBCDIC (Denmark-Norway)"},
|
||||
{"charset": "x-ebcdic-denmarknorway-euro", "name": "IBM EBCDIC (Denmark-Norway-Euro)"},
|
||||
{"charset": "x-EBCDIC-FinlandSweden", "name": "IBM EBCDIC (Finland-Sweden)"},
|
||||
{"charset": "x-ebcdic-finlandsweden-euro", "name": "IBM EBCDIC (Finland-Sweden-Euro)"},
|
||||
{"charset": "x-ebcdic-finlandsweden-euro", "name": "IBM EBCDIC (Finland-Sweden-Euro)"},
|
||||
{"charset": "x-ebcdic-france-euro", "name": "IBM EBCDIC (France-Euro)"},
|
||||
{"charset": "x-EBCDIC-Germany", "name": "IBM EBCDIC (Germany)"},
|
||||
{"charset": "x-ebcdic-germany-euro", "name": "IBM EBCDIC (Germany-Euro)"},
|
||||
{"charset": "x-EBCDIC-GreekModern", "name": "IBM EBCDIC (Greek Modern)"},
|
||||
{"charset": "x-EBCDIC-Greek", "name": "IBM EBCDIC (Greek)"},
|
||||
{"charset": "x-EBCDIC-Hebrew", "name": "IBM EBCDIC (Hebrew)"},
|
||||
{"charset": "x-EBCDIC-Icelandic", "name": "IBM EBCDIC (Icelandic)"},
|
||||
{"charset": "x-ebcdic-icelandic-euro", "name": "IBM EBCDIC (Icelandic-Euro)"},
|
||||
{"name": "IBM EBCDIC (International-Euro)", "charset": "x-ebcdic-international-euro"},
|
||||
{"charset": "x-EBCDIC-Italy", "name": "IBM EBCDIC (Italy)"},
|
||||
{"charset": "x-ebcdic-italy-euro", "name": "IBM EBCDIC (Italy-Euro)"},
|
||||
{"charset": "x-EBCDIC-JapaneseAndKana", "name": "IBM EBCDIC (Japanese and Japanese Katakana)"},
|
||||
{"charset": "x-EBCDIC-JapaneseAndJapaneseLatin", "name": "IBM EBCDIC (Japanese and Japanese-Latin)"},
|
||||
{"name": "IBM EBCDIC (Japanese and US-Canada)", "charset": "x-EBCDIC-JapaneseAndUSCanada"},
|
||||
{"name": "IBM EBCDIC (Japanese katakana)", "charset": "x-EBCDIC-JapaneseKatakana"},
|
||||
{"charset": "x-EBCDIC-KoreanAndKoreanExtended", "name": "IBM EBCDIC (Korean and Korean Extended)"},
|
||||
{"charset": "x-EBCDIC-KoreanExtended", "name": "IBM EBCDIC (Korean Extended)"},
|
||||
{"charset": "CP870", "name": "IBM EBCDIC (Multilingual Latin-2)"},
|
||||
{"charset": "x-EBCDIC-SimplifiedChinese", "name": "IBM EBCDIC (Simplified Chinese)"},
|
||||
{"charset": "X-EBCDIC-Spain", "name": "IBM EBCDIC (Spain)"},
|
||||
{"charset": "x-ebcdic-spain-euro", "name": "IBM EBCDIC (Spain-Euro)"},
|
||||
{"charset": "x-EBCDIC-Thai", "name": "IBM EBCDIC (Thai)"},
|
||||
{"charset": "x-EBCDIC-TraditionalChinese", "name": "IBM EBCDIC (Traditional Chinese)"},
|
||||
{"charset": "CP1026", "name": "IBM EBCDIC (Turkish Latin-5)"},
|
||||
{"charset": "x-EBCDIC-Turkish", "name": "IBM EBCDIC (Turkish)"},
|
||||
{"charset": "x-EBCDIC-UK", "name": "IBM EBCDIC (UK)"},
|
||||
{"charset": "x-ebcdic-uk-euro", "name": "IBM EBCDIC (UK-Euro)"},
|
||||
{"charset": "ebcdic-cp-us", "name": "IBM EBCDIC (US-Canada)"},
|
||||
{"name": "IBM EBCDIC (US-Canada-Euro)", "charset": "x-ebcdic-cp-us-euro"},
|
||||
{"charset": "ibm861", "name": "Icelandic (DOS)"},
|
||||
{"charset": "x-mac-icelandic", "name": "Icelandic (Mac)"},
|
||||
{"charset": "x-iscii-as", "name": "ISCII Assamese"},
|
||||
{"charset": "x-iscii-be", "name": "ISCII Bengali"},
|
||||
{"charset": "x-iscii-de", "name": "ISCII Devanagari"},
|
||||
{"charset": "x-iscii-gu", "name": "ISCII Gujarathi"},
|
||||
{"charset": "x-iscii-ka", "name": "ISCII Kannada"},
|
||||
{"charset": "x-iscii-ma", "name": "ISCII Malayalam"},
|
||||
{"charset": "x-iscii-or", "name": "ISCII Oriya"},
|
||||
{"charset": "x-iscii-pa", "name": "ISCII Panjabi"},
|
||||
{"charset": "x-iscii-ta", "name": "ISCII Tamil"},
|
||||
{"charset": "x-iscii-te", "name": "ISCII Telugu"},
|
||||
{"charset": "euc-jp", "name": "Japanese (EUC)"},
|
||||
{"charset": "x-euc-jp", "name": "Japanese (EUC)"},
|
||||
{"charset": "iso-2022-jp", "name": "Japanese (JIS)"},
|
||||
{"charset": "iso-2022-jp", "name": "Japanese (JIS-Allow 1 byte Kana - SO/SI)"},
|
||||
{"charset": "csISO2022JP", "name": "Japanese (JIS-Allow 1 byte Kana)"},
|
||||
{"charset": "x-mac-japanese", "name": "Japanese (Mac)"},
|
||||
{"charset": "shift_jis", "name": "Japanese (Shift-JIS)"},
|
||||
{"charset": "ks_c_5601-1987", "name": "Korean"},
|
||||
{"charset": "euc-kr", "name": "Korean (EUC)"},
|
||||
{"charset": "iso-2022-kr", "name": "Korean (ISO)"},
|
||||
{"charset": "Johab", "name": "Korean (Johab)"},
|
||||
{"charset": "x-mac-korean", "name": "Korean (Mac)"},
|
||||
{"charset": "iso-8859-3", "name": "Latin 3 (ISO)"},
|
||||
{"charset": "iso-8859-15", "name": "Latin 9 (ISO)"},
|
||||
{"charset": "x-IA5-Norwegian", "name": "Norwegian (IA5)"},
|
||||
{"charset": "IBM437", "name": "OEM United States"},
|
||||
{"charset": "x-IA5-Swedish", "name": "Swedish (IA5)"},
|
||||
{"charset": "windows-874", "name": "Thai (Windows)"},
|
||||
{"charset": "ibm857", "name": "Turkish (DOS)"}, {"charset": "iso-8859-9", "name": "Turkish (ISO)"},
|
||||
{"charset": "x-mac-turkish", "name": "Turkish (Mac)"},
|
||||
{"charset": "windows-1254", "name": "Turkish (Windows)"},
|
||||
{"charset": "unicode", "name": "Unicode"},
|
||||
{"charset": "unicodeFFFE", "name": "Unicode (Big-Endian)"},
|
||||
{"charset": "utf-7", "name": "Unicode (UTF-7)"},
|
||||
{"name": "Unicode (UTF-8)", "charset": "utf-8"},
|
||||
{"charset": "us-ascii", "name": "US-ASCII"},
|
||||
{"charset": "windows-1258", "name": "Vietnamese (Windows)"},
|
||||
{"charset": "ibm850", "name": "Western European (DOS)"},
|
||||
{"charset": "x-IA5", "name": "Western European (IA5)"},
|
||||
{"charset": "iso-8859-1", "name": "Western European (ISO)"},
|
||||
{"name": "Western European (Mac)", "charset": "macintosh"},
|
||||
{"charset": "Windows-1252", "name": "Western European (Windows)"},
|
||||
}
|
||||
20
pkg/configutils/copy.go
Normal file
20
pkg/configutils/copy.go
Normal 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)
|
||||
}
|
||||
}
|
||||
28
pkg/configutils/copy_test.go
Normal file
28
pkg/configutils/copy_test.go
Normal 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)
|
||||
}
|
||||
59
pkg/configutils/domain.go
Normal file
59
pkg/configutils/domain.go
Normal 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
|
||||
}
|
||||
79
pkg/configutils/domain_test.go
Normal file
79
pkg/configutils/domain_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
11
pkg/configutils/log.go
Normal file
11
pkg/configutils/log.go
Normal 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...)
|
||||
}
|
||||
25
pkg/configutils/match.go
Normal file
25
pkg/configutils/match.go
Normal 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
|
||||
}
|
||||
13
pkg/configutils/match_test.go
Normal file
13
pkg/configutils/match_test.go
Normal 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"))
|
||||
}
|
||||
15
pkg/configutils/state.go
Normal file
15
pkg/configutils/state.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package configutils
|
||||
|
||||
import "github.com/iwind/TeaGo/types"
|
||||
|
||||
type BoolState = int8
|
||||
|
||||
const (
|
||||
BoolStateAll BoolState = 0 // 全部
|
||||
BoolStateYes BoolState = 1 // 已安装
|
||||
BoolStateNo BoolState = 2 // 未安装
|
||||
)
|
||||
|
||||
func ToBoolState(v interface{}) BoolState {
|
||||
return types.Int8(v)
|
||||
}
|
||||
54
pkg/configutils/variable.go
Normal file
54
pkg/configutils/variable.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package configutils
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 变量信息存储类型
|
||||
type VariableHolder string
|
||||
|
||||
var variableMapping = map[string][]interface{}{}
|
||||
var variableLocker = sync.RWMutex{}
|
||||
var regexpNamedVariable = regexp.MustCompile("\\${[\\w.-]+}")
|
||||
|
||||
// 分析变量
|
||||
func ParseVariables(source string, replacer func(varName string) (value string)) string {
|
||||
variableLocker.RLock()
|
||||
holders, found := variableMapping[source]
|
||||
variableLocker.RUnlock()
|
||||
if !found {
|
||||
indexes := regexpNamedVariable.FindAllStringIndex(source, -1)
|
||||
before := 0
|
||||
for _, loc := range indexes {
|
||||
holders = append(holders, []byte(source[before:loc[0]]))
|
||||
holder := source[loc[0]+2 : loc[1]-1]
|
||||
holders = append(holders, VariableHolder(holder))
|
||||
before = loc[1]
|
||||
}
|
||||
if before < len(source) {
|
||||
holders = append(holders, []byte(source[before:]))
|
||||
}
|
||||
variableLocker.Lock()
|
||||
variableMapping[source] = holders
|
||||
variableLocker.Unlock()
|
||||
}
|
||||
|
||||
// no variables
|
||||
if len(holders) == 0 {
|
||||
return source
|
||||
}
|
||||
|
||||
// replace
|
||||
result := strings.Builder{}
|
||||
for _, h := range holders {
|
||||
holder, ok := h.(VariableHolder)
|
||||
if ok {
|
||||
result.WriteString(replacer(string(holder)))
|
||||
} else {
|
||||
result.Write(h.([]byte))
|
||||
}
|
||||
}
|
||||
return result.String()
|
||||
}
|
||||
51
pkg/configutils/variable_test.go
Normal file
51
pkg/configutils/variable_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package configutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseVariables(t *testing.T) {
|
||||
v := ParseVariables("hello, ${name}, world", func(s string) string {
|
||||
return "Lu"
|
||||
})
|
||||
t.Log(v)
|
||||
}
|
||||
|
||||
func TestParseNoVariables(t *testing.T) {
|
||||
for i := 0; i < 2; i++ {
|
||||
v := ParseVariables("hello, world", func(s string) string {
|
||||
return "Lu"
|
||||
})
|
||||
t.Log(v)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkParseVariables(b *testing.B) {
|
||||
_ = ParseVariables("hello, ${name}, ${age}, ${gender}, ${home}, world", func(s string) string {
|
||||
return "Lu"
|
||||
})
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = ParseVariables("hello, ${name}, ${age}, ${gender}, ${home}, world", func(s string) string {
|
||||
return "Lu"
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkParseVariablesUnique(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = ParseVariables("hello, ${name} "+strconv.Itoa(i%1000), func(s string) string {
|
||||
return "Lu"
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkParseNoVariables(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = ParseVariables("hello, world, "+fmt.Sprintf("%d", i%1000), func(s string) string {
|
||||
return "Lu"
|
||||
})
|
||||
}
|
||||
}
|
||||
14
pkg/configutils/yaml.go
Normal file
14
pkg/configutils/yaml.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user