Skip to content

Commit

Permalink
feat: add VerifyStates for early state names assert (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
pancsta committed Feb 22, 2024
1 parent c0d6eca commit e4608e7
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,13 +709,30 @@ func (m *Machine) recoverToErr(emitter *emitter, r any) {
// MustParseStates parses the states and returns them as a list.
// Panics when a state is not defined.
func (m *Machine) MustParseStates(states S) S {
// check if all FromState are defined in m.FromState
// check if all states are defined in m.States
for _, s := range states {
if _, ok := m.States[s]; !ok {
panic(fmt.Sprintf("state %s is not defined", s))
}
}
return states
return lo.Uniq(states)
}

// VerifyStates verifies an array of state names and returns an error in case
// at least one isn't defined.
func (m *Machine) VerifyStates(states S) error {
var errs []error
for _, s := range states {
if _, ok := m.States[s]; !ok {
errs = append(errs, fmt.Errorf("state %s is not defined", s))
}
}
if len(errs) > 1 {
return errors.Join(errs...)
} else if len(errs) == 1 {
return errs[0]
}
return nil
}

// setActiveStates sets the new active states incrementing the counters and
Expand Down

0 comments on commit e4608e7

Please sign in to comment.