github.com/safing/portbase@v0.19.5/runtime/provider.go (about)

     1  package runtime
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/safing/portbase/database/record"
     7  )
     8  
     9  var (
    10  	// ErrReadOnly should be returned from ValueProvider.Set if a
    11  	// runtime record is considered read-only.
    12  	ErrReadOnly = errors.New("runtime record is read-only")
    13  	// ErrWriteOnly should be returned from ValueProvider.Get if
    14  	// a runtime record is considered write-only.
    15  	ErrWriteOnly = errors.New("runtime record is write-only")
    16  )
    17  
    18  type (
    19  	// PushFunc is returned when registering a new value provider
    20  	// and can be used to inform the database system about the
    21  	// availability of a new runtime record value. Similar to
    22  	// database.Controller.PushUpdate, the caller must hold
    23  	// the lock for each record passed to PushFunc.
    24  	PushFunc func(...record.Record)
    25  
    26  	// ValueProvider provides access to a runtime-computed
    27  	// database record.
    28  	ValueProvider interface {
    29  		// Set is called when the value is set from outside.
    30  		// If the runtime value is considered read-only ErrReadOnly
    31  		// should be returned. It is guaranteed that the key of
    32  		// the record passed to Set is prefixed with the key used
    33  		// to register the value provider.
    34  		Set(record.Record) (record.Record, error)
    35  		// Get should return one or more records that match keyOrPrefix.
    36  		// keyOrPrefix is guaranteed to be at least the prefix used to
    37  		// register the ValueProvider.
    38  		Get(keyOrPrefix string) ([]record.Record, error)
    39  	}
    40  
    41  	// SimpleValueSetterFunc is a convenience type for implementing a
    42  	// write-only value provider.
    43  	SimpleValueSetterFunc func(record.Record) (record.Record, error)
    44  
    45  	// SimpleValueGetterFunc is a convenience type for implementing a
    46  	// read-only value provider.
    47  	SimpleValueGetterFunc func(keyOrPrefix string) ([]record.Record, error)
    48  )
    49  
    50  // Set implements ValueProvider.Set and calls fn.
    51  func (fn SimpleValueSetterFunc) Set(r record.Record) (record.Record, error) {
    52  	return fn(r)
    53  }
    54  
    55  // Get implements ValueProvider.Get and returns ErrWriteOnly.
    56  func (SimpleValueSetterFunc) Get(_ string) ([]record.Record, error) {
    57  	return nil, ErrWriteOnly
    58  }
    59  
    60  // Set implements ValueProvider.Set and returns ErrReadOnly.
    61  func (SimpleValueGetterFunc) Set(r record.Record) (record.Record, error) {
    62  	return nil, ErrReadOnly
    63  }
    64  
    65  // Get implements ValueProvider.Get and calls fn.
    66  func (fn SimpleValueGetterFunc) Get(keyOrPrefix string) ([]record.Record, error) {
    67  	return fn(keyOrPrefix)
    68  }
    69  
    70  // Compile time checks.
    71  var (
    72  	_ ValueProvider = SimpleValueGetterFunc(nil)
    73  	_ ValueProvider = SimpleValueSetterFunc(nil)
    74  )