docs(添加文档): 为方法添加注释

This commit is contained in:
xiaoqidun 2020-08-01 14:59:17 +08:00
parent 06eddd18e4
commit 620566d3b4

@ -10,6 +10,7 @@ import (
"strings" "strings"
) )
// GoINI GoINI数据结构
type GoINI struct { type GoINI struct {
data []byte data []byte
dataMap map[string]map[string]string dataMap map[string]map[string]string
@ -18,11 +19,14 @@ type GoINI struct {
commonField string commonField string
} }
// NewGoINI 获取GoINI对象
func NewGoINI() *GoINI { func NewGoINI() *GoINI {
return &GoINI{ return &GoINI{
commonField: "BuiltCommon", commonField: "BuiltCommon",
} }
} }
// parse ini配置文件解析器
func (ini *GoINI) parse() { func (ini *GoINI) parse() {
currentName := ini.commonField currentName := ini.commonField
iniData := make(map[string]map[string]string) iniData := make(map[string]map[string]string)
@ -80,6 +84,8 @@ func (ini *GoINI) parse() {
} }
ini.dataMap = iniData ini.dataMap = iniData
} }
// String 获取GoINI对象字符串形式
func (ini *GoINI) String() string { func (ini *GoINI) String() string {
var iniLines []string var iniLines []string
for _, name := range ini.GetNames("") { for _, name := range ini.GetNames("") {
@ -113,10 +119,14 @@ func (ini *GoINI) String() string {
} }
return strings.Join(iniLines, "\n") return strings.Join(iniLines, "\n")
} }
// SetData 从代码读取配置并解析
func (ini *GoINI) SetData(fileData []byte) { func (ini *GoINI) SetData(fileData []byte) {
ini.data = bytes.TrimSpace(fileData) ini.data = bytes.TrimSpace(fileData)
ini.parse() ini.parse()
} }
// LoadFile 从文件读取配置并解析
func (ini *GoINI) LoadFile(fileName string) error { func (ini *GoINI) LoadFile(fileName string) error {
b, err := ioutil.ReadFile(fileName) b, err := ioutil.ReadFile(fileName)
if err != nil { if err != nil {
@ -126,6 +136,8 @@ func (ini *GoINI) LoadFile(fileName string) error {
ini.parse() ini.parse()
return nil return nil
} }
// GetNames 获取配置文件分区列表
func (ini *GoINI) GetNames(match string) []string { func (ini *GoINI) GetNames(match string) []string {
if match == "" { if match == "" {
return ini.nameList return ini.nameList
@ -138,6 +150,8 @@ func (ini *GoINI) GetNames(match string) []string {
} }
return matchNameList return matchNameList
} }
// GetNameKeys 获取分区下配置项列表
func (ini *GoINI) GetNameKeys(name string, match string) []string { func (ini *GoINI) GetNameKeys(name string, match string) []string {
var keyList []string var keyList []string
if name == "" { if name == "" {
@ -157,6 +171,8 @@ func (ini *GoINI) GetNameKeys(name string, match string) []string {
} }
return keyList return keyList
} }
// GetBool 获取单个配置项的布尔值
func (ini *GoINI) GetBool(name string, key string, value bool) bool { func (ini *GoINI) GetBool(name string, key string, value bool) bool {
boolStr := ini.GetString(name, key, "") boolStr := ini.GetString(name, key, "")
switch strings.ToLower(boolStr) { switch strings.ToLower(boolStr) {
@ -170,6 +186,8 @@ func (ini *GoINI) GetBool(name string, key string, value bool) bool {
} }
return value return value
} }
// GetInt64 获取单个配置项的数字值
func (ini *GoINI) GetInt64(name string, key string, value int64) int64 { func (ini *GoINI) GetInt64(name string, key string, value int64) int64 {
int64Str := ini.GetString(name, key, "") int64Str := ini.GetString(name, key, "")
if i, e := strconv.ParseInt(int64Str, 10, 64); e == nil { if i, e := strconv.ParseInt(int64Str, 10, 64); e == nil {
@ -177,6 +195,8 @@ func (ini *GoINI) GetInt64(name string, key string, value int64) int64 {
} }
return value return value
} }
// GetString 获取单个配置项的字符值
func (ini *GoINI) GetString(name string, key string, value string) string { func (ini *GoINI) GetString(name string, key string, value string) string {
if 0 == len(name) { if 0 == len(name) {
name = ini.commonField name = ini.commonField
@ -188,6 +208,8 @@ func (ini *GoINI) GetString(name string, key string, value string) string {
} }
return value return value
} }
// GetFloat64 获取单个配置项的小数值
func (ini *GoINI) GetFloat64(name string, key string, value float64) float64 { func (ini *GoINI) GetFloat64(name string, key string, value float64) float64 {
float64Str := ini.GetString(name, key, "") float64Str := ini.GetString(name, key, "")
if f, e := strconv.ParseFloat(float64Str, 64); e == nil { if f, e := strconv.ParseFloat(float64Str, 64); e == nil {
@ -195,6 +217,8 @@ func (ini *GoINI) GetFloat64(name string, key string, value float64) float64 {
} }
return value return value
} }
// MapToStruct 将配置映射到一个结构体
func (ini *GoINI) MapToStruct(ptr interface{}) (err error) { func (ini *GoINI) MapToStruct(ptr interface{}) (err error) {
t := reflect.TypeOf(ptr) t := reflect.TypeOf(ptr)
v := reflect.ValueOf(ptr) v := reflect.ValueOf(ptr)