示例#1
0
文件: node.go 项目: kego/ke
func mutateAddNode(ctx context.Context, n *node.Node, p *node.Node, key string, index int, t *system.Type, name string) error {
	switch {
	case p == nil:
		n.InitialiseRoot()
	case p.Type.NativeJsonType(ctx) == system.J_ARRAY:
		if err := n.InitialiseArrayItem(ctx, p, index); err != nil {
			return kerr.Wrap("QLBGMSQENC", err)
		}
		if err := n.AddToArray(ctx, p, index, true); err != nil {
			return kerr.Wrap("PLEJOTCSGH", err)
		}
	case p.Type.NativeJsonType(ctx) == system.J_MAP:
		if err := n.InitialiseMapItem(ctx, p, key); err != nil {
			return kerr.Wrap("KRTGPFYWIH", err)
		}
		if err := n.AddToMap(ctx, p, key, true); err != nil {
			return kerr.Wrap("UEPLLMTLDB", err)
		}
	}
	if err := n.SetValueZero(ctx, false, t); err != nil {
		return kerr.Wrap("NLSRNQGLLW", err)
	}
	if p == nil {
		// for root nodes, id field must be set.
		if err := n.SetIdField(ctx, name); err != nil {
			return kerr.Wrap("VDPFOWLHIL", err)
		}
	}
	return nil
}
示例#2
0
文件: node.go 项目: kego/ke
func mutateRestoreNode(ctx context.Context, n *node.Node, p *node.Node, b *node.Node) error {
	n.Restore(ctx, b)
	if p == nil {
		return nil
	}
	switch p.Type.NativeJsonType(ctx) {
	case system.J_MAP:
		// don't have to call n.InitialiseMapItem because the node is already
		// initialized
		if err := n.AddToMap(ctx, p, n.Key, true); err != nil {
			return kerr.Wrap("TOPLOONYCL", err)
		}
	case system.J_ARRAY:
		// don't have to call n.InitialiseArrayItem because the node is already
		// initialized
		if err := n.AddToArray(ctx, p, n.Index, true); err != nil {
			return kerr.Wrap("WFXSQYOEAY", err)
		}
	case system.J_OBJECT:
		// don't have to call n.InitialiseObjectField because the node is
		// already initialized
		if err := n.AddToObject(ctx, p, n.Rule, n.Key, true); err != nil {
			return kerr.Wrap("QMBJQMLOCY", err)
		}
	}
	return nil
}