Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,329 @@
|
||||
package configUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"goutil/typeUtil"
|
||||
)
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// 返回值:
|
||||
// []bool:结果
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) BoolList(xpath string, attrName string) (result []bool, err error) {
|
||||
result = make([]bool, 0)
|
||||
|
||||
// 获取值列表
|
||||
valList, err := this.getValList(xpath, attrName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 转换成指定类型
|
||||
for _, valItem := range valList {
|
||||
resultItem, err1 := typeUtil.Bool(valItem)
|
||||
if err1 != nil {
|
||||
err = err1
|
||||
return
|
||||
}
|
||||
|
||||
result = append(result, resultItem)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// defaultVal:默认值
|
||||
// ifAdddefaultVal:如果某项值转换失败,是否把默认值添加到结果集合中
|
||||
// 返回值:
|
||||
// []bool:结果
|
||||
func (this *XmlConfig) DefaultBoolList(xpath string, attrName string, defaultVal bool, ifAdddefaultVal bool) (result []bool) {
|
||||
result = make([]bool, 0)
|
||||
|
||||
// 获取值列表
|
||||
valList, err := this.getValList(xpath, attrName)
|
||||
if err != nil {
|
||||
if ifAdddefaultVal {
|
||||
result = append(result, defaultVal)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换成指定类型
|
||||
for _, valItem := range valList {
|
||||
resultItem, err := typeUtil.Bool(valItem)
|
||||
if err != nil {
|
||||
if ifAdddefaultVal {
|
||||
result = append(result, defaultVal)
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
result = append(result, resultItem)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// 返回值:
|
||||
// []int:结果
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) IntList(xpath string, attrName string) (result []int, err error) {
|
||||
result = make([]int, 0)
|
||||
|
||||
// 获取值列表
|
||||
valList, err := this.getValList(xpath, attrName)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
// 转换成指定类型
|
||||
for _, valItem := range valList {
|
||||
resultItem, err := typeUtil.Int(valItem)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
result = append(result, resultItem)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// defaultVal:默认值
|
||||
// ifAdddefaultVal:如果某项值转换失败,是否把默认值添加到结果集合中
|
||||
// 返回值:
|
||||
// []int:结果
|
||||
func (this *XmlConfig) DefaultIntList(xpath string, attrName string, defaultVal int, ifAdddefaultVal bool) []int {
|
||||
result := make([]int, 0)
|
||||
|
||||
// 获取值列表
|
||||
valList, err := this.getValList(xpath, attrName)
|
||||
if err != nil {
|
||||
if ifAdddefaultVal {
|
||||
result = append(result, defaultVal)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换成指定类型
|
||||
for _, valItem := range valList {
|
||||
resultItem, err := typeUtil.Int(valItem)
|
||||
if err != nil {
|
||||
if ifAdddefaultVal {
|
||||
result = append(result, defaultVal)
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
result = append(result, resultItem)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// 返回值:
|
||||
// []int64:结果
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) Int64List(xpath string, attrName string) ([]int64, error) {
|
||||
result := make([]int64, 0)
|
||||
|
||||
// 获取值列表
|
||||
valList, err := this.getValList(xpath, attrName)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
// 转换成指定类型
|
||||
for _, valItem := range valList {
|
||||
resultItem, err := typeUtil.Int64(valItem)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
result = append(result, resultItem)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// defaultVal:默认值
|
||||
// ifAdddefaultVal:如果某项值转换失败,是否把默认值添加到结果集合中
|
||||
// 返回值:
|
||||
// []int64:结果
|
||||
func (this *XmlConfig) DefaultInt64List(xpath string, attrName string, defaultVal int64, ifAdddefaultVal bool) []int64 {
|
||||
result := make([]int64, 0)
|
||||
|
||||
// 获取值列表
|
||||
valList, err := this.getValList(xpath, attrName)
|
||||
if err != nil {
|
||||
if ifAdddefaultVal {
|
||||
result = append(result, defaultVal)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换成指定类型
|
||||
for _, valItem := range valList {
|
||||
resultItem, err := typeUtil.Int64(valItem)
|
||||
if err != nil {
|
||||
if ifAdddefaultVal {
|
||||
result = append(result, defaultVal)
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
result = append(result, resultItem)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// 返回值:
|
||||
// []float64:结果
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) FloatList(xpath string, attrName string) ([]float64, error) {
|
||||
result := make([]float64, 0)
|
||||
|
||||
// 获取值列表
|
||||
valList, err := this.getValList(xpath, attrName)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
// 转换成指定类型
|
||||
for _, valItem := range valList {
|
||||
resultItem, err := typeUtil.Float64(valItem)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
result = append(result, resultItem)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// defaultVal:默认值
|
||||
// ifAdddefaultVal:如果某项值转换失败,是否把默认值添加到结果集合中
|
||||
// 返回值:
|
||||
// []float64:结果
|
||||
func (this *XmlConfig) DefaultFloatList(xpath string, attrName string, defaultVal float64, ifAdddefaultVal bool) []float64 {
|
||||
result := make([]float64, 0)
|
||||
|
||||
// 获取值列表
|
||||
valList, err := this.getValList(xpath, attrName)
|
||||
if err != nil {
|
||||
if ifAdddefaultVal {
|
||||
result = append(result, defaultVal)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换成指定类型
|
||||
for _, valItem := range valList {
|
||||
resultItem, err := typeUtil.Float64(valItem)
|
||||
if err != nil {
|
||||
if ifAdddefaultVal {
|
||||
result = append(result, defaultVal)
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
result = append(result, resultItem)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// 返回值:
|
||||
// string:结果
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) StringList(xpath string, attrName string) ([]string, error) {
|
||||
// 获取值列表
|
||||
return this.getValList(xpath, attrName)
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// defaultVal:默认值
|
||||
// ifAdddefaultVal:如果某项值转换失败,是否把默认值添加到结果集合中
|
||||
// 返回值:
|
||||
// string:结果
|
||||
func (this *XmlConfig) DefaultStringList(xpath string, attrName string, defaultVal string, ifAdddefaultVal bool) []string {
|
||||
result := make([]string, 0)
|
||||
|
||||
// 获取值列表
|
||||
valList, err := this.getValList(xpath, attrName)
|
||||
if err != nil {
|
||||
if ifAdddefaultVal {
|
||||
result = append(result, defaultVal)
|
||||
}
|
||||
|
||||
return result
|
||||
} else {
|
||||
return valList
|
||||
}
|
||||
}
|
||||
|
||||
// 获取指定路径的之
|
||||
// xpath:xpath路径
|
||||
// attrName:要获取的属性值,如果为空,则返回内部文本
|
||||
func (this *XmlConfig) getValList(xpath string, attrName string) ([]string, error) {
|
||||
result := make([]string, 0)
|
||||
|
||||
targetNodeList := this.root.SelectElements(xpath)
|
||||
if targetNodeList == nil {
|
||||
return result, fmt.Errorf("no find target node:%v", xpath)
|
||||
}
|
||||
|
||||
// 依次获取各个节点
|
||||
for _, nodeItem := range targetNodeList {
|
||||
val := ""
|
||||
if attrName == "" {
|
||||
val = strings.TrimSpace(nodeItem.InnerText())
|
||||
} else {
|
||||
val, _ = nodeItem.SelectAttr(attrName)
|
||||
}
|
||||
|
||||
result = append(result, val)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package sqlSync
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"goutil/fileUtil"
|
||||
"goutil/logUtil"
|
||||
)
|
||||
|
||||
const (
|
||||
// 第一个文件名
|
||||
con_Default_FileName = "00000000"
|
||||
|
||||
// 文件名后缀
|
||||
con_FileName_Suffix = "data"
|
||||
)
|
||||
|
||||
// 同步数据对象(用于往文件中写入sql语句)
|
||||
type SqlFile struct {
|
||||
// 存放同步数据的文件夹路径
|
||||
dirPath string
|
||||
|
||||
// 同步数据对象的唯一标识,用于进行重复判断
|
||||
identifier string
|
||||
|
||||
// 保存数据的大文件对象
|
||||
bigFileObj *fileUtil.BigFile
|
||||
|
||||
// 数据同步对象
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
// 将数据写入同步数据对象
|
||||
// data:待写入的数据
|
||||
func (this *SqlFile) Write(data string) {
|
||||
this.mutex.Lock()
|
||||
defer this.mutex.Unlock()
|
||||
|
||||
// 写入数据
|
||||
err := Write(this.bigFileObj, data)
|
||||
if err != nil {
|
||||
prefix := fmt.Sprintf("%s-%s", this.identifier, "SqlFile.write.bigFileObj.WriteMessage")
|
||||
err = fmt.Errorf("%s-Write message to big file object failed:%s", prefix, err)
|
||||
logUtil.ErrorLog(err.Error())
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取大文件对象的文件绝对路径
|
||||
func (this *SqlFile) FileFullName() string {
|
||||
return filepath.Join(this.dirPath, this.bigFileObj.FileName())
|
||||
}
|
||||
|
||||
// 当前读写的文件名
|
||||
func (this *SqlFile) FileName() string {
|
||||
return this.bigFileObj.FileName()
|
||||
}
|
||||
|
||||
// 创建同步数据对象
|
||||
// _dirPath:目录的路径
|
||||
// _identifier:当前数据的唯一标识(可以使用数据库表名)
|
||||
// _maxFileSize:每个大文件的最大写入值(单位:Byte)
|
||||
// 返回值:
|
||||
// 同步数据对象
|
||||
func NewSqlFile(dirPath, identifier, fileName string, maxFileSize int) *SqlFile {
|
||||
result := &SqlFile{
|
||||
dirPath: dirPath,
|
||||
identifier: identifier,
|
||||
}
|
||||
|
||||
// 初始化大文件对象
|
||||
if fileName == "" {
|
||||
fileName = con_Default_FileName
|
||||
}
|
||||
bigFileObj, err := fileUtil.NewBigFileWithNewFileNameFunc2(dirPath, "", fileName, maxFileSize, NewFileName)
|
||||
if err != nil {
|
||||
prefix := fmt.Sprintf("%s-%s", result.identifier, "SqlFile.newSqlFile.fileUtil.NewBigFileWithNewFileNameFunc")
|
||||
err = fmt.Errorf("%s-Create big file object failed:%s", prefix, err)
|
||||
logUtil.ErrorLog(err.Error())
|
||||
panic(err)
|
||||
}
|
||||
result.bigFileObj = bigFileObj
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 根据当前文件名生成下一个sql文件名
|
||||
// prefix:文件名前缀
|
||||
// path:当前文件的路径
|
||||
// 返回值:
|
||||
// string:下一个文件的完整路径
|
||||
func NewFileName(prefix, path string) string {
|
||||
fullName := filepath.Base(path)
|
||||
curFileName := strings.Split(fullName, ".")[0]
|
||||
curFileId, err := strconv.Atoi(curFileName)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%s-Convert newFileName:%s to int failed:%s", prefix, curFileName, err)
|
||||
logUtil.ErrorLog(err.Error())
|
||||
panic(err)
|
||||
}
|
||||
|
||||
newFileId := curFileId + 1
|
||||
newFileName := fmt.Sprintf("%08d", newFileId)
|
||||
|
||||
// 加上文件后缀
|
||||
newFileName = fmt.Sprintf("%s.%s", newFileName, con_FileName_Suffix)
|
||||
|
||||
return newFileName
|
||||
}
|
||||
|
||||
// 获取文件夹下所有的sql文件
|
||||
// dirPath:指定要获取的文件夹路径
|
||||
// 返回值:
|
||||
// []string:sql文件列表
|
||||
func GetDataFileList(dirPath string) []string {
|
||||
// 获取当前目录中所有的数据文件列表
|
||||
fileList, err := fileUtil.GetFileList2(dirPath, "", con_FileName_Suffix)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
} else {
|
||||
err = fmt.Errorf("%s/*.%s-Get file list failed:%s", dirPath, con_FileName_Suffix, err)
|
||||
logUtil.ErrorLog(err.Error())
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 如果文件数量大于1,则进行排序,以便于后续处理
|
||||
if len(fileList) > 1 {
|
||||
sort.Strings(fileList)
|
||||
}
|
||||
|
||||
return fileList
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package ipMgr
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsIpValid(t *testing.T) {
|
||||
ip := "10.255.0.7"
|
||||
if IsIpValid(ip) {
|
||||
t.Errorf("%s应该无效,但是现在却有效", ip)
|
||||
}
|
||||
|
||||
ipList := []string{"10.255.0.7", "10.1.0.21"}
|
||||
Init(ipList)
|
||||
|
||||
if IsIpValid(ip) == false {
|
||||
t.Errorf("%s应该有效,但是现在却无效", ip)
|
||||
}
|
||||
|
||||
ipStr := "10.255.0.7,10.1.0.21;10.255.0.6|10.1.0.30||"
|
||||
InitString(ipStr)
|
||||
if IsIpValid(ip) == false {
|
||||
t.Errorf("%s应该有效,但是现在却无效", ip)
|
||||
}
|
||||
|
||||
ip = "192.168.1.1"
|
||||
RegisterIpCheckFunc("", alwaysFalse)
|
||||
if IsIpValid(ip) {
|
||||
t.Errorf("%s应该无效,但是现在却有效", ip)
|
||||
}
|
||||
|
||||
RegisterIpCheckFunc("", alwaysTrue)
|
||||
if IsIpValid(ip) == false {
|
||||
t.Errorf("%s应该有效,但是现在却无效", ip)
|
||||
}
|
||||
}
|
||||
|
||||
func alwaysTrue(ip string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func alwaysFalse(ip string) bool {
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user