Exemplo n.º 1
0
Arquivo: glue.go Projeto: Laller/chill
// Returns error if input is not valid according to the rules found in d.nouns
func (d *Descriptor) CreateInputs(filterCreator func(string, map[string]interface{}) iface.Filter) ([]interface{}, map[string]interface{}, error) {
	module := mod.NewModule(d.VerbLocation)
	if !module.Exists() {
		return nil, nil, fmt.Errorf("Module named %v does not exist.", d.VerbLocation)
	}
	ins := module.Instance()
	verb := ins.Method(d.Sentence.Verb)
	an := verbinfo.NewAnalyzer(verb)
	ac := an.ArgCount()
	if len(d.Route.Queries) < ac {
		return nil, nil, fmt.Errorf("Not enough input to supply.")
	}
	if ac == 0 {
		return nil, nil, nil
	}
	fc := an.FilterCount()
	if fc > 0 && filterCreator == nil {
		return nil, nil, fmt.Errorf("filterCreator is needed but it is nil.")
	}
	var inp []interface{}
	var data map[string]interface{}
	source := []map[string]interface{}{}
	for _, v := range d.Route.Queries {
		source = append(source, v)
	}
	if an.NeedsData() {
		data = source[len(source)-1]
		if data == nil {
			data = map[string]interface{}{} // !Important.
		}
	}
	if fc > 0 {
		if fc != 1 && len(source) != fc {
			return nil, nil, fmt.Errorf("Got %v inputs, but method %v needs only %v filters. Currently filters can only be reduced to 1.", len(source), d.Sentence.Verb, fc)
		}
		filters := []iface.Filter{}
		for i, v := range source {
			if d.Sentence.Verb != "Get" && d.Sentence.Verb != "GetSingle" && i == len(source)-1 {
				break
			}
			filters = append(filters, filterCreator(d.Route.Words[i], v))
		}
		if len(filters) > 1 {
			filter := filters[0]
			red, err := filter.Reduce(filters[1:]...)
			if err != nil {
				return nil, nil, err
			}
			filters = []iface.Filter{red}
		}
		for _, v := range filters {
			inp = append(inp, v)
		}
	}
	return inp, data, nil
}
Exemplo n.º 2
0
func TestB(t *testing.T) {
	methB := mod.ToInstance(&mockObject{}).Method("MethodB")
	an := verbinfo.NewAnalyzer(methB)
	if an.ArgCount() != 2 {
		t.Fatal()
	}
	if an.FilterCount() != 2 {
		t.Fatal()
	}
	if an.NeedsData() {
		t.Fatal()
	}
}