gparser

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2019 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package gparser provides convenient API for accessing/converting variable and JSON/XML/YAML/TOML.

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 VarToJsonIndentString

func VarToJsonIndentString(value interface{}) (string, error)

func VarToJsonString

func VarToJsonString(value interface{}) (string, error)

func VarToStruct

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

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, unsafe ...bool) (*Parser, error)

Load loads content from specified file <path>, and creates a Parser object from its content.

func LoadContent

func LoadContent(data interface{}, unsafe ...bool) (*Parser, error)

LoadContent creates a Parser object from given content, it checks the data type of <content> automatically, supporting JSON, XML, YAML and TOML types of data.

func New

func New(value interface{}, unsafe ...bool) *Parser

New creates a Parser object with any variable type of <data>, but <data> should be a map or slice for data access reason, or it will make no sense. The <unsafe> param specifies whether using this Parser object in un-concurrent-safe context, which is false in default.

func NewUnsafe

func NewUnsafe(value ...interface{}) *Parser

NewUnsafe creates a un-concurrent-safe Parser object.

func (*Parser) Append

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

Append appends value to the value by specified <pattern>. The target value by <pattern> should be type of slice.

func (*Parser) Dump

func (p *Parser) Dump()

Dump prints current Json object with more manually readable.

func (*Parser) Export

func (p *Parser) Export() string

func (*Parser) Get

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

Get returns value by specified <pattern>. It returns all values of current Json object, if <pattern> is empty or not specified. It returns nil if no value found by <pattern>.

We can also access slice item by its index number in <pattern>, eg: "items.name.first", "list.10".

It returns a default value specified by <def> if value for <pattern> is not found.

func (*Parser) GetArray

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

GetArray gets the value by specified <pattern>, and converts it to a slice of []interface{}.

func (*Parser) GetBool

func (p *Parser) GetBool(pattern string, def ...interface{}) bool

GetBool gets the value by specified <pattern>, and converts it to bool. It returns false when value is: "", 0, false, off, nil; or returns true instead.

func (*Parser) GetDuration

func (p *Parser) GetDuration(pattern string, def ...interface{}) time.Duration

func (*Parser) GetFloat32

func (p *Parser) GetFloat32(pattern string, def ...interface{}) float32

func (*Parser) GetFloat64

func (p *Parser) GetFloat64(pattern string, def ...interface{}) float64

func (*Parser) GetFloats

func (p *Parser) GetFloats(pattern string, def ...interface{}) []float64

func (*Parser) GetGTime

func (p *Parser) GetGTime(pattern string, format ...string) *gtime.Time

func (*Parser) GetInt

func (p *Parser) GetInt(pattern string, def ...interface{}) int

func (*Parser) GetInt16

func (p *Parser) GetInt16(pattern string, def ...interface{}) int16

func (*Parser) GetInt32

func (p *Parser) GetInt32(pattern string, def ...interface{}) int32

func (*Parser) GetInt64

func (p *Parser) GetInt64(pattern string, def ...interface{}) int64

func (*Parser) GetInt8

func (p *Parser) GetInt8(pattern string, def ...interface{}) int8

func (*Parser) GetInterfaces

func (p *Parser) GetInterfaces(pattern string, def ...interface{}) []interface{}

func (*Parser) GetInts

func (p *Parser) GetInts(pattern string, def ...interface{}) []int

func (*Parser) GetMap

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

GetMap gets the value by specified <pattern>, and converts it to map[string]interface{}.

func (*Parser) GetString

func (p *Parser) GetString(pattern string, def ...interface{}) string

GetString gets the value by specified <pattern>, and converts it to string.

func (*Parser) GetStrings

func (p *Parser) GetStrings(pattern string, def ...interface{}) []string

GetStrings gets the value by specified <pattern>, and converts it to a slice of []string.

func (*Parser) GetStruct

func (p *Parser) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error

GetStruct gets the value by specified <pattern>, and converts it to specified object <pointer>. The <pointer> should be the pointer to an object.

func (*Parser) GetStructDeep

func (p *Parser) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error

GetStructDeep does GetStruct recursively.

func (*Parser) GetStructs

func (p *Parser) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error

GetStructs converts any slice to given struct slice.

func (*Parser) GetStructsDeep

func (p *Parser) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error

GetStructsDeep converts any slice to given struct slice recursively.

func (*Parser) GetTime

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

func (*Parser) GetToStruct

func (p *Parser) GetToStruct(pattern string, pointer interface{}, mapping ...map[string]string) error

GetToStruct is alias of GetStruct. Deprecated.

func (*Parser) GetToVar

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

GetToVar gets the value by specified <pattern>, and converts it to specified golang variable <v>. The <v> should be a pointer type. Deprecated.

func (*Parser) GetUint

func (p *Parser) GetUint(pattern string, def ...interface{}) uint

func (*Parser) GetUint16

func (p *Parser) GetUint16(pattern string, def ...interface{}) uint16

func (*Parser) GetUint32

func (p *Parser) GetUint32(pattern string, def ...interface{}) uint32

func (*Parser) GetUint64

func (p *Parser) GetUint64(pattern string, def ...interface{}) uint64

func (*Parser) GetUint8

func (p *Parser) GetUint8(pattern string, def ...interface{}) uint8

func (*Parser) GetVar

func (p *Parser) GetVar(pattern string, def ...interface{}) *gvar.Var

GetVar returns a *gvar.Var with value by given <pattern>.

func (*Parser) Len

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

Len returns the length/size of the value by specified <pattern>. The target value by <pattern> should be type of slice or map. It returns -1 if the target value is not found, or its type is invalid.

func (*Parser) MarshalJSON

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

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*Parser) Remove

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

Remove deletes value with specified <pattern>. It supports hierarchical data access by char separator, which is '.' in default.

func (*Parser) Set

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

Set sets value with specified <pattern>. It supports hierarchical data access by char separator, which is '.' in default.

func (*Parser) SetSplitChar

func (p *Parser) SetSplitChar(char byte)

SetSplitChar sets the separator char for hierarchical data access.

func (*Parser) SetViolenceCheck

func (p *Parser) SetViolenceCheck(check bool)

SetViolenceCheck enables/disables violence check for hierarchical data access.

func (*Parser) ToArray

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

ToArray converts current object values to []interface{}. It returns nil if fails.

func (*Parser) ToJson

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

func (*Parser) ToJsonIndent

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

func (*Parser) ToJsonIndentString

func (p *Parser) ToJsonIndentString() (string, error)

func (*Parser) ToJsonString

func (p *Parser) ToJsonString() (string, error)

func (*Parser) ToMap

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

ToMap converts current object values to map[string]interface{}. It returns nil if fails.

func (*Parser) ToStruct

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

ToStruct converts current Json object to specified object. The <objPointer> should be a pointer type.

func (*Parser) ToStructDeep

func (p *Parser) ToStructDeep(pointer interface{}) error

func (*Parser) ToStructs

func (p *Parser) ToStructs(pointer interface{}) error

func (*Parser) ToStructsDeep

func (p *Parser) ToStructsDeep(pointer interface{}) error

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)

func (*Parser) Value

func (p *Parser) Value() interface{}

Val returns the value.

Jump to

Keyboard shortcuts

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