Skip to content

Commit

Permalink
Merge pull request #59 from qmuntal/renameany
Browse files Browse the repository at this point in the history
Rewrite interface{} to any
  • Loading branch information
qmuntal authored Jul 13, 2023
2 parents 096d0dc + c1b06b7 commit 99960e7
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 165 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ phoneCall := stateless.NewStateMachine(stateOffHook)
phoneCall.Configure(stateOffHook).Permit(triggerCallDialed, stateRinging)

phoneCall.Configure(stateRinging).
OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...interface{}) error {
OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...any) error {
onDialed(args[0].(string))
return nil
}).
Permit(triggerCallConnected, stateConnected)

phoneCall.Configure(stateConnected).
OnEntry(func(_ context.Context, _ ...interface{}) error {
OnEntry(func(_ context.Context, _ ...any) error {
startCallTimer()
return nil
}).
OnExit(func(_ context.Context, _ ...interface{}) error {
OnExit(func(_ context.Context, _ ...any) error {
stopCallTimer()
return nil
}).
Expand Down Expand Up @@ -126,10 +126,10 @@ The state machine will choose between multiple transitions based on guard clause

```go
phoneCall.Configure(stateOffHook).
Permit(triggerCallDialled, stateRinging, func(_ context.Context, _ ...interface{}) bool {
Permit(triggerCallDialled, stateRinging, func(_ context.Context, _ ...any) bool {
return IsValidNumber()
}).
Permit(triggerCallDialled, stateBeeping, func(_ context.Context, _ ...interface{}) bool {
Permit(triggerCallDialled, stateBeeping, func(_ context.Context, _ ...any) bool {
return !IsValidNumber()
})
```
Expand All @@ -146,7 +146,7 @@ Strongly-typed parameters can be assigned to triggers:
stateMachine.SetTriggerParameters(triggerCallDialed, reflect.TypeOf(""))

stateMachine.Configure(stateRinging).
OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...interface{}) error {
OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...any) error {
fmt.Println(args[0].(string))
return nil
})
Expand Down Expand Up @@ -174,7 +174,7 @@ Alternatively, a state can be marked reentrant so its entry and exit events will
```go
stateMachine.Configure(stateAssigned).
PermitReentry(triggerAssigned).
OnEntry(func(_ context.Context, _ ...interface{}) error {
OnEntry(func(_ context.Context, _ ...any) error {
startCallTimer()
return nil
})
Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ func GetTransition(ctx context.Context) Transition {

// ActionFunc describes a generic action function.
// The context will always contain Transition information.
type ActionFunc = func(ctx context.Context, args ...interface{}) error
type ActionFunc = func(ctx context.Context, args ...any) error

// GuardFunc defines a generic guard function.
type GuardFunc = func(ctx context.Context, args ...interface{}) bool
type GuardFunc = func(ctx context.Context, args ...any) bool

// DestinationSelectorFunc defines a functions that is called to select a dynamic destination.
type DestinationSelectorFunc = func(ctx context.Context, args ...interface{}) (State, error)
type DestinationSelectorFunc = func(ctx context.Context, args ...any) (State, error)

// StateConfiguration is the configuration for a single state value.
type StateConfiguration struct {
Expand Down
12 changes: 6 additions & 6 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@ func Example() {
Permit(triggerCallDialed, stateRinging)

phoneCall.Configure(stateRinging).
OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...interface{}) error {
OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...any) error {
onDialed(args[0].(string))
return nil
}).
Permit(triggerCallConnected, stateConnected)

phoneCall.Configure(stateConnected).
OnEntry(startCallTimer).
OnExit(func(_ context.Context, _ ...interface{}) error {
OnExit(func(_ context.Context, _ ...any) error {
stopCallTimer()
return nil
}).
InternalTransition(triggerMuteMicrophone, func(_ context.Context, _ ...interface{}) error {
InternalTransition(triggerMuteMicrophone, func(_ context.Context, _ ...any) error {
onMute()
return nil
}).
InternalTransition(triggerUnmuteMicrophone, func(_ context.Context, _ ...interface{}) error {
InternalTransition(triggerUnmuteMicrophone, func(_ context.Context, _ ...any) error {
onUnmute()
return nil
}).
InternalTransition(triggerSetVolume, func(_ context.Context, args ...interface{}) error {
InternalTransition(triggerSetVolume, func(_ context.Context, args ...any) error {
onSetVolume(args[0].(int))
return nil
}).
Expand Down Expand Up @@ -111,7 +111,7 @@ func onDialed(callee string) {
fmt.Printf("[Phone Call] placed for : [%s]\n", callee)
}

func startCallTimer(_ context.Context, _ ...interface{}) error {
func startCallTimer(_ context.Context, _ ...any) error {
fmt.Println("[Timer:] Call started at 11:00am")
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ func (g *graph) formatOneLine(fromNodeName, toNodeName, label string) string {
return sb.String()
}

func clusterStr(state interface{}, quote, init bool) string {
func clusterStr(state any, quote, init bool) string {
s := fmt.Sprint(state)
if init {
s += "-init"
}
return esc("cluster_"+s, quote)
}

func str(v interface{}, quote bool) string {
func str(v any, quote bool) string {
return esc(fmt.Sprint(v), quote)
}

Expand Down
16 changes: 8 additions & 8 deletions graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ func withGuards() *stateless.StateMachine {
sm := stateless.NewStateMachine("B")
sm.SetTriggerParameters("X", reflect.TypeOf(0))
sm.Configure("A").
Permit("X", "D", func(_ context.Context, args ...interface{}) bool {
Permit("X", "D", func(_ context.Context, args ...any) bool {
return args[0].(int) == 3
})

sm.Configure("B").
SubstateOf("A").
Permit("X", "C", func(_ context.Context, args ...interface{}) bool {
Permit("X", "C", func(_ context.Context, args ...any) bool {
return args[0].(int) == 2
})
return sm
}

func œ(_ context.Context, args ...interface{}) bool {
func œ(_ context.Context, args ...any) bool {
return args[0].(int) == 2
}

Expand Down Expand Up @@ -88,23 +88,23 @@ func phoneCall() *stateless.StateMachine {
Permit(triggerCallDialed, stateRinging)

phoneCall.Configure(stateRinging).
OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...interface{}) error {
OnEntryFrom(triggerCallDialed, func(_ context.Context, args ...any) error {
return nil
}).
Permit(triggerCallConnected, stateConnected)

phoneCall.Configure(stateConnected).
OnEntry(startCallTimer).
OnExit(func(_ context.Context, _ ...interface{}) error {
OnExit(func(_ context.Context, _ ...any) error {
return nil
}).
InternalTransition(triggerMuteMicrophone, func(_ context.Context, _ ...interface{}) error {
InternalTransition(triggerMuteMicrophone, func(_ context.Context, _ ...any) error {
return nil
}).
InternalTransition(triggerUnmuteMicrophone, func(_ context.Context, _ ...interface{}) error {
InternalTransition(triggerUnmuteMicrophone, func(_ context.Context, _ ...any) error {
return nil
}).
InternalTransition(triggerSetVolume, func(_ context.Context, args ...interface{}) error {
InternalTransition(triggerSetVolume, func(_ context.Context, args ...any) error {
return nil
}).
Permit(triggerLeftMessage, stateOffHook).
Expand Down
32 changes: 16 additions & 16 deletions statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

// State is used to to represent the possible machine states.
type State = interface{}
type State = any

// Trigger is used to represent the triggers that cause state transitions.
type Trigger = interface{}
type Trigger = any

// FiringMode enumerate the different modes used when Fire-ing a trigger.
type FiringMode uint8
Expand Down Expand Up @@ -145,12 +145,12 @@ func (sm *StateMachine) MustState() State {
}

// PermittedTriggers see PermittedTriggersCtx.
func (sm *StateMachine) PermittedTriggers(args ...interface{}) ([]Trigger, error) {
func (sm *StateMachine) PermittedTriggers(args ...any) ([]Trigger, error) {
return sm.PermittedTriggersCtx(context.Background(), args...)
}

// PermittedTriggersCtx returns the currently-permissible trigger values.
func (sm *StateMachine) PermittedTriggersCtx(ctx context.Context, args ...interface{}) ([]Trigger, error) {
func (sm *StateMachine) PermittedTriggersCtx(ctx context.Context, args ...any) ([]Trigger, error) {
sr, err := sm.currentState(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -206,12 +206,12 @@ func (sm *StateMachine) IsInStateCtx(ctx context.Context, state State) (bool, er
}

// CanFire see CanFireCtx.
func (sm *StateMachine) CanFire(trigger Trigger, args ...interface{}) (bool, error) {
func (sm *StateMachine) CanFire(trigger Trigger, args ...any) (bool, error) {
return sm.CanFireCtx(context.Background(), trigger, args...)
}

// CanFireCtx returns true if the trigger can be fired in the current state.
func (sm *StateMachine) CanFireCtx(ctx context.Context, trigger Trigger, args ...interface{}) (bool, error) {
func (sm *StateMachine) CanFireCtx(ctx context.Context, trigger Trigger, args ...any) (bool, error) {
sr, err := sm.currentState(ctx)
if err != nil {
return false, err
Expand All @@ -229,7 +229,7 @@ func (sm *StateMachine) SetTriggerParameters(trigger Trigger, argumentTypes ...r
}

// Fire see FireCtx
func (sm *StateMachine) Fire(trigger Trigger, args ...interface{}) error {
func (sm *StateMachine) Fire(trigger Trigger, args ...any) error {
return sm.FireCtx(context.Background(), trigger, args...)
}

Expand All @@ -246,7 +246,7 @@ func (sm *StateMachine) Fire(trigger Trigger, args ...interface{}) error {
//
// The context is passed down to all actions and callbacks called within the scope of this method.
// There is no context error checking, although it may be implemented in future releases.
func (sm *StateMachine) FireCtx(ctx context.Context, trigger Trigger, args ...interface{}) error {
func (sm *StateMachine) FireCtx(ctx context.Context, trigger Trigger, args ...any) error {
return sm.internalFire(ctx, trigger, args...)
}

Expand Down Expand Up @@ -313,7 +313,7 @@ func (sm *StateMachine) stateRepresentation(state State) (sr *stateRepresentatio
return
}

func (sm *StateMachine) internalFire(ctx context.Context, trigger Trigger, args ...interface{}) error {
func (sm *StateMachine) internalFire(ctx context.Context, trigger Trigger, args ...any) error {
switch sm.firingMode {
case FiringImmediate:
return sm.internalFireOne(ctx, trigger, args...)
Expand All @@ -327,10 +327,10 @@ func (sm *StateMachine) internalFire(ctx context.Context, trigger Trigger, args
type queuedTrigger struct {
Context context.Context
Trigger Trigger
Args []interface{}
Args []any
}

func (sm *StateMachine) internalFireQueued(ctx context.Context, trigger Trigger, args ...interface{}) error {
func (sm *StateMachine) internalFireQueued(ctx context.Context, trigger Trigger, args ...any) error {
sm.firingMutex.Lock()
sm.eventQueue.PushBack(queuedTrigger{Context: ctx, Trigger: trigger, Args: args})
sm.firingMutex.Unlock()
Expand All @@ -354,7 +354,7 @@ func (sm *StateMachine) internalFireQueued(ctx context.Context, trigger Trigger,
return nil
}

func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, args ...interface{}) (err error) {
func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, args ...any) (err error) {
sm.ops.Add(1)
defer sm.ops.Add(^uint64(0))
var (
Expand All @@ -380,7 +380,7 @@ func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, ar
transition := Transition{Source: source, Destination: t.Destination, Trigger: trigger}
err = sm.handleReentryTrigger(ctx, representativeState, transition, args...)
case *dynamicTriggerBehaviour:
var destination interface{}
var destination any
destination, err = t.Destination(ctx, args...)
if err == nil {
transition := Transition{Source: source, Destination: destination, Trigger: trigger}
Expand All @@ -400,7 +400,7 @@ func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, ar
return
}

func (sm *StateMachine) handleReentryTrigger(ctx context.Context, sr *stateRepresentation, transition Transition, args ...interface{}) error {
func (sm *StateMachine) handleReentryTrigger(ctx context.Context, sr *stateRepresentation, transition Transition, args ...any) error {
if err := sr.Exit(ctx, transition, args...); err != nil {
return err
}
Expand All @@ -423,7 +423,7 @@ func (sm *StateMachine) handleReentryTrigger(ctx context.Context, sr *stateRepre
return nil
}

func (sm *StateMachine) handleTransitioningTrigger(ctx context.Context, sr *stateRepresentation, transition Transition, args ...interface{}) error {
func (sm *StateMachine) handleTransitioningTrigger(ctx context.Context, sr *stateRepresentation, transition Transition, args ...any) error {
if err := sr.Exit(ctx, transition, args...); err != nil {
return err
}
Expand All @@ -446,7 +446,7 @@ func (sm *StateMachine) handleTransitioningTrigger(ctx context.Context, sr *stat
return nil
}

func (sm *StateMachine) enterState(ctx context.Context, sr *stateRepresentation, transition Transition, args ...interface{}) (*stateRepresentation, error) {
func (sm *StateMachine) enterState(ctx context.Context, sr *stateRepresentation, transition Transition, args ...any) (*stateRepresentation, error) {
// Enter the new state
err := sr.Enter(ctx, transition, args...)
if err != nil {
Expand Down
Loading

0 comments on commit 99960e7

Please sign in to comment.