Apply .gitignore rules

This commit is contained in:
皮蛋13361098506
2025-01-06 16:21:36 +08:00
parent 1b77f62820
commit ccd2c530cf
580 changed files with 69806 additions and 0 deletions

View File

@@ -0,0 +1,129 @@
/*
url的格式如下https://cmq-{$type}-{$region}.api.{$network}.com
其最终内容受到以下因素的影响:地域、网络、消息队列模型
地域
gz广州、sh上海、bj北京、shjr上海金融、szjr深圳金融、hk中国香港、cd成都、ca(北美)、usw美西、use美东、in印度、th泰国、sg新加坡
网络
外网接口请求域名后缀api.qcloud.com
内网接口请求域名后缀api.tencentyun.com
队列模型
请参照下面说明将域名中的 {$region} 替换成相应地域:
外网接口请求域名https://cmq-queue-{$region}.api.qcloud.com
内网接口请求域名http://cmq-queue-{$region}.api.tencentyun.com
主题模型
请参照下面说明将域名中的 {$region} 替换成相应地域:
外网接口请求域名https://cmq-topic-{$region}.api.qcloud.com
内网接口请求域名http://cmq-topic-{$region}.api.tencentyun.com
*/
package mqMgr
import (
"fmt"
"strings"
"goutil/securityUtil"
"goutil/stringUtil"
"goutil/webUtil"
)
func getPrefix(network string) string {
if network == MQ_NETWORK_INTERNAL {
return "http://"
}
return "https://"
}
// // 获取请求url
// // region:地域
// // network:网络类型:内网、外网
// // _type:消息队列类型:消息队列、消息主题
// // 返回:
// // 请求url
// func getHost(region, network, _type string) string {
// url := "cmq-{$type}-{$region}.api.{$network}.com"
// url = strings.Replace(url, "{$region}", region, 1)
// url = strings.Replace(url, "{$network}", network, 1)
// url = strings.Replace(url, "{$type}", _type, 1)
// return url
// }
// 获取请求url todo:切换成tdmq之后需要用这个方法
// region:地域
// network:网络类型:内网、外网
// _type:消息队列类型:消息队列、消息主题
// 返回:
// 请求url
func getHost(region, network, _type string) string {
var url string = ""
if network == MQ_NETWORK_INTERNAL {
url = "{$region}.mqadapter.cmq.{$network}.com"
} else {
url = "cmq-{$region}.public.{$network}.com"
}
url = strings.Replace(url, "{$region}", region, 1)
url = strings.Replace(url, "{$network}", network, 1)
return url
}
func getPath() string {
return "/v2/index.php"
}
func getMethod() string {
return "POST"
}
// AssembleUrl 组装请求url
// 参数
// region:地域
// network:网络类型:内网、外网
// _type:消息队列类型:消息队列、消息主题
// secretKey:密钥的key
// paramMap:参数字典
// 返回值
// string:组装好的请求url
// string:签名
// error:错误
func AssembleUrl(region, network, _type, secretKey string, paramMap map[string]string) (url, signature string, err error) {
// 1. 申请安全凭证(已经得到)
// 2. 生成签名串
// 2.1、对参数排序
// 2.2、拼接请求字符串
// 注意:
// “参数值”为原始值而非 url 编码后的值。
// 若输入参数中包含下划线,则需要将其转换为“.”。(指的是参数的名称,不是参数的值)
paramStr := webUtil.AssembleRequestParamSort(paramMap, true)
// 2.3、拼接签名原文字符串
host := getHost(region, network, _type)
path := getPath()
signatureSource := fmt.Sprintf("%s%s%s?%s", getMethod(), host, path, paramStr)
// 2.4、生成签名串
data, err := securityUtil.HmacSha256(signatureSource, secretKey)
if err != nil {
return
}
signature = string(stringUtil.Base64Encode2(data))
// 3. 签名串编码
// 注意:
// 生成的签名串并不能直接作为请求参数,需要对其进行 URL 编码。
// 如果用户的请求方法是 GET则对所有请求参数值均需要做 URL 编码。
// 如果是POST则不用进行URL编码
// signature = url.QueryEscape(signature)
// 将签名添加到参数集合中
url = fmt.Sprintf("%s%s%s", getPrefix(network), host, path)
return
}

