10 Commits
v1.0.0 ... main

Author SHA1 Message Date
0edc5682de docs(更新文档): 更新文档 2024-06-12 13:49:07 +08:00
9f5b09376b feat(废弃函数): 替换废弃函数 2024-06-12 13:41:33 +08:00
898da94c95 Update README.md
All checks were successful
continuous-integration/drone/push Build is passing
2021-01-21 17:30:45 +08:00
eeeb8b179a Update README.md 2021-01-21 17:30:25 +08:00
09ce6a39f0 feat(映射结构): 支持自定义映射结构体的tag键名 2020-12-07 16:43:09 +08:00
b8fd6932d7 Merge branch 'master' of github.com:xiaoqidun/goini 2020-11-26 11:04:09 +08:00
6310bae091 refactor(优化代码): 优化代码 2020-11-26 11:04:00 +08:00
0eb4132cab Update README.md 2020-10-21 14:45:00 +08:00
fce8036b7c Update README.md 2020-10-21 14:43:37 +08:00
4349dffbe5 chore(自动测试): 自动测试代码 2020-09-11 21:34:10 +08:00
5 changed files with 45 additions and 13 deletions

10
.drone.yml Normal file
View File

@ -0,0 +1,10 @@
kind: pipeline
type: docker
name: default
steps:
- name: build
pull: if-not-exists
image: golang
commands:
- go build goini.go

2
.gitignore vendored
View File

@ -1 +1,3 @@
.idea/ .idea/
.vscode/
.devcontainer/

View File

@ -1,26 +1,40 @@
# GoINI # GoINI [![PkgGoDev](https://pkg.go.dev/badge/github.com/xiaoqidun/goini)](https://pkg.go.dev/github.com/xiaoqidun/goini)
简单易用的Golang INI配置解析库 简单易用的Golang INI配置解析库
# 安装方法 # 安装方法
```shell
go get -u github.com/xiaoqidun/goini go get -u github.com/xiaoqidun/goini
```
# 读取配置 # 读取配置
## 从文件读取配置 ## 从文件读取配置
```go ```go
//初始GoINI对象 // 初始GoINI对象
ini := goini.NewGoINI() ini := goini.NewGoINI()
//从文件获取配置 // 从文件获取配置
if err := ini.LoadFile("./config.ini"); err != nil { if err := ini.LoadFile("./config.ini"); err != nil {
log.Println(err) log.Println(err)
return return
} }
``` ```
## 从字符读取配置 ## 从字符读取配置
```go ```go
//初始GoINI对象 // 初始GoINI对象
ini := goini.NewGoINI() ini := goini.NewGoINI()
//从字符获取配置 // 从字符获取配置
ini.SetData([]byte("")) ini.SetData([]byte(""))
``` ```
# 注释方法 # 注释方法
goini将;或#开头的行识别为注释信息 goini将;或#开头的行识别为注释信息
# 分区支持 # 分区支持
goini将[](英文中括号)识别为分区 goini将[](英文中括号)识别为分区

2
go.mod
View File

@ -1,3 +1,3 @@
module github.com/xiaoqidun/goini module github.com/xiaoqidun/goini
go 1.13 go 1.20

View File

@ -3,7 +3,7 @@ package goini
import ( import (
"bytes" "bytes"
"errors" "errors"
"io/ioutil" "os"
"reflect" "reflect"
"regexp" "regexp"
"strconv" "strconv"
@ -12,6 +12,7 @@ import (
// GoINI GoINI数据结构 // GoINI GoINI数据结构
type GoINI struct { type GoINI struct {
tag string
data []byte data []byte
dataMap map[string]map[string]string dataMap map[string]map[string]string
nameList []string nameList []string
@ -22,6 +23,7 @@ type GoINI struct {
// NewGoINI 获取GoINI对象 // NewGoINI 获取GoINI对象
func NewGoINI() *GoINI { func NewGoINI() *GoINI {
return &GoINI{ return &GoINI{
tag: "goini",
commonField: "BuiltCommon", commonField: "BuiltCommon",
} }
} }
@ -120,6 +122,11 @@ func (ini *GoINI) String() string {
return strings.Join(iniLines, "\n") return strings.Join(iniLines, "\n")
} }
// SetTag 设置结构体的tag键名称
func (ini *GoINI) SetTag(tag string) {
ini.tag = tag
}
// SetData 从代码读取配置并解析 // SetData 从代码读取配置并解析
func (ini *GoINI) SetData(fileData []byte) { func (ini *GoINI) SetData(fileData []byte) {
ini.data = bytes.TrimSpace(fileData) ini.data = bytes.TrimSpace(fileData)
@ -128,7 +135,7 @@ func (ini *GoINI) SetData(fileData []byte) {
// LoadFile 从文件读取配置并解析 // LoadFile 从文件读取配置并解析
func (ini *GoINI) LoadFile(fileName string) error { func (ini *GoINI) LoadFile(fileName string) error {
b, err := ioutil.ReadFile(fileName) b, err := os.ReadFile(fileName)
if err != nil { if err != nil {
return err return err
} }
@ -225,15 +232,14 @@ func (ini *GoINI) MapToStruct(ptr interface{}) (err error) {
if t.Kind() != reflect.Ptr { if t.Kind() != reflect.Ptr {
err = errors.New("input struct ptr") err = errors.New("input struct ptr")
return return
} else {
t = t.Elem()
v = v.Elem()
} }
t = t.Elem()
v = v.Elem()
for i := 0; i < t.NumField(); i++ { for i := 0; i < t.NumField(); i++ {
if !v.CanInterface() { if !v.CanInterface() {
continue continue
} }
k := t.Field(i).Tag.Get("goini") k := t.Field(i).Tag.Get(ini.tag)
if k == "" { if k == "" {
k = t.Field(i).Name k = t.Field(i).Name
} }
@ -245,7 +251,7 @@ func (ini *GoINI) MapToStruct(ptr interface{}) (err error) {
if !v.CanInterface() { if !v.CanInterface() {
continue continue
} }
kk := tt.Field(ii).Tag.Get("goini") kk := tt.Field(ii).Tag.Get(ini.tag)
if kk == "" { if kk == "" {
kk = t.Field(ii).Name kk = t.Field(ii).Name
} }