tags

package
v0.0.32 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package tags generates table metadata by reading picard struct tag annotations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetStructTagsMap

func GetStructTagsMap(field reflect.StructField, tagType string) map[string]string

GetStructTagsMap generates a map of struct tag to values Example

input: testKeyOne=test_value_one,testKeyTwo=test_value_two
output: map[string]string{"testKeyOne": "test_value_one", "testKeyTwo": "test_value_two"

Types

type AndFilterGroup

type AndFilterGroup []Filterable

AndFilterGroup applies a group of filters using ands

func (AndFilterGroup) Apply

func (afg AndFilterGroup) Apply(table *qp.Table, metadata *TableMetadata) squirrel.Sqlizer

Apply applies the filter

type Association

type Association struct {
	Name         string
	Associations []Association
	OrderBy      []qp.OrderByRequest
	SelectFields []string
	FieldFilters Filterable
}
Association represents a data model relationship in the form of hasOne, hasMany, belongsTo between parent and child structs.

Including Associations in FilterRequests will eager load the model relationship results in a single query with JOINs.

Name refers to the name of the struct field that will hold the filter results for the relationship. For belongsTo relationships, this is the `related` tag value on a `foreign_key“ field on the struct

Example:

type ChildModel struct {
	Metadata	metadata.Metadata	`picard:"tablename=child"`
	ID			string				`picard:"primary_key,column=id"`
	ParentID 	string				`picard:"foreign_key,required,related=Parent,column=parent_id
	Parent		ParentModel
}

p.FilterModel(picard.FilterRequest{
	FilterModel: ChildModel{},
	Associations: []tags.Association{
		{
			Name: "Parent",
		},
	},
})

For hasOne or hasMany relationships, this is the field with the `child` tag

Example:

type ParentModel struct {
	Metadata	metadata.Metadata	`picard:"tablename=parent"`
	ID			string				`picard:"primary_key,column=id"`
	Children	[]ChildModel		`picard:"child,foreign_key=ParentID"`
}

p.FilterModel(picard.FilterRequest{
	FilterModel: ParentModel{},
	Associations: []tags.Association{
		{
			Name: "Children",
		},
	},
})

Each association may have nested relationships, like so:

type ParentModel struct {
	Metadata	metadata.Metadata	`picard:"tablename=parent"`
	ID			string				`picard:"primary_key,column=id"`
	Children	[]ChildModel		`picard:"child,foreign_key=ParentID"`
}

type ChildModel struct {
	Metadata	metadata.Metadata	`picard:"tablename=child"`
	ID			string				`picard:"primary_key,column=id"`
	Children	[]GrandChildModel	`picard:"child,foreign_key=ParentID"`
	ParentID 	string				`picard:"foreign_key,required,related=Parent,column=parent_id"``
	Parent		ParentModel
}

type GrandChild struct {
	Metadata	metadata.Metadata	`picard:"tablename=grandchild"`
	ID			string				`picard:"primary_key,column=id"`
	ParentID 	string				`picard:"foreign_key,required,related=Parent,column=parent_id
	Parent		ChildModel
}

p.FilterModel(picard.FilterRequest{
	FilterModel: ParentModel{},
	Associations: []tags.Association{
	{
		Name: "Children",
		Associations: []tags.Association{
			{
				Name: "Children",
			},
		},
	},
})

OrderBy lets you define the ordering of filter results by adding an ORDER BY clause with an OrderByRequest

Example:

type ParentModel struct {
	Metadata	metadata.Metadata	`picard:"tablename=parent"`
	ID			string				`picard:"primary_key,column=id"`
	Children	[]ChildModel		`picard:"child,foreign_key=ParentID"`
}

type ChildModel struct {
	Metadata	metadata.Metadata	`picard:"tablename=child"`
	ID			string				`picard:"primary_key,column=id"`
	Name		string				`picard:"column=name"`
}
p.FilterModel(picard.FilterRequest{
	FilterModel: ParentModel{},
	Associations: []tags.Association{
			{
				Name: "Children",
				OrderBy: []qp.OrderByRequest{
				{
					Field:      "Name",
					Descending: true,
				},
			},
		},
})

// SELECT ... FROM parent AS t0 LEFT JOIN child AS t1 ON ...ORDER BY t1.name DESC

SelectFields lets you define the exact columns to query for. Without `SelectFields`, all the columns defined in the table will be included in the query.

From the example above for OrderBy:

p.FilterModel(picard.FilterRequest{
	FilterModel: ParentModel{},
	Associations: []tags.Association{
			{
				Name: "Children",
				SelectFields: []string{
					"Name",
				}
			},
		},
	}
})

// SELECT ... t1.name FROM parent AS t0 LEFT JOIN child AS t1 ON ...

FieldFilters generates a `WHERE` clause grouping with either an `OR` grouping via `tags.OrFilterGroup` or an `AND` grouping via `tags.AndFilterGroup`. The `tags.FieldFilter`

type ParentModel struct {
	Metadata	metadata.Metadata	`picard:"tablename=parent"`
	ID			string				`picard:"primary_key,column=id"`
	Children	[]ChildModel		`picard:"child,foreign_key=ParentID"`
}

type ChildModel struct {
	Metadata	metadata.Metadata	`picard:"tablename=child"`
	ID			string				`picard:"primary_key,column=id"`
	FieldA		string				`picard:"column=field_a"`
	FieldB		string				`picard:"column=field_b"`
}

import "github.com/skuid/picard/tags"

p.FilterModel(picard.FilterRequest{
		FilterModel: ParentModel{},
		Associations: []tags.Association{
			{
				Name: "Children",
				FieldFilters: tags.OrFilterGroup{
					tags.FieldFilter{
						FieldName:   "FieldA",
						FilterValue: "foo",
					},
					tags.FieldFilter{
						FieldName:   "FieldB",
						FilterValue: "bar",
					},
				},
			}
		},
	}
})

