toml

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2014 License: BSD-2-Clause Imports: 10 Imported by: 6

README

tom-toml

TOML format parser for Golang

This library supports TOML version v0.2.0

wercker status

中文 README 更详尽.

Import

import "github.com/achun/tom-toml"

Usage

Say you have a TOML file that looks like this:

[servers.alpha]
ip = "10.0.0.1" # IP
dc = "eqdc10"

[smtpAuth]
Identity = ""
Username = "Do_Not_Reply"
Password = "password"
Host     = "example.com"
Subject  = "message"
To       = ["me@example.com","you@example.com"]

Read the ip and dc like this:

package main

import (
	"fmt"
	"github.com/achun/tom-toml"
)

type smtpAuth struct {
	Identity string
	Username string
	Password string
	Host     string
	Subject  string
	To       []string
}

func main() {
	conf, err := toml.LoadFile("good.toml")

	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(conf["servers.alpha.ip"].String())
	fmt.Println(conf["servers.alpha.dc"].String())

	sa := smtpAuth{}
	auth := conf.Fetch("smtpAuth") // case sensitive

	sa.To = make([]string, auth["To"].Len())
	auth.Apply(&sa)

	fmt.Println(sa)
}

outputs:

10.0.0.1
eqdc10
{ Do_Not_Reply password example.com message [me@example.com you@example.com]}

Documentation

The documentation is available at gowalker.org.

Contribute

Feel free to report bugs and patches using GitHub's pull requests system on achun/tom-toml. Any feedback would be much appreciated!

License

Copyright (c) 2014, achun All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Documentation

Overview

* 硬编码实现 PEG.

Index

Constants

View Source
const (
	EOF       = 0xF8
	RuneError = 0xFFFD
)
View Source
const (
	BOM = 0xFEFF // UTF-8 encoded byte order mark
)

Variables

View Source
var (
	NotSupported  = errors.New("not supported")
	OutOfRange    = errors.New("out of range")
	InternalError = errors.New("internal error")
	InvalidItem   = errors.New("invalid Item")
)
View Source
var (
	InValidFormat = errors.New("invalid TOML format")
	Redeclared    = errors.New("duplicate definitionin")
)

Functions

This section is empty.

Types

type Item

type Item struct {
	*Value
}

Item 扩展自 *Value,支持 ArrayOfTables.

func GenItem

func GenItem(kind Kind) Item

GenItem 函数返回一个新 Item. 为保持格式化输出次序, GenItem 内部使用了一个计数器. 使用者应该使用该函数来得到新的 Item. 而不是用 new(Item) 获得. 那样的话就无法保持格式化输出次序.

func (Item) AddTable

func (i Item) AddTable(tm Toml) error

* 如果是 ArrayOfTables 追加 toml, 返回发生的错误.

func (Item) Apply

func (it Item) Apply(dst interface{}) (count int)

func (Item) Len

func (i Item) Len() int

* Len 返回数组类型的元素个数. 否则返回 -1.

func (Item) Table

func (i Item) Table(idx int) Toml

* 如果是 ArrayOfTables 返回下标为 idx 的 Toml, 否则返回 nil. 支持倒序下标.

func (Item) TomlArray

func (i Item) TomlArray() TomlArray

* 如果是 ArrayOfTables 返回 TomlArray, 否则返回 nil. 使用返回的 TomlArray 时, 注意其数组特性.

type Kind

type Kind uint

Kind 用来标识 TOML 所有的规格. 对于用于配置的 TOML 定义, Kind 其实是分段定义和值定义的. 由于 TOML 官方没有使用 段/值 这样的词汇, tom-toml 选用规格这个词.

const (
	InvalidKind Kind = iota
	String
	Integer
	Float
	Boolean
	Datetime
	StringArray
	IntegerArray
	FloatArray
	BooleanArray
	DatetimeArray
	Array
	// TableName 因为 tom-toml 支持注释的原因, 不存储具体值数据, 只存储规格本身信息.
	// 又因为 Toml 是个 map, 本身就具有 key/value 的存储功能, 所以无需另外定义 Table
	TableName
	ArrayOfTables
)

