示例#1
0
文件: dag.go 项目: yonglehou/hpipe
func (this *DAG) ResolveJob(job *Job) error {
	vars := ""
	if v, ok := job.Attrs["vars"]; ok {
		vars = v
	}

	if resolved, err := symbol.Resolve(strings.Trim(vars, "\"'"), this.Builtins.builtins); err != nil {
		return err
	} else {
		for _, stmt := range resolved {
			job.Vars[stmt.Value.(string)] = stmt.Children[0].Value.(string)
		}
	}

	for k, v := range job.Attrs {
		if k == "vars" {
			continue
		}
		nval, err := ApplyVarToString(v, job.Vars)
		if err != nil {
			log.Fatal(err)
			return err
		}
		job.Attrs[k] = nval
	}

	return nil
}
示例#2
0
func TestSymbolResolveAll(t *testing.T) {
	var builtins = map[string]*ast.Stmt{
		// Date
		"gmtdate": ast.NewDate(stdtime.Now(), "YYYYMMDD"),
		"bizdate": ast.NewDate(stdtime.Now().AddDate(0, 0, -1), "YYYYMMDD"),
		// Duration
		"year":   ast.NewDurationExt(1, 0, 0),
		"month":  ast.NewDurationExt(0, 1, 0),
		"day":    ast.NewDurationExt(0, 0, 1),
		"hour":   ast.NewDuration(stdtime.Hour),
		"minute": ast.NewDuration(stdtime.Minute),
		"second": ast.NewDuration(stdtime.Second),
	}

	src := "$res=$gmtdate-1*$day"
	ret, err := symbol.Resolve(src, builtins)
	if err != nil {
		t.Error(err)
		return
	}
	res := ret[0]
	check := ast.NewLeftID("res",
		ast.NewDate(stdtime.Now().AddDate(0, 0, -1), "YYYYMMDD"))
	if !res.Equals(check) {
		t.Error(fmt.Errorf("%s=%d", src, res.Value.(string)))
		return
	}
	//fmt.Println(res)
}