eav

package
v0.0.0-...-b553abb Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package eav contains the logic for the Entity-Attribute-Value pattern (WIP).

To use this library with additional columns in the EAV tables you must run from the tools folder first `tableToStruct` and then build the program `eavToStruct` and run it.

Definition of attribute backend, source, and frontend models:

- Backend: Provides hooks before and after save, load, and delete operations with an attribute value. - Source: Provides option values and labels for select and multi-select attributes. - Frontend: Prepares an attribute value for rendering on the storefront and admin backend.

Backend models can be an alternative to an observer; for example, when you have to do something that depends on an attribute value when an entity is saved.

TODO(CSC): idea to import data quickly: see Entity-Attribute-Value_(EAV)_The_Antipattern_Too_Great_to_Give_Up_-__Andy_Novick_2016-03-19.pdf Break it down to single partition operations • SQLCLR proc breaks the file by attribute_id • SEND attribute_id’s data to a Service Broker QUEUE • Each task is working on ONE attribute_id – That’s one HOBT / Partition • Run 1-2 tasks per core CSC: in our case run a pool goroutines to work on the attribute IDs or for each attribute_id a dedicated goroutine (maybe code generated)

Index

Constants

View Source
const (
	OutputFormatJSON uint8 = iota + 1
	OutputFormatText
	OutputFormatHTML
	OutputFormatPDF
	OutputFormatOneline
	OutputFormatArray // not sure about that one
)
View Source
const (
	// TypeStatic use to check if an attribute is static, means part of the eav prefix table
	TypeStatic string = "static"
)

Variables

View Source
var DefaultScopeNames = [...]string{"Store", "Group", "Website", "Default"}

DefaultScopeNames specifies the name of the scopes used in all EAV* function to generate scope based hierarchical fall backs.

View Source
var (
	// TableCollection handles all tables and its columns. init() in generated Go file will set the value.
	TableCollection csdb.Tables
)

Functions

func GetAttributeSelectSql

func GetAttributeSelectSql(dbrSess dbr.Session, aat EntityTypeAdditionalAttributeTabler, entityTypeID, websiteID int64) (*dbr.Select, error)

GetAttributeSelectSql generates the select query to retrieve full attribute configuration Implements the scope on a SQL query basis so that attribute functions does not need to deal with it. Tests see the tools package @see magento2/app/code/Magento/Eav/Model/Resource/Attribute/Collection.php::_initSelect()

func IfNull

func IfNull(alias, columnName, defaultVal string, scopeNames ...string) string

IfNull creates a nested IFNULL SQL statement when a scope based fall back hierarchy is required. Alias argument will be used as a prefix for the alias table name and as the final alias name.

func SetEntityTypeCollection

func SetEntityTypeCollection(sc CSEntityTypeSlice)

SetEntityTypeCollection sets the collection. Panics if slice is empty.

Types

type Attribute

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

Attribute defines properties for an attribute. This struct must be embedded in other EAV attributes.

func NewAttribute

func NewAttribute(
	wa WSASlice,
	websiteID int64,
	attributeCode string,
	attributeID int64,
	backendModel AttributeBackendModeller,
	backendTable string,
	backendType string,
	defaultValue string,
	entityTypeID int64,
	frontendClass string,
	frontendInput string,
	frontendLabel string,
	frontendModel AttributeFrontendModeller,
	isRequired bool,
	isUnique bool,
	isUserDefined bool,
	note string,
	sourceModel AttributeSourceModeller,
) *Attribute

NewAttribute only for use in auto generated code. Looks terrible 8-) TODO: Use functional options because only a few fields are required.

func (*Attribute) AttributeCode

func (a *Attribute) AttributeCode() string

func (*Attribute) AttributeID

func (a *Attribute) AttributeID() int64

func (*Attribute) BackendModel

func (a *Attribute) BackendModel() AttributeBackendModeller

func (*Attribute) BackendTable

func (a *Attribute) BackendTable() string

