improve gjson/gparser/gcfg/gvar

This commit is contained in:
John
2019-07-29 20:37:49 +08:00
parent ef8351151d
commit a98ad9577b
11 changed files with 353 additions and 611 deletions

View File

@ -43,6 +43,10 @@ func (j *Json) Get(pattern string, def ...interface{}) interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
if pattern == "." {
pattern = ""
}
var result *interface{}
if j.vc {
result = j.getPointerByPattern(pattern)

View File

@ -1,63 +0,0 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gjson
//func MarshalOrdered(value interface{}) ([]byte, error) {
// buffer := bytes.NewBuffer(nil)
// rv := reflect.ValueOf(value)
// kind := rv.Kind()
// if kind == reflect.Ptr {
// rv = rv.Elem()
// kind = rv.Kind()
// }
// switch kind {
// case reflect.Slice: fallthrough
// case reflect.Array:
// buffer.WriteByte('[')
// length := rv.Len()
// for i := 0; i < length; i++ {
// if p, err := MarshalOrdered(rv.Index(i).Interface()); err != nil {
// return nil, err
// } else {
// buffer.Write(p)
// if i < length - 1 {
// buffer.WriteByte(',')
// }
// }
// }
// buffer.WriteByte(']')
// case reflect.Map: fallthrough
// case reflect.Struct:
// m := gconv.Map(value, "json")
// keys := make([]string, len(m))
// index := 0
// for key := range m {
// keys[index] = key
// index++
// }
// sort.Strings(keys)
// buffer.WriteByte('{')
// for i, key := range keys {
// if p, err := MarshalOrdered(m[key]); err != nil {
// return nil, err
// } else {
// buffer.WriteString(fmt.Sprintf(`"%s":%s`, key, string(p)))
// if i < index - 1 {
// buffer.WriteByte(',')
// }
// }
// }
// buffer.WriteByte('}')
// default:
// if p, err := json.Marshal(value); err != nil {
// return nil, err
// } else {
// buffer.Write(p)
// }
// }
// return buffer.Bytes(), nil
//}

View File

@ -11,6 +11,5 @@ import (
"github.com/gogf/gf/g/encoding/gjson"
)
type Parser struct {
json *gjson.Json
}
// Parser is actually alias of gjson.Json.
type Parser = gjson.Json

View File

@ -1,240 +0,0 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gp.
package gparser
import (
"time"
"github.com/gogf/gf/g/container/gvar"
"github.com/gogf/gf/g/os/gtime"
)
// Val returns the value.
func (p *Parser) Value() interface{} {
return p.json.Value()
}
// 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 (p *Parser) Get(pattern string, def ...interface{}) interface{} {
return p.json.Get(pattern, def...)
}
// GetVar returns a *gvar.Var with value by given <pattern>.
func (p *Parser) GetVar(pattern string, def ...interface{}) *gvar.Var {
return p.json.GetVar(pattern, def...)
}
// GetMap gets the value by specified <pattern>,
// and converts it to map[string]interface{}.
func (p *Parser) GetMap(pattern string, def ...interface{}) map[string]interface{} {
return p.json.GetMap(pattern, def...)
}
// GetArray gets the value by specified <pattern>,
// and converts it to a slice of []interface{}.
func (p *Parser) GetArray(pattern string, def ...interface{}) []interface{} {
return p.json.GetArray(pattern, def...)
}
// GetString gets the value by specified <pattern>,
// and converts it to string.
func (p *Parser) GetString(pattern string, def ...interface{}) string {
return p.json.GetString(pattern, def...)
}
// 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 (p *Parser) GetBool(pattern string, def ...interface{}) bool {
return p.json.GetBool(pattern, def...)
}
func (p *Parser) GetInt(pattern string, def ...interface{}) int {
return p.json.GetInt(pattern, def...)
}
func (p *Parser) GetInt8(pattern string, def ...interface{}) int8 {
return p.json.GetInt8(pattern, def...)
}
func (p *Parser) GetInt16(pattern string, def ...interface{}) int16 {
return p.json.GetInt16(pattern, def...)
}
func (p *Parser) GetInt32(pattern string, def ...interface{}) int32 {
return p.json.GetInt32(pattern, def...)
}
func (p *Parser) GetInt64(pattern string, def ...interface{}) int64 {
return p.json.GetInt64(pattern, def...)
}
func (p *Parser) GetInts(pattern string, def ...interface{}) []int {
return p.json.GetInts(pattern, def...)
}
func (p *Parser) GetUint(pattern string, def ...interface{}) uint {
return p.json.GetUint(pattern, def...)
}
func (p *Parser) GetUint8(pattern string, def ...interface{}) uint8 {
return p.json.GetUint8(pattern, def...)
}
func (p *Parser) GetUint16(pattern string, def ...interface{}) uint16 {
return p.json.GetUint16(pattern, def...)
}
func (p *Parser) GetUint32(pattern string, def ...interface{}) uint32 {
return p.json.GetUint32(pattern, def...)
}
func (p *Parser) GetUint64(pattern string, def ...interface{}) uint64 {
return p.json.GetUint64(pattern, def...)
}
func (p *Parser) GetFloat32(pattern string, def ...interface{}) float32 {
return p.json.GetFloat32(pattern, def...)
}
func (p *Parser) GetFloat64(pattern string, def ...interface{}) float64 {
return p.json.GetFloat64(pattern, def...)
}
func (p *Parser) GetFloats(pattern string, def ...interface{}) []float64 {
return p.json.GetFloats(pattern, def...)
}
// GetStrings gets the value by specified <pattern>,
// and converts it to a slice of []string.
func (p *Parser) GetStrings(pattern string, def ...interface{}) []string {
return p.json.GetStrings(pattern, def...)
}
func (p *Parser) GetInterfaces(pattern string, def ...interface{}) []interface{} {
return p.json.GetInterfaces(pattern, def...)
}
func (p *Parser) GetTime(pattern string, format ...string) time.Time {
return p.json.GetTime(pattern, format...)
}
func (p *Parser) GetDuration(pattern string, def ...interface{}) time.Duration {
return p.json.GetDuration(pattern, def...)
}
func (p *Parser) GetGTime(pattern string, format ...string) *gtime.Time {
return p.json.GetGTime(pattern, format...)
}
// GetToVar gets the value by specified <pattern>,
// and converts it to specified golang variable <v>.
// The <v> should be a pointer type.
// Deprecated.
func (p *Parser) GetToVar(pattern string, pointer interface{}) error {
return p.json.GetToVar(pattern, pointer)
}
// GetStruct gets the value by specified <pattern>,
// and converts it to specified object <pointer>.
// The <pointer> should be the pointer to an object.
func (p *Parser) GetStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
return p.json.GetStruct(pattern, pointer, mapping...)
}
// GetStructDeep does GetStruct recursively.
func (p *Parser) GetStructDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return p.json.GetStructDeep(pattern, pointer, mapping...)
}
// GetStructs converts any slice to given struct slice.
func (p *Parser) GetStructs(pattern string, pointer interface{}, mapping ...map[string]string) error {
return p.json.GetStructs(pattern, pointer, mapping...)
}
// GetStructsDeep converts any slice to given struct slice recursively.
func (p *Parser) GetStructsDeep(pattern string, pointer interface{}, mapping ...map[string]string) error {
return p.json.GetStructsDeep(pattern, pointer, mapping...)
}
// GetToStruct is alias of GetStruct.
// Deprecated.
func (p *Parser) GetToStruct(pattern string, pointer interface{}, mapping ...map[string]string) error {
return p.json.GetStruct(pattern, pointer, mapping...)
}
// Set sets value with specified <pattern>.
// It supports hierarchical data access by char separator, which is '.' in default.
func (p *Parser) Set(pattern string, value interface{}) error {
return p.json.Set(pattern, value)
}
// 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 (p *Parser) Len(pattern string) int {
return p.json.Len(pattern)
}
// Append appends value to the value by specified <pattern>.
// The target value by <pattern> should be type of slice.
func (p *Parser) Append(pattern string, value interface{}) error {
return p.json.Append(pattern, value)
}
// Remove deletes value with specified <pattern>.
// It supports hierarchical data access by char separator, which is '.' in default.
func (p *Parser) Remove(pattern string) error {
return p.json.Remove(pattern)
}
// ToMap converts current object values to map[string]interface{}.
// It returns nil if fails.
func (p *Parser) ToMap() map[string]interface{} {
return p.json.ToMap()
}
// ToArray converts current object values to []interface{}.
// It returns nil if fails.
func (p *Parser) ToArray() []interface{} {
return p.json.ToArray()
}
// ToStruct converts current Json object to specified object.
// The <objPointer> should be a pointer type.
func (p *Parser) ToStruct(pointer interface{}) error {
return p.json.ToStruct(pointer)
}
func (p *Parser) ToStructDeep(pointer interface{}) error {
return p.json.ToStructDeep(pointer)
}
func (p *Parser) ToStructs(pointer interface{}) error {
return p.json.ToStructs(pointer)
}
func (p *Parser) ToStructsDeep(pointer interface{}) error {
return p.json.ToStructsDeep(pointer)
}
// Dump prints current Json object with more manually readable.
func (p *Parser) Dump() {
p.json.Dump()
}
func (p *Parser) Export() string {
return p.json.Export()
}

