示例#1
0
文件: node.go 项目: kego/ke
func (n *Node) AddToObject(ctx context.Context, parent *Node, rule *system.RuleWrapper, key string, updateParentVal bool) error {

	parent.Map[key] = n

	if updateParentVal {
		rt, err := rule.GetReflectType()
		if err != nil {
			return kerr.Wrap("QMGGBWEMPT", err)
		}
		val := n.Val
		if val == (reflect.Value{}) {
			val = reflect.Zero(rt)
		}
		p := parent.Val
		for p.Kind() == reflect.Interface || p.Kind() == reflect.Ptr {
			p = p.Elem()
		}
		f := p.FieldByName(system.GoName(key))
		f.Set(val)
	}

	n.initialiseValFromParent()

	return nil
}
示例#2
0
文件: node.go 项目: kego/ke
func (n *Node) initialiseCollectionItem(ctx context.Context, parent *Node, key string, index int) error {
	n.resetAllValues()
	n.Parent = parent
	n.Index = index
	n.Key = key
	n.Missing = false

	var collectionRule *system.RuleWrapper
	if n.Parent.Type.Alias != nil {
		collectionRule = system.WrapRule(ctx, n.Parent.Type.Alias)
	} else {
		collectionRule = parent.Rule
	}
	rule, err := collectionRule.ItemsRule()
	if err != nil {
		return kerr.Wrap("SBJVMGUOOA", err)
	}
	n.Rule = rule

	t, err := extractType(ctx, system.Pack(nil), rule)
	if err != nil {
		return kerr.Wrap("EQNRHQWXFJ", err)
	}

	if err := n.setType(ctx, t); err != nil {
		return kerr.Wrap("UPAQMUGDNH", err)
	}

	return nil
}
示例#3
0
文件: type.go 项目: kego/ke
// collectionPrefix recursively digs down through collection rules, recursively
// calling itself as long as it finds a collection rule (map or array). It returns
// the full collection prefix (e.g. any number of appended [] and map[string]'s)
// and the inner (non collection) rule.
func collectionPrefixInnerRule(ctx context.Context, prefix string, outer *system.RuleWrapper) (fullPrefix string, inner *system.RuleWrapper, err error) {

	kind, alias := outer.Kind(ctx)
	if alias {
		return prefix, outer, nil
	}
	switch kind {
	case system.KindValue, system.KindStruct, system.KindInterface:
		return prefix, outer, nil
	case system.KindArray:
		prefix += "[]"
	case system.KindMap:
		prefix += "map[string]"
	default:
		panic("unknown kind")
	}

	items, err := outer.ItemsRule()
	if err != nil {
		return "", nil, kerr.Wrap("SUTYJEGBKW", err)
	}
	return collectionPrefixInnerRule(ctx, prefix, items)
}