Exemple #1
0
func TestSaveTemplateTsB(t *testing.T) {
	tsB := MakeTsB()
	tpl := TCSave.GetTemplate(reflect.TypeOf(tsB))
	trans := trans.NewTransaction(pool)
	tpl.Engrave(&trans.Actions, mdl.NewKey(reflect.ValueOf(tsB)), reflect.ValueOf(tsB))

	printActions(trans.Actions)
	trans.Exec()
}
Exemple #2
0
func (gom *Gomdise) Save(arg interface{}) (key string, err error) {
	defer func() {
		err = errorRecover()
		if err != nil {
			key = ""
		}
	}()

	tpl := tpl.TCSave.GetTemplate(reflect.TypeOf(arg))
	tran := trans.NewTransaction(gom.pool)
	v := reflect.ValueOf(arg)
	key = mdl.NewKey(v)
	tpl.Engrave(&tran.Actions, key, v)
	tran.Exec()
	return key, nil
}
Exemple #3
0
func (ast *arraySaveTemplate) Engrave(actions *[]*trans.Action, args ...interface{}) {
	action := &trans.Action{
		Name: "RPUSH",
		Args: redis.Args{args[0]},
	}
	*actions = append(*actions, action)

	v := args[1].(reflect.Value)
	n := v.Len()
	for i := 0; i < n; i++ {
		eKey := mdl.NewKey(v.Index(i))
		if eKey != "" {
			action.Args = action.Args.Add(eKey)
		}
		ast.elemTpl.Engrave(actions, eKey, v.Index(i))
	}

	action.Handler = func(tran *trans.Transaction, action *trans.Action, reply interface{}) {
		numbericReplyValidator(action, n, reply)
	}
}
Exemple #4
0
func (mst *mapSaveTemplate) Engrave(actions *[]*trans.Action, args ...interface{}) {
	action := &trans.Action{
		Name: "HMSET",
		Args: redis.Args{args[0]},
	}
	*actions = append(*actions, action)

	v := args[1].(reflect.Value)
	mKey := v.MapKeys()
	for _, mKey := range mKey {
		action.Args = action.Args.Add(mKey)
		mVal := mdl.NewKey(v.MapIndex(mKey))
		if mVal != "" {
			action.Args = action.Args.Add(mVal)
		}
		mst.elemTpl.Engrave(actions, mVal, v.MapIndex(mKey))
	}

	action.Handler = func(tran *trans.Transaction, action *trans.Action, reply interface{}) {
		stringReplyValidator(action, "OK", reply)
	}
}
Exemple #5
0
func (sst *structSaveTemplate) Engrave(actions *[]*trans.Action, args ...interface{}) {
	action := &trans.Action{
		Name: "HMSET",
		Args: redis.Args{args[0]},
	}
	*actions = append(*actions, action)

	v := args[1].(reflect.Value)
	for i, fld := range sst.spec.Fields {
		action.Args = action.Args.Add(fld.Name)
		fVal := fld.ValueOf(v)
		fKey := mdl.NewKey(fVal)
		if fKey != "" {
			action.Args = action.Args.Add(fKey)
		}
		sst.elemTpls[i].Engrave(actions, fKey, fVal)
	}

	action.Handler = func(tran *trans.Transaction, action *trans.Action, reply interface{}) {
		stringReplyValidator(action, "OK", reply)
	}
}