1 Star 0 Fork 0

一只北京的小修狗/测试自动化

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
reader.go 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
一只北京的小修狗 提交于 2024-10-02 12:30 +08:00 . 测试
package resource
import (
"bytes"
"encoding/json"
"github.com/bitly/go-simplejson"
"gopkg.in/yaml.v3"
"io/ioutil"
"path/filepath"
)
type format string
const (
formatOther format = "other"
formatJson format = "json"
formatYaml format = "yaml"
)
func read2Json(path string) (*simplejson.Json, error) {
_bytes, e := ioutil.ReadFile(path)
if e != nil {
return nil, e
}
return simplejson.NewJson(_bytes)
}
func loadLocalFile(path string, target interface{}) error {
_bytes, e := ioutil.ReadFile(path)
if e != nil {
return e
}
fm := determineFormat(path)
if fm == formatJson {
d := json.NewDecoder(bytes.NewReader(_bytes))
d.UseNumber()
return d.Decode(target)
}
return unmarshaler(determineFormat(path))(_bytes, target)
}
func determineFormat(path string) format {
switch filepath.Ext(path) {
case ".json":
return formatJson
case ".yaml", ".yml":
return formatYaml
}
return formatOther
}
func unmarshaler(f format) func([]byte, interface{}) error {
switch f {
case formatJson:
return json.Unmarshal
case formatYaml:
return yaml.Unmarshal
}
return func(bytes []byte, i interface{}) error {
return nil
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/beijingxiugo/GolandAuto.git
git@gitee.com:beijingxiugo/GolandAuto.git
beijingxiugo
GolandAuto
测试自动化
v0.2.1

搜索帮助