func()

in common/domain/archivalConfigStateMachine.go [77:235]


func (s *ArchivalState) getNextState(
	e *ArchivalEvent,
	URIValidationFunc func(URI string) error,
) (nextState *ArchivalState, changed bool, err error) {
	defer func() {
		// ensure that any existing URI name was not mutated
		if nextState != nil && len(s.URI) != 0 && s.URI != nextState.URI {
			nextState = nil
			changed = false
			err = errCannotHandleStateChange
			return
		}

		// ensure that next state is valid
		if nextState != nil {
			if nextStateErr := nextState.validate(); nextStateErr != nil {
				nextState = nil
				changed = false
				err = nextStateErr
				return
			}
		}

		if nextState != nil && nextState.URI != "" {
			if validateURIErr := URIValidationFunc(nextState.URI); validateURIErr != nil {
				nextState = nil
				changed = false
				err = validateURIErr
				return
			}
		}
	}()

	if s == nil || e == nil {
		return nil, false, errCannotHandleStateChange
	}
	if err := s.validate(); err != nil {
		return nil, false, err
	}
	if err := e.validate(); err != nil {
		return nil, false, err
	}

	/**
	At this point state and event are both non-nil and valid.

	State can be any one of the following:
	{status=enabled,  URI="foo"}
	{status=disabled, URI="foo"}
	{status=disabled, URI=""}

	Event can be any one of the following:
	{status=enabled,  URI="foo", defaultURI="bar"}
	{status=enabled,  URI="",    defaultURI="bar"}
	{status=disabled, URI="foo", defaultURI="bar"}
	{status=disabled, URI="",    defaultURI="bar"}
	{status=nil,      URI="foo", defaultURI="bar"}
	{status=nil,      URI="",    defaultURI="bar"}
	*/

	stateURISet := len(s.URI) != 0
	eventURISet := len(e.URI) != 0

	// factor this case out to ensure that URI is immutable
	if stateURISet && eventURISet && s.URI != e.URI {
		return nil, false, errURIUpdate
	}

	// state 1
	if s.Status == types.ArchivalStatusEnabled && stateURISet {
		if e.status != nil && *e.status == types.ArchivalStatusEnabled && eventURISet {
			return s, false, nil
		}
		if e.status != nil && *e.status == types.ArchivalStatusEnabled && !eventURISet {
			return s, false, nil
		}
		if e.status != nil && *e.status == types.ArchivalStatusDisabled && eventURISet {
			return &ArchivalState{
				Status: types.ArchivalStatusDisabled,
				URI:    s.URI,
			}, true, nil
		}
		if e.status != nil && *e.status == types.ArchivalStatusDisabled && !eventURISet {
			return &ArchivalState{
				Status: types.ArchivalStatusDisabled,
				URI:    s.URI,
			}, true, nil
		}
		if e.status == nil && eventURISet {
			return s, false, nil
		}
		if e.status == nil && !eventURISet {
			return s, false, nil
		}
	}

	// state 2
	if s.Status == types.ArchivalStatusDisabled && stateURISet {
		if e.status != nil && *e.status == types.ArchivalStatusEnabled && eventURISet {
			return &ArchivalState{
				URI:    s.URI,
				Status: types.ArchivalStatusEnabled,
			}, true, nil
		}
		if e.status != nil && *e.status == types.ArchivalStatusEnabled && !eventURISet {
			return &ArchivalState{
				Status: types.ArchivalStatusEnabled,
				URI:    s.URI,
			}, true, nil
		}
		if e.status != nil && *e.status == types.ArchivalStatusDisabled && eventURISet {
			return s, false, nil
		}
		if e.status != nil && *e.status == types.ArchivalStatusDisabled && !eventURISet {
			return s, false, nil
		}
		if e.status == nil && eventURISet {
			return s, false, nil
		}
		if e.status == nil && !eventURISet {
			return s, false, nil
		}
	}

	// state 3
	if s.Status == types.ArchivalStatusDisabled && !stateURISet {
		if e.status != nil && *e.status == types.ArchivalStatusEnabled && eventURISet {
			return &ArchivalState{
				Status: types.ArchivalStatusEnabled,
				URI:    e.URI,
			}, true, nil
		}
		if e.status != nil && *e.status == types.ArchivalStatusEnabled && !eventURISet {
			return &ArchivalState{
				Status: types.ArchivalStatusEnabled,
				URI:    e.defaultURI,
			}, true, nil
		}
		if e.status != nil && *e.status == types.ArchivalStatusDisabled && eventURISet {
			return &ArchivalState{
				Status: types.ArchivalStatusDisabled,
				URI:    e.URI,
			}, true, nil
		}
		if e.status != nil && *e.status == types.ArchivalStatusDisabled && !eventURISet {
			return s, false, nil
		}
		if e.status == nil && eventURISet {
			return &ArchivalState{
				Status: types.ArchivalStatusDisabled,
				URI:    e.URI,
			}, true, nil
		}
		if e.status == nil && !eventURISet {
			return s, false, nil
		}
	}
	return nil, false, errCannotHandleStateChange
}