mirror of
https://gitee.com/johng/gf
synced 2026-07-06 13:42:46 +08:00
change default value from unsafe to safe for garray/gmap/glist/gset/grong/gtree/gjson/gparser
This commit is contained in:
@ -60,7 +60,7 @@ func (j *Json) Get(pattern string, def ...interface{}) interface{} {
|
||||
|
||||
// GetVar returns a *gvar.Var with value by given <pattern>.
|
||||
func (j *Json) GetVar(pattern string, def ...interface{}) *gvar.Var {
|
||||
return gvar.New(j.Get(pattern, def...), true)
|
||||
return gvar.New(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetMap gets the value by specified <pattern>,
|
||||
@ -76,7 +76,7 @@ func (j *Json) GetMap(pattern string, def ...interface{}) map[string]interface{}
|
||||
// GetJson gets the value by specified <pattern>,
|
||||
// and converts it to a un-concurrent-safe Json object.
|
||||
func (j *Json) GetJson(pattern string, def ...interface{}) *Json {
|
||||
return New(j.Get(pattern, def...), true)
|
||||
return New(j.Get(pattern, def...))
|
||||
}
|
||||
|
||||
// GetJsons gets the value by specified <pattern>,
|
||||
@ -86,7 +86,7 @@ func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json {
|
||||
if len(array) > 0 {
|
||||
jsonSlice := make([]*Json, len(array))
|
||||
for i := 0; i < len(array); i++ {
|
||||
jsonSlice[i] = New(array[i], true)
|
||||
jsonSlice[i] = New(array[i])
|
||||
}
|
||||
return jsonSlice
|
||||
}
|
||||
@ -100,7 +100,7 @@ func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json {
|
||||
if len(m) > 0 {
|
||||
jsonMap := make(map[string]*Json, len(m))
|
||||
for k, v := range m {
|
||||
jsonMap[k] = New(v, true)
|
||||
jsonMap[k] = New(v)
|
||||
}
|
||||
return jsonMap
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ import (
|
||||
// or it will make no sense.
|
||||
// The <unsafe> param specifies whether using this Json object
|
||||
// in un-concurrent-safe context, which is false in default.
|
||||
func New(data interface{}, unsafe ...bool) *Json {
|
||||
func New(data interface{}, safe ...bool) *Json {
|
||||
j := (*Json)(nil)
|
||||
switch data.(type) {
|
||||
case string, []byte:
|
||||
@ -79,18 +79,10 @@ func New(data interface{}, unsafe ...bool) *Json {
|
||||
}
|
||||
}
|
||||
}
|
||||
j.mu = rwmutex.New(unsafe...)
|
||||
j.mu = rwmutex.New(safe...)
|
||||
return j
|
||||
}
|
||||
|
||||
// NewUnsafe creates a un-concurrent-safe Json object.
|
||||
func NewUnsafe(data ...interface{}) *Json {
|
||||
if len(data) > 0 {
|
||||
return New(data[0], true)
|
||||
}
|
||||
return New(nil, true)
|
||||
}
|
||||
|
||||
// Valid checks whether <data> is a valid JSON data type.
|
||||
func Valid(data interface{}) bool {
|
||||
return json.Valid(gconv.Bytes(data))
|
||||
@ -120,41 +112,41 @@ func DecodeTo(data interface{}, v interface{}) error {
|
||||
}
|
||||
|
||||
// DecodeToJson codes <data>(string/[]byte) to a Json object.
|
||||
func DecodeToJson(data interface{}, unsafe ...bool) (*Json, error) {
|
||||
func DecodeToJson(data interface{}, safe ...bool) (*Json, error) {
|
||||
if v, err := Decode(gconv.Bytes(data)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return New(v, unsafe...), nil
|
||||
return New(v, safe...), nil
|
||||
}
|
||||
}
|
||||
|
||||
// Load loads content from specified file <path>,
|
||||
// and creates a Json object from its content.
|
||||
func Load(path string, unsafe ...bool) (*Json, error) {
|
||||
return doLoadContent(gfile.Ext(path), gfcache.GetBinContents(path), unsafe...)
|
||||
func Load(path string, safe ...bool) (*Json, error) {
|
||||
return doLoadContent(gfile.Ext(path), gfcache.GetBinContents(path), safe...)
|
||||
}
|
||||
|
||||
func LoadJson(data interface{}, unsafe ...bool) (*Json, error) {
|
||||
return doLoadContent("json", gconv.Bytes(data), unsafe...)
|
||||
func LoadJson(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("json", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
func LoadXml(data interface{}, unsafe ...bool) (*Json, error) {
|
||||
return doLoadContent("xml", gconv.Bytes(data), unsafe...)
|
||||
func LoadXml(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("xml", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
func LoadYaml(data interface{}, unsafe ...bool) (*Json, error) {
|
||||
return doLoadContent("yaml", gconv.Bytes(data), unsafe...)
|
||||
func LoadYaml(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("yaml", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
func LoadToml(data interface{}, unsafe ...bool) (*Json, error) {
|
||||
return doLoadContent("toml", gconv.Bytes(data), unsafe...)
|
||||
func LoadToml(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContent("toml", gconv.Bytes(data), safe...)
|
||||
}
|
||||
|
||||
func doLoadContent(dataType string, data []byte, unsafe ...bool) (*Json, error) {
|
||||
func doLoadContent(dataType string, data []byte, safe ...bool) (*Json, error) {
|
||||
var err error
|
||||
var result interface{}
|
||||
if len(data) == 0 {
|
||||
return New(nil, unsafe...), nil
|
||||
return New(nil, safe...), nil
|
||||
}
|
||||
if dataType == "" {
|
||||
dataType = checkDataType(data)
|
||||
@ -194,15 +186,15 @@ func doLoadContent(dataType string, data []byte, unsafe ...bool) (*Json, error)
|
||||
return nil, fmt.Errorf(`json decoding failed for content: %s`, string(data))
|
||||
}
|
||||
}
|
||||
return New(result, unsafe...), nil
|
||||
return New(result, safe...), nil
|
||||
}
|
||||
|
||||
func LoadContent(data interface{}, unsafe ...bool) (*Json, error) {
|
||||
func LoadContent(data interface{}, safe ...bool) (*Json, error) {
|
||||
content := gconv.Bytes(data)
|
||||
if len(content) == 0 {
|
||||
return New(nil, unsafe...), nil
|
||||
return New(nil, safe...), nil
|
||||
}
|
||||
return doLoadContent(checkDataType(content), content, unsafe...)
|
||||
return doLoadContent(checkDataType(content), content, safe...)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -24,18 +24,6 @@ func Test_New(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewUnsafe(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
j := gjson.NewUnsafe(data)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Valid(t *testing.T) {
|
||||
data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`)
|
||||
|
||||
@ -175,7 +175,7 @@ func Test_Load_TOML2(t *testing.T) {
|
||||
|
||||
func Test_Load_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
j := gjson.NewUnsafe()
|
||||
j := gjson.New(nil)
|
||||
gtest.Assert(j.Value(), nil)
|
||||
_, err := gjson.Decode(nil)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
@ -15,22 +15,14 @@ import (
|
||||
// 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 New(value interface{}, unsafe ...bool) *Parser {
|
||||
return &Parser{gjson.New(value, unsafe...)}
|
||||
}
|
||||
|
||||
// NewUnsafe creates a un-concurrent-safe Parser object.
|
||||
func NewUnsafe(value ...interface{}) *Parser {
|
||||
if len(value) > 0 {
|
||||
return &Parser{gjson.New(value[0], false)}
|
||||
}
|
||||
return &Parser{gjson.New(nil, false)}
|
||||
func New(value interface{}, safe ...bool) *Parser {
|
||||
return &Parser{gjson.New(value, safe...)}
|
||||
}
|
||||
|
||||
// Load loads content from specified file <path>,
|
||||
// and creates a Parser object from its content.
|
||||
func Load(path string, unsafe ...bool) (*Parser, error) {
|
||||
if j, e := gjson.Load(path, unsafe...); e == nil {
|
||||
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
|
||||
@ -40,8 +32,8 @@ func Load(path string, 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 LoadContent(data interface{}, unsafe ...bool) (*Parser, error) {
|
||||
if j, e := gjson.LoadContent(data, unsafe...); e == nil {
|
||||
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
|
||||
|
||||
@ -28,7 +28,7 @@ func Test_New(t *testing.T) {
|
||||
func Test_NewUnsafe(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
j := gparser.NewUnsafe(data)
|
||||
j := gparser.New(data)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
|
||||
@ -176,7 +176,7 @@ func Test_Load_TOML2(t *testing.T) {
|
||||
|
||||
func Test_Load_Nil(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
p := gparser.NewUnsafe()
|
||||
p := gparser.New(nil)
|
||||
gtest.Assert(p.Value(), nil)
|
||||
file := "test22222.json"
|
||||
filePath := gfile.Pwd() + gfile.Separator + file
|
||||
|
||||
Reference in New Issue
Block a user