view

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: Apache-2.0 Imports: 45 Imported by: 1

README

Resource

Resource groups and represents set of Views, Connectors, Parameters and Types needed to build all views provided in resorce. In resource following sections can be defined:

Section Description Type Required
SourceURL string false
Metrics View specific Metrics configuration inheritable by other Views Metrics Metrics false
Connectors Database connector definitions inheritable by other Views Connectors []Connector false
Views Views definitions provided by the resource []View false
Parameters Parameters configurations inheritable by other Views Parameters []Parameter false
Types Inlined types definitions []Type false
Loggers Loggers inheritable by other Views Loggers []Logger false

View

For View description read one of the following docs: programmatically usage or yaml usage

Section Description Type Required Default
Ref Other View name that given View should inherit from string false
Connector Connector used by the View Connector true
Name Unique view name string, unique across the Resource true
Alias View table alias string false
Table Table name. string Table, FromURL or From need to be specified
From SQL inner select statement that will be used as source of data string Table, FromURL or From need to be specified
FromURL Source of the SQL in case when SQL is specified in different file string Table, FromURL or From need to be specified
Exclude Columns that should not be exposed when column detection is performed []string false
Columns Explicitly specified columns that current View can use []Column true
InheritSchemaColumns Indicates whether all Columns that not match Struct Type should be removed or not bool false false
CaseFormat Database columns case format CaseFormat false lowerunderscore
Criteria Dynamic criteria expanded with the parameters values string false
Selector Selector configuration SelectorConfig false
Template Template configuration using parameters and velty syntax Template false
Schema View schema type Schema false
With View relations in order to produce results with nested objects []Relation false
MatchStrategy Match strategy specific for given View MatchStrategy false read_matched
Batch Batch configuration specific for given View Batch false Batch{Parent: 10000}
Logger Logger specific for given View Logger false
Counter Metrics specific for given View Metrics false

Column

Section Description Type Required Default
Name Database column name string true
DataType Column type enum: Int, Float, Float64, Bool, String, Date, Time false
Expression SQL expression i.e. COALESCE(price, 0) string false
Filterable Indicates whether column can be used using Selector criteria or not bool false false
Nullable Indicates whether column is nullable or not. In case if table is specified and DataType is not Time nor Date, Datly will automaticaly add COALESCE for the expression bool false false
Default Default output value that will be replaced when zero value occur string false
Format Date output format string false

SelectorConfig

SelectorConfig holds information about what can be used on specific View using Selectors built from the Request data.

Section Description Type Required Default
OrderBy Default Column that will be used to Sorted string false
Limit Maximum and default limit that can be used on the View int false No default and maximum limit
Constraints Selector constraints Constraints false everything disabled

Constraints

By default everything is forbidden. In order to allow datly create and populate Selectors from Http requests, it need to be explicitly enabled.

Section Description Type Required Default
Criteria Allows to parse _criteria into SQL statement boolean false false
OrderBy Allows to parse _orderBy into SQL order by boolean false false
Limit Allows to parse _limit into SQL limit boolean false false
Offset Allows to parse _orrset into SQL offset boolean false false
Filterable Allowed columns to be used in the criteria, * in case of allowed all columns []string false

Parameter

Parameters are defined in order to read data specific for the given http request.

Section Description Type Required Default
Ref Other Parameter name that given Parameter will inherit from string false
Name Identifier used to access parameter value in the templates string true
PresenceName Identifier used to check if parameter was set in the templates string false same as Name
In Source of the parameter Location true
Required Indicates if parameter is required or not boolean false false
Description Parameter description string false
Schema Schema configuration Schema true
Codec Codec configuration Codec false

Location

Section Description Type Required
Kind Represents the source of the parameter i.e. Header, QueryParam. enum: query,header, cookie, data_view, body, env true
Name Parameter source identifier i.e. Authorization, userId string true

Codec

In some cases it is needed to transform raw parameter value to some different value. For example Authorization Header with JWT Token. In this case it is needed to provide and configure Codec that will transform raw JWT token into some struct. The Codec need to be created programmatically and provided during the Resource initialization.

The interface needed to be implemented by the Codec:

Value(ctx context.Context, raw string) (interface{}, error)
Section Description Type Required
Name Codec name, have to match the codec name provided programmatically string true

Schema

Schema holds and defines actual type of the parent. It can either load type from the types provided programmatically, or by generating type using predefined primitive types.

Section Description Type Required
Name Schema name, needs to match one of the types provided programmatically string true unless DataType specified
DataType Primitive data type name enum: Int, Float, Float64, Bool, String, Date, Time true unless Name specified

Type

In some cases the Type definition can be provided in the yaml file, it allows to use them in templates but they will not be accessible programmatically. The usage for them might be f.e. parsing RequestBody to some struct.

Section Description Type Required
Name Type name string true
Fields Metadata fields descriptions []Field true

Field

Section Description Type Required Default
Name Struct field name string, UpperCamelCase true
Embed Indicates whether field should be Anonymous (i.e. while parsing JSON, if field is Anonymous and type of Struct the Struct will be flattened) bool false false
Column Database column name string true unless name doesn't match actual database column name
Schema Field schema Schema Schema or Fields need to be specified
Fields Describes non-primitive field type []Field Schema or Fields need to be specified ---

