支持解析ini文件

This commit is contained in:
白茶清欢 2022-07-02 13:24:29 +08:00
parent fc651441ab
commit b5ae4cf809
3 changed files with 20 additions and 0 deletions

16
file.go
View File

@ -12,6 +12,8 @@ import (
"os"
"strings"
"github.com/go-ini/ini"
"github.com/pkg/errors"
yml "gopkg.in/yaml.v3"
)
@ -81,6 +83,18 @@ func (f *file) ReadJSONContent(filePath string, result interface{}) error {
return JSON.UnmarshalWithNumber(fileContent, result)
}
// ReadIniContent 读取并解析ini文件
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 13:21 2022/7/2
func (f *file) ReadIniContent(filePath string, result interface{}) error {
if nil == result {
return errors.New("接收读取结果的数据指针为NIL")
}
return ini.MapTo(result, filePath)
}
// ReadAnyFileContent 读取任意类型的文件并解析
//
// Author : go_developer@163.com<白茶清欢>
@ -103,6 +117,8 @@ func (f *file) ReadAnyFileContent(filePath string, receiver interface{}) error {
fallthrough
case "yaml":
parseFunc = f.ReadYmlContent
case "ini":
parseFunc = f.ReadIniContent
default:
return errors.New(fileExt + " 暂不支持当前格式的文件解析")
}

2
go.mod
View File

@ -7,3 +7,5 @@ require (
github.com/spaolacci/murmur3 v1.1.0
gopkg.in/yaml.v3 v3.0.1
)
require github.com/go-ini/ini v1.66.6 // indirect

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/go-ini/ini v1.66.6 h1:h6k2Bb0HWS/BXXHCXj4QHjxPmlIU4NK+7MuLp9SD+4k=
github.com/go-ini/ini v1.66.6/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=