提交 78b15011 编写于 作者: 5 520MianXiangDuiXiang520

:bug:fix: 修复ini_tool的一个Bug

上级 d96efc7f
......@@ -3,9 +3,11 @@ package util
import (
"bufio"
"io"
"log"
"os"
"path"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
......@@ -22,10 +24,14 @@ func ReadLines(filePath string) []string {
rd := bufio.NewReader(f)
for {
line, err := rd.ReadString('\n') //以'\n'为结束符读入一行
if err != nil || io.EOF == err {
break
if err != nil && err != io.EOF {
log.Println(err)
panic("Read Error!!")
}
result = append(result, line)
if err == io.EOF {
break
}
}
return result
}
......@@ -37,20 +43,30 @@ func Load(iniPath, block string, s interface{}) {
lines := ReadLines(filename)
t := reflect.TypeOf(s)
v := reflect.ValueOf(s)
//v.Elem().Field(0).SetString("ssss")
offset := 0
for i, line := range lines {
if line != "["+block+"]" {
pat := "(\\[" + block + "\\]).*"
patt, _ := regexp.Compile(pat)
ok := patt.MatchString(line)
if !ok {
continue
}
offset = i
}
lines = lines[offset:]
for _, line := range lines {
if len(line) <= 0 || string(line[0]) == "[" {
if len(line) <= 0 {
continue
}
pat := "(\\[" + block + "\\]).*"
patt, _ := regexp.Compile(pat)
ok := patt.MatchString(line)
if string(line[0]) == "[" && ok {
continue
}
if string(line[0]) == "[" && !ok {
break
}
line = strings.Trim(line, "\n")
line = strings.Trim(line, "\r")
lr := strings.Split(line, "=")
......
......@@ -9,7 +9,7 @@ func TestLoad(t *testing.T) {
// 新建ini,写入信息
fw, err := os.Create("./test.ini")
if err == nil {
_, _ = fw.WriteString("[mysql]\nuser=testName\npassword=testPassword\n")
_, _ = fw.WriteString("[mysql]\nuser=testName\npassword=testPassword\n[redis]\npassword=redisPassword")
}
// 执行Load
......@@ -17,13 +17,21 @@ func TestLoad(t *testing.T) {
User string `ini:"user"`
Password string `ini:"password"`
}{}
var rs = struct {
Password string `ini:"password"`
}{}
Load("./test.ini", "mysql", &s)
Load("./test.ini", "redis", &rs)
// 断言
if s.User != "testName" || s.Password != "testPassword" {
t.Error("LOAD ERROR")
}
if rs.Password != "redisPassword" {
t.Error("LOAD ERROR2" + rs.Password)
}
// 删除文件
defer func() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册