Template

In order to create more complex and optimized SQL statements, the velty syntax can be used to produce SQL dynamically based on the Parameters created based on f.e. http request.

Namespace:

  • Has - the parameter presence can be checked using the prefix $Has.
  • Unsafe - to access raw parameter value in the template, you can use prefix $Unsafe. This namespace should be used extremely careful. It should be used only for the parameters data_view Kind, or only to check value using velty statements. The parameters without $Unsafe prefix will be replaced with placeholders.
  • View - to access basic details about the current View in template, you can use $View prefix. Those values will be expanded as is. They will not be pushed as placeholders so it is important to wrap them with quotes.
Section Description Type Required Default
Source The actual SQL template string true
SourceURL Indicates whether field should be Anonymous (i.e. while parsing JSON, if field is Anonymous and type of Struct the Struct will be flattened) string false false
Schema The actual type holder of the combined parameters. Each parameter name and type has to match the current Schema Type Schema false false
PresenceSchema Similar to the Schema, but each Parameter name has to match field in the struct and every non primitive type has to be a boolean Schema false false
Parameters Parameters that can be used inside the template []Parameter false false

Connector

In order to communicate with database, database credentials need to be specified. To provide secure credentials store, the DSN should be represented using variables (i.e. ${user}:${password}) and expanded using external store.

Section Description Type Required
Name Connector name string true
Ref Other connector name which given connector should inherit from string false
Driver Database driver string true
DSN Database source name, the uri needed to connect to database. string true
Secret Secret true

Secret

Metrics

Used to collect data about View usage, including average time, success/failure ratio etc.

Section Description Type Required
URIPart string true

Logger

Programmatically created and provided logger.

Section Description Type Required
Ref Other Logger name which given Logger should inherit from string false
Name Logger name string true

Batch

Batches data fetched from database.

Section Description Type Required
Parent Number of parent placeholders in column in (?,?,?,?) statement if View is a child of any other View int false

CaseFormat

Enum, possible values:

  • uu, upperunderscore - i.e. EMPLOYEE_ID
  • lu, lowerunderscore - i.e. employee_id
  • uc, uppercamel - i.e. EmployeeId
  • lc, lowercamel - i.e. employeeId
  • l, lower - i.e. employeeid
  • u, upper - i.e. EMPLOYEEID

MatchStrategy

Enum, possible values:

  • read_matched
  • read_all

Parameters Codecs

Supported built in datly codecs:

  • VeltyCriteria - parses template using velocity syntax to sanitize criteria built from templates. Supported prefixes:
    • Unsafe - in order to access parameter values in the template, it is needed to use Unsafe prefix.
    • Safe_Column - if column with given parameter value doesn't exist the error will be thrown. i.e. $Safe_Column.Columns[$i].Name
    • Safe_Value - the parameter values with this prefix will be passed as binding parameters.
  • Strings - splits string using , into []string

Documentation

Index

Constants

View Source
const (
	SafeColumn = "Safe_Column"
	SafeValue  = "Safe_Value"
	SafeInt    = "Safe_Int"
	SafeString = "Safe_String"
	SafeBool   = "Safe_Bool"
	SafeFloat  = "Safe_Float"

	Criteria = "criteria"
)
View Source
const (
	VeltyCriteriaCodec = "VeltyCriteria"
)

Variables

View Source
var Now = time.Now

Functions

func DetectCase added in v0.2.1

func DetectCase(data ...string) string

DetectCase detect case format

func ParseType added in v0.2.1

func ParseType(dataType string) (reflect.Type, error)

func TemplateField

func TemplateField(name string, rType reflect.Type) (reflect.StructField, error)

Types

type Accessor

type Accessor struct {
	// contains filtered or unexported fields
}

func (*Accessor) Value

func (a *Accessor) Value(values interface{}, indexes ...int) (interface{}, error)

type Accessors

type Accessors struct {
	// contains filtered or unexported fields
}

func (*Accessors) AccessorByName added in v0.2.1

func (a *Accessors) AccessorByName(name string) (*Accessor, error)

type Batch

type Batch struct {
	Parent int `json:",omitempty"`
}

type Cache added in v0.2.2

type Cache struct {
	Location     string
	TimeToLiveMs int
	PartSize     int
	Size         int
	// contains filtered or unexported fields
}

func (*Cache) Service added in v0.2.2

func (c *Cache) Service() *cache.Service

type Cardinality

type Cardinality string
const (
	One  Cardinality = "One"
	Many Cardinality = "Many"
)

type CaseFormat

type CaseFormat string
const (
	UpperUnderscoreShort CaseFormat = "uu"
	UpperUnderscore      CaseFormat = "upperunderscore"
	LowerUnderscoreShort CaseFormat = "lu"
	LowerUnderscore      CaseFormat = "lowerunderscore"
	UpperCamelShort      CaseFormat = "uc"
	UpperCamel           CaseFormat = "uppercamel"
	LowerCamelShort      CaseFormat = "lc"
	LowerCamel           CaseFormat = "lowercamel"
	LowerShort           CaseFormat = "l"
	Lower                CaseFormat = "lower"
	UpperShort           CaseFormat = "u"
	Upper                CaseFormat = "upper"
)