don't change order

func (Kind) String

func (k Kind) String() string

type Scanner

type Scanner interface {
	Fetch(skip bool) string
	Rune() rune
	Next() rune
	Eof() bool
	LastLine() (int, int, string)
}

func NewScanner

func NewScanner(source []byte) Scanner

first scanner

type Status

type Status int
const (
	SNot Status = iota
	SInvalid
	SUnexpected
	SMaybe
	SYes
	SYesKeep // SYes 并且多读了一个字符, 保持当前的字符给后续解析
)

func (Status) String

func (t Status) String() string

type Token

type Token uint

func (Token) String

func (t Token) String() string

type TokenHandler

type TokenHandler func(Token, string) error

type Toml

type Toml map[string]Item

Toml 是一个 maps, 不是 tree 实现.

func LoadFile

func LoadFile(path string) (toml Toml, err error)

Create a Toml from a file. 便捷方法, 从 TOML 文件解析出 Toml 对象.

func New

func New() Toml

Must be use New() to got Toml, do not use Toml{}. 新建一个 Toml, 必须使用 New() 函数, 不要用 Toml{}. 因为 Toml 的实现需要有一个用于管理的 Id Value, New() 可以做到.

func Parse

func Parse(source []byte) (tm Toml, err error)

从 TOML 格式 source 解析出 Toml 对象.

func (Toml) Apply

func (p Toml) Apply(dst interface{}) (count int)

Apply to each field in the struct, case sensitive. * Apply 把 p 存储的值赋给 dst , TypeOf(dst).Kind() 为 reflect.Struct, 返回赋值成功的次数.

func (Toml) Fetch

func (p Toml) Fetch(prefix string) Toml

* such as:

p.Fetch("")       // returns all valid elements in p
p.Fetch("prefix") // same as p.Fetch("prefix.")

从 Toml 中提取出 prefix 开头的所有 Table 元素, 返回值也是一个 Toml. 注意:

返回值是原 Toml 的子集.
返回子集中不包括 [prefix] TableName.
对返回子集添加 *Item 不会增加到原 Toml 中.
对返回子集中的 *Item 进行更新, 原 Toml 也会更新.
子集中不会含有 ArrayOfTables 类型数据.

func (Toml) Id

func (tm Toml) Id() Value

* Id 返回用于管理的 ".ID..." 对象副本. 如果 Id 不存在, 会自动建立一个, 但这不能保证顺序的可靠性.

func (Toml) String

func (p Toml) String() string

String returns TOML layout string. 格式化输出带缩进的 TOML 格式.

func (Toml) TableNames

func (p Toml) TableNames() (tableNames []string, arrayOfTablesNames []string)

TableNames returns all name of TableName and ArrayOfTables. 返回所有 TableName 的名字和 ArrayOfTables 的名字.

type TomlArray

type TomlArray []Toml

for ArrayOfTables

func (TomlArray) Index

func (t TomlArray) Index(idx int) Toml

func (TomlArray) Len

func (t TomlArray) Len() int

type Value

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

Value 用来存储 String 至 Array 的值.

func NewValue

func NewValue(kind Kind) *Value

NewValue 函数返回一个新 *Value. 为保持格式化输出次序, NewValue 内部使用了一个计数器. 使用者应该使用该函数来得到新的 *Value. 而不是用 new(Value) 获得. 那样的话就无法保持格式化输出次序.

func (*Value) Add

func (p *Value) Add(ai ...interface{}) error

Add element for Array or typeArray. * Add 方法为数组添加元素, 支持空数组元素. p 本身的 Kind 决定是否支持参数元素 Kind.

func (*Value) Apply

func (it *Value) Apply(dst interface{}) (count int)

func (*Value) Boolean