View File

@@ -0,0 +1,36 @@
package timeUtil
import (
"testing"
"time"
)
func TestGetTime(t *testing.T) {
timeVal := time.Date(2018, 4, 25, 9, 36, 1, 0, time.Local)
timeStr1 := ToDateTimeString2(timeVal)
utcTime := GetUTCTime(timeVal)
timeStr2 := ToDateTimeString2(utcTime)
if timeStr1 != timeStr2 {
t.Errorf("获取UTC时间出错两个时间不对等")
}
utcTime2 := GetUTCTime(utcTime)
timeStr3 := ToDateTimeString2(utcTime2)
if timeStr1 != timeStr3 {
t.Errorf("两次的UTC时间不对等")
}
utcTime4 := GetLocalTime(utcTime)
timeStr4 := ToDateTimeString2(utcTime4)
if timeStr4 != timeStr1 {
t.Errorf("local变更了时间 time1:%v time4:%v", timeStr1, timeStr4)
}
utcTime5 := GetLocalTime(utcTime)
timeStr5 := ToDateTimeString2(utcTime5)
if timeStr4 != timeStr5 {
t.Errorf("两次的local时间不对等")
}
}

View File

@@ -0,0 +1,658 @@
package query
import (
"reflect"
"goutil/xmlUtil/gxpath/xpath"
)
// An XPath query interface.
type Query interface {
// Select traversing Iterator returns a query matched node xpath.NodeNavigator.
Select(Iterator) xpath.NodeNavigator
// Evaluate evaluates query and returns values of the current query.
Evaluate(Iterator) interface{}
// Test checks a specified xpath.NodeNavigator can passed by the current query.
//Test(xpath.NodeNavigator) bool
}
// ContextQuery is returns current node on the Iterator object query.
type ContextQuery struct {
count int
Root bool // Moving to root-level node in the current context Iterator.
}
func (c *ContextQuery) Select(t Iterator) (n xpath.NodeNavigator) {
if c.count == 0 {
c.count++
n = t.Current().Copy()
if c.Root {
n.MoveToRoot()
}
}
return n
}
func (c *ContextQuery) Evaluate(Iterator) interface{} {
c.count = 0
return c
}
// AncestorQuery is an XPath ancestor node query.(ancestor::*|ancestor-self::*)
type AncestorQuery struct {
iterator func() xpath.NodeNavigator
Self bool
Input Query
Predicate func(xpath.NodeNavigator) bool
}
func (a *AncestorQuery) Select(t Iterator) xpath.NodeNavigator {
for {
if a.iterator == nil {
node := a.Input.Select(t)
if node == nil {
return nil
}
first := true
a.iterator = func() xpath.NodeNavigator {
if first && a.Self {
first = false
if a.Predicate(node) {
return node
}
}
for node.MoveToParent() {
if !a.Predicate(node) {
break
}
return node
}
return nil
}
}
if node := a.iterator(); node != nil {
return node
}
a.iterator = nil
}
}
func (a *AncestorQuery) Evaluate(t Iterator) interface{} {
a.Input.Evaluate(t)
return a
}
func (a *AncestorQuery) Test(n xpath.NodeNavigator) bool {
return a.Predicate(n)
}
// AttributeQuery is an XPath attribute node query.(@*)
type AttributeQuery struct {
iterator func() xpath.NodeNavigator
Input Query
Predicate func(xpath.NodeNavigator) bool
}
func (a *AttributeQuery) Select(t Iterator) xpath.NodeNavigator {
for {
if a.iterator == nil {
node := a.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
a.iterator = func() xpath.NodeNavigator {
for {
onAttr := node.MoveToNextAttribute()
if !onAttr {
return nil
}
if a.Predicate(node) {
return node
}
}
}
}
if node := a.iterator(); node != nil {
return node
}
a.iterator = nil
}
}
func (a *AttributeQuery) Evaluate(t Iterator) interface{} {
a.Input.Evaluate(t)
a.iterator = nil
return a
}
func (a *AttributeQuery) Test(n xpath.NodeNavigator) bool {
return a.Predicate(n)
}
// ChildQuery is an XPath child node query.(child::*)
type ChildQuery struct {
posit int
iterator func() xpath.NodeNavigator
Input Query
Predicate func(xpath.NodeNavigator) bool
}
func (c *ChildQuery) Select(t Iterator) xpath.NodeNavigator {
for {
if c.iterator == nil {
c.posit = 0
node := c.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
first := true
c.iterator = func() xpath.NodeNavigator {
for {
if (first && !node.MoveToChild()) || (!first && !node.MoveToNext()) {
return nil
}
first = false
if c.Predicate(node) {
return node
}
}
}
}
if node := c.iterator(); node != nil {
c.posit++
return node
}
c.iterator = nil
}
}
func (c *ChildQuery) Evaluate(t Iterator) interface{} {
c.Input.Evaluate(t)
c.iterator = nil
return c
}
func (c *ChildQuery) Test(n xpath.NodeNavigator) bool {
return c.Predicate(n)
}
// position returns a position of current xpath.NodeNavigator.
func (c *ChildQuery) position() int {
return c.posit
}
// DescendantQuery is an XPath descendant node query.(descendant::* | descendant-or-self::*)
type DescendantQuery struct {
iterator func() xpath.NodeNavigator
Self bool
Input Query
Predicate func(xpath.NodeNavigator) bool
}
func (d *DescendantQuery) Select(t Iterator) xpath.NodeNavigator {
for {
if d.iterator == nil {
node := d.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
level := 0
first := true
d.iterator = func() xpath.NodeNavigator {
if first && d.Self {
first = false
if d.Predicate(node) {
return node
}
}
for {
if node.MoveToChild() {
level++
} else {
for {
if level == 0 {
return nil
}
if node.MoveToNext() {
break
}
node.MoveToParent()
level--
}
}
if d.Predicate(node) {
return node
}
}
}
}
if node := d.iterator(); node != nil {
return node
}
d.iterator = nil
}
}
func (d *DescendantQuery) Evaluate(t Iterator) interface{} {
d.Input.Evaluate(t)
d.iterator = nil
return d
}
func (d *DescendantQuery) Test(n xpath.NodeNavigator) bool {
return d.Predicate(n)
}
// FollowingQuery is an XPath following node query.(following::*|following-sibling::*)
type FollowingQuery struct {
iterator func() xpath.NodeNavigator
Input Query
Sibling bool // The matching sibling node of current node.
Predicate func(xpath.NodeNavigator) bool
}
func (f *FollowingQuery) Select(t Iterator) xpath.NodeNavigator {
for {
if f.iterator == nil {
node := f.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
if f.Sibling {
f.iterator = func() xpath.NodeNavigator {
for {
if !node.MoveToNext() {
return nil
}
if f.Predicate(node) {
return node
}
}
}
} else {
var q Query // descendant query
f.iterator = func() xpath.NodeNavigator {
for {
if q == nil {
for !node.MoveToNext() {
if !node.MoveToParent() {
return nil
}
}
q = &DescendantQuery{
Self: true,
Input: &ContextQuery{},
Predicate: f.Predicate,
}
t.Current().MoveTo(node)
}
if node := q.Select(t); node != nil {
return node
}
q = nil
}
}
}
}
if node := f.iterator(); node != nil {
return node
}
f.iterator = nil
}
}
func (f *FollowingQuery) Evaluate(t Iterator) interface{} {
f.Input.Evaluate(t)
return f
}
func (f *FollowingQuery) Test(n xpath.NodeNavigator) bool {
return f.Predicate(n)
}
// PrecedingQuery is an XPath preceding node query.(preceding::*)
type PrecedingQuery struct {
iterator func() xpath.NodeNavigator
Input Query
Sibling bool // The matching sibling node of current node.
Predicate func(xpath.NodeNavigator) bool
}
func (p *PrecedingQuery) Select(t Iterator) xpath.NodeNavigator {
for {
if p.iterator == nil {
node := p.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
if p.Sibling {
p.iterator = func() xpath.NodeNavigator {
for {
for !node.MoveToPrevious() {
return nil
}
if p.Predicate(node) {
return node
}
}
}
} else {
var q Query
p.iterator = func() xpath.NodeNavigator {
for {
if q == nil {
for !node.MoveToPrevious() {
if !node.MoveToParent() {
return nil
}
}
q = &DescendantQuery{
Self: true,
Input: &ContextQuery{},
Predicate: p.Predicate,
}
t.Current().MoveTo(node)
}
if node := q.Select(t); node != nil {
return node
}
q = nil
}
}
}
}
if node := p.iterator(); node != nil {
return node
}
p.iterator = nil
}
}
func (p *PrecedingQuery) Evaluate(t Iterator) interface{} {
p.Input.Evaluate(t)
return p
}
func (p *PrecedingQuery) Test(n xpath.NodeNavigator) bool {
return p.Predicate(n)
}
// ParentQuery is an XPath parent node query.(parent::*)
type ParentQuery struct {
Input Query
Predicate func(xpath.NodeNavigator) bool
}
func (p *ParentQuery) Select(t Iterator) xpath.NodeNavigator {
for {
node := p.Input.Select(t)
if node == nil {
return nil
}
node = node.Copy()
if node.MoveToParent() && p.Predicate(node) {
return node
}
}
}
func (p *ParentQuery) Evaluate(t Iterator) interface{} {
p.Input.Evaluate(t)
return p
}
func (p *ParentQuery) Test(n xpath.NodeNavigator) bool {
return p.Predicate(n)
}
// SelfQuery is an Self node query.(self::*)
type SelfQuery struct {
Input Query
Predicate func(xpath.NodeNavigator) bool
}
func (s *SelfQuery) Select(t Iterator) xpath.NodeNavigator {
for {
node := s.Input.Select(t)
if node == nil {
return nil
}
if s.Predicate(node) {
return node
}
}
}
func (s *SelfQuery) Evaluate(t Iterator) interface{} {
s.Input.Evaluate(t)
return s
}
func (s *SelfQuery) Test(n xpath.NodeNavigator) bool {
return s.Predicate(n)
}
// FilterQuery is an XPath query for predicate filter.
type FilterQuery struct {
Input Query
Predicate Query
}
func (f *FilterQuery) do(t Iterator) bool {
val := reflect.ValueOf(f.Predicate.Evaluate(t))
switch val.Kind() {
case reflect.Bool:
return val.Bool()
case reflect.String:
return len(val.String()) > 0
case reflect.Float64:
pt := float64(getNodePosition(f.Input))
return int(val.Float()) == int(pt)
default:
if q, ok := f.Predicate.(Query); ok {
return q.Select(t) != nil
}
}
return false
}
func (f *FilterQuery) Select(t Iterator) xpath.NodeNavigator {
for {
node := f.Input.Select(t)
if node == nil {
return node
}
node = node.Copy()
//fmt.Println(node.LocalName())
t.Current().MoveTo(node)
if f.do(t) {
return node
}
}
}
func (f *FilterQuery) Evaluate(t Iterator) interface{} {
f.Input.Evaluate(t)
return f
}
// FunctionQuery is an XPath function that call a function to returns
// value of current xpath.NodeNavigator node.
type XPathFunction struct {
Input Query // Node Set
Func func(Query, Iterator) interface{} // The xpath function.
}
func (f *XPathFunction) Select(t Iterator) xpath.NodeNavigator {
return nil
}
// Evaluate call a specified function that will returns the
// following value type: number,string,boolean.
func (f *XPathFunction) Evaluate(t Iterator) interface{} {
return f.Func(f.Input, t)
}
// XPathConstant is an XPath constant operand.
type XPathConstant struct {
Val interface{}
}
func (c *XPathConstant) Select(t Iterator) xpath.NodeNavigator {
return nil
}
func (c *XPathConstant) Evaluate(t Iterator) interface{} {
return c.Val
}
// LogicalExpr is an XPath logical expression.
type LogicalExpr struct {
Left, Right Query
Do func(Iterator, interface{}, interface{}) interface{}
}
func (l *LogicalExpr) Select(t Iterator) xpath.NodeNavigator {
// When a XPath expr is logical expression.
node := t.Current().Copy()
val := l.Evaluate(t)
switch val.(type) {
case bool:
if val.(bool) == true {
return node
}
}
return nil
}
func (l *LogicalExpr) Evaluate(t Iterator) interface{} {
m := l.Left.Evaluate(t)
n := l.Right.Evaluate(t)
return l.Do(t, m, n)
}
// NumericExpr is an XPath numeric operator expression.
type NumericExpr struct {
Left, Right Query
Do func(interface{}, interface{}) interface{}
}
func (n *NumericExpr) Select(t Iterator) xpath.NodeNavigator {
return nil
}
func (n *NumericExpr) Evaluate(t Iterator) interface{} {
m := n.Left.Evaluate(t)
k := n.Right.Evaluate(t)
return n.Do(m, k)
}
type BooleanExpr struct {
IsOr bool
Left, Right Query
iterator func() xpath.NodeNavigator
}
func (b *BooleanExpr) Select(t Iterator) xpath.NodeNavigator {
if b.iterator == nil {
var list []xpath.NodeNavigator
i := 0
root := t.Current().Copy()
if b.IsOr {
for {
node := b.Left.Select(t)
if node == nil {
break
}
node = node.Copy()
list = append(list, node)
}
t.Current().MoveTo(root)
for {
node := b.Right.Select(t)
if node == nil {
break
}
node = node.Copy()
list = append(list, node)
}
} else {
var m []xpath.NodeNavigator
var n []xpath.NodeNavigator
for {
node := b.Left.Select(t)
if node == nil {
break
}
node = node.Copy()
list = append(m, node)
}
t.Current().MoveTo(root)
for {
node := b.Right.Select(t)
if node == nil {
break
}
node = node.Copy()
list = append(n, node)
}
for _, k := range m {
for _, j := range n {
if k == j {
list = append(list, k)
}
}
}
}
b.iterator = func() xpath.NodeNavigator {
if i >= len(list) {
return nil
}
node := list[i]
i++
return node
}
}
return b.iterator()
}
func (b *BooleanExpr) Evaluate(t Iterator) interface{} {
m := b.Left.Evaluate(t)
if m.(bool) == b.IsOr {
return m
}
return b.Right.Evaluate(t)
}
func getNodePosition(q Query) int {
type Position interface {
position() int
}
if count, ok := q.(Position); ok {
return count.position()
}
return 1
}

