diff --git a/cluster/const.go b/cluster/const.go index 34b793e..233c9d4 100644 --- a/cluster/const.go +++ b/cluster/const.go @@ -17,8 +17,13 @@ const ( ClusterParam = "cluster" ) + +type Command uint64 + +// need: go get golang.org/x/tools/cmd/stringer +//go:generate stringer -type=Command -output const_string.go const ( - _ = iota + _ Command = iota UpdateBackendDeclarationCommand DeleteBackendDeclarationCommand @@ -80,9 +85,9 @@ func PrepareUpsertCommonConfigCommand(config UpsertCommonConfig) []byte { return prepareCommand(UpsertCommonConfigCommand, config) } -func prepareCommand(command uint64, payload interface{}) []byte { +func prepareCommand(command Command, payload interface{}) []byte { cmd := make([]byte, 8) - binary.BigEndian.PutUint64(cmd, command) + binary.BigEndian.PutUint64(cmd, uint64(command)) buf := bytes.NewBuffer(cmd) err := json.NewEncoder(buf).Encode(payload) if err != nil { diff --git a/cluster/const_string.go b/cluster/const_string.go new file mode 100644 index 0000000..5aa85ab --- /dev/null +++ b/cluster/const_string.go @@ -0,0 +1,34 @@ +// Code generated by "stringer -type=Command -output const_string.go"; DO NOT EDIT. + +package cluster + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[UpdateBackendDeclarationCommand-1] + _ = x[DeleteBackendDeclarationCommand-2] + _ = x[UpdateConfigSchemaCommand-3] + _ = x[ModuleConnectedCommand-4] + _ = x[ModuleDisconnectedCommand-5] + _ = x[DeleteModulesCommand-6] + _ = x[ActivateConfigCommand-7] + _ = x[DeleteConfigsCommand-8] + _ = x[UpsertConfigCommand-9] + _ = x[DeleteCommonConfigsCommand-10] + _ = x[UpsertCommonConfigCommand-11] +} + +const _Command_name = "UpdateBackendDeclarationCommandDeleteBackendDeclarationCommandUpdateConfigSchemaCommandModuleConnectedCommandModuleDisconnectedCommandDeleteModulesCommandActivateConfigCommandDeleteConfigsCommandUpsertConfigCommandDeleteCommonConfigsCommandUpsertCommonConfigCommand" + +var _Command_index = [...]uint16{0, 31, 62, 87, 109, 134, 154, 175, 195, 214, 240, 265} + +func (i Command) String() string { + i -= 1 + if i >= Command(len(_Command_index)-1) { + return "Command(" + strconv.FormatInt(int64(i+1), 10) + ")" + } + return _Command_name[_Command_index[i]:_Command_index[i+1]] +} diff --git a/store/store.go b/store/store.go index 870229a..f26a479 100644 --- a/store/store.go +++ b/store/store.go @@ -23,7 +23,7 @@ var ( type Store struct { state *state.State lock sync.RWMutex - handlers map[uint64]func([]byte) (interface{}, error) + handlers map[cluster.Command]func([]byte) (interface{}, error) } func (s *Store) Apply(l *raft.Log) interface{} { @@ -33,8 +33,8 @@ func (s *Store) Apply(l *raft.Log) interface{} { if len(l.Data) < cluster.CommandSizeBytes { log.Errorf(codes.ApplyLogCommandError, "invalid log data command: %s", l.Data) } - command := binary.BigEndian.Uint64(l.Data[:8]) - log.Debugf(0, "Apply %d command. Data: %s", command, l.Data) + command := cluster.Command(binary.BigEndian.Uint64(l.Data[:8])) + log.Debugf(0, "Apply %s. Data: %s", command, l.Data) var ( result interface{} @@ -119,7 +119,7 @@ func NewStateStore(st *state.State) *Store { store := &Store{ state: st, } - store.handlers = map[uint64]func([]byte) (interface{}, error){ + store.handlers = map[cluster.Command]func([]byte) (interface{}, error){ cluster.UpdateBackendDeclarationCommand: store.applyUpdateBackendDeclarationCommand, cluster.DeleteBackendDeclarationCommand: store.applyDeleteBackendDeclarationCommand, cluster.ModuleConnectedCommand: store.applyModuleConnectedCommand,