Skip to content

Configure API Document

Bob Bai edited this page Oct 17, 2020 · 10 revisions

Overview

Configure 是一个能够解析配置文件,同时根据配置文件的修改做监听与相应的包。

Index

Variables

变量为一些自定义的异常,默认配置选项和用于解析文本的正则表达式。

  1. File 的默认配置项
var (
	DefaultSeparation = "\n"			// 分隔符,默认为换行符
	DefaultDescriptionIdentity = "#"		// 注释符号,默认为 #
)
  1. 解析 Section 名字的正则表达式
var (
	SectionExp = "\\[[a-zA-Z0-9]*\\]"
)
  1. 解析 Description 和键值等式的变量
var (
	Description = "# .*"
	Equation = ".* = .*"
)

func Watch

Watch 是 configure包提供给用户的最主要的接口,这个接口提供了配置文件解析,监听等全部功能。

func Watch(filename string, listenFunc ListenFunc) (configure *File, err error)

func WatchWithOption

WatchWithOption 与 Watch 功能类似,但是通过option参数使得用户能够自定义一些解析选项。

func WatchWithOption(filename string, listenFunc ListenFunc, option Option) (configure *File, err error)

listenFunc 是用户自定义的修改后的事件,其定义如下:

type ListenFunc func(string)

Option

Option 是解析配置文件时的一些设置选项的结构定义。其中包含了配置文件的切割符号,以及用于标识注释的符号,定义如下:

type Option struct {

	// 如何切割给定的配置文件,默认为按行
	Separation string

	// 用于标识注释的符号
	DescriptionIdentity string
}

Watcher

Watcher 是用于接收用户传入的文件路径,listenFunc, 并根据这些参数来解析配置文件,监听配置文件。Watcher的定义如下:

type Watcher struct {
	Done chan bool				// 子进程是否已经完成的管道
	listen ListenFunc			// 配置文件发生改变时
	File *File				// 存储 Watcher 监听的文件的相关信息和解析器
}

File

File 是存放用户的配置文件相关信息(如Reader),解析时的设置,以及解析后的结果,同时提供了能够访问解析得到的结果的区域,键值等方法。 File 的定义如下:

// 配置文件的结构体
type File struct {
	file *os.File			// 读取的配置文件的 reader
	sections []*Section	    // 配置文件中包含的 section
	parser *Parser			// 用于解析配置文件的 parser
}

func Parse

func (f * File) Parse() error

Parse的作用是根据parser中的内容解析配置文件,解析出得结果会放在 sections 中

func SetContent

func (f * File) SetContent(content string) 

SetContent 的作用是设置 parser 中的内容为content,以便之后进行解析

func Section

func (f *File) Section(name string) (s *Section)

Section 会根据name查找名字为 name 的区域s,如果没有找到则返回nil

func Filename

func (f *File) Filename() string

Filename 获取当前读取的配置文件的文件路径

Section

Section 代表着配置文件中的不同区域,它承载了配置文件解析后的结果,同时提供了能够返回其中的键值对的接口。 Section实体的定义如下:

type Section struct {
	name string				// 区域名称
	items []*Item			        // 该区域下的全部 Item
	file *File				// 该区域所属的配置文件
}

func Name

func (s *Section)Name() string

Name 返回了这个 Section 的名称

func AppendInFile

func (s *Section)AppendInFile(f *File)

AppendInFile 将区域s加入配置文件实体f当中

func Key

func (s *Section) Key(name string) (item *Item)

Key 返回了名称为name的该区域下的section,如果不存在名称为 name 的section则返回nil

func NewSection

func NewSection(name string, content []string, f *File) (s *Section, e error)

NewSection 返回一个名称为 name,内容为 content,所属的配置文件实体为 f 的section

Item

Item 是存放键值对的实体,提供了获取 key, value 以及 description 的接口。 Item 的定义如下:

type Item struct {
	name string			// 这个配置项的名称
	val string			// 配置项的值
	description string		// 配置项的描述
	section *Section		// 上一级的Section
}

func Val

func (t *Item) Val() string

Val 获取这个 Item 对应的 Value

func Key

func (t *Item) Name() string

Name 获取这个 Item 对应的 Name

func Description

func (t *Item) Description() string

Description 获取这个 Item 对应的描述

func SetSection

func (t *Item) SetSection(s *Section)

SetSection 将 t 所属的 Section 定义为 s

func NewItem

func NewItem(section *Section, s string, description string) *Item

NewItem 返回属于 section,名字为s,描述为description的 Item