View File

@@ -0,0 +1,73 @@
package fileUtil
import (
"fmt"
"strings"
"testing"
)
func TestTar(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
fileName1 := fmt.Sprintf("%s/%s", path, "test1.txt")
fileName2 := fmt.Sprintf("%s/%s", path, "test2.txt")
if err := WriteFile(path, "test1.txt", true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
if err := WriteFile(path, "test2.txt", true, "first line"); err != nil {
t.Errorf("there should be no error, but now it is:%s", err)
}
sourceList := make([]string, 0, 2)
sourceList = append(sourceList, fileName1)
sourceList = append(sourceList, fileName2)
target := fmt.Sprintf("%s/%s", path, "test.tar")
if err := Tar(sourceList, target); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
}
if fileList, err := GetFileList(path); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
} else {
for _, item := range fileList {
fmt.Printf("item:%s\n", item)
}
}
DeleteFile(fileName1)
DeleteFile(fileName2)
}
func TestUntar(t *testing.T) {
path := GetCurrentPath()
fmt.Printf("CurrPath:%s\n", path)
source := fmt.Sprintf("%s/%s", path, "test.tar")
// target := path
target := ""
if err := Untar(source, target); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
}
if fileList, err := GetFileList(path); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
} else {
for _, item := range fileList {
fmt.Printf("item:%s\n", item)
if strings.HasSuffix(item, "txt") {
if content, err := ReadFileContent(item); err != nil {
t.Errorf("There should be no error, but now it has:%s", err)
} else {
fmt.Printf("content:%s\n", content)
}
DeleteFile(item)
}
}
DeleteFile(source)
}
}