func (p *Value) Boolean() bool

* 如果值是 Boolean 可以使用 Boolean 返回其 bool 值. 否则返回 false

func (*Value) BooleanArray

func (p *Value) BooleanArray() []bool

func (*Value) Comment

func (p *Value) Comment() string

returns end-of-line comment

func (*Value) Comments

func (p *Value) Comments() []string

returns multiline comments

func (*Value) Datetime

func (p *Value) Datetime() time.Time

* 如果值是 Datetime 可以使用 Datetime 返回其 time.Time 值. 否则返回UTC时间公元元年1月1日 00:00:00. 可以用 IsZero() 进行判断.

func (*Value) DatetimeArray

func (p *Value) DatetimeArray() []time.Time

func (*Value) Float

func (p *Value) Float() float64

* 如果值是 Float 可以使用 Float 返回其 float64 值. 否则返回 0

func (*Value) FloatArray

func (p *Value) FloatArray() []float64

func (*Value) Id

func (p *Value) Id() int

* Id 返回 int 值, 此值表示 Value 在运行期中生成次序的唯一序号. 返回 0 表示该 Value 无效.

func (*Value) Index

func (p *Value) Index(idx int) *Value

* Index 根据 idx 下标返回类型数组或二维数组对应的元素. idx 可以用负数作为下标. 如果非数组或者下标超出范围返回 nil.

func (*Value) Int

func (p *Value) Int() int64

* 如果值是 Integer 可以使用 Int 返回其 int64 值. 否则返回 0

func (*Value) IntArray

func (p *Value) IntArray() []int64

func (*Value) Integer

func (p *Value) Integer() int

* 如果值是 Integer 可以使用 Integer 返回其 int 值. 否则返回 0

func (*Value) IntegerArray

func (p *Value) IntegerArray() []int

func (*Value) IsValid

func (p *Value) IsValid() bool

IsValid 返回 p 是否有效.

func (*Value) IsValue

func (p *Value) IsValue() bool

IsValue 返回 p 是否存储了值数据

func (*Value) Kind

func (p *Value) Kind() Kind

Kind 返回数据的风格.

func (*Value) KindIs

func (p *Value) KindIs(kind ...Kind) bool

func (*Value) Len

func (p *Value) Len() int

* Len 返回数组类型元素个数. 否则返回 -1.

func (*Value) Set

func (p *Value) Set(x interface{}) error

Set 用来设置 *Value 要存储的具体值. 参数 x 的类型范围可以是 String,Integer,Float,Boolean,Datetime 之一 如果 *Value 的 Kind 是 InvalidKind(也就是没有明确值类型), 调用 Set 后, *Value 的 kind 会相应的更改, 否则要求 x 的类型必须符合 *Value 的 kind Set 失败会返回 NotSupported 错误.

func (*Value) SetAs

func (p *Value) SetAs(s string, kind Kind) (err error)

SetAs是个便捷方法, 通过参数 kind 对 string 参数进行转换并执行 Set.

func (*Value) SetComment

func (p *Value) SetComment(s string)

set end-of-line comment

func (*Value) SetComments

func (p *Value) SetComments(as []string)

set multiline comments

func (*Value) String

func (p *Value) String() string

String 返回 *Value 存储数据的字符串表示. 注意所有的规格定义都是可以字符串化的.

func (*Value) StringArray

func (p *Value) StringArray() []string

func (*Value) UInt

func (p *Value) UInt() uint64

* 如果值是 Integer 可以使用 UInt 返回其 uint64 值. 否则返回 0

func (*Value) UIntArray

func (p *Value) UIntArray() []uint64

func (*Value) UInteger

func (p *Value) UInteger() uint

* 如果值是 Integer 可以使用 UInteger 返回其 uint 值. 否则返回 0. 注意此方法不检查值是否为负数.

func (*Value) UIntegerArray

func (p *Value) UIntegerArray() []uint

Jump to

Keyboard shortcuts

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