xini

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

inigo (go ini 文件解析库)

  • @author Joshua Conero
  • @descrip ini 文件解析器

项目管理

  • master 主分支;用户可下载使用
  • alpha 开发数据分支(develop),程序开发不直接操作master 而由开发该分组再合并到主分支
  • demo 项目实际测试;
  • document 项目文档
  • v{n} 历史版本分支,历史保存

设计

  • Parser 解析器接口
    • BaseParser 默认ini 文件解析器
    • RongParser rong ini 文件解析器
    • TomlParser toml 文件解析的支持
  • FileParser 文件解析器接口
  • StrParser 字符串解析器接口
Base 解析器

支持基本的 ini 文件解析,和简单的扩展语法

支持类型


与 go 语言特性紧密结合

bool
b1 = true				// 不区分大小写
b2 = false

int64
i = 56

float64
f64 = 78.455

string
s1 = 字符串,无效引号
s2 = '可使用单引号'
s3 = "依赖可用双引号"

array/slice
// 单行数组
inta = 1, 5, 4, 6, 7, 9
floata = 7.54, 6.24, 74.24
stra = tttt, kdjd, ddd
stra2 = "ffff,fff", 'hhhh', ttt
stra2 = "ffff,fff", 'hhhh', "ttt"


map
// 简单二级"."操作,不能大于三年级如: map.c1.c2
// 该写法与 PHP.ini 配置文件相识,亦可考虑设置开关键
// map[interface{}]interface{}
m.name = map 数据类型处理
m.78 = 5555

指定定义变量/引用值

; 定义变量
$var = 85
author = Joshua Conero


str = "the var is : $var"     	; the var is : 85
str2 = 'the var is : $var'     	; the var is : $var
str3 = "the var is : &author"   ; the var is : Joshua Conero

分支

  • v0.x 版本
    • 详情
    • 开发周期: @20170119 - 20170424 v1.x 版本 (开发中)
    • 开始: 20171028 -> v2.x (版本)
      • 通过对 go 语言的学习重新库;v1.x中项目设计多数受其他语言的影响,完全按照go语言的风格。

使用

安装
# github
$ go get -u  github.com/conero/inigo

获取解析器
// 获取默认解析器(BaseParser)
ini := inigo.NewParser()

rong := inigo.NewParser("rong")
//或者
rong2 := inigo.NewParser(map[string]interface{}{
    "driver": "rong"
})

v2.x (20180819 - )

v2.0 第二版本的初始版本,项目开发中。到 v2.1 将趋于稳定

特性
  • 使用新的 git 管理方式;见 项目管理
  • 程序测试使用go语言提供的 test 测试程序
  • 删除项目中与库无关的文件夹,转移至分支

go 开发环境:

Documentation

Overview

Package xini parse ini config files and utils.

Index

Constants

View Source
const (
	SupportNameIni  = "ini"
	SupportNameRong = "rong"
	SupportNameToml = "toml"
)
View Source
const (
	Author      = "Joshua Conero" // author 作者
	Name        = "conero/ini"
	Version     = "2.1.0-alpha.4"         // version	版本号
	Release     = "20190617"              // build 发布日期
	Description = "ini parser for golang" // name 名称
	Since       = "20171028"              // start 开始时间
	Copyright   = "@Conero"               // copyright 版权
)

Variables

View Source
var IniParseSettings map[string]string = map[string]string{
	"equal":          "=",
	"comment":        "#|;",
	"mcomment1":      "'''",
	"mcomment2":      "'''",
	"limiter":        ",",
	"scope1":         "{",
	"scope2":         "}",
	"reg_comment":    "^[#;]",
	"reg_section":    "^\\[[^\\[^\\]}]*\\]$",
	"reg_section_sg": "(\\[)|(\\])",
	"reg_scope":      "\\{[^\\{^\\}]*\\}",

	"reg_scope_sg":    "^\\{.*\\}$",
	"mlstring":        `"|'`,
	"reg_clear_mls":   `"|'`,
	"reg_has_comment": `#|;`,

	"reg_is_mlstring":    `^[\w]+[=\s]+("|'){1}[^"']*$`,
	"reg_is_mlstring_nk": `^["']{1}[^"'\,]+$`,
	"reg_mlstring_sta":   `^['"].*`,

	"reg_mlstring_end": `^[^"'=]*['"\,]+$`,
}

IniParseSettings ini-parse set base

View Source
var TranCommentMap map[string]string = map[string]string{
	`\;`: "_JC__COMMIT1",
	`\#`: "_JC__COMMIT2",
}

TranCommentMap comment character parsing

View Source
var TranStrMap map[string]string = map[string]string{
	`\,`: "_JC__COMMA",
	`\{`: "_L__BRACE",
	`\}`: "_R__BRACE",
	`\=`: "_JC__EQUAL",
}

TranStrMap transfer character parsing

Functions

func Marshal added in v1.3.0

func Marshal(v any) ([]byte, error)

Marshal returns the ini encoding of v.

func ParseValue

func ParseValue(v string) any

ParseValue parse the data value, the format like

bool:   true/false/TRUE/FALSE
string: '字符串', "字符串" 以及无法解析的参数
int64: 47, 52, -49552
float64: 3.14, -0.24552
null: nil     空值时,默认为 nil

func StrClear

func StrClear(s string) string

StrClear the string data clear

Types

type BaseParser

type BaseParser struct {
	Container
	// contains filtered or unexported fields
}

BaseParser base and default file parse, support the standard ini configure file

func (*BaseParser) Del

func (p *BaseParser) Del(key string) bool

func (BaseParser) Driver

func (p BaseParser) Driver() string

