Ejemplo n.º 1
0
Archivo: node.go Proyecto: 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
}
Ejemplo n.º 2
0
Archivo: type.go Proyecto: 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)
}