View File

@ -1,17 +0,0 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gp.
package gparser
// SetSplitChar sets the separator char for hierarchical data access.
func (p *Parser) SetSplitChar(char byte) {
p.json.SetSplitChar(char)
}
// SetViolenceCheck enables/disables violence check for hierarchical data access.
func (p *Parser) SetViolenceCheck(check bool) {
p.json.SetViolenceCheck(check)
}

View File

@ -6,43 +6,6 @@
package gparser
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (p *Parser) MarshalJSON() ([]byte, error) {
return p.json.MarshalJSON()
}
func (p *Parser) ToXml(rootTag ...string) ([]byte, error) {
return p.json.ToXml(rootTag...)
}
func (p *Parser) ToXmlIndent(rootTag ...string) ([]byte, error) {
return p.json.ToXmlIndent(rootTag...)
}
func (p *Parser) ToJson() ([]byte, error) {
return p.json.ToJson()
}
func (p *Parser) ToJsonString() (string, error) {
return p.json.ToJsonString()
}
func (p *Parser) ToJsonIndent() ([]byte, error) {
return p.json.ToJsonIndent()
}
func (p *Parser) ToJsonIndentString() (string, error) {
return p.json.ToJsonIndentString()
}
func (p *Parser) ToYaml() ([]byte, error) {
return p.json.ToYaml()
}
func (p *Parser) ToToml() ([]byte, error) {
return p.json.ToToml()
}
func VarToXml(value interface{}, rootTag ...string) ([]byte, error) {
return New(value).ToXml(rootTag...)
}
@ -74,7 +37,3 @@ func VarToYaml(value interface{}) ([]byte, error) {
func VarToToml(value interface{}) ([]byte, error) {
return New(value).ToToml()
}
func VarToStruct(value interface{}, obj interface{}) error {
return New(value).ToStruct(obj)
}

View File

@ -16,26 +16,34 @@ import (
// The <unsafe> param specifies whether using this Parser object
// in un-concurrent-safe context, which is false in default.
func New(value interface{}, safe ...bool) *Parser {
return &Parser{gjson.New(value, safe...)}
return gjson.New(value, safe...)
}
// Load loads content from specified file <path>,
// and creates a Parser object from its content.
func Load(path string, safe ...bool) (*Parser, error) {
if j, e := gjson.Load(path, safe...); e == nil {
return &Parser{j}, nil
} else {
return nil, e
}
return gjson.Load(path, safe...)
}
// 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 LoadContent(data interface{}, safe ...bool) (*Parser, error) {
if j, e := gjson.LoadContent(data, safe...); e == nil {
return &Parser{j}, nil
} else {
return nil, e
}
return gjson.LoadContent(data, safe...)
}
func LoadJson(data interface{}, safe ...bool) (*Parser, error) {
return gjson.LoadJson(data, safe...)
}
func LoadXml(data interface{}, safe ...bool) (*Parser, error) {
return gjson.LoadXml(data, safe...)
}
func LoadYaml(data interface{}, safe ...bool) (*Parser, error) {
return gjson.LoadYaml(data, safe...)
}
func LoadToml(data interface{}, safe ...bool) (*Parser, error) {
return gjson.LoadToml(data, safe...)
}

View File

@ -7,10 +7,11 @@
package gparser_test
import (
"testing"
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/encoding/gparser"
"github.com/gogf/gf/g/test/gtest"
"testing"
)
func Test_New(t *testing.T) {
@ -243,10 +244,6 @@ func Test_Convert(t *testing.T) {
p = gparser.New(`[0,1,2]`)
gtest.Assert(p.ToArray()[0], 0)
err = gparser.VarToStruct(`{"name":"gf"}`, &name)
gtest.Assert(err, nil)
gtest.Assert(name.Name, "gf")
})
}