func (CaseFormat) Caser

func (f CaseFormat) Caser() (format.Case, error)

func (*CaseFormat) Init

func (f *CaseFormat) Init() error

type Codec

type Codec struct {
	shared.Reference
	Name      string  `json:",omitempty"`
	Source    string  `json:",omitempty"`
	SourceURL string  `json:",omitempty"`
	Schema    *Schema `json:",omitempty"`
	// contains filtered or unexported fields
}

func (*Codec) Init

func (v *Codec) Init(resource *Resource, view *View, paramType reflect.Type) error

func (*Codec) Transform added in v0.2.1

func (v *Codec) Transform(ctx context.Context, raw string, options ...interface{}) (interface{}, error)

type CodecFn

type CodecFn func(context context.Context, rawValue interface{}, options ...interface{}) (interface{}, error)

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector collects and build result from view fetched from Database If View or any of the View.With MatchStrategy support Parallel fetching, it is important to call MergeData when all needed view was fetched

func NewCollector

func NewCollector(slice *xunsafe.Slice, view *View, dest interface{}, supportParallel bool) *Collector

NewCollector creates a collector

func (*Collector) Dest

func (r *Collector) Dest() interface{}

Dest returns collector slice

func (*Collector) Fetched

func (r *Collector) Fetched()

func (*Collector) Lock

func (r *Collector) Lock() *sync.Mutex

func (*Collector) MergeData

func (r *Collector) MergeData()

MergeData merges view with Collectors produced via Relations It is sufficient to call it on the most Parent Collector to produce result

func (*Collector) NewItem

func (r *Collector) NewItem() func() interface{}

NewItem creates and return item provider

func (*Collector) ParentPlaceholders

func (r *Collector) ParentPlaceholders() ([]interface{}, string)

ParentPlaceholders if Collector doesn't support parallel fetching and has a Parent, it will return a parent _field values and column name that the relation was created from, otherwise empty slice and empty string i.e. if Parent Collector collects Employee{AccountId: int}, Column.Name is account_id and Collector collects Account it will extract and return all the AccountId that were accumulated and account_id

func (*Collector) Relation

func (r *Collector) Relation() *Relation

func (*Collector) Relations

func (r *Collector) Relations(selector *Selector) []*Collector

Relations creates and register new Collector for each Relation present in the Selector.Columns if View allows use Selector.Columns

func (*Collector) Resolve

func (r *Collector) Resolve(column io.Column) func(ptr unsafe.Pointer) interface{}

Resolve resolved unmapped column

func (*Collector) Slice

func (r *Collector) Slice() (unsafe.Pointer, *xunsafe.Slice)

func (*Collector) SupportsParallel

func (r *Collector) SupportsParallel() bool

SupportsParallel if Collector supports parallelism, it means that his Relations can fetch view in the same time Later on it will be merged with the parent Collector

func (*Collector) View

func (r *Collector) View() *View

View returns View assigned to the Collector

func (*Collector) Visitor

func (r *Collector) Visitor(ctx context.Context) Visitor

Visitor creates visitor function

func (*Collector) WaitIfNeeded

func (r *Collector) WaitIfNeeded()

type Column

type Column struct {
	Name       string `json:",omitempty"`
	DataType   string `json:",omitempty"`
	Expression string `json:",omitempty"`
	Filterable bool   `json:",omitempty"`
	Nullable   bool   `json:",omitempty"`
	Default    string `json:",omitempty"`
	Format     string `json:",omitempty"`

	Codec *Codec `json:",omitempty"`
	// contains filtered or unexported fields
}

Column represents view View column

func (*Column) ColumnName

func (c *Column) ColumnName() string

ColumnName returns Column Name

func (*Column) ColumnType added in v0.2.1

func (c *Column) ColumnType() reflect.Type

func (*Column) FieldName

func (c *Column) FieldName() string

func (*Column) Init

func (c *Column) Init(resource *Resource, caser format.Case, allowNulls bool, config *ColumnConfig) error

Init initializes Column

func (*Column) SqlExpression

func (c *Column) SqlExpression() string

SqlExpression builds column sql expression if any expression specified in format: Expression AS Name

type ColumnConfig added in v0.2.2

type ColumnConfig struct {
	Name       string
	Expression *string
	Codec      *Codec
	DataType   *string
}

type ColumnIndex added in v0.2.1

type ColumnIndex map[string]*Column

ColumnIndex represents *Column registry.

func (ColumnIndex) Lookup added in v0.2.1

func (c ColumnIndex) Lookup(name string) (*Column, error)

Lookup returns Column with given name.

func (ColumnIndex) Register added in v0.2.1

func (c ColumnIndex) Register(caser format.Case, column *Column)

Register registers *Column

func (ColumnIndex) RegisterHolder added in v0.2.1

func (c ColumnIndex) RegisterHolder(relation *Relation) error

RegisterHolder looks for the Column by Relation.Column name. If it finds registers that Column with Relation.Holder key.

func (ColumnIndex) RegisterWithName added in v0.2.1

func (c ColumnIndex) RegisterWithName(name string, column *Column)

