mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
完善删除操作,完善gparser功能
This commit is contained in:
@ -60,19 +60,6 @@ func New(value interface{}) *Json {
|
||||
}
|
||||
}
|
||||
|
||||
// 设置自定义的层级分隔符号
|
||||
func (j *Json) SetSplitChar(char byte) {
|
||||
j.mu.Lock()
|
||||
j.c = char
|
||||
j.mu.Unlock()
|
||||
}
|
||||
|
||||
// 设置自定义的层级分隔符号
|
||||
func (j *Json) SetViolenceCheck(check bool) {
|
||||
j.mu.Lock()
|
||||
j.vc = check
|
||||
j.mu.Unlock()
|
||||
}
|
||||
// 编码go变量为json字符串,并返回json字符串指针
|
||||
func Encode (v interface{}) ([]byte, error) {
|
||||
return json.Marshal(v)
|
||||
@ -144,6 +131,20 @@ func LoadContent (data []byte, t string) (*Json, error) {
|
||||
return New(result), nil
|
||||
}
|
||||
|
||||
// 设置自定义的层级分隔符号
|
||||
func (j *Json) SetSplitChar(char byte) {
|
||||
j.mu.Lock()
|
||||
j.c = char
|
||||
j.mu.Unlock()
|
||||
}
|
||||
|
||||
// 设置自定义的层级分隔符号
|
||||
func (j *Json) SetViolenceCheck(check bool) {
|
||||
j.mu.Lock()
|
||||
j.vc = check
|
||||
j.mu.Unlock()
|
||||
}
|
||||
|
||||
// 将指定的json内容转换为指定结构返回,查找失败或者转换失败,目标对象转换为nil
|
||||
// 注意第二个参数需要给的是**变量地址**
|
||||
func (j *Json) GetToVar(pattern string, v interface{}) error {
|
||||
@ -233,7 +234,6 @@ func (j *Json) Remove(pattern string) error {
|
||||
// 注意:
|
||||
// 1、写入的value为nil且removed为true时,表示删除;
|
||||
// 2、里面的层级处理比较复杂,逻辑较复杂的地方在于层级检索及节点创建,叶子赋值;
|
||||
// @todo 梳理删除逻辑,单独封装成方法或者逻辑块处理
|
||||
func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
|
||||
array := strings.Split(pattern, string(j.c))
|
||||
length := len(array)
|
||||
@ -263,6 +263,9 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
|
||||
} else {
|
||||
// 当键名不存在的情况这里会进行处理
|
||||
if v, ok := (*pointer).(map[string]interface{})[array[i]]; !ok {
|
||||
if removed && value == nil {
|
||||
goto done
|
||||
}
|
||||
// 创建新节点
|
||||
if isNumeric(array[i + 1]) {
|
||||
// 创建array节点
|
||||
@ -295,6 +298,7 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
valn, err := strconv.Atoi(array[i])
|
||||
if err != nil {
|
||||
return err
|
||||
@ -309,6 +313,9 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
|
||||
(*pointer).([]interface{})[valn] = value
|
||||
}
|
||||
} else {
|
||||
if removed && value == nil {
|
||||
goto done
|
||||
}
|
||||
j.setPointerWithValue(pointer, array[i], value)
|
||||
}
|
||||
} else {
|
||||
@ -319,6 +326,9 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
|
||||
pparent = pointer
|
||||
pointer = &(*pointer).([]interface{})[valn]
|
||||
} else {
|
||||
if removed && value == nil {
|
||||
goto done
|
||||
}
|
||||
var v interface{} = make([]interface{}, n + 1)
|
||||
pparent = j.setPointerWithValue(pointer, array[i], v)
|
||||
pointer = &v
|
||||
@ -370,43 +380,6 @@ done:
|
||||
return nil
|
||||
}
|
||||
|
||||
//func (j *Json) createNodeForCurrentLevel(value interface{}, array []string) interface{} {
|
||||
// // 判断当前节点应当为map或者数组
|
||||
// if isNumeric(array[i]) {
|
||||
// if n, err := strconv.Atoi(array[i]); err == nil {
|
||||
// s := make([]interface{}, n + 1)
|
||||
// if i == length - 1 {
|
||||
// s[n] = value
|
||||
// }
|
||||
// if pparent != nil {
|
||||
// pparent = j.setPointerWithValue(pparent, array[i - 1], s)
|
||||
// } else {
|
||||
// *pointer = s
|
||||
// pparent = pointer
|
||||
// }
|
||||
// pointer = &s[n]
|
||||
// } else {
|
||||
// return err
|
||||
// }
|
||||
// } else {
|
||||
// var v interface{}
|
||||
// if i == length - 1 {
|
||||
// v = map[string]interface{}{
|
||||
// array[i] : value,
|
||||
// }
|
||||
// } else {
|
||||
// v = map[string]interface{}{}
|
||||
// }
|
||||
// if pparent != nil {
|
||||
// pparent = j.setPointerWithValue(pparent, array[i - 1], v)
|
||||
// } else {
|
||||
// *pointer = v
|
||||
// pparent = pointer
|
||||
// }
|
||||
// pointer = &v
|
||||
// }
|
||||
//}
|
||||
|
||||
// 数据结构转换,map参数必须转换为map[string]interface{},数组参数必须转换为[]interface{}
|
||||
func (j *Json) convertValue(value interface{}) interface{} {
|
||||
switch value.(type) {
|
||||
|
||||
@ -42,6 +42,16 @@ func LoadContent (data []byte, fileType string) (*Parser, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// 设置自定义的层级分隔符号
|
||||
func (p *Parser) SetSplitChar(char byte) {
|
||||
p.json.SetSplitChar(char)
|
||||
}
|
||||
|
||||
// 设置自定义的层级分隔符号
|
||||
func (p *Parser) SetViolenceCheck(check bool) {
|
||||
p.SetViolenceCheck(check)
|
||||
}
|
||||
|
||||
// 将指定的json内容转换为指定结构返回,查找失败或者转换失败,目标对象转换为nil
|
||||
// 注意第二个参数需要给的是变量地址
|
||||
func (p *Parser) GetToVar(pattern string, v interface{}) error {
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://gitee.com/johng/gf.
|
||||
|
||||
// 单元测试
|
||||
|
||||
package gparser_test
|
||||
|
||||
import (
|
||||
@ -171,5 +173,21 @@ func Test_Set10(t *testing.T) {
|
||||
}
|
||||
|
||||
|
||||
func Test_Set11(t *testing.T) {
|
||||
e := []byte(`{"a":{"b":{}}}`)
|
||||
p, _ := gparser.LoadContent([]byte(`{"a":{"b":{"c":1}}}`), "json")
|
||||
p.Remove("a.b.c")
|
||||
if c, err := p.ToJson(); err == nil {
|
||||
fmt.Println(string(c))
|
||||
if bytes.Compare(c, e) != 0 {
|
||||
t.Error("expect:", string(e))
|
||||
}
|
||||
} else {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user