Пример #1
0
func Test_Region(t *testing.T) {

	z.DebugOn()

	r1 := new(z.Region)
	r1.Left = 18
	r1.Right = 55
	if r1.LeftInt() != 18 {
		t.Errorf("left should be %d", 18)
	}
	if r1.RightInt() != 55 {
		t.Errorf("right should be %d", 55)
	}

	t1 := time.Now()
	t2 := time.Date(2013, 9, 10, 11, 32, 50, 200, time.Local)
	r1.Left = time.Unix(t1.Unix(), 0)
	r1.Right = time.Unix(0, t2.UnixNano())
	if r1.LeftDate().Unix() != t1.Unix() {
		t.Errorf("left should be %d, but be %d", t1.Unix(), r1.LeftDate().Unix())
	}
	if r1.RightDate().UnixNano() != t2.UnixNano() {
		t.Errorf("right should be %d, but be %d", t2.UnixNano(), r1.RightDate().UnixNano())
	}

	r2 := z.MakeRegion("[12355, 33535)")
	if r2.LeftOpen == true {
		t.Error("left should be close")
	}
	if r2.RightOpen == false {
		t.Error("right should be open")
	}
	if r2.Left != int(12355) {
		t.Error("left should be int 12355")
	}

	r3 := z.MakeRegion("[12.333, -0.555]")
	if r3.Left != float32(12.333) {
		t.Error("left should be float 12.333")
	}
	if r3.Right != float32(-0.555) {
		t.Error("right should be float -0.555, but be ", r3.Right)
	}

	r5 := z.MakeRegion("[2013-05-22 11:22:33, 2013-11-12 15:45:00]")
	if r5.LeftDate().Unix() != time.Date(2013, 5, 22, 11, 22, 33, 0, time.Local).Unix() {
		t.Error("left should be 2013-05-22 11:22:33")
	}

	r4 := z.MakeRegion("[2013-05-22, 2013-11-12]")
	if r4.LeftDate().Unix() != time.Date(2013, 5, 22, 0, 0, 0, 0, time.Local).Unix() {
		t.Error("left should be 2013-05-22")
	}
}
Пример #2
0
// 配备并重新组合查询语句, 给出QCnd对象
func (qb *QWBuilder) evalQCnd(fld string) *QCnd {
	for _, rule := range qb.Rules {
		if rule.Regex.MatchString(fld) {
			z.DebugPrintf("fld [%s] match regex [%s]\n", fld, rule.Regex.String())
			groups := rule.Regex.FindStringSubmatch(fld)
			for _, grp := range groups {
				z.DebugPrintf("    g_%s\n", grp)
			}
			extractStr := z.Trim(findMatchStr(rule.Seg, groups))
			// 生成QCnd
			qc := new(QCnd)
			qc.Key = rule.Key
			qc.Origin = fld
			qc.Plain = extractStr
			qc.Type = rule.Type
			switch qc.Type {
			case String:
				qc.Value = extractStr
			case Regex:
				qc.Value = regexp.MustCompile(extractStr)
			case IntRegion, LongRegion, DateRegion:
				qc.Value = z.MakeRegion(extractStr)
			case StringEnum:
				senum := extractStr[1 : len(extractStr)-1]
				qc.Value = z.SplitIgnoreBlank(senum, ",")
			case IntEnum:
				ienum := extractStr[1 : len(extractStr)-1]
				iarray := make([]int, 0, 5)
				for _, ie := range z.SplitIgnoreBlank(ienum, ",") {
					ione, _ := strconv.Atoi(ie)
					iarray = append(iarray, ione)
				}
				qc.Value = iarray
			case Json:
				jmap := new(map[string]interface{})
				jerr := z.JsonFromString(extractStr, jmap)
				if jerr != nil {
					qc.Value = jmap
				}
			default:
				// unsupport
				qc.Value = nil
			}
			return qc
		}
	}
	z.DebugPrintf("fld [%s] miss match\n", fld)
	return nil
}