type Columns

type Columns []*Column

Columns wrap slice of Column

func (Columns) Index added in v0.2.1

func (c Columns) Index(caser format.Case) ColumnIndex

Index indexes columns by Column.Name

func (Columns) Init added in v0.2.1

func (c Columns) Init(resource *Resource, config map[string]*ColumnConfig, caser format.Case, allowNulls bool) error

Init initializes each Column in the slice.

type CommonParams

type CommonParams struct {
	ColumnsIn   string `velty:"COLUMN_IN"`
	WhereClause string `velty:"CRITERIA"`
	Pagination  string `velty:"PAGINATION"`
}

type Config

type Config struct {
	//TODO: Should order by be a slice?
	OrderBy     string       `json:",omitempty"`
	Limit       int          `json:",omitempty"`
	Constraints *Constraints `json:",omitempty"`

	LimitParam    *Parameter `json:",omitempty"`
	OffsetParam   *Parameter `json:",omitempty"`
	FieldsParam   *Parameter `json:",omitempty"`
	OrderByParam  *Parameter `json:",omitempty"`
	CriteriaParam *Parameter `json:",omitempty"`
}

Config represent a view config selector

func (*Config) Init added in v0.2.1

func (c *Config) Init(ctx context.Context, resource *Resource, parent *View) error

type Connector

type Connector struct {
	shared.Reference
	Secret *scy.Resource `json:",omitempty"`
	Name   string        `json:",omitempty"`
	Driver string        `json:",omitempty"`
	DSN    string        `json:",omitempty"`
	// contains filtered or unexported fields
}

Connector represents database/sql named connection config

func (*Connector) Close

func (c *Connector) Close() error

Close closes connector

func (*Connector) Db

func (c *Connector) Db() (*sql.DB, error)

Db creates connection to the DB. It is important to not close the DB since the connection is shared.

func (*Connector) Init

func (c *Connector) Init(ctx context.Context, connectors Connectors) error

Init initializes connector. It is possible to inherit from other Connector using Ref _field. If Ref is specified, then Connector with the name has to be registered in Connectors

func (*Connector) Reset

func (c *Connector) Reset()

Reset reset connector

func (*Connector) Validate

func (c *Connector) Validate() error

Validate check if connector was configured properly. Name, Driver and DSN are required.

type ConnectorSlice

type ConnectorSlice []*Connector

ConnectorSlice represents Slice of *Connector

func (ConnectorSlice) Index

func (c ConnectorSlice) Index() Connectors

Index indexes Connectors by Connector.Name.

func (ConnectorSlice) Init

func (c ConnectorSlice) Init(ctx context.Context, connectors Connectors) error

Init initializes each connector

type Connectors

type Connectors map[string]*Connector

Connectors represents Connector registry Key was produced based on Connector.Name

func (Connectors) Lookup

func (v Connectors) Lookup(name string) (*Connector, error)

Lookup returns Connector by Connector.Name

func (*Connectors) Register

func (v *Connectors) Register(connector *Connector)

Register registers connector

type Constraints

type Constraints struct {
	Criteria   bool
	OrderBy    bool
	Limit      bool
	Offset     bool
	Projection bool //enables columns projection from client (default ${NS}_fields= query param)
	Filterable []string
	SQLMethods []*Method `json:",omitempty"`
	// contains filtered or unexported fields
}

Constraints configure what can be selected by Selector For each _field, default value is `false`

func (*Constraints) SqlMethodsIndexed added in v0.2.1

func (c *Constraints) SqlMethodsIndexed() map[string]*Method

type CriteriaSanitizer added in v0.2.1

type CriteriaSanitizer struct {
	Columns      ColumnIndex
	Placeholders []interface{}
}

func NewCriteria added in v0.2.1

func NewCriteria(columns ColumnIndex) *CriteriaSanitizer

func (*CriteriaSanitizer) AsBinding added in v0.2.1

func (c *CriteriaSanitizer) AsBinding(value interface{}) string

func (*CriteriaSanitizer) AsColumn added in v0.2.1

func (c *CriteriaSanitizer) AsColumn(columnName string) (string, error)

type Definition

type Definition struct {
	Name   string `json:",omitempty"`
	Fields []*Field
	*Schema
}

func (*Definition) Init

func (d *Definition) Init(ctx context.Context, types Types) error

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

func NewEvaluator added in v0.2.1

func NewEvaluator(paramSchema, presenceSchema reflect.Type, template string, options ...interface{}) (*Evaluator, error)

func (*Evaluator) Evaluate added in v0.2.1

func (e *Evaluator) Evaluate(schemaType reflect.Type, externalParams, presenceMap interface{}, viewParam *Param) (string, error)

type Field

type Field struct {
	Name        string      `json:",omitempty"`
	Embed       bool        `json:",omitempty"`
	Column      string      `json:",omitempty"`
	FromName    string      `json:",omitempty"`
	Cardinality Cardinality `json:",omitempty"`
	Schema      *Schema     `json:",omitempty"`
	Fields      []*Field    `json:",omitempty"`
	Tag         string
}

func (*Field) Init

func (f *Field) Init(ctx context.Context, types Types) error