BackendTable returns the attribute backend table name. This function panics. @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/AbstractAttribute.php::getBackendTable

func (*Attribute) BackendType

func (a *Attribute) BackendType() string

func (*Attribute) DefaultValue

func (a *Attribute) DefaultValue() string

func (*Attribute) EntityType

func (a *Attribute) EntityType() (*CSEntityType, error)

EntityType returns EntityType object or an error. Does not consider websiteID.

func (*Attribute) EntityTypeID

func (a *Attribute) EntityTypeID() int64

func (*Attribute) FrontendClass

func (a *Attribute) FrontendClass() string

func (*Attribute) FrontendInput

func (a *Attribute) FrontendInput() string

func (*Attribute) FrontendLabel

func (a *Attribute) FrontendLabel() string

func (*Attribute) FrontendModel

func (a *Attribute) FrontendModel() AttributeFrontendModeller

func (*Attribute) IsInGroup

func (a *Attribute) IsInGroup(_ int64) bool

IsInGroup checks if attribute in specified attribute group @todo

func (*Attribute) IsInSet

func (a *Attribute) IsInSet(_ int64) bool

IsInSet checks if attribute in specified attribute set @todo

func (*Attribute) IsRequired

func (a *Attribute) IsRequired() bool

func (*Attribute) IsStatic

func (a *Attribute) IsStatic() bool

IsStatic checks if an attribute is a static one. Considers websiteID.

func (*Attribute) IsUnique

func (a *Attribute) IsUnique() bool

func (*Attribute) IsUserDefined

func (a *Attribute) IsUserDefined() bool

func (*Attribute) Note

func (a *Attribute) Note() string

func (*Attribute) SourceModel

func (a *Attribute) SourceModel() AttributeSourceModeller

func (*Attribute) UsesSource

func (a *Attribute) UsesSource() bool

UsesSource checks whether possible attribute values are retrieved from a finite source

type AttributeBackend

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

AttributeBackend implements abstract functions @todo @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Backend/AbstractBackend.php

func AttributeBackendDatetime

func AttributeBackendDatetime() *AttributeBackend

AttributeBackendDatetime handles date times @todo @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Backend/Datetime.php

func AttributeBackendTimeCreated

func AttributeBackendTimeCreated() *AttributeBackend

AttributeBackendTimeCreated @todo @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Backend/Time/Created.php

func AttributeBackendTimeUpdated

func AttributeBackendTimeUpdated() *AttributeBackend

AttributeBackendTimeUpdated @todo @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Backend/Time/Updated.php

func NewAttributeBackend

func NewAttributeBackend(cfgs ...AttributeBackendConfig) *AttributeBackend

NewAttributeBackend creates a pointer to a new attribute source

func (*AttributeBackend) Config

Config runs the configuration functions

func (*AttributeBackend) GetEntityIDField

func (ab *AttributeBackend) GetEntityIDField() string

func (*AttributeBackend) GetTable

func (ab *AttributeBackend) GetTable() string

func (*AttributeBackend) GetType

func (ab *AttributeBackend) GetType() string

func (*AttributeBackend) IsScalar

func (ab *AttributeBackend) IsScalar() bool

func (*AttributeBackend) IsStatic

func (ab *AttributeBackend) IsStatic() bool

func (*AttributeBackend) Validate

func (ab *AttributeBackend) Validate() bool

type AttributeBackendConfig

type AttributeBackendConfig func(*AttributeBackend)

func AttributeBackendIdx

func AttributeBackendIdx(i AttributeIndex) AttributeBackendConfig

AttributeBackendIdx only used in generated code to set the current index in the attribute slice

type AttributeBackendModeller

type AttributeBackendModeller interface {
	// GetTable @todo
	GetTable() string
	IsStatic() bool
	GetType() string
	// GetEntityIDField @todo
	GetEntityIDField() string

	// Validate @todo
	Validate() bool

	// IsScalar By default attribute value is considered scalar that can be stored in a generic way
	IsScalar() bool
	// Config to configure the current instance
	Config(...AttributeBackendConfig) AttributeBackendModeller
}

