func ParseTwoPhaseActionByInterface()

in pkg/rm/two_phase.go [162:212]


func ParseTwoPhaseActionByInterface(v interface{}) (*TwoPhaseAction, error) {
	valueOfElem := reflect.ValueOf(v).Elem()
	typeOf := valueOfElem.Type()
	k := typeOf.Kind()
	if k != reflect.Struct {
		return nil, fmt.Errorf("param should be a struct, instead of a pointer")
	}
	numField := typeOf.NumField()

	var (
		hasPrepareMethodName bool
		hasCommitMethodName  bool
		hasRollbackMethod    bool
		twoPhaseName         string
		result               = TwoPhaseAction{
			twoPhaseService: v,
		}
	)
	for i := 0; i < numField; i++ {
		t := typeOf.Field(i)
		f := valueOfElem.Field(i)
		if ms, m, ok := getPrepareAction(t, f); ok {
			hasPrepareMethodName = true
			result.prepareMethod = m
			result.prepareMethodName = ms
		} else if ms, m, ok = getCommitMethod(t, f); ok {
			hasCommitMethodName = true
			result.commitMethod = m
			result.commitMethodName = ms
		} else if ms, m, ok = getRollbackMethod(t, f); ok {
			hasRollbackMethod = true
			result.rollbackMethod = m
			result.rollbackMethodName = ms
		}
	}
	if !hasPrepareMethodName {
		return nil, fmt.Errorf("missing prepare method")
	}
	if !hasCommitMethodName {
		return nil, fmt.Errorf("missing commit method")
	}
	if !hasRollbackMethod {
		return nil, fmt.Errorf("missing rollback method")
	}
	twoPhaseName = getActionName(v)
	if twoPhaseName == "" {
		return nil, fmt.Errorf("missing two phase name")
	}
	result.actionName = twoPhaseName
	return &result, nil
}