type Kind

type Kind string

Kind represents parameter location Parameter value can be retrieved from the i.e. HTTP Header, Path Variable or using other View

const (
	DataViewKind    Kind = "data_view"
	HeaderKind      Kind = "header"
	QueryKind       Kind = "query"
	PathKind        Kind = "path"
	CookieKind      Kind = "cookie"
	RequestBodyKind Kind = "body"
	EnvironmentKind Kind = "env"
)

func (Kind) Validate

func (k Kind) Validate() error

Validate checks if Kind is valid.

type Location

type Location struct {
	Kind Kind   `json:",omitempty"`
	Name string `json:",omitempty"`
}

Location tells how to get parameter value.

func (*Location) Validate

func (l *Location) Validate() error

Validate checks if Location is valid

type MatchStrategy

type MatchStrategy string

MatchStrategy in some cases it might be better to fetch parent View view and all Relation view in the same time and merge it on the backend side in those cases ReadAll strategy will do that. in other cases it might be better to filter Relation view and fetch only those records that matches with View view in those cases ReadMatched will do that.

const (
	ReadAll     MatchStrategy = "read_all"     // read all and later we match on backend side
	ReadMatched MatchStrategy = "read_matched" // read parent view and then filter id to match with the current view
	ReadDerived MatchStrategy = "read_derived" // use parent sql selector to add criteria to the relation view, this can only work if the connector of the relation view and parent view is the same
)

func (MatchStrategy) SupportsParallel

func (s MatchStrategy) SupportsParallel() bool

SupportsParallel indicates whether MatchStrategy support parallel read.

func (MatchStrategy) Validate

func (s MatchStrategy) Validate() error

Validate checks if MatchStrategy is valid

type Method added in v0.2.1

type Method struct {
	Name string    `json:",omitempty"`
	Args []*Schema `json:",omitempty"`
}

type Metrics

type Metrics struct {
	*gmetric.Service
	URIPart string
}

type Namer added in v0.2.2

type Namer interface {
	Names(rField reflect.StructField) []string
}

type Names

type Names []string

Names represents columns names slice.

func (Names) Index

func (c Names) Index() map[string]bool

Index creates presence map.

type Option

type Option interface{}

Option represents generic options

type Param

type Param struct {
	Name  string
	Alias string
	Table string
}

type ParamName

type ParamName string

ParamName represents name of parameter in given Location.Kind i.e. if you want to extract lang from query string: ?foo=bar&lang=eng required Kind is QueryKind and ParamName `lang`

func (ParamName) Validate

func (p ParamName) Validate(kind Kind) error

Validate checks if ParamName is valid

type ParamState

type ParamState struct {
	Values interface{} `json:",omitempty"`
	Has    interface{} `json:",omitempty"`
}

Selector allows customizing view fetched from Database

func (*ParamState) Init

func (s *ParamState) Init(view *View)

type Parameter

type Parameter struct {
	shared.Reference
	Name         string `json:",omitempty"`
	PresenceName string `json:",omitempty"`

	In          *Location `json:",omitempty"`
	Required    *bool     `json:",omitempty"`
	Description string    `json:",omitempty"`
	Style       string    `json:",omitempty"`
	Schema      *Schema   `json:",omitempty"`

	Codec *Codec `json:",omitempty"`

	Positions []int `json:",omitempty"`
	// contains filtered or unexported fields
}

Parameter describes parameters used by the Criteria to filter the view.

func (*Parameter) ConvertAndSet

func (p *Parameter) ConvertAndSet(ctx context.Context, paramPtr unsafe.Pointer, value string, selector *Selector) error

func (*Parameter) Init

func (p *Parameter) Init(ctx context.Context, view *View, resource *Resource, structType reflect.Type) error

Init initializes Parameter

func (*Parameter) IsRequired

func (p *Parameter) IsRequired() bool

func (*Parameter) Set

func (p *Parameter) Set(ptr unsafe.Pointer, value interface{}) error

func (*Parameter) SetAccessor

func (p *Parameter) SetAccessor(accessor *Accessor)

func (*Parameter) SetPresenceField

func (p *Parameter) SetPresenceField(structType reflect.Type) error

func (*Parameter) UpdatePresence

func (p *Parameter) UpdatePresence(presencePtr unsafe.Pointer)

func (*Parameter) Validate

func (p *Parameter) Validate() error

Validate checks if parameter is valid

func (*Parameter) Value

func (p *Parameter) Value(values interface{}) (interface{}, error)

func (*Parameter) View

func (p *Parameter) View() *View

View returns View related with Parameter if Location.Kind is set to data_view

type ParametersIndex

type ParametersIndex map[string]*Parameter

ParametersIndex represents Parameter map indexed by Parameter.Name

func (ParametersIndex) Lookup

func (p ParametersIndex) Lookup(paramName string) (*Parameter, error)

Lookup returns Parameter with given name

func (ParametersIndex) Register

func (p ParametersIndex) Register(parameter *Parameter)

Register registers parameter

type ParametersSlice

type ParametersSlice []*Parameter

ParametersSlice represents slice of parameters

func (ParametersSlice) Filter

func (p ParametersSlice) Filter(kind Kind) ParametersIndex