AttributeBackendModeller defines the attribute backend model @todo @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Backend/BackendInterface.php

type AttributeData

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

func NewAttributeData

func NewAttributeData(cfgs ...AttributeDataConfig) *AttributeData

NewAttributeData creates a pointer to a new attribute source

func (AttributeData) CompactValue

func (AttributeData) CompactValue(value []string)

func (*AttributeData) Config

func (AttributeData) ExtractValue

func (AttributeData) ExtractValue(req *http.Request)

func (AttributeData) OutputValue

func (AttributeData) OutputValue(format uint8)

func (AttributeData) RestoreValue

func (AttributeData) RestoreValue(value []string)

func (AttributeData) ValidateValue

func (AttributeData) ValidateValue(value []string)

type AttributeDataConfig

type AttributeDataConfig func(*AttributeData)

func AttributeDataIdx

func AttributeDataIdx(i AttributeIndex) AttributeDataConfig

AttributeDataIdx only used in generated code to set the current index in the attribute slice

type AttributeDataModeller

type AttributeDataModeller interface {

	// ExtractValue Extract data from request and return value
	ExtractValue(req *http.Request)

	// ValidateValue Validate data
	ValidateValue(value []string)

	// CompactValue Export attribute value to entity model
	CompactValue(value []string)

	// RestoreValue Restore attribute value from SESSION to entity model
	RestoreValue(value []string)

	//OutputValue return formatted attribute value from entity model
	OutputValue(format uint8)

	// Config to configure the current instance
	Config(...AttributeDataConfig) AttributeDataModeller
}

AttributeDataModeller implements methods from magento2/site/app/code/Magento/Eav/Model/Attribute/Data/AbstractData.php All functions: @todo implementation, parameters, returns and if function is really needed.

type AttributeFrontend

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

AttributeFrontend implements abstract functions @todo @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Backend/AbstractBackend.php

func AttributeFrontendDatetime

func AttributeFrontendDatetime() *AttributeFrontend

AttributeFrontendDatetime handles date times @todo @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Frontend/Datetime.php

func NewAttributeFrontend

func NewAttributeFrontend(cfgs ...AttributeFrontendConfig) *AttributeFrontend

NewAttributeFrontend creates a pointer to a new attribute source

func (*AttributeFrontend) Config

Config runs the configuration functions

func (*AttributeFrontend) GetInputType

func (af *AttributeFrontend) GetInputType() string

func (*AttributeFrontend) GetValue

func (af *AttributeFrontend) GetValue()

func (*AttributeFrontend) InputRenderer

func (af *AttributeFrontend) InputRenderer() FrontendInputRendererIFace

type AttributeFrontendConfig

type AttributeFrontendConfig func(*AttributeFrontend)

func AttributeFrontendIdx

func AttributeFrontendIdx(i AttributeIndex) AttributeFrontendConfig

AttributeFrontendIdx only used in generated code to set the current index in the attribute slice

type AttributeFrontendModeller

type AttributeFrontendModeller interface {
	InputRenderer() FrontendInputRendererIFace
	GetValue()
	GetInputType() string

	// Config to configure the current instance
	Config(...AttributeFrontendConfig) AttributeFrontendModeller
}

AttributeFrontendModeller defines the attribute frontend model @todo @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php

type AttributeGetter

type AttributeGetter interface {
	// ByID returns an index using the AttributeID. This index identifies an attribute within an AttributeSlice.
	ByID(id int64) (AttributeIndex, error)
	// ByCode returns an index using the AttributeCode. This index identifies an attribute within an AttributeSlice.
	ByCode(code string) (AttributeIndex, error)
}

AttributeGetter implements functions on how to retrieve directly a certain attribute. This interface is used in concrete attribute models by generated code. The logic behind this interface is to provide a fast access to the AttributeIndex. We will use as key the id int64 or code string which will then map to the value of an AttributeIndex.

type AttributeIndex

type AttributeIndex uint

AttributeIndex used for index in a slice with constants (iota)

type AttributeMapGet

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

AttributeMapGet contains two maps for faster retrieving of the attribute index. Only used in generated code. Implements interface AttributeGetter.

func NewAttributeMapGet

func NewAttributeMapGet(i map[int64]AttributeIndex, c map[string]AttributeIndex) *AttributeMapGet

NewAttributeMapGet returns a new pointer to an AttributeMapGet.

func (*AttributeMapGet) ByCode

func (si *AttributeMapGet) ByCode(code string) (_ AttributeIndex, ok bool)

ByCode returns an attribute index

func (*AttributeMapGet) ByID

func (si *AttributeMapGet) ByID(id int64) (_ AttributeIndex, ok bool)

ByID returns an attribute index by the id from the database table

type AttributeSliceGetter

type AttributeSliceGetter interface {
	Index(i AttributeIndex) interface{}
	Len() int
	ByID(g AttributeGetter, id int64) (interface{}, bool)
	ByCode(g AttributeGetter, code string) (interface{}, bool)
}

type AttributeSource

type AttributeSource struct {

	// Source is the internal source []string where i%0 is the value and i%1 is the label.
	Source []string
	// contains filtered or unexported fields
}

AttributeSource should implement all abstract ideas of @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Source/AbstractSource.php maybe extend also the interface

func AttributeSourceBoolean

func AttributeSourceBoolean() *AttributeSource

AttributeSourceBoolean @todo @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Source/Boolean.php

func AttributeSourceTable

func AttributeSourceTable() *AttributeSource

AttributeSourceTable @todo @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Source/Table.php

func NewAttributeSource

func NewAttributeSource(cfgs ...AttributeSourceConfig) *AttributeSource

NewAttributeSource creates a pointer to a new attribute source

func (*AttributeSource) Config

Config runs the configuration functions

func (*AttributeSource) GetAllOptions

func (as *AttributeSource) GetAllOptions() AttributeSourceOptions

GetAllOptions returns an option slice

func (*AttributeSource) GetOptionText

func (as *AttributeSource) GetOptionText(v string) string

GetOptionText returns for a value v the label

type AttributeSourceConfig

type AttributeSourceConfig func(*AttributeSource)

func AttributeSourceIdx

func AttributeSourceIdx(i AttributeIndex) AttributeSourceConfig

AttributeSourceIdx only used in generated code to set the current index in the attribute slice

type AttributeSourceModeller

type AttributeSourceModeller interface {
	// GetAllOptions returns all options in a value/label slice
	GetAllOptions() AttributeSourceOptions
	// GetOptionText returns for a value the appropriate label
	// @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Source/AbstractSource.php
	GetOptionText(value string) string
	// Config to configure the current instance
	Config(...AttributeSourceConfig) AttributeSourceModeller
}

AttributeSourceModeller interface implements the functions needed to retrieve data from a source model @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/Source/SourceInterface.php and its abstract class plus other default implementations. Refers to tables eav_attribute_option and eav_attribute_option_value OR other tables. @todo

type AttributeSourceOption

type AttributeSourceOption struct {
	// Value can be any value and is now here a temporary string. @todo check if maybe interface is needed
	Value string
	// Label is the name of a value
	Label string
}

AttributeSourceOption contains a value and label mostly for output in the frontend

type AttributeSourceOptions

type AttributeSourceOptions []AttributeSourceOption

AttributeSourceOptions is a slice of AttributeSourceOption structs

type Attributer

type Attributer interface {
	// IsStatic checks if an attribute is a static one
	IsStatic() bool
	// EntityType returns EntityType object or an error
	EntityType() (*CSEntityType, error)
	// UsesSource checks whether possible attribute values are retrieved from a finite source
	UsesSource() bool
	// IsInSet checks if attribute in specified attribute set
	IsInSet(int64) bool
	// IsInGroup checks if attribute in specified attribute group
	IsInGroup(int64) bool

	AttributeID() int64
	EntityTypeID() int64
	AttributeCode() string
	BackendModel() AttributeBackendModeller
	BackendType() string
	BackendTable() string
	FrontendModel() AttributeFrontendModeller
	FrontendInput() string
	FrontendLabel() string
	FrontendClass() string
	SourceModel() AttributeSourceModeller
	IsRequired() bool
	IsUserDefined() bool
	DefaultValue() string
	IsUnique() bool
	Note() string
}

Attributer defines the minimal requirements for one attribute. The interface relates to the table eav_attribute. Developers can even extend this table with additional columns. Additional columns will result into more generated functions. Even other EAV entities can embed this interface to extend an attribute. For example the customer attributes extends this interface two times. The column of func AttributeModel() has been removed because it is not used and misplaced in the eav_attribute table. This interface can be extended to more than 40 functions which is of course not idiomatic but for generated code it provides the best flexibility to extend with other custom structs. @see magento2/site/app/code/Magento/Eav/Model/Entity/Attribute/AbstractAttribute.php @see magento2/site/app/code/Magento/Eav/Api/Data/AttributeInterface.php

type CSEntityType

type CSEntityType struct {
	EntityTypeID              int64
	EntityTypeCode            string
	EntityModel               EntityTypeModeller
	AttributeModel            EntityTypeAttributeModeller
	EntityTable               EntityTypeTabler
	ValueTablePrefix          string
	EntityIDField             string
	IsDataSharing             bool
	DataSharingKey            string
	DefaultAttributeSetID     int64
	IncrementModel            EntityTypeIncrementModeller
	IncrementPerStore         bool
	IncrementPadLength        int64
	IncrementPadChar          string
	AdditionalAttributeTable  EntityTypeAdditionalAttributeTabler
	EntityAttributeCollection EntityTypeAttributeCollectioner
}

CSEntityType Go Type of the Mage database models and types. The prefix CS indicates that this EntityType is not generated because it contains special interfaces.

func GetEntityTypeByCode

func GetEntityTypeByCode(code string) (*CSEntityType, error)

GetEntityTypeByCode returns an entity type by its code

func GetEntityTypeByID

func GetEntityTypeByID(id int64) (*CSEntityType, error)

GetEntityTypeByID returns an entity type by its id

func (*CSEntityType) GetEntityTablePrefix

func (e *CSEntityType) GetEntityTablePrefix() string

EntityTablePrefix eav table name prefix @see magento2/site/app/code/Magento/Eav/Model/Entity/AbstractEntity.php::getEntityTablePrefix()

func (*CSEntityType) GetValueTablePrefix

func (e *CSEntityType) GetValueTablePrefix() string

ValueTablePrefix returns the table prefix for all value tables @see magento2/site/app/code/Magento/Eav/Model/Entity/AbstractEntity.php::getValueTablePrefix()

type CSEntityTypeSlice

type CSEntityTypeSlice []*CSEntityType

CSEntityTypeSlice Types starting with CS are the CoreStore mappings with the DB data

func GetEntityTypeCollection

func GetEntityTypeCollection() CSEntityTypeSlice

GetEntityTypeCollection to avoid leaking global variable. Maybe returning a copy?

func (CSEntityTypeSlice) GetByCode

func (es CSEntityTypeSlice) GetByCode(code string) (*CSEntityType, error)

GetByCode returns a CSEntityType using the entity code

func (CSEntityTypeSlice) GetByID

func (es CSEntityTypeSlice) GetByID(id int64) (*CSEntityType, error)

GetByID returns a CSEntityType using the entity id

type EntityTypeAdditionalAttributeTabler

type EntityTypeAdditionalAttributeTabler interface {
	TableAdditionalAttribute() (*csdb.Table, error)
	// TableEavWebsite gets the table, where website-dependent attribute parameters are stored in.
	// If an EAV model doesn't demand this functionality, let this function just return nil,nil
	TableEavWebsite() (*csdb.Table, error)
}

EntityTypeAdditionalAttributeTabler implements methods for EAV table structures to retrieve attributes

type EntityTypeAttributeCollectioner

type EntityTypeAttributeCollectioner interface {
	Collection() interface{}
}

EntityTypeAttributeCollectioner defines an attribute collection @todo it returns a slice so use type assertion.

type EntityTypeAttributeModeller

type EntityTypeAttributeModeller interface {
	// Creates a new attribute to the corresponding entity. @todo options?
	// The return type must embed eav.Attributer interface and of course its custom attribute interface
	New() interface{}
	Get(i AttributeIndex) (interface{}, error)
	MustGet(i AttributeIndex) interface{}
	GetByID(id int64) (interface{}, error)
	GetByCode(code string) (interface{}, error)
}

EntityTypeAttributeModeller defines an attribute model @todo

type EntityTypeIncrementModeller

type EntityTypeIncrementModeller interface {
	TBD()
}

EntityTypeIncrementModeller defines who to increment a number @todo

type EntityTypeModeller

type EntityTypeModeller interface {
	TBD()
}

EntityTypeModeller defines an entity type model @todo

type EntityTypeTabler

type EntityTypeTabler interface {
	// Base returns the base/prefix table name. E.g.: catalog_product_entity
	TableNameBase() string
	// Type returns for a type the table name. E.g.: catalog_product_entity_int
	TableNameValue(ValueIndex) string
}

EntityTypeTabler returns the table name

type FrontendInputRendererIFace

type FrontendInputRendererIFace interface {
}

FrontendInputRendererIFace see table catalog_eav_attribute.frontend_input_renderer @todo Stupid name :-( Fix later.

type Handler

type Handler struct {
	// EntityTyeID to load the entity type. @todo implementation
	EntityTyeID int64
	// C collection of a materialized slice
	C AttributeSliceGetter
	// G getter knows how to return an AttributeIndex by id or by code
	G AttributeGetter
}

Handler internal wrapper for attribute collection C, getter G and entity type id. must be embedded into a concrete attribute struct. Implements interface EntityTypeAttributeModeller and EntityTypeAttributeCollectioner.

func (*Handler) Collection

func (h *Handler) Collection() interface{}

Collection returns the full attribute collection AttributeSlice. You must use type assertion to convert to custattr.AttributeSlice.

func (*Handler) Get

func (h *Handler) Get(i AttributeIndex) (_ interface{}, ok bool)

Get uses an AttributeIndex to return an attribute or an error. Use type assertion to convert to Attributer.

func (*Handler) GetByCode

func (h *Handler) GetByCode(code string) (_ interface{}, ok bool)

GetByCode returns an address attribute by its code Use type assertion to convert to Attributer.

func (*Handler) GetByID

func (h *Handler) GetByID(id int64) (_ interface{}, ok bool)

GetByID returns an address attribute by its id Use type assertion to convert to Attributer.

func (*Handler) MustGet

func (h *Handler) MustGet(i AttributeIndex) interface{}

MustGet returns an attribute by AttributeIndex. Panics if not found. Use type assertion to convert to Attributer.

func (*Handler) New

func (h *Handler) New() interface{}

New creates a new attribute and returns interface custattr.Attributer

type ValueIndex

type ValueIndex int
const (
	EntityTypeDatetime ValueIndex = iota + 1
	EntityTypeDecimal
	EntityTypeInt
	EntityTypeText
	EntityTypeVarchar
)

type WSASlice

type WSASlice []*Attribute

WSASlice WebSiteAttributeSlice is a slice of pointers to attributes. Despite being a slice the websiteID is used as an index to return the attribute for a specific websiteID.

func (WSASlice) Index

func (s WSASlice) Index(i int64) *Attribute

Index returns the attribute from the current index or nil

Jump to

Keyboard shortcuts

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