github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/appdef/interface_projector.go (about)

     1  /*
     2   * Copyright (c) 2021-present Sigma-Soft, Ltd.
     3   * @author: Nikolay Nikitin
     4   */
     5  
     6  package appdef
     7  
     8  // Projector is a extension that executes every time when some event is triggered and data need to be updated.
     9  type IProjector interface {
    10  	IExtension
    11  
    12  	// Returns is synchronous projector.
    13  	Sync() bool
    14  
    15  	// Events to trigger.
    16  	Events() IProjectorEvents
    17  
    18  	// Schedule to trigger projector as cron expression.
    19  	CronSchedule() string
    20  
    21  	// Returns is projector is able to handle `sys.Error` events.
    22  	// False by default.
    23  	WantErrors() bool
    24  }
    25  
    26  // Describe all events to trigger the projector.
    27  type IProjectorEvents interface {
    28  	// Enumerate events to trigger the projector.
    29  	//
    30  	// Events enumerated in alphabetical QNames order.
    31  	Enum(func(IProjectorEvent))
    32  
    33  	// Returns event by name.
    34  	//
    35  	// Returns nil if event not found.
    36  	Event(QName) IProjectorEvent
    37  
    38  	// Returns number of events.
    39  	Len() int
    40  
    41  	// Returns events to trigger as map.
    42  	Map() map[QName][]ProjectorEventKind
    43  }
    44  
    45  // Describe event to trigger the projector.
    46  type IProjectorEvent interface {
    47  	IWithComments
    48  
    49  	// Returns type to trigger projector.
    50  	//
    51  	// This can be a record or command.
    52  	On() IType
    53  
    54  	// Returns set (sorted slice) of event kind to trigger.
    55  	Kind() []ProjectorEventKind
    56  }
    57  
    58  // Events enumeration to trigger the projector
    59  type ProjectorEventKind uint8
    60  
    61  //go:generate stringer -type=ProjectorEventKind -output=stringer_projectoreventkind.go
    62  
    63  const (
    64  	ProjectorEventKind_Insert ProjectorEventKind = iota + 1
    65  	ProjectorEventKind_Update
    66  	ProjectorEventKind_Activate
    67  	ProjectorEventKind_Deactivate
    68  	ProjectorEventKind_Execute
    69  	ProjectorEventKind_ExecuteWithParam
    70  
    71  	ProjectorEventKind_Count
    72  )
    73  
    74  // ProjectorEventKind_AnyChanges describes events for record any change.
    75  var ProjectorEventKind_AnyChanges = []ProjectorEventKind{
    76  	ProjectorEventKind_Insert,
    77  	ProjectorEventKind_Update,
    78  	ProjectorEventKind_Activate,
    79  	ProjectorEventKind_Deactivate,
    80  }
    81  
    82  type IProjectorBuilder interface {
    83  	IExtensionBuilder
    84  
    85  	// Sets is synchronous projector.
    86  	SetSync(bool) IProjectorBuilder
    87  
    88  	// Events builder.
    89  	Events() IProjectorEventsBuilder
    90  
    91  	// Schedule to trigger projector as cron expression.
    92  	SetCronSchedule(string) IProjectorBuilder
    93  
    94  	// Sets is projector is able to handle `sys.Error` events.
    95  	SetWantErrors() IProjectorBuilder
    96  }
    97  
    98  type IProjectorEventsBuilder interface {
    99  	// Adds event to trigger the projector.
   100  	//
   101  	// QName can be some record type or command. QName can be one of QNameAny××× compatible substitutions.
   102  	//
   103  	// If event kind is missed then default is:
   104  	//   - ProjectorEventKind_Any for GDoc/GRecords, CDoc/CRecords and WDoc/WRecords
   105  	//	 - ProjectorEventKind_Execute for Commands
   106  	//	 - ProjectorEventKind_ExecuteWith for Objects and ODocs
   107  	//
   108  	// # Panics:
   109  	//	- if QName is empty (NullQName)
   110  	//	- if QName type is not a record and not a command
   111  	//	- if event kind is not applicable for QName type.
   112  	Add(on QName, event ...ProjectorEventKind) IProjectorEventsBuilder
   113  
   114  	// Sets event comment.
   115  	//
   116  	// # Panics:
   117  	//	- if event for QName is not added.
   118  	SetComment(on QName, comment ...string) IProjectorEventsBuilder
   119  }
   120  
   121  type IWithProjectors interface {
   122  	// Return projector by name.
   123  	//
   124  	// Returns nil if not found.
   125  	Projector(QName) IProjector
   126  
   127  	// Enumerates all application projectors.
   128  	//
   129  	// Projectors are enumerated in alphabetical order by QName.
   130  	Projectors(func(IProjector))
   131  }
   132  
   133  type IProjectorsBuilder interface {
   134  	// Adds new projector.
   135  	//
   136  	// # Panics:
   137  	//   - if name is empty (appdef.NullQName),
   138  	//   - if name is invalid,
   139  	//   - if type with name already exists.
   140  	AddProjector(QName) IProjectorBuilder
   141  }