// SELECT ... WHERE (t1.field_a = 'foo' OR t1.field_b = 'bar')

p.FilterModel(picard.FilterRequest{
		FilterModel: ParentModel{},
		Associations: []tags.Association{
			{
				Name: "Children",
				FieldFilters: tags.AndFilterGroup{
					tags.FieldFilter{
						FieldName:   "FieldA",
						FilterValue: "foo",
					},
					tags.FieldFilter{
						FieldName:   "FieldB",
						FilterValue: "bar",
					},
				},
			}
		},
	}
})

// SELECT ... WHERE (t1.field_a = 'foo' AND t1.field_b = 'bar')

type Child

type Child struct {
	FieldName        string
	FieldType        reflect.Type
	FieldKind        reflect.Kind
	ForeignKey       string
	KeyMapping       string
	ValueMappings    map[string]string
	GroupingCriteria map[string]string
	DeleteOrphans    bool
}

Child structure

type FieldFilter

type FieldFilter struct {
	FieldName      string
	FilterValue    interface{}
	FilterOperator string
}
FieldFilter defines an arbitrary filter on a FilterRequest

Specify the fields that should be added in a AndFilterGroup or a OrFilterGroup WHERE clause grouping.

Example:

import "github.com/skuid/picard/tags"

tags.FieldFilter{
	FieldName:   "FieldB",
	FilterValue: "bar",
},

SQL translation in WHERE clause grouping:

t0.field_B = "bar"

func (FieldFilter) Apply

func (ff FieldFilter) Apply(table *qp.Table, metadata *TableMetadata) squirrel.Sqlizer

Apply applies the filter

type FieldMetadata

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

FieldMetadata structure

func (FieldMetadata) GetAudit

func (fm FieldMetadata) GetAudit() string

GetAudit function

func (FieldMetadata) GetColumnName

func (fm FieldMetadata) GetColumnName() string

GetColumnName function

func (FieldMetadata) GetFieldType

func (fm FieldMetadata) GetFieldType() reflect.Type

GetFieldType function

func (FieldMetadata) GetName

func (fm FieldMetadata) GetName() string

GetName function

func (FieldMetadata) GetRelatedName

func (fm FieldMetadata) GetRelatedName() string

GetRelatedName function

func (FieldMetadata) GetRelatedType

func (fm FieldMetadata) GetRelatedType() reflect.Type

GetRelatedType function

func (FieldMetadata) IncludeInUpdate

func (fm FieldMetadata) IncludeInUpdate() bool

IncludeInUpdate function

func (FieldMetadata) IsEncrypted

func (fm FieldMetadata) IsEncrypted() bool

IsEncrypted function

func (FieldMetadata) IsFK

func (fm FieldMetadata) IsFK() bool

IsFK function

func (FieldMetadata) IsJSONB

func (fm FieldMetadata) IsJSONB() bool

IsJSONB function

func (FieldMetadata) IsMultitenancyKey

func (fm FieldMetadata) IsMultitenancyKey() bool

IsMultitenancyKey function

func (FieldMetadata) IsPrimaryKey

func (fm FieldMetadata) IsPrimaryKey() bool

IsPrimaryKey function

type Filterable

type Filterable interface {
	Apply(*qp.Table, *TableMetadata) squirrel.Sqlizer
}

