diff --git a/acl.go b/acl.go index 3e8a5bd..ccb681a 100644 --- a/acl.go +++ b/acl.go @@ -48,7 +48,7 @@ func (ext *ACLExt) Process(ctx *Context, node *Node, source NodeID, signal Signa response, is_response := signal.(ResponseSignal) if is_response == true { var messages Messages = nil - var changes Changes = nil + var changes = Changes{} info, waiting := ext.Pending[response.ResponseID()] if waiting == true { changes.Add(ACLExtType, "pending") @@ -106,7 +106,7 @@ func (ext *ACLExt) Process(ctx *Context, node *Node, source NodeID, signal Signa } var messages Messages = nil - var changes Changes = nil + var changes = Changes{} switch sig := signal.(type) { case *ACLSignal: diff --git a/context.go b/context.go index 52f4ccc..d2131f9 100644 --- a/context.go +++ b/context.go @@ -435,6 +435,16 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) { return nil, err } + err = ctx.RegisterType(reflect.TypeOf(EventCommand("")), EventCommandType, nil, nil, nil, DeserializeString[EventState]) + if err != nil { + return nil, err + } + + err = ctx.RegisterType(reflect.TypeOf(EventState("")), EventStateType, nil, nil, nil, DeserializeString[EventState]) + if err != nil { + return nil, err + } + wait_info_type := reflect.TypeOf(WaitInfo{}) wait_info_info, err := GetStructInfo(ctx, wait_info_type) if err != nil { diff --git a/event.go b/event.go index 40b8203..f4d0882 100644 --- a/event.go +++ b/event.go @@ -5,6 +5,9 @@ import ( "fmt" ) +type EventCommand string +type EventState string + type ParentOfPolicy struct { PolicyHeader Policy Tree @@ -41,7 +44,7 @@ var DefaultEventPolicy = NewParentOfPolicy(Tree{ type EventExt struct { Name string `gv:"name"` - State string `gv:"state"` + State EventState `gv:"state"` Parent NodeID `gv:"parent"` } @@ -55,9 +58,9 @@ func NewEventExt(parent NodeID, name string) *EventExt { type EventStateSignal struct { SignalHeader - Source NodeID - State string - Time time.Time + Source NodeID `gv:"source"` + State EventState `gv:"state"` + Time time.Time `gv:"time"` } func (signal EventStateSignal) Permission() Tree { @@ -70,7 +73,7 @@ func (signal EventStateSignal) String() string { return fmt.Sprintf("EventStateSignal(%s, %s, %s, %+v)", signal.SignalHeader, signal.Source, signal.State, signal.Time) } -func NewEventStateSignal(source NodeID, state string, t time.Time) *EventStateSignal { +func NewEventStateSignal(source NodeID, state EventState, t time.Time) *EventStateSignal { return &EventStateSignal{ SignalHeader: NewSignalHeader(Up), Source: source, @@ -81,14 +84,14 @@ func NewEventStateSignal(source NodeID, state string, t time.Time) *EventStateSi type EventControlSignal struct { SignalHeader - Command string `gv:"command"` + Command EventCommand `gv:"command"` } func (signal EventControlSignal) String() string { return fmt.Sprintf("EventControlSignal(%s, %s)", signal.SignalHeader, signal.Command) } -func NewEventControlSignal(command string) *EventControlSignal { +func NewEventControlSignal(command EventCommand) *EventControlSignal { return &EventControlSignal{ NewSignalHeader(Direct), command, @@ -98,12 +101,12 @@ func NewEventControlSignal(command string) *EventControlSignal { func (signal EventControlSignal) Permission() Tree { return Tree{ SerializedType(EventControlSignalType): { - Hash("command", signal.Command): nil, + Hash("command", string(signal.Command)): nil, }, } } -func (ext *EventExt) UpdateState(node *Node, changes Changes, state string) { +func (ext *EventExt) UpdateState(node *Node, changes Changes, state EventState) { if ext.State != state { changes.Add(EventExtType, "state") ext.State = state @@ -126,7 +129,8 @@ type TestEventExt struct { Length time.Duration } -var test_event_commands = map[string]map[string]string{ +type EventCommandMap map[EventCommand]map[EventState]EventState +var test_event_commands = EventCommandMap{ "ready?": { "init": "ready", }, @@ -173,11 +177,7 @@ func (ext *TestEventExt) Process(ctx *Context, node *Node, source NodeID, signal return messages, changes } -type TransitionValidation struct { - ToState string -} - -func(ext *EventExt) ValidateEventCommand(signal *EventControlSignal, commands map[string]map[string]string) (string, *ErrorSignal) { +func(ext *EventExt) ValidateEventCommand(signal *EventControlSignal, commands EventCommandMap) (EventState, *ErrorSignal) { transitions, command_mapped := commands[signal.Command] if command_mapped == false { return "", NewErrorSignal(signal.Id, "unknown command %s", signal.Command) diff --git a/serialize.go b/serialize.go index 68e92a4..5a0f422 100644 --- a/serialize.go +++ b/serialize.go @@ -267,6 +267,8 @@ var ( InterfaceType = NewSerializedType("INTERFACE") MapType = NewSerializedType("MAP") + EventStateType = NewSerializedType("EVENT_STATE") + EventCommandType = NewSerializedType("EVENT_COMMAND") ReqStateType = NewSerializedType("REQ_STATE") WaitInfoType = NewSerializedType("WAIT_INFO") SignalDirectionType = NewSerializedType("SIGNAL_DIRECTION")