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

4
xml.go
View File

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