支持xml配置文件解析

This commit is contained in:
白茶清欢 2024-10-23 18:41:41 +08:00
parent bfa244f97d
commit b9826a24a0
2 changed files with 26 additions and 3 deletions

25
file.go
View File

@ -8,6 +8,7 @@
package serialize
import (
"encoding/xml"
"errors"
"git.zhangdeman.cn/zhangdeman/consts"
"io"
@ -22,8 +23,6 @@ import (
"git.zhangdeman.cn/zhangdeman/util/define"
"github.com/go-ini/ini"
yml "gopkg.in/yaml.v3"
)
var (
@ -78,7 +77,7 @@ func (f *file) ReadYmlContent(filePath string, result any) error {
if fileContent, err = f.ReadFileContent(filePath); nil != err {
return err
}
return yml.Unmarshal(fileContent, result)
return Yml.UnmarshalWithNumber(fileContent, result)
}
// ReadJSONContent 读取JSON内容,并解析到指定的结构体中
@ -127,6 +126,24 @@ func (f *file) ReadTomlContent(filePath string, result any) error {
return nil
}
// ReadXmlContent ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:39 2024/10/23
func (f *file) ReadXmlContent(filePath string, result any) error {
var (
fileContent []byte
err error
)
if fileContent, err = f.ReadFileContent(filePath); nil != err {
return err
}
return xml.Unmarshal(fileContent, result)
}
// ReadAnyFileContent 读取任意类型的文件并解析
//
// Author : go_developer@163.com<白茶清欢>
@ -153,6 +170,8 @@ func (f *file) ReadAnyFileContent(filePath string, receiver any) error {
parseFunc = f.ReadIniContent
case consts.FileTypeToml:
parseFunc = f.ReadTomlContent
case consts.FileTypeXml:
parseFunc = f.ReadXmlContent
default:
return errors.New(fileExt + " 暂不支持当前格式的文件解析")
}

4
xml.go
View File

@ -13,6 +13,10 @@ import (
"io"
)
var (
Xml = &ownXml{}
)
type ownXml struct{}
func (o *ownXml) UnmarshalWithNumber(byteData []byte, receiver any) error {