github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/vent/types/event_class.go (about)

     1  package types
     2  
     3  import (
     4  	"github.com/alecthomas/jsonschema"
     5  	validation "github.com/go-ozzo/ozzo-validation"
     6  	"github.com/hyperledger/burrow/event/query"
     7  )
     8  
     9  // ProjectionSpec contains all event class specifications
    10  type ProjectionSpec []*EventClass
    11  
    12  func ProjectionSpecSchema() *jsonschema.Schema {
    13  	return jsonschema.Reflect(ProjectionSpec{})
    14  }
    15  
    16  // EventClass struct (table name where to persist filtered events and it structure)
    17  type EventClass struct {
    18  	// Destination table in DB
    19  	TableName string
    20  	// Burrow event filter query in query peg grammar
    21  	Filter string
    22  	// The name of a solidity event field that when present indicates that the rest of the event should be interpreted
    23  	// as requesting a row deletion (rather than upsert) in the projection table.
    24  	DeleteMarkerField string `json:",omitempty"`
    25  	// EventFieldMapping from solidity event field name to EventFieldMapping descriptor
    26  	FieldMappings []*EventFieldMapping
    27  	// Memoised lookup/query
    28  	query  query.Query
    29  	fields map[string]*EventFieldMapping
    30  }
    31  
    32  // Validate checks the structure of an EventClass
    33  func (ec *EventClass) Validate() error {
    34  	return validation.ValidateStruct(ec,
    35  		validation.Field(&ec.TableName, validation.Required, validation.Length(1, 60)),
    36  		validation.Field(&ec.Filter, validation.Required),
    37  		validation.Field(&ec.FieldMappings, validation.Required, validation.Length(1, 0)),
    38  	)
    39  }
    40  
    41  // Get a (memoised) Query from the EventClass Filter string
    42  func (ec *EventClass) Query() (query.Query, error) {
    43  	if ec.query == nil {
    44  		var err error
    45  		ec.query, err = query.New(ec.Filter)
    46  		if err != nil {
    47  			return nil, err
    48  		}
    49  	}
    50  	return ec.query, nil
    51  }
    52  
    53  func (ec *EventClass) GetFieldMapping(fieldName string) *EventFieldMapping {
    54  	if ec.fields == nil {
    55  		ec.fields = make(map[string]*EventFieldMapping, len(ec.FieldMappings))
    56  		for _, fm := range ec.FieldMappings {
    57  			ec.fields[fm.Field] = fm
    58  		}
    59  	}
    60  	return ec.fields[fieldName]
    61  }
    62  
    63  func (ec *EventClass) GetFilter() string {
    64  	if ec == nil {
    65  		return ""
    66  	}
    67  	return ec.Filter
    68  }
    69  
    70  // EventFieldMapping struct (table column definition)
    71  type EventFieldMapping struct {
    72  	// EVM event field name to process
    73  	Field string
    74  	// EVM type of this field - used to derive SQL type
    75  	Type string
    76  	// Destination SQL column name to which to map this event field
    77  	ColumnName string
    78  	// Whether this event field should map to a primary key
    79  	Primary bool `json:",omitempty"`
    80  	// Whether to convert this event field from bytes32 to string
    81  	BytesToString bool `json:",omitempty"`
    82  	// Whether to convert this event field from bytes32 to hex-encoded string
    83  	BytesToHex bool `json:",omitempty"`
    84  	// Notification channels on which submit (via a trigger) a payload that contains this column's new value (upsert) or
    85  	// old value (delete). The payload will contain all other values with the same channel set as a JSON object.
    86  	Notify []string `json:",omitempty"`
    87  }
    88  
    89  // Validate checks the structure of an EventFieldMapping
    90  func (evColumn EventFieldMapping) Validate() error {
    91  	return validation.ValidateStruct(&evColumn,
    92  		validation.Field(&evColumn.ColumnName, validation.Required, validation.Length(1, 60)),
    93  	)
    94  }