// cleanupAllProps cleans all props. func (s *scene) cleanupAllProps() error { for _, box := range s.props { if box.cleanup != nil { err := box.cleanup(box.key, box.prop) if err != nil { return errors.Annotate(err, ErrCleanupFailed, errorMessages, box.key) } } } return nil }
// NewUUIDByHex creates a UUID based on the passed hex string which has to // have the length of 32 bytes. func NewUUIDByHex(source string) (UUID, error) { uuid := UUID{} if len([]byte(source)) != 32 { return uuid, errors.New(ErrInvalidHexLength, errorMessages) } raw, err := hex.DecodeString(source) if err != nil { return uuid, errors.Annotate(err, ErrInvalidHexValue, errorMessages) } copy(uuid[:], raw) return uuid, nil }
// Test the annotation of errors. func TestAnnotation(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) ec := 123 messages := errors.Messages{ec: "annotated"} aerr := testError("wrapped") err := errors.Annotate(aerr, ec, messages) assert.ErrorMatch(err, `\[ERRORS_TEST:123\] annotated: wrapped`) assert.Equal(errors.Annotated(err), aerr) assert.True(errors.IsInvalidTypeError(errors.Annotated(aerr))) assert.Length(errors.Stack(err), 2) }
// processCommand processes the sent commands. func (s *scene) processCommand(command *envelope) { switch command.kind { case storeProp: // Add a new prop. _, ok := s.props[command.box.key] if ok { command.err = errors.New(ErrPropAlreadyExist, errorMessages, command.box.key) } else { s.props[command.box.key] = command.box } case fetchProp: // Retrieve a prop. box, ok := s.props[command.box.key] if !ok { command.err = errors.New(ErrPropNotFound, errorMessages, command.box.key) } else { command.box = box } case disposeProp: // Remove a prop. box, ok := s.props[command.box.key] if !ok { command.err = errors.New(ErrPropNotFound, errorMessages, command.box.key) } else { delete(s.props, command.box.key) command.box = box if box.cleanup != nil { cerr := box.cleanup(box.key, box.prop) if cerr != nil { command.err = errors.Annotate(cerr, ErrCleanupFailed, errorMessages, box.key) } } } case flag: // Signal a topic. s.flags[command.signaling.topic] = true // Notify subscribers. subscribers, ok := s.signalings[command.signaling.topic] if ok { delete(s.signalings, command.signaling.topic) for _, subscriber := range subscribers { subscriber <- struct{}{} } } case unflag: // Drop a topic. delete(s.flags, command.signaling.topic) case wait: // Add a waiter for a topic. active := s.flags[command.signaling.topic] if active { command.signaling.signalChan <- struct{}{} } else { waiters := s.signalings[command.signaling.topic] s.signalings[command.signaling.topic] = append(waiters, command.signaling.signalChan) } default: panic("illegal command") } // Return the changed command as response. command.respChan <- command }