Filter filters ParametersSlice with given Kind and creates Template

func (ParametersSlice) Index

func (p ParametersSlice) Index() ParametersIndex

Index indexes parameters by Parameter.Name

type ReferenceView

type ReferenceView struct {
	View          // event type
	Column string `json:",omitempty"`
	Field  string `json:",omitempty"`
	// contains filtered or unexported fields
}

ReferenceView represents referenced View In our example it would be Account

func (*ReferenceView) Init

func (r *ReferenceView) Init(_ context.Context, _ *Resource) error

Init initializes ReferenceView

func (*ReferenceView) Validate

func (r *ReferenceView) Validate() error

Validate checks if ReferenceView is valid

type Relation

type Relation struct {
	Name string         `json:",omitempty"`
	Of   *ReferenceView `json:",omitempty"`

	Cardinality   Cardinality `json:",omitempty"` //One, or Many
	Column        string      `json:",omitempty"` //Represents parent column that would be used to assemble nested objects. In our example it would be Employee#AccountId
	ColumnAlias   string      `json:",omitempty"` //Represents column alias, can be specified if $shared.Criteria / $shared.ColumnInPosition is inside the "from" statement
	Holder        string      `json:",omitempty"` //Represents column created due to the merging. In our example it would be Employee#Account
	IncludeColumn bool        `json:",omitempty"` //tells if Column _field should be kept in the struct type. In our example, if set false in produced Employee would be also AccountId _field
	// contains filtered or unexported fields
}

Relation used to build more complex View that represents database tables with relations one-to-one or many-to-many In order to understand it better our example is: Parent View represents Employee{AccountId: int}, Relation represents Account{Id: int} We want to create result like: Employee{Account{Id:int}}

func (*Relation) BeforeViewInit

func (r *Relation) BeforeViewInit(ctx context.Context) error

func (*Relation) Init

func (r *Relation) Init(ctx context.Context, resource *Resource, parent *View) error

Init initializes Relation

func (*Relation) Validate

func (r *Relation) Validate() error

Validate checks if Relation is valid

type RelationsSlice

type RelationsSlice []*Relation

RelationsSlice represents slice of Relation

func (RelationsSlice) Columns

func (r RelationsSlice) Columns() []string

Columns extract Relation.Column from RelationsSlice

func (RelationsSlice) Index

func (r RelationsSlice) Index() map[string]*Relation

Index indexes Relations by Relation.Holder

func (RelationsSlice) PopulateWithResolve

func (r RelationsSlice) PopulateWithResolve() []*Relation

PopulateWithResolve filters RelationsSlice by the columns that won't be present in Database due to the removing StructField after assembling nested StructType.

func (RelationsSlice) PopulateWithVisitor

func (r RelationsSlice) PopulateWithVisitor() []*Relation

PopulateWithVisitor filters RelationsSlice by the columns that will be present in Database, and because of that they wouldn't be resolved as unmapped columns.

type Resource

type Resource struct {
	Metrics    *Metrics
	SourceURL  string `json:",omitempty"`
	Connectors []*Connector

	Views []*View `json:",omitempty"`

	Parameters []*Parameter `json:",omitempty"`

	Types []*Definition

	Loggers logger.Adapters `json:",omitempty"`

	ModTime time.Time `json:",omitempty"`
	// contains filtered or unexported fields
}

Resource represents grouped view needed to build the View can be loaded from i.e. yaml file

func EmptyResource

func EmptyResource() *Resource

func LoadResourceFromURL

func LoadResourceFromURL(ctx context.Context, URL string, fs afs.Service) (*Resource, error)

LoadResourceFromURL load resource from URL

func NewResource

func NewResource(types Types) *Resource

NewResource creates a Resource and register provided Types

func NewResourceFromURL

func NewResourceFromURL(ctx context.Context, url string, types Types, visitors codec.Visitors) (*Resource, error)

NewResourceFromURL loads and initializes Resource from file .yaml

func (*Resource) AddConnectors

func (r *Resource) AddConnectors(connectors ...*Connector)

AddConnectors register connectors in the resource

func (*Resource) AddLoggers

func (r *Resource) AddLoggers(loggers ...*logger.Adapter)

AddLoggers register loggers in the resource

func (*Resource) AddParameters

func (r *Resource) AddParameters(parameters ...*Parameter)

AddParameters register parameters in the resource

func (*Resource) AddViews

func (r *Resource) AddViews(views ...*View)

AddViews register views in the resource

func (*Resource) ConnectorByName

func (r *Resource) ConnectorByName() map[string]*Connector

func (*Resource) FindConnector

func (r *Resource) FindConnector(view *View) (*Connector, error)

func (*Resource) GetConnectors

func (r *Resource) GetConnectors() Connectors

GetConnectors returns Connectors supplied with the Resource

func (*Resource) GetTypes

func (r *Resource) GetTypes() Types

func (*Resource) GetViews

func (r *Resource) GetViews() Views

GetViews returns Views supplied with the Resource

func (*Resource) Init

func (r *Resource) Init(ctx context.Context, options ...interface{}) error

Init initializes Resource

func (*Resource) LoadObject added in v0.2.1

