初始化项目
This commit is contained in:
4
trunk/goutil/configUtil/doc.go
Normal file
4
trunk/goutil/configUtil/doc.go
Normal file
@@ -0,0 +1,4 @@
|
||||
/*
|
||||
配置助手类,用于处理以JSON格式存储的配置文件
|
||||
*/
|
||||
package configUtil
|
||||
99
trunk/goutil/configUtil/json.go
Normal file
99
trunk/goutil/configUtil/json.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package configUtil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// 读取JSON格式的配置文件
|
||||
// config_file_path:配置文件路径
|
||||
// 返回值:
|
||||
// 配置内容的map格式
|
||||
// 错误对象
|
||||
func ReadJsonConfig(config_file_path string) (config map[string]interface{}, err error) {
|
||||
// 读取配置文件(一次性读取整个文件,则使用ioutil)
|
||||
bytes, err := ioutil.ReadFile(config_file_path)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("读取配置文件的内容出错:%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 使用json反序列化
|
||||
config = make(map[string]interface{})
|
||||
if err = json.Unmarshal(bytes, &config); err != nil {
|
||||
err = fmt.Errorf("反序列化配置文件的内容出错:%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 从config配置中获取int类型的配置值
|
||||
// config:从config文件中反序列化出来的map对象
|
||||
// configName:配置名称
|
||||
// 返回值:
|
||||
// 配置值
|
||||
// 错误对象
|
||||
func ReadIntJsonValue(config map[string]interface{}, configName string) (value int, err error) {
|
||||
configValue, exist := config[configName]
|
||||
if !exist {
|
||||
err = fmt.Errorf("不存在名为%s的配置或配置为空", configName)
|
||||
return
|
||||
}
|
||||
|
||||
configValue_float, ok := configValue.(float64)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%s必须为int型", configName)
|
||||
return
|
||||
}
|
||||
value = int(configValue_float)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 从config配置中获取string类型的配置值
|
||||
// config:从config文件中反序列化出来的map对象
|
||||
// configName:配置名称
|
||||
// 返回值:
|
||||
// 配置值
|
||||
// 错误对象
|
||||
func ReadStringJsonValue(config map[string]interface{}, configName string) (value string, err error) {
|
||||
configValue, exist := config[configName]
|
||||
if !exist {
|
||||
err = fmt.Errorf("不存在名为%s的配置或配置为空", configName)
|
||||
return
|
||||
}
|
||||
|
||||
configValue_string, ok := configValue.(string)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%s必须为string型", configName)
|
||||
return
|
||||
}
|
||||
value = configValue_string
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 从config配置中获取string类型的配置值
|
||||
// config:从config文件中反序列化出来的map对象
|
||||
// configName:配置名称
|
||||
// 返回值:
|
||||
// 配置值
|
||||
// 错误对象
|
||||
func ReadBoolJsonValue(config map[string]interface{}, configName string) (value bool, err error) {
|
||||
configValue, exist := config[configName]
|
||||
if !exist {
|
||||
err = fmt.Errorf("不存在名为%s的配置或配置为空", configName)
|
||||
return
|
||||
}
|
||||
|
||||
configValue_bool, ok := configValue.(bool)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%s必须为bool型", configName)
|
||||
return
|
||||
}
|
||||
value = configValue_bool
|
||||
|
||||
return
|
||||
}
|
||||
109
trunk/goutil/configUtil/jsonArray.go
Normal file
109
trunk/goutil/configUtil/jsonArray.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package configUtil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// 读取JSON格式的配置文件
|
||||
// config_file_path:配置文件路径
|
||||
// 返回值:
|
||||
// 配置内容的map格式数组
|
||||
// 错误对象
|
||||
func ReadJsonConfig_Array(config_file_path string) ([]map[string]interface{}, error) {
|
||||
// 读取配置文件(一次性读取整个文件,则使用ioutil)
|
||||
bytes, err := ioutil.ReadFile(config_file_path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取配置文件的内容出错:%s", err)
|
||||
}
|
||||
|
||||
// 使用json反序列化
|
||||
config := make([]map[string]interface{}, 0, 4)
|
||||
if err = json.Unmarshal(bytes, &config); err != nil {
|
||||
return nil, fmt.Errorf("反序列化配置文件的内容出错:%s", err)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func getConfigValue(config []map[string]interface{}, configName string) (configValue interface{}, err error) {
|
||||
var exist bool
|
||||
for _, configItem := range config {
|
||||
if configValue, exist = configItem[configName]; exist {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !exist {
|
||||
err = fmt.Errorf("不存在名为%s的配置或配置为空", configName)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 从config配置中获取int类型的配置值
|
||||
// config:从config文件中反序列化出来的map对象
|
||||
// configName:配置名称
|
||||
// 返回值:
|
||||
// 配置值
|
||||
// 错误对象
|
||||
func ReadIntJsonValue_Array(config []map[string]interface{}, configName string) (value int, err error) {
|
||||
configValue, err := getConfigValue(config, configName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
configValue_float, ok := configValue.(float64)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%s必须为int型", configName)
|
||||
return
|
||||
}
|
||||
value = int(configValue_float)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 从config配置中获取string类型的配置值
|
||||
// config:从config文件中反序列化出来的map对象
|
||||
// configName:配置名称
|
||||
// 返回值:
|
||||
// 配置值
|
||||
// 错误对象
|
||||
func ReadStringJsonValue_Array(config []map[string]interface{}, configName string) (value string, err error) {
|
||||
configValue, err := getConfigValue(config, configName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
configValue_string, ok := configValue.(string)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%s必须为string型", configName)
|
||||
return
|
||||
}
|
||||
value = configValue_string
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 从config配置中获取string类型的配置值
|
||||
// config:从config文件中反序列化出来的map对象
|
||||
// configName:配置名称
|
||||
// 返回值:
|
||||
// 配置值
|
||||
// 错误对象
|
||||
func ReadBoolJsonValue_Array(config []map[string]interface{}, configName string) (value bool, err error) {
|
||||
configValue, err := getConfigValue(config, configName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
configValue_bool, ok := configValue.(bool)
|
||||
if !ok {
|
||||
err = fmt.Errorf("%s必须为bool型", configName)
|
||||
return
|
||||
}
|
||||
value = configValue_bool
|
||||
|
||||
return
|
||||
}
|
||||
53
trunk/goutil/configUtil/jsonArray_test.go
Normal file
53
trunk/goutil/configUtil/jsonArray_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package configUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
config_Array []map[string]interface{}
|
||||
err_Array error
|
||||
)
|
||||
|
||||
func TestReadJsonConfig_Array(t *testing.T) {
|
||||
config_Array, err_Array = ReadJsonConfig_Array("testdata/jsonConfigArray.ini")
|
||||
if err_Array != nil {
|
||||
t.Errorf("读取JSON配置失败,错误信息为:%s", err_Array)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadIntJsonValue_Array(t *testing.T) {
|
||||
actualValue, err_Array := ReadIntJsonValue_Array(config_Array, "ServerGroupId")
|
||||
if err_Array != nil {
|
||||
t.Errorf("读取JSON配置失败,错误信息为:%s", err_Array)
|
||||
}
|
||||
|
||||
expectedValue := 1
|
||||
if actualValue != expectedValue {
|
||||
t.Errorf("期望的值为%d,实际的值为%d", expectedValue, actualValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStringJsonValue_Array(t *testing.T) {
|
||||
actualValue, err_Array := ReadStringJsonValue_Array(config_Array, "ChatDBConnection")
|
||||
if err_Array != nil {
|
||||
t.Errorf("读取JSON配置失败,错误信息为:%s", err_Array)
|
||||
}
|
||||
|
||||
expectedValue := "root:moqikaka@tcp(192.168.1.226:3306)/chatserver?charset=utf8&parseTime=true&loc=Local&timeout=30s"
|
||||
if actualValue != expectedValue {
|
||||
t.Errorf("期望的值为%s,实际的值为%s", expectedValue, actualValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadBoolJsonValue_Array(t *testing.T) {
|
||||
actualValue, err_Array := ReadBoolJsonValue_Array(config_Array, "IfRecordMessage")
|
||||
if err_Array != nil {
|
||||
t.Errorf("读取JSON配置失败,错误信息为:%s", err_Array)
|
||||
}
|
||||
|
||||
expectedValue := true
|
||||
if actualValue != expectedValue {
|
||||
t.Errorf("期望的值为%v,实际的值为%v", expectedValue, actualValue)
|
||||
}
|
||||
}
|
||||
53
trunk/goutil/configUtil/json_test.go
Normal file
53
trunk/goutil/configUtil/json_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package configUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
config map[string]interface{}
|
||||
err error
|
||||
)
|
||||
|
||||
func TestReadJsonConfig(t *testing.T) {
|
||||
config, err = ReadJsonConfig("testdata/jsonConfig.ini")
|
||||
if err != nil {
|
||||
t.Errorf("读取JSON配置失败,错误信息为:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadIntJsonValue(t *testing.T) {
|
||||
actualValue, err := ReadIntJsonValue(config, "ServerGroupId")
|
||||
if err != nil {
|
||||
t.Errorf("读取JSON配置失败,错误信息为:%s", err)
|
||||
}
|
||||
|
||||
expectedValue := 1
|
||||
if actualValue != expectedValue {
|
||||
t.Errorf("期望的值为%d,实际的值为%d", expectedValue, actualValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStringJsonValue(t *testing.T) {
|
||||
actualValue, err := ReadStringJsonValue(config, "ChatDBConnection")
|
||||
if err != nil {
|
||||
t.Errorf("读取JSON配置失败,错误信息为:%s", err)
|
||||
}
|
||||
|
||||
expectedValue := "root:moqikaka@tcp(192.168.1.226:3306)/chatserver?charset=utf8&parseTime=true&loc=Local&timeout=30s"
|
||||
if actualValue != expectedValue {
|
||||
t.Errorf("期望的值为%s,实际的值为%s", expectedValue, actualValue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadBoolJsonValue(t *testing.T) {
|
||||
actualValue, err := ReadBoolJsonValue(config, "IfRecordMessage")
|
||||
if err != nil {
|
||||
t.Errorf("读取JSON配置失败,错误信息为:%s", err)
|
||||
}
|
||||
|
||||
expectedValue := true
|
||||
if actualValue != expectedValue {
|
||||
t.Errorf("期望的值为%v,实际的值为%v", expectedValue, actualValue)
|
||||
}
|
||||
}
|
||||
13
trunk/goutil/configUtil/testdata/jsonConfig.ini
vendored
Normal file
13
trunk/goutil/configUtil/testdata/jsonConfig.ini
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"ServerGroupId": 1,
|
||||
"ChatDBConnection": "root:moqikaka@tcp(192.168.1.226:3306)/chatserver?charset=utf8&parseTime=true&loc=Local&timeout=30s",
|
||||
"ChatDBMaxOpenConns": 10,
|
||||
"ChatDBMaxIdleConns": 5,
|
||||
"ModelDBConnection": "root:moqikaka@tcp(192.168.1.226:3306)/dzz_model_online?charset=utf8&parseTime=true&loc=Local&timeout=30s",
|
||||
"ModelDBMaxOpenConns": 0,
|
||||
"ModelDBMaxIdleConns": 0,
|
||||
"GameDBConnection": "root:moqikaka@tcp(192.168.1.226:3306)/dzz_online?charset=utf8&parseTime=true&loc=Local&timeout=30s",
|
||||
"GameDBMaxOpenConns": 10,
|
||||
"GameDBMaxIdleConns": 5,
|
||||
"IfRecordMessage": true
|
||||
}
|
||||
18
trunk/goutil/configUtil/testdata/jsonConfigArray.ini
vendored
Normal file
18
trunk/goutil/configUtil/testdata/jsonConfigArray.ini
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
[
|
||||
{
|
||||
"ServerGroupId": 1,
|
||||
"ChatDBConnection": "root:moqikaka@tcp(192.168.1.226:3306)/chatserver?charset=utf8&parseTime=true&loc=Local&timeout=30s",
|
||||
"ChatDBMaxOpenConns": 10,
|
||||
"ChatDBMaxIdleConns": 5,
|
||||
"ModelDBConnection": "root:moqikaka@tcp(192.168.1.226:3306)/dzz_model_online?charset=utf8&parseTime=true&loc=Local&timeout=30s",
|
||||
"ModelDBMaxOpenConns": 0,
|
||||
"ModelDBMaxIdleConns": 0,
|
||||
"GameDBConnection": "root:moqikaka@tcp(192.168.1.226:3306)/dzz_online?charset=utf8&parseTime=true&loc=Local&timeout=30s",
|
||||
"GameDBMaxOpenConns": 10,
|
||||
"GameDBMaxIdleConns": 5
|
||||
},
|
||||
{
|
||||
"AppId": "DZZ",
|
||||
"IfRecordMessage": true
|
||||
}
|
||||
]
|
||||
299
trunk/goutil/configUtil/xmlConfig.go
Normal file
299
trunk/goutil/configUtil/xmlConfig.go
Normal file
@@ -0,0 +1,299 @@
|
||||
package configUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"goutil/typeUtil"
|
||||
"goutil/xmlUtil"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type XmlConfig struct {
|
||||
root *xmlUtil.Node
|
||||
}
|
||||
|
||||
// 从文件加载
|
||||
// xmlFilePath:xml文件路径
|
||||
// 返回值:
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) LoadFromFile(xmlFilePath string) error {
|
||||
if this.root != nil {
|
||||
return fmt.Errorf("There has been an xml file loaded.")
|
||||
}
|
||||
|
||||
root, err := xmlUtil.LoadFromFile(xmlFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
this.root = root
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 从node节点加载(会取其根节点)
|
||||
// xmlRoot:xml节点
|
||||
// 返回值:
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) LoadFromXmlNode(xmlRoot *xmlUtil.Node) error {
|
||||
if this.root != nil {
|
||||
return fmt.Errorf("There has been an xml file loaded.")
|
||||
}
|
||||
|
||||
if xmlRoot == nil {
|
||||
return fmt.Errorf("xmlRoot is nil")
|
||||
}
|
||||
|
||||
this.root = xmlRoot
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// 返回值:
|
||||
// bool:结果
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) Bool(xpath string, attrName string) (bool, error) {
|
||||
value, err := this.getVal(xpath, attrName)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return typeUtil.Bool(value)
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// defaultVal:默认值
|
||||
// 返回值:
|
||||
// bool:结果
|
||||
func (this *XmlConfig) DefaultBool(xpath string, attrName string, defaultVal bool) bool {
|
||||
value, err := this.Bool(xpath, attrName)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) Int(xpath string, attrName string) (int, error) {
|
||||
value, err := this.getVal(xpath, attrName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return typeUtil.Int(value)
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// defaultVal:默认值
|
||||
// 返回值:
|
||||
// int:结果
|
||||
func (this *XmlConfig) DefaultInt(xpath string, attrName string, defaultVal int) int {
|
||||
value, err := this.Int(xpath, attrName)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// 返回值:
|
||||
// int64:结果
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) Int64(xpath string, attrName string) (int64, error) {
|
||||
value, err := this.getVal(xpath, attrName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return typeUtil.Int64(value)
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// defaultVal:默认值
|
||||
// 返回值:
|
||||
// int64:结果
|
||||
func (this *XmlConfig) DefaultInt64(xpath string, attrName string, defaultVal int64) int64 {
|
||||
value, err := this.Int64(xpath, attrName)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
return value
|
||||
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// 返回值:
|
||||
// float64:结果
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) Float(xpath string, attrName string) (float64, error) {
|
||||
value, err := this.getVal(xpath, attrName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return typeUtil.Float64(value)
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// defaultVal:默认值
|
||||
// 返回值:
|
||||
// float64:结果
|
||||
func (this *XmlConfig) DefaultFloat(xpath string, attrName string, defaultVal float64) float64 {
|
||||
value, err := this.Float(xpath, attrName)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// 返回值:
|
||||
// string:结果
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) String(xpath string, attrName string) (string, error) {
|
||||
return this.getVal(xpath, attrName)
|
||||
}
|
||||
|
||||
// 获取指定xpath路径下的值
|
||||
// xpath:xpath路径
|
||||
// attrName:属性名,如果为空,则返回节点的内部文本
|
||||
// defaultVal:默认值
|
||||
// 返回值:
|
||||
// string:结果
|
||||
func (this *XmlConfig) DefaultString(xpath string, attrName string, defaultVal string) string {
|
||||
value, err := this.String(xpath, attrName)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// 获取指定位置的节点
|
||||
// xpath:xpath路径
|
||||
// 返回值:
|
||||
// []*xmlUtil.Node:结果
|
||||
func (this *XmlConfig) Nodes(xpath string) []*xmlUtil.Node {
|
||||
return this.root.SelectElements(xpath)
|
||||
}
|
||||
|
||||
// 获取指定位置的节点
|
||||
// xpath:xpath路径
|
||||
// 返回值:
|
||||
// *xmlUtil.Node:结果
|
||||
func (this *XmlConfig) Node(xpath string) *xmlUtil.Node {
|
||||
return this.root.SelectElement(xpath)
|
||||
}
|
||||
|
||||
// 反序列化指定的整个节点
|
||||
// xpath:xml的path
|
||||
// data:反序列化得到的数据
|
||||
// 返回值:
|
||||
// error:错误信息
|
||||
func (this *XmlConfig) Unmarshal(xpath string, data interface{}) error {
|
||||
nodeItem := this.Node(xpath)
|
||||
|
||||
//不存在节点,这里直接返回空的就行了
|
||||
if nodeItem == nil {
|
||||
data = nil
|
||||
return nil
|
||||
//return fmt.Errorf("节点不存在,XPATH:%s", xpath)
|
||||
}
|
||||
|
||||
value := reflect.ValueOf(data)
|
||||
if value.Kind() == reflect.Ptr {
|
||||
value = value.Elem()
|
||||
}
|
||||
dataType := value.Type()
|
||||
|
||||
// 依次设置字段值
|
||||
var err error
|
||||
fieldCount := value.NumField()
|
||||
for i := 0; i < fieldCount; i++ {
|
||||
fieldItem := value.Field(i)
|
||||
fieldName := dataType.Field(i).Name
|
||||
|
||||
// 读取数据
|
||||
var valueString string
|
||||
tmpXpath := fmt.Sprintf("%s/%s", xpath, fieldName)
|
||||
if valueString, err = this.getVal(tmpXpath, ""); err != nil {
|
||||
valueString, err = this.getVal(xpath, fieldName)
|
||||
if err != nil {
|
||||
// 压根儿无此字段的配置数据,则略过
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// 字符串转换成目标值
|
||||
fieldValue, err := typeUtil.Convert(valueString, fieldItem.Kind())
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取字段失败, DataType:%s FieldName:%s Value:%v 错误信息:%v ", dataType.Name(), fieldName, valueString, err)
|
||||
}
|
||||
|
||||
// 设置到字段上面
|
||||
valType := reflect.ValueOf(fieldValue)
|
||||
if valType.Type() == fieldItem.Type() {
|
||||
fieldItem.Set(valType)
|
||||
} else {
|
||||
fieldItem.Set(valType.Convert(fieldItem.Type()))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取指定路径的之
|
||||
// xpath:xpath路径
|
||||
// attrName:要获取的属性值,如果为空,则返回内部文本
|
||||
func (this *XmlConfig) getVal(xpath string, attrName string) (val string, err error) {
|
||||
targetRoot := this.root.SelectElement(xpath)
|
||||
if targetRoot == nil {
|
||||
err = fmt.Errorf("no find target node:%v", xpath)
|
||||
return
|
||||
}
|
||||
|
||||
if attrName == "" {
|
||||
val = strings.TrimSpace(targetRoot.InnerText())
|
||||
return
|
||||
}
|
||||
|
||||
exist := false
|
||||
val, exist = targetRoot.SelectAttr(attrName)
|
||||
if exist == false {
|
||||
err = fmt.Errorf("no find target attr, node:%v attr:%v", xpath, attrName)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 创建新的xml配置对象
|
||||
func NewXmlConfig() *XmlConfig {
|
||||
return &XmlConfig{}
|
||||
}
|
||||
329
trunk/goutil/configUtil/xmlConfigList.go
Normal file
329
trunk/goutil/configUtil/xmlConfigList.go
Normal file
@@ -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
|
||||
}
|
||||
135
trunk/goutil/configUtil/xmlConfigList_test.go
Normal file
135
trunk/goutil/configUtil/xmlConfigList_test.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package configUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"goutil/xmlUtil"
|
||||
)
|
||||
|
||||
// bool值读取测试
|
||||
func TestBoolList(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigListData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
booList, errMsg := xmlConfigData.BoolList("html/body/ul/li/a", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("TestBoolList读取到的值:", booList)
|
||||
}
|
||||
|
||||
// int值读取测试
|
||||
func TestIntList(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigListData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
valList, errMsg := xmlConfigData.IntList("html/body/ul/li/a", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("TestInt读取到的值:", valList)
|
||||
}
|
||||
|
||||
// int64值读取测试
|
||||
func TestInt64List(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigListData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
valList, errMsg := xmlConfigData.Int64List("html/body/ul/li/a", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("TestInt64读取到的值:", valList)
|
||||
}
|
||||
|
||||
// Float值读取测试
|
||||
func TestFloatList(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigListData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
valList, errMsg := xmlConfigData.FloatList("html/body/ul/li/a", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("TestFloat读取到的值:", valList)
|
||||
}
|
||||
|
||||
// 字符串读取测试
|
||||
func TestStringList(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigListData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
valList, errMsg := xmlConfigData.StringList("html/body/ul/li/a", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("TestString读取到的值:", valList)
|
||||
}
|
||||
|
||||
func getxmlConfigListData() (xmlConfigData *XmlConfig, errMsg error) {
|
||||
content := `
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Hello</title>
|
||||
<meta name="language" content="en"/>
|
||||
</head>
|
||||
<body IsPost='true'>
|
||||
<h1> This is a H1 </h1>
|
||||
<ul>
|
||||
<li><a id="1" dd='1.1' href="/">Home</a></li>
|
||||
<li><a id="2" href="/about">about</a></li>
|
||||
<li><a id="3" href="/account">login</a></li>
|
||||
<li></li>
|
||||
</ul>
|
||||
<p>
|
||||
Hello,This is an example for gxpath.
|
||||
</p>
|
||||
<footer>footer script</footer>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
var root *xmlUtil.Node
|
||||
root, errMsg = xmlUtil.LoadFromString(content)
|
||||
if errMsg == nil {
|
||||
xmlConfigData = NewXmlConfig()
|
||||
xmlConfigData.LoadFromXmlNode(root)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
321
trunk/goutil/configUtil/xmlConfig_test.go
Normal file
321
trunk/goutil/configUtil/xmlConfig_test.go
Normal file
@@ -0,0 +1,321 @@
|
||||
package configUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"goutil/xmlUtil"
|
||||
)
|
||||
|
||||
// bool值读取测试
|
||||
func TestBool(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var ispost bool
|
||||
ispost, errMsg = xmlConfigData.Bool("html/body", "IsPost")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("读取到的值:", ispost)
|
||||
if ispost == false {
|
||||
t.Error("html/body的isPost读取错误")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
ispost = xmlConfigData.DefaultBool("html/body", "IsPost", false)
|
||||
if ispost == false {
|
||||
t.Error("html/body的isPost读取错误")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// int值读取测试
|
||||
func TestInt(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var id int
|
||||
id, errMsg = xmlConfigData.Int("html/body/ul/li/a[@id=1]", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if id != 1 {
|
||||
t.Errorf("html/body的isPost读取错误,读取到的值:%v", id)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
id = xmlConfigData.DefaultInt("html/body", "id", 2)
|
||||
if id != 2 {
|
||||
t.Error("TestInt html/body的id读取错误")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// int64值读取测试
|
||||
func TestInt64(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var id int64
|
||||
id, errMsg = xmlConfigData.Int64("html/body/ul/li/a[@id=1]", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if id != 1 {
|
||||
t.Errorf("TestInt64 html/body/ul/li/a[@id=1]的id读取错误,读取到的值:%v", id)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
id = xmlConfigData.DefaultInt64("html/body", "id", 2)
|
||||
if id != 2 {
|
||||
t.Error("TestInt64 html/body的id读取错误")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// Float值读取测试
|
||||
func TestFloat(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var id float64
|
||||
id, errMsg = xmlConfigData.Float("html/body/ul/li/a[@id=1]", "dd")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if id != 1.1 {
|
||||
t.Errorf("TestFloat html/body/ul/li/a[@id=1]的id读取错误,读取到的值:%v", id)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
id = xmlConfigData.DefaultFloat("html/body", "id", 2)
|
||||
if id != 2 {
|
||||
t.Error("TestFloat html/body的id读取错误")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// 字符串读取测试
|
||||
func TestString(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var id string
|
||||
id, errMsg = xmlConfigData.String("html/body/ul/li/a[@id=1]", "dd")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
if id != "1.1" {
|
||||
t.Errorf("TestString html/body/ul/li/a[@id=1]的id读取错误,读取到的值:%v", id)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
id = xmlConfigData.DefaultString("html/body", "id", "2")
|
||||
if id != "2" {
|
||||
t.Error("TestString html/body的id读取错误")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
type HelloStruct struct {
|
||||
// 连接字符串
|
||||
ConnectionString string
|
||||
|
||||
// 最大开启连接数量
|
||||
MaxOpenConns int `xml:",attr"`
|
||||
|
||||
// 最大空闲连接数量
|
||||
MaxIdleConns int `xml:",attr"`
|
||||
}
|
||||
|
||||
func (this *HelloStruct) Equal(other *HelloStruct) bool {
|
||||
return this.MaxOpenConns == other.MaxOpenConns && this.MaxIdleConns == other.MaxIdleConns
|
||||
}
|
||||
|
||||
func TestUnmarshal(t *testing.T) {
|
||||
data, _ := getxmlConfigData2(`
|
||||
<DBConnection>
|
||||
<GameServerCenterDB MaxOpenConns="10" MaxIdleConns="5">
|
||||
<![CDATA[
|
||||
root:moqikaka3312@tcp(10.1.0.10:3312)/2_gsc_develop?charset=utf8&parseTime=true&loc=Local&timeout=60s
|
||||
]]>
|
||||
</GameServerCenterDB>
|
||||
</DBConnection>
|
||||
`)
|
||||
|
||||
val := &HelloStruct{}
|
||||
err := data.Unmarshal("/DBConnection/GameServerCenterDB", val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
want := &HelloStruct{
|
||||
ConnectionString: "root:moqikaka3312@tcp(10.1.0.10:3312)/2_gsc_develop?charset=utf8&parseTime=true&loc=Local&timeout=60s",
|
||||
MaxOpenConns: 10,
|
||||
MaxIdleConns: 5,
|
||||
}
|
||||
if want.Equal(val) == false {
|
||||
t.Errorf("Expected %v, but now got %v", want, val)
|
||||
}
|
||||
}
|
||||
|
||||
type ConnsNum int
|
||||
type Hello2Struct struct {
|
||||
// 连接字符串
|
||||
ConnectionString string
|
||||
|
||||
// 最大开启连接数量
|
||||
MaxOpenConns ConnsNum
|
||||
|
||||
// 最大空闲连接数量
|
||||
MaxIdleConns ConnsNum
|
||||
}
|
||||
|
||||
func (this *Hello2Struct) Equal(other *Hello2Struct) bool {
|
||||
return this.MaxOpenConns == other.MaxOpenConns && this.MaxIdleConns == other.MaxIdleConns
|
||||
}
|
||||
|
||||
func TestUnmarshal2(t *testing.T) {
|
||||
data, _ := getxmlConfigData2(`
|
||||
<DBConnection>
|
||||
<GameServerCenterDB MaxOpenConns="10" MaxIdleConns="5">
|
||||
<![CDATA[
|
||||
root:moqikaka3312@tcp(10.1.0.10:3312)/2_gsc_develop?charset=utf8&parseTime=true&loc=Local&timeout=60s
|
||||
]]>
|
||||
</GameServerCenterDB>
|
||||
</DBConnection>
|
||||
`)
|
||||
|
||||
val := &Hello2Struct{}
|
||||
err := data.Unmarshal("/DBConnection/GameServerCenterDB", val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
want := &Hello2Struct{
|
||||
ConnectionString: "root:moqikaka3312@tcp(10.1.0.10:3312)/2_gsc_develop?charset=utf8&parseTime=true&loc=Local&timeout=60s",
|
||||
MaxOpenConns: 10,
|
||||
MaxIdleConns: 5,
|
||||
}
|
||||
if want.Equal(val) == false {
|
||||
t.Errorf("Expected %v, but now got %v", want, val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshal3(t *testing.T) {
|
||||
data, _ := getxmlConfigData2(`
|
||||
<DBConnection>
|
||||
<GameServerCenterDB MaxOpenConns="10">
|
||||
<![CDATA[
|
||||
root:moqikaka3312@tcp(10.1.0.10:3312)/2_gsc_develop?charset=utf8&parseTime=true&loc=Local&timeout=60s
|
||||
]]>
|
||||
</GameServerCenterDB>
|
||||
</DBConnection>
|
||||
`)
|
||||
|
||||
val := &HelloStruct{}
|
||||
err := data.Unmarshal("/DBConnection/GameServerCenterDB", val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
want := &HelloStruct{
|
||||
ConnectionString: "root:moqikaka3312@tcp(10.1.0.10:3312)/2_gsc_develop?charset=utf8&parseTime=true&loc=Local&timeout=60s",
|
||||
MaxOpenConns: 10,
|
||||
MaxIdleConns: 0,
|
||||
}
|
||||
if want.Equal(val) == false {
|
||||
t.Errorf("Expected %v, but now got %v", want, val)
|
||||
}
|
||||
}
|
||||
|
||||
func getxmlConfigData() (xmlConfigData *XmlConfig, errMsg error) {
|
||||
content := `
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Hello</title>
|
||||
<meta name="language" content="en"/>
|
||||
</head>
|
||||
<body IsPost='true'>
|
||||
<h1> This is a H1 </h1>
|
||||
<ul>
|
||||
<li><a id="1" dd='1.1' href="/">Home</a></li>
|
||||
<li><a id="2" href="/about">about</a></li>
|
||||
<li><a id="3" href="/account">login</a></li>
|
||||
<li></li>
|
||||
</ul>
|
||||
<p>
|
||||
Hello,This is an example for gxpath.
|
||||
</p>
|
||||
<footer>footer script</footer>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
var root *xmlUtil.Node
|
||||
root, errMsg = xmlUtil.LoadFromString(content)
|
||||
if errMsg == nil {
|
||||
xmlConfigData = NewXmlConfig()
|
||||
xmlConfigData.LoadFromXmlNode(root)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func getxmlConfigData2(xml string) (xmlConfigData *XmlConfig, errMsg error) {
|
||||
var root *xmlUtil.Node
|
||||
root, errMsg = xmlUtil.LoadFromString(xml)
|
||||
if errMsg == nil {
|
||||
xmlConfigData = NewXmlConfig()
|
||||
xmlConfigData.LoadFromXmlNode(root)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user