1// Package datasource defines generic interfaces for datasources.2//3// Datasources contain a set of records which can optionally be4// taggable. Tags can optionally be used to filter records by taxonomy.5//6// Datasources can help in cases where the data sent during7// communication between different realms needs to be generic8// to avoid direct dependencies.9package datasource1011import "errors"1213// ErrInvalidRecord indicates that a datasource contains invalid records.14var ErrInvalidRecord = errors.New("datasource records is not valid")1516type (17 // Fields defines an interface for read-only fields.18 Fields interface {19 // Has checks whether a field exists.20 Has(name string) bool2122 // Get retrieves the value associated with the given field.23 Get(name string) (value any, found bool)24 }2526 // Record defines a datasource record.27 Record interface {28 // ID returns the unique record's identifier.29 ID() string3031 // String returns a string representation of the record.32 String() string3334 // Fields returns record fields and values.35 Fields() (Fields, error)36 }3738 // TaggableRecord defines a datasource record that supports tags.39 // Tags can be used to build a taxonomy to filter records by category.40 TaggableRecord interface {41 // Tags returns a list of tags for the record.42 Tags() []string43 }4445 // ContentRecord defines a datasource record that can return content.46 ContentRecord interface {47 // Content returns the record content.48 Content() (string, error)49 }5051 // Iterator defines an iterator of datasource records.52 Iterator interface {53 // Next returns true when a new record is available.54 Next() bool5556 // Err returns any error raised when reading records.57 Err() error5859 // Record returns the current record.60 Record() Record61 }6263 // Datasource defines a generic datasource.64 Datasource interface {65 // Records returns a new datasource records iterator.66 Records(Query) Iterator6768 // Size returns the total number of records in the datasource.69 // When -1 is returned it means datasource doesn't support size.70 Size() int7172 // Record returns a single datasource record.73 Record(id string) (Record, error)74 }75)7677// NewIterator returns a new record iterator for a datasource query.78func NewIterator(ds Datasource, options ...QueryOption) Iterator {79 return ds.Records(NewQuery(options...))80}8182// QueryRecords return a slice of records for a datasource query.83func QueryRecords(ds Datasource, options ...QueryOption) ([]Record, error) {84 var (85 records []Record86 query = NewQuery(options...)87 iter = ds.Records(query)88 )8990 for i := 0; i < query.Count && iter.Next(); i++ {91 r := iter.Record()92 if r == nil {93 return nil, ErrInvalidRecord94 }9596 records = append(records, r)97 }9899 if err := iter.Err(); err != nil {100 return nil, err101 }102 return records, nil103}104Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.