userdriver

package
v0.24.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package userdriver implements the "user-driver" functionality that allows users to define source driver types declaratively. Note pkg userdriver itself is the framework: an actual implementation for each genre (such as XML) must be defined separately as in the "xmlud" sub-package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NamesFromCols

func NamesFromCols(cols []*ColMapping) []string

NamesFromCols is a convenience function that returns a slice containing the name of each column.

func ToTableDef

func ToTableDef(tblMapping *TableMapping) (*sqlmodel.TableDef, error)

ToTableDef builds a TableDef from the TableMapping.

func ValidateDriverDef

func ValidateDriverDef(def *DriverDef) []error

ValidateDriverDef checks that def is valid, returning one or more errors if not.

Types

type ColMapping

type ColMapping struct {
	// Name is the column name.
	Name string `yaml:"col" json:"col"`

	// Selector is an optional selector for the col value, e.g. "./guid/@isPermaLink" for an attribute of an XML element.
	Selector string `yaml:"selector,omitempty" json:"selector,omitempty"`

	// Kind is the data kind, e.g. "int", "text.
	Kind kind.Kind `yaml:"kind" json:"kind"`

	// Format is an optional type format for text values, e.g. "RFC3339" for a string.
	Format string `yaml:"format,omitempty" json:"format,omitempty"`

	// Charset is an optional charset for text values, e.g. "utf-8".
	Charset string `yaml:"charset,omitempty" json:"charset,omitempty"`

	// Foreign indicates that this column is a foreign key into a parent tbl.
	Foreign string `yaml:"foreign,omitempty" json:"foreign,omitempty"`

	// Unique is true if the column value is unique.
	Unique bool `yaml:"unique,omitempty" json:"unique,omitempty"`

	// Required is true if the column is required.
	Required bool `yaml:"required" json:"required"`

	// Comment is an optional column comment.
	Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}

ColMapping models a database table column.

func (*ColMapping) String

func (c *ColMapping) String() string

type Detector

type Detector struct {
	// Type is the detector type, e.g. "suffix", "header", "scheme", etc.
	Type string `yaml:"type" json:"type"`
	// Key is the expected match for the detector's key field. E.g. "Content-Type". May be empty.
	Key string `yaml:"key,omitempty" json:"key,omitempty"`
	// Value is the expected match for the detector's value field. E.g. "application/rss+xml"
	Value string `yaml:"value" json:"value"`
	// Example is an example value that would match the detector, e.g. "Content-Type: application/rss+xml"
	Example string `yaml:"example,omitempty" json:"example,omitempty"`
}

Detector defines a document type detector.

func (*Detector) String

func (d *Detector) String() string

type DriverDef

type DriverDef struct {
	// Name is short name of the driver type, e.g. "rss".
	Name string `yaml:"driver" json:"driver"`

	// Genre is the generic document type, e.g. XML or JSON etc.
	Genre string `yaml:"genre" json:"genre"`

	// Title is the full name of the driver
	// type, e.g. "RSS (Really Simple Syndication)".
	Title string `yaml:"title" json:"title"`

	// Doc typically has a link to documentation for the driver..
	Doc string `yaml:"doc,omitempty" json:"doc,omitempty"`

	// Selector is the root doc element, e.g. "/rss"
	Selector string `yaml:"selector" json:"selector"`

	// Tables is the set of tables that define the type.
	Tables []*TableMapping `yaml:"tables" json:"tables"`
}

DriverDef is a user-defined driver definition.

func (*DriverDef) String

func (d *DriverDef) String() string

func (*DriverDef) TableBySelector

func (d *DriverDef) TableBySelector(sel string) *TableMapping

TableBySelector returns the TableMapping that matches sel, or nil.

type ImportFunc

type ImportFunc func(ctx context.Context, log lg.Log, def *DriverDef, data io.Reader, destDB driver.Database) error

ImportFunc is a function that can import data (as defined in def) to destDB.

type Provider

type Provider struct {
	Log       lg.Log
	DriverDef *DriverDef
	Scratcher driver.ScratchDatabaseOpener
	Files     *source.Files
	ImportFn  ImportFunc
}

Provider implements driver.Provider for a DriverDef.

func (*Provider) DriverFor

func (p *Provider) DriverFor(typ source.Type) (driver.Driver, error)

DriverFor implements driver.Provider.

func (*Provider) TypeDetectors

func (p *Provider) TypeDetectors() []source.TypeDetectFunc

TypeDetectors returns funcs that can detect the source type.

type TableMapping

type TableMapping struct {
	// Name is the table name.
	Name string `yaml:"table" json:"table"`

	// Selector specifies how the table data is selected.
	Selector string `yaml:"selector" json:"selector"`

	// Cols is the set of columns in the table.
	Cols []*ColMapping `yaml:"cols" json:"cols"`

	// PrimaryKey is a slice of the column names that constitute
	// the primary key. Typically this is one column, but can be
	// more than one for composite primary keys.
	PrimaryKey []string `yaml:"primary_key" json:"primary_key"`

	// Comment is an optional table comment.
	Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
}

TableMapping describes how document data is mapped to a table.

func (*TableMapping) ColBySelector

func (t *TableMapping) ColBySelector(sel string) *ColMapping

ColBySelector returns the ColMapping associated with the element, or nil if no such col.

func (*TableMapping) PKCols

func (t *TableMapping) PKCols() ([]*ColMapping, error)

PKCols returns the cols that constitute this table's primary key, or an error if none defined. If error is non-nil, the returned slice will contain at least one ColMapping.

func (*TableMapping) RequiredCols

func (t *TableMapping) RequiredCols() []*ColMapping

RequiredCols returns the cols that are required. This includes columns with explicit ColMapping.Required field as well as other columns such as those part of the primary key or sequence cols.

func (*TableMapping) SequenceCols

func (t *TableMapping) SequenceCols() []*ColMapping

SequenceCols returns the cols whose selector value is "../sequence()". In effect, this method returns the columns for whom a sequence value should be set during a db insert, similar to a db auto-increment column.

func (*TableMapping) String

func (t *TableMapping) String() string

Directories

Path Synopsis
Package xmlud provides user driver XML import functionality.
Package xmlud provides user driver XML import functionality.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL