Beispiel #1
0
func (p *Parser) kegoProduction(value interface{}) func(*node.Node) (bool, error) {
	logger.Print("Creating kegoProduction validator ", value)
	return func(n *node.Node) (bool, error) {

		if n.Null || n.Missing {
			return false, nil
		}

		tokenString := value.(string)
		kegoType := tokenString[1 : len(tokenString)-1]
		r, err := system.NewReferenceFromString(p.ctx, kegoType)
		if err != nil {
			return false, kerr.Wrap("RWDOYBBDVK", err)
		}
		logger.Print("kegoProduction ? ", n.Type.Id.Value(), " == ", r.Value())

		if n.Type.Id.Value() == r.Value() {
			return true, nil
		}

		// If the specified type is an interface, we should check to see if the
		// node implements the interface.
		if i, ok := r.GetType(p.ctx); ok && i.Interface {
			rt, ok := i.GetReflectType(p.ctx)
			if ok {
				if n.Type.Implements(p.ctx, rt) {
					return true, nil
				}
			}
		}

		return false, nil
	}
}
Beispiel #2
0
Datei: addpop.go Projekt: kego/ke
func (v *AddPopupView) save() {

	var t *system.Type
	if len(v.model.Types) == 1 {
		t = v.model.Types[0]
	} else {
		options := v.typeSelect.Node.Get("options")
		selectedIndex := v.typeSelect.Node.Get("selectedIndex").Int()
		value := options.Index(selectedIndex).Get("id").String()
		if value == "" {
			return
		}
		r, err := system.NewReferenceFromString(v.Ctx, value)
		if err != nil {
			v.App.Fail <- kerr.Wrap("SEMCIELKRN", err)
			return
		}
		ty, ok := system.GetTypeFromCache(v.Ctx, r.Package, r.Name)
		if !ok {
			v.App.Fail <- kerr.New("RWHSCOFNQM", "Type %s not found in cache", r.Value())
			return
		}
		t = ty
	}

	switch {
	case v.model.IsMap():
		key := v.nameInput.Node.Get("value").String()
		if key == "" {
			// TODO: show an error
			return
		}
		if _, duplicate := v.model.Parent.Map[key]; duplicate {
			// TODO: show an error
			return
		}
		v.App.Dispatch(&actions.Add{
			Undoer: &actions.Undoer{},
			Node:   node.NewNode(),
			Parent: v.model.Parent,
			Key:    key,
			Type:   t,
		})
	case v.model.IsArray():
		v.App.Dispatch(&actions.Add{
			Undoer: &actions.Undoer{},
			Node:   node.NewNode(),
			Parent: v.model.Parent,
			Index:  len(v.model.Parent.Array),
			Type:   t,
		})
	case v.model.IsField():
		v.App.Dispatch(&actions.Add{
			Undoer: &actions.Undoer{},
			Parent: v.model.Parent,
			Node:   v.model.Node,
			Key:    v.model.Node.Key,
			Type:   t,
		})
	case v.model.IsFile():
		name := v.nameInput.Node.Get("value").String()
		if name == "" {
			// TODO: show an error
			return
		}
		// TODO: show an error if a duplicate file exists
		v.App.Dispatch(&actions.Add{
			Undoer:     &actions.Undoer{},
			Node:       node.NewNode(),
			Type:       t,
			BranchName: name,
			BranchFile: name + ".json", // TODO: choose filename based on package
		})
	}

	v.App.Dispatch(&actions.CloseAddPopup{})

}