支持任意格式的文件解析
This commit is contained in:
parent
cbac0b676f
commit
fc651441ab
28
file.go
28
file.go
@ -81,6 +81,34 @@ func (f *file) ReadJSONContent(filePath string, result interface{}) error {
|
||||
return JSON.UnmarshalWithNumber(fileContent, result)
|
||||
}
|
||||
|
||||
// ReadAnyFileContent 读取任意类型的文件并解析
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 13:11 2022/7/2
|
||||
func (f *file) ReadAnyFileContent(filePath string, receiver interface{}) error {
|
||||
fileInfoArr := strings.Split(filePath, ".")
|
||||
if len(fileInfoArr) < 2 {
|
||||
return errors.New("未知的文件类型")
|
||||
}
|
||||
var (
|
||||
parseFunc func(filePath string, receiver interface{}) error
|
||||
)
|
||||
|
||||
fileExt := strings.ToLower(fileInfoArr[len(fileInfoArr)-1])
|
||||
switch fileExt {
|
||||
case "json":
|
||||
parseFunc = f.ReadJSONContent
|
||||
case "yml":
|
||||
fallthrough
|
||||
case "yaml":
|
||||
parseFunc = f.ReadYmlContent
|
||||
default:
|
||||
return errors.New(fileExt + " 暂不支持当前格式的文件解析")
|
||||
}
|
||||
return parseFunc(filePath, receiver)
|
||||
}
|
||||
|
||||
// ReadFileContent 读取文件内容
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
|
Loading…
Reference in New Issue
Block a user