Driver the current reader driver type

func (*BaseParser) ErrorMsg

func (p *BaseParser) ErrorMsg() string

ErrorMsg get the last error message

func (*BaseParser) Get

func (p *BaseParser) Get(key string) (bool, any)

func (*BaseParser) GetAllSection

func (p *BaseParser) GetAllSection() []string

func (*BaseParser) GetDef

func (p *BaseParser) GetDef(key string, def any) any

func (*BaseParser) HasKey

func (p *BaseParser) HasKey(key string) bool

func (*BaseParser) IsValid

func (p *BaseParser) IsValid() bool

func (*BaseParser) OpenFile

func (p *BaseParser) OpenFile(filename string) Parser

func (*BaseParser) Raw

func (p *BaseParser) Raw(key string) string

Raw get the raw value that not parse to what the data by itself

func (*BaseParser) ReadStr

func (p *BaseParser) ReadStr(content string) Parser

func (*BaseParser) Save

func (p *BaseParser) Save() bool

func (*BaseParser) SaveAsFile

func (p *BaseParser) SaveAsFile(filename string) bool

func (*BaseParser) Section

func (p *BaseParser) Section(params ...any) any

func (*BaseParser) Set

func (p *BaseParser) Set(key string, value any) Parser

func (*BaseParser) SetFunc

func (p *BaseParser) SetFunc(key string, regFn func() any) Parser

type BaseStrParse

type BaseStrParse struct {
	// contains filtered or unexported fields
}

BaseStrParse base string to parse using ini syntax

func (*BaseStrParse) GetData

func (p *BaseStrParse) GetData() map[any]any

func (*BaseStrParse) Line

func (p *BaseStrParse) Line() int

func (*BaseStrParse) LoadContent

func (p *BaseStrParse) LoadContent(content string) StrParser

type Container

type Container struct {
	Data map[any]any
	// contains filtered or unexported fields
}

Container abstract data container

func (*Container) Del

func (c *Container) Del(key any) bool

Del del key from container

func (*Container) Get

func (c *Container) Get(key any) (bool, any)

func (*Container) GetData

func (c *Container) GetData() map[any]any

GetData the data of `Container`

func (*Container) GetDef

func (c *Container) GetDef(key any, def any) any

GetDef get value by key with default.

func (*Container) HasKey

func (c *Container) HasKey(keys ...any) bool

HasKey checkout if keys exist

func (*Container) Merge

func (c *Container) Merge(data map[any]any) *Container

Merge merge data from map set

func (*Container) Reset

func (c *Container) Reset() *Container

Reset reset container will del all data

func (*Container) Set

func (c *Container) Set(key, value any) *Container

Set set container value

func (*Container) SetFunc

func (c *Container) SetFunc(key string, fn func() any) *Container

SetFunc set func to container

func (*Container) Value

func (c *Container) Value(params ...any) any

Value the container get or set value Container.Value(key interface{}) get data value Container.Value(key, value interface{}) set data value Container.Value(key, nil, def interface{}) get data value with default c.GetDef(key, def interface{})

type Encoder added in v1.3.0

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder added in v1.3.0

func NewEncoder() *Encoder

func (*Encoder) Put added in v1.3.0

func (c *Encoder) Put(v any)

type FileParser

type FileParser interface {
	Line() int // 获取总行数
	GetData() map[any]any
}

FileParser File parser

type LnReader

type LnReader struct {
	Filename string // 文件名
	// contains filtered or unexported fields
}

LnReader the lines of file reader

func NewLnRer

func NewLnRer(filename string) *LnReader

func (*LnReader) Scan

func (ln *LnReader) Scan(callback func(line string)) bool

Scan scan file lines

type Parser

type Parser interface {
	Get(key string) (bool, any)
	GetDef(key string, def any) any
	HasKey(key string) bool
	// SetFunc 函数式值获取
	// 实现如动态值获取,类似 js 对象中的 [get function()]
	SetFunc(key string, regFn func() any) Parser

	// Raw 支持多级数据访问,获取元素数据
	// 实际读取的原始数据为 map[string]string
	Raw(key string) string

	// Value get or set value: key, value(nil), default
	Value(params ...any) any

	GetAllSection() []string
	// Section the param format support
	//		1.     fun Section(section, key string) 	二级访问
	//		2.     fun Section(format string) 			点操作
	Section(params ...any) any

	GetData() map[any]any

	Set(key string, value any) Parser // 设置值
	Del(key string) bool              // 删除键值

	IsValid() bool
	OpenFile(filename string) Parser
	ReadStr(content string) Parser
	ErrorMsg() string // 错误信息

	Save() bool
	SaveAsFile(filename string) bool
	Driver() string
}

Parser the ini file base parse interface

func NewParser

func NewParser(params ...any) Parser

NewParser instantiate the Parser param format(single param)

opts map[string]string{}|string
	driver SupportNameRong SupportNameIni

default BaseParser

type RongParser

type RongParser struct {
	BaseParser
}

RongParser the Rong Parser inherited from BaseParser

func (RongParser) Driver

func (p RongParser) Driver() string

func (*RongParser) MoreToDo

func (p *RongParser) MoreToDo() string

MoreToDo @need todo

type StrParser

type StrParser interface {
	Line() int
	GetData() map[any]any
	LoadContent(content string) StrParser
}

StrParser the string parse

type TomlParser

type TomlParser struct {
	BaseParser
}

TomlParser Toml file syntax parse @todo need todo

func (TomlParser) Driver

func (p TomlParser) Driver() string

func (*TomlParser) MoreToDo

func (p *TomlParser) MoreToDo() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL