gparser

package
v0.0.0-...-3631402 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2018 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

数据文件编码/解析.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func VarToJson

func VarToJson(value interface{}) ([]byte, error)

func VarToJsonIndent

func VarToJsonIndent(value interface{}) ([]byte, error)

func VarToStruct

func VarToStruct(value interface{}, obj interface{}) error

将变量解析为对应的struct对象,注意传递的参数为struct对象指针

func VarToToml

func VarToToml(value interface{}) ([]byte, error)

func VarToXml

func VarToXml(value interface{}, rootTag ...string) ([]byte, error)

func VarToXmlIndent

func VarToXmlIndent(value interface{}, rootTag ...string) ([]byte, error)

func VarToYaml

func VarToYaml(value interface{}) ([]byte, error)

Types

type Parser

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

func Load

func Load(path string) (*Parser, error)

func LoadContent

func LoadContent(data []byte, dataType ...string) (*Parser, error)

支持的数据内容格式:json(默认), xml, yaml/yml, toml

func New

func New(values ...interface{}) *Parser

将变量转换为Parser对象进行处理,该变量至少应当是一个map或者array,否者转换没有意义 该参数为非必需参数,默认为创建一个空的Parser对象

func (*Parser) Get

func (p *Parser) Get(pattern ...string) interface{}

根据约定字符串方式访问json解析数据,参数形如: "items.name.first", "list.0"; 当pattern为空时,表示获取所有数据 返回的结果类型的interface{},因此需要自己做类型转换; 如果找不到对应节点的数据,返回nil;

func (*Parser) GetArray

func (p *Parser) GetArray(pattern string) []interface{}

获得一个数组[]interface{},方便操作,不需要自己做类型转换 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil

func (*Parser) GetBool

func (p *Parser) GetBool(pattern string) bool

返回指定json中的bool(false:"", 0, false, off)

func (*Parser) GetFloat32

func (p *Parser) GetFloat32(pattern string) float32

func (*Parser) GetFloat64

func (p *Parser) GetFloat64(pattern string) float64

func (*Parser) GetInt

func (p *Parser) GetInt(pattern string) int

func (*Parser) GetInt16

func (p *Parser) GetInt16(pattern string) int16

func (*Parser) GetInt32

func (p *Parser) GetInt32(pattern string) int32

func (*Parser) GetInt64

func (p *Parser) GetInt64(pattern string) int64

func (*Parser) GetInt8

func (p *Parser) GetInt8(pattern string) int8

func (*Parser) GetMap

func (p *Parser) GetMap(pattern string) map[string]interface{}

获得一个键值对关联数组/哈希表,方便操作,不需要自己做类型转换 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil

func (*Parser) GetString

func (p *Parser) GetString(pattern string) string

返回指定json中的string

func (*Parser) GetStrings

func (p *Parser) GetStrings(pattern string) []string

func (*Parser) GetTime

func (p *Parser) GetTime(pattern string, format ...string) time.Time

func (*Parser) GetTimeDuration

func (p *Parser) GetTimeDuration(pattern string) time.Duration

func (*Parser) GetToVar

func (p *Parser) GetToVar(pattern string, v interface{}) error

将指定的json内容转换为指定结构返回,查找失败或者转换失败,目标对象转换为nil 注意第二个参数需要给的是变量地址

func (*Parser) GetUint

func (p *Parser) GetUint(pattern string) uint

func (*Parser) GetUint16

func (p *Parser) GetUint16(pattern string) uint16

func (*Parser) GetUint32

func (p *Parser) GetUint32(pattern string) uint32

func (*Parser) GetUint64

func (p *Parser) GetUint64(pattern string) uint64

func (*Parser) GetUint8

func (p *Parser) GetUint8(pattern string) uint8

func (*Parser) Remove

func (p *Parser) Remove(pattern string) error

动态删除变量节点

func (*Parser) Set

func (p *Parser) Set(pattern string, value interface{}) error

根据pattern查找并设置数据 注意:写入的时候"."符号只能表示层级,不能使用带"."符号的键名

func (*Parser) SetSplitChar

func (p *Parser) SetSplitChar(char byte)

设置自定义的层级分隔符号

func (*Parser) SetViolenceCheck

func (p *Parser) SetViolenceCheck(check bool)

设置是否执行层级冲突检查,当键名中存在层级符号时需要开启该特性,默认为关闭。 开启比较耗性能,也不建议允许键名中存在分隔符,最好在应用端避免这种情况。

func (*Parser) ToArray

func (p *Parser) ToArray() []interface{}

转换为[]interface{}类型,如果转换失败,返回nil

func (*Parser) ToJson

func (p *Parser) ToJson() ([]byte, error)

func (*Parser) ToJsonIndent

func (p *Parser) ToJsonIndent() ([]byte, error)

func (*Parser) ToMap

func (p *Parser) ToMap() map[string]interface{}

转换为map[string]interface{}类型,如果转换失败,返回nil

func (*Parser) ToStruct

func (p *Parser) ToStruct(o interface{}) error

将变量解析为对应的struct对象,注意传递的参数为struct对象指针

func (*Parser) ToToml

func (p *Parser) ToToml() ([]byte, error)

func (*Parser) ToXml

func (p *Parser) ToXml(rootTag ...string) ([]byte, error)

func (*Parser) ToXmlIndent

func (p *Parser) ToXmlIndent(rootTag ...string) ([]byte, error)

func (*Parser) ToYaml

func (p *Parser) ToYaml() ([]byte, error)

Jump to

Keyboard shortcuts

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