Filterable interface allows filters to be specified in Filter Requests

type ForeignKey

type ForeignKey struct {
	TableMetadata    *TableMetadata
	FieldName        string
	KeyColumn        string
	RelatedFieldName string
	Required         bool
	NeedsLookup      bool
	LookupResults    map[string]interface{}
	LookupsUsed      []Lookup
	KeyMapField      string
}

ForeignKey structure

type Lookup

type Lookup struct {
	TableName           string
	MatchDBColumn       string
	MatchObjectProperty string
	JoinKey             string
	Value               interface{}
	SubQuery            []Lookup
	SubQueryForeignKey  string
	SubQueryMetadata    *TableMetadata
}

Lookup structure

type OrFilterGroup

type OrFilterGroup []Filterable

OrFilterGroup applies a group of filters using ors

func (OrFilterGroup) Apply

func (ofg OrFilterGroup) Apply(table *qp.Table, metadata *TableMetadata) squirrel.Sqlizer

Apply applies the filter

type TableMetadata

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

TableMetadata structure

func GetTableMetadata

func GetTableMetadata(data interface{}) (*TableMetadata, error)

GetTableMetadata function

func TableMetadataFromType

func TableMetadataFromType(t reflect.Type) *TableMetadata

TableMetadataFromType gets table metadata from a reflect type

func (TableMetadata) GetChildField

func (tm TableMetadata) GetChildField(childName string) *Child

GetChildField function

func (TableMetadata) GetChildFieldFromForeignKey

func (tm TableMetadata) GetChildFieldFromForeignKey(foreignKeyName string, foreignKeyType reflect.Type) *Child

GetChildFieldFromForeignKey function

func (TableMetadata) GetChildren

func (tm TableMetadata) GetChildren() []Child

GetChildren function

func (TableMetadata) GetColumnNames

func (tm TableMetadata) GetColumnNames() []string

GetColumnNames gets the column names

func (TableMetadata) GetColumnNamesForUpdate

func (tm TableMetadata) GetColumnNamesForUpdate() []string

GetColumnNamesForUpdate gets the columm names, but excludes certain fields

func (TableMetadata) GetColumnNamesWithoutPrimaryKey

func (tm TableMetadata) GetColumnNamesWithoutPrimaryKey() []string

GetColumnNamesWithoutPrimaryKey gets the columm names, but excludes the primary key

func (TableMetadata) GetEncryptedColumns

func (tm TableMetadata) GetEncryptedColumns() []string

GetEncryptedColumns function

func (TableMetadata) GetField

func (tm TableMetadata) GetField(fieldName string) FieldMetadata

GetField returns the fields in the order they appear in the struct

func (TableMetadata) GetFields

func (tm TableMetadata) GetFields() []FieldMetadata

GetFields returns the fields in the order they appear in the struct

func (TableMetadata) GetForeignKeyField

func (tm TableMetadata) GetForeignKeyField(foreignKeyName string) *ForeignKey

GetForeignKeyField function

func (TableMetadata) GetForeignKeyFieldFromRelation

func (tm TableMetadata) GetForeignKeyFieldFromRelation(relationName string) *ForeignKey

GetForeignKeyFieldFromRelation function

func (TableMetadata) GetForeignKeys

func (tm TableMetadata) GetForeignKeys() []ForeignKey

GetForeignKeys function

func (TableMetadata) GetJSONBColumns

func (tm TableMetadata) GetJSONBColumns() []string

GetJSONBColumns function

func (TableMetadata) GetLookups

func (tm TableMetadata) GetLookups() []Lookup

GetLookups function

func (TableMetadata) GetMultitenancyKeyColumnName

func (tm TableMetadata) GetMultitenancyKeyColumnName() string

GetMultitenancyKeyColumnName function

func (TableMetadata) GetMultitenancyKeyMetadata

func (tm TableMetadata) GetMultitenancyKeyMetadata() *FieldMetadata

GetMultitenancyKeyMetadata function

func (TableMetadata) GetPrimaryKeyColumnName

func (tm TableMetadata) GetPrimaryKeyColumnName() string

GetPrimaryKeyColumnName function

func (TableMetadata) GetPrimaryKeyFieldName

func (tm TableMetadata) GetPrimaryKeyFieldName() string

GetPrimaryKeyFieldName function

func (TableMetadata) GetPrimaryKeyMetadata

func (tm TableMetadata) GetPrimaryKeyMetadata() *FieldMetadata

GetPrimaryKeyMetadata function

func (TableMetadata) GetTableName

func (tm TableMetadata) GetTableName() string

GetTableName gets the name of the table

Jump to

Keyboard shortcuts

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