func (r *Resource) LoadObject(ctx context.Context, URL string) (storage.Object, error)

func (*Resource) LoadText

func (r *Resource) LoadText(ctx context.Context, URL string) (string, error)

func (*Resource) MergeFrom

func (r *Resource) MergeFrom(resource *Resource, types Types)

func (*Resource) SetTypes

func (r *Resource) SetTypes(types Types)

func (*Resource) TypeName added in v0.2.1

func (r *Resource) TypeName(p reflect.Type) (string, bool)

func (*Resource) View

func (r *Resource) View(name string) (*View, error)

View returns View with given name

func (*Resource) VisitorByName added in v0.2.2

func (r *Resource) VisitorByName(name string) (codec.LifecycleVisitor, bool)

type Sanitizer added in v0.2.1

type Sanitizer struct {
	// contains filtered or unexported fields
}

type Schema

type Schema struct {
	Name string `json:",omitempty" yaml:"name,omitempty"`

	DataType    string `json:",omitempty" yaml:"dataType,omitempty"`
	Cardinality Cardinality
	// contains filtered or unexported fields
}

Schema represents View as Go type.

func NewSchema

func NewSchema(compType reflect.Type) *Schema

func (*Schema) AutoGen

func (c *Schema) AutoGen() bool

AutoGen indicates whether Schema was generated using ColumnTypes fetched from DB or was passed programmatically.

func (*Schema) Init

func (c *Schema) Init(columns []*Column, relations []*Relation, viewCaseFormat format.Case, types Types) error

Init build struct type

func (*Schema) Slice

func (c *Schema) Slice() *xunsafe.Slice

Slice returns slice as xunsafe.Slice

func (*Schema) SliceType

func (c *Schema) SliceType() reflect.Type

SliceType returns reflect.SliceOf() Schema type

func (*Schema) Type

func (c *Schema) Type() reflect.Type

Type returns struct type

func (*Schema) XType

func (c *Schema) XType() *xunsafe.Type

XType returns structType as *xunsafe.Type

type Selector

type Selector struct {
	DatabaseFormat format.Case
	OutputFormat   format.Case
	Columns        []string   `json:",omitempty"`
	Fields         []string   `json:",omitempty"`
	OrderBy        string     `json:",omitempty"`
	Offset         int        `json:",omitempty"`
	Limit          int        `json:",omitempty"`
	Parameters     ParamState `json:",omitempty"`

	Criteria     string        `json:",omitempty"`
	Placeholders []interface{} `json:",omitempty"`
	// contains filtered or unexported fields
}

Selector allows customizing view fetched from Database

func NewSelector added in v0.2.1

func NewSelector() *Selector

func (*Selector) Add added in v0.2.1

func (s *Selector) Add(fieldName string, isHolder bool)

func (*Selector) Has

func (s *Selector) Has(field string) bool

Has checks if Field is present in Selector.Columns

func (*Selector) Init

func (s *Selector) Init()

Init initializes Selector

type Selectors

type Selectors struct {
	Index map[string]*Selector
	sync.RWMutex
}

Selectors represents Selector registry

func (*Selectors) Init

func (s *Selectors) Init()

Init initializes each Selector

func (*Selectors) Lookup

func (s *Selectors) Lookup(view *View) *Selector

Lookup returns and initializes Selector attached to View. Creates new one if doesn't exist.

type SqlxNamer added in v0.2.2

type SqlxNamer struct {
}

func (*SqlxNamer) Names added in v0.2.2

func (s *SqlxNamer) Names(rField reflect.StructField) []string

type Template

type Template struct {
	Source         string       `json:",omitempty" yaml:"source,omitempty"`
	SourceURL      string       `json:",omitempty" yaml:"sourceURL,omitempty"`
	Schema         *Schema      `json:",omitempty" yaml:"schema,omitempty"`
	PresenceSchema *Schema      `json:",omitempty" yaml:"presenceSchema,omitempty"`
	Parameters     []*Parameter `json:",omitempty" yaml:"parameters,omitempty"`
	// contains filtered or unexported fields
}

func (*Template) AccessorByName

func (t *Template) AccessorByName(name string) (*Accessor, error)

func (*Template) EvaluateSource

func (t *Template) EvaluateSource(externalParams, presenceMap interface{}, parent *View) (string, error)

func (*Template) Init

func (t *Template) Init(ctx context.Context, resource *Resource, view *View) error

func (*Template) IsActualTemplate added in v0.2.1

func (t *Template) IsActualTemplate() bool

type Types

type Types map[string]reflect.Type

Types represents reflect.Type registry map key should match Schema.Name

func (Types) Lookup

func (r Types) Lookup(name string) (reflect.Type, error)

Lookup returns Type

func (Types) Register

func (r Types) Register(name string, rType reflect.Type)

Register registers Type

type VeltyCodec added in v0.2.1

type VeltyCodec struct {
	// contains filtered or unexported fields
}

func NewVeltyCodec added in v0.2.1

func NewVeltyCodec(template string, paramType reflect.Type, view *View) (*VeltyCodec, error)

func (*VeltyCodec) Value added in v0.2.1

func (v *VeltyCodec) Value(ctx context.Context, raw interface{}, options ...interface{}) (interface{}, error)

type VeltyNamer added in v0.2.2

type VeltyNamer struct {
}

func (*VeltyNamer) Names added in v0.2.2

func (v *VeltyNamer) Names(rField reflect.StructField) []string

type View

type View struct {
	shared.Reference
	Connector  *Connector `json:",omitempty"`
	Standalone bool       `json:",omitempty"`
	Name       string     `json:",omitempty"`
	Alias      string     `json:",omitempty"`
	Table      string     `json:",omitempty"`
	From       string     `json:",omitempty"`
	FromURL    string     `json:",omitempty"`

	Exclude              []string   `json:",omitempty"`
	Columns              []*Column  `json:",omitempty"`
	InheritSchemaColumns bool       `json:",omitempty"`
	CaseFormat           CaseFormat `json:",omitempty"`

	Criteria string `json:",omitempty"`

	Selector *Config   `json:",omitempty"`
	Template *Template `json:",omitempty"`

	Schema *Schema `json:",omitempty"`

	With []*Relation `json:",omitempty"`

	MatchStrategy MatchStrategy `json:",omitempty"`
	Batch         *Batch        `json:",omitempty"`

	Logger  *logger.Adapter `json:",omitempty"`
	Counter logger.Counter  `json:"-"`
	Caser   format.Case     `json:",omitempty"`

	DiscoverCriteria    *bool  `json:",omitempty"`
	AllowNulls          *bool  `json:",omitempty"`
	UseBindingPositions *bool  `json:",omitempty"`
	Cache               *Cache `json:",omitempty"`

	ColumnsConfig map[string]*ColumnConfig `json:",omitempty"`
	// contains filtered or unexported fields
}

View represents a view View

func ViewReference

func ViewReference(name, ref string, options ...Option) *View

ViewReference creates a view reference

func (*View) CanUseSelectorCriteria

func (v *View) CanUseSelectorCriteria() bool

CanUseSelectorCriteria indicates if Selector.Criteria can be used

func (*View) CanUseSelectorLimit

func (v *View) CanUseSelectorLimit() bool

CanUseSelectorLimit indicates if Selector.Limit can be used

func (*View) CanUseSelectorOffset

func (v *View) CanUseSelectorOffset() bool

CanUseSelectorOffset indicates if Selector.Offset can be used

func (*View) CanUseSelectorOrderBy

func (v *View) CanUseSelectorOrderBy() bool

CanUseSelectorOrderBy indicates if Selector.OrderBy can be used

func (*View) CanUseSelectorProjection added in v0.2.2

func (v *View) CanUseSelectorProjection() bool

CanUseSelectorProjection indicates if Selector.Fields can be used

func (*View) Collector

func (v *View) Collector(dest interface{}, supportParallel bool) *Collector

Collector creates new Collector for View.DataType

func (*View) ColumnByName

func (v *View) ColumnByName(name string) (*Column, bool)

ColumnByName returns Column by Column.Name

func (*View) DataType

func (v *View) DataType() reflect.Type

DataType returns struct type.

func (*View) DatabaseType added in v0.2.2

func (v *View) DatabaseType() reflect.Type

func (*View) Db

func (v *View) Db() (*sql.DB, error)

Db returns database connection that View was assigned to.

func (*View) IndexedColumns

func (v *View) IndexedColumns() ColumnIndex

IndexedColumns returns Columns

func (*View) Init

func (v *View) Init(ctx context.Context, resource *Resource, options ...interface{}) error

Init initializes View using view provided in Resource. i.e. If View, Connector etc. should inherit from others - it has te bo included in Resource. It is important to call Init for every View because it also initializes due to the optimization reasons.

func (*View) IsHolder

func (v *View) IsHolder(value string) bool

func (*View) LimitWithSelector

func (v *View) LimitWithSelector(selector *Selector) int

LimitWithSelector returns Selector.Limit if it is allowed by the View to use Selector.Columns (see Constraints.Limit)

func (*View) ShouldTryDiscover added in v0.2.1

func (v *View) ShouldTryDiscover() bool

func (*View) Source

func (v *View) Source() string

Source returns database view source. It prioritizes From, Table then View.Name

func (*View) UnwrapDatabaseType added in v0.2.2

func (v *View) UnwrapDatabaseType(ctx context.Context, value interface{}) (interface{}, error)

func (*View) UseParamBindingPositions added in v0.2.1

func (v *View) UseParamBindingPositions() bool

type ViewSlice

type ViewSlice []*View

ViewSlice wraps slice of Views

func (ViewSlice) Index

func (v ViewSlice) Index() Views

Index indexes ViewSlice by View.Name

func (ViewSlice) Init

func (v ViewSlice) Init(ctx context.Context, resource *Resource, transforms marshal.TransformIndex) error

Init initializes views.

type Views

type Views map[string]*View

Views represents views registry indexed by view name.

func (Views) Lookup

func (v Views) Lookup(viewName string) (*View, error)

Lookup returns view by given name or error if view is not present.

func (*Views) Register

func (v *Views) Register(view *View)

Register registers view in registry using View name.

type Visitor

type Visitor func(value interface{}) error

Visitor represents visitor function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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