Documentation ¶
Overview ¶
Package generator implements the processor of source code and generator of kallax models based on Go source code.
Index ¶
- Constants
- type Event
- type Events
- type Field
- func (f *Field) Address() string
- func (f *Field) ColumnName() string
- func (f *Field) ForeignKey() string
- func (f *Field) Inline() bool
- func (f *Field) IsAutoIncrement() bool
- func (f *Field) IsInverse() bool
- func (f *Field) IsOneToManyRelationship() bool
- func (f *Field) IsPrimaryKey() bool
- func (f *Field) JSONName() string
- func (f *Field) SetFields(sf []*Field)
- func (f *Field) String() string
- func (f *Field) TypeSchemaName() string
- func (f *Field) Value() string
- type FieldKind
- type Generator
- type Model
- func (m *Model) Alias() string
- func (m *Model) CtorArgVars() string
- func (m *Model) CtorArgs() string
- func (m *Model) CtorRetVars() string
- func (m *Model) CtorReturns() string
- func (m *Model) HasRelationships() bool
- func (m *Model) Relationships() []*Field
- func (m *Model) SetFields(fields []*Field) error
- func (m *Model) String() string
- func (m *Model) Validate() error
- type Package
- type Processor
- type Template
- type TemplateData
- func (td *TemplateData) GenColumnAddresses(model *Model) string
- func (td *TemplateData) GenColumnValues(model *Model) string
- func (td *TemplateData) GenFindBy(model *Model) string
- func (td *TemplateData) GenModelColumns(model *Model) string
- func (td *TemplateData) GenModelSchema(model *Model) string
- func (td *TemplateData) GenSchemaInit(model *Model) string
- func (td *TemplateData) GenSubSchemas() string
- func (td *TemplateData) GenTypeName(f *Field) string
- func (td *TemplateData) IdentifierType(f *Field) string
- func (td *TemplateData) IsPtrSlice(f *Field) bool
Constants ¶
const ( // BaseModel is the type name of the kallax base model. BaseModel = "gopkg.in/src-d/go-kallax.v1.Model" //URL is the type name of the net/url.URL. URL = "url.URL" )
const ( // StoreNamePattern is the pattern used to name stores. StoreNamePattern = "%sStore" // QueryNamePattern is the pattern used to name queries. QueryNamePattern = "%sQuery" // ResultSetNamePattern is the pattern used to name result sets. ResultSetNamePattern = "%sResultSet" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Event ¶
type Event string
Event is the name of an event.
const ( // BeforeInsert is an event that will happen before Insert operations. BeforeInsert Event = "BeforeInsert" // AfterInsert is an event that will happen after Insert operations. AfterInsert Event = "AfterInsert" // BeforeUpdate is an event that will happen before Update operations. BeforeUpdate Event = "BeforeUpdate" // AfterUpdate is an event that will happen after Update operations. AfterUpdate Event = "AfterUpdate" // BeforeSave is an event that will happen before Insert or Update // operations. BeforeSave Event = "BeforeSave" // AfterSave is an event that will happen after Insert or Update // operations. AfterSave Event = "AfterSave" // BeforeDelete is an event that will happen before Delete. BeforeDelete Event = "BeforeDelete" // AfterDelete is an event that will happen after Delete. AfterDelete Event = "AfterDelete" )
type Field ¶
type Field struct { // Name is the field name. Name string // Type is the string representation of the field type. Type string // Kind is the kind of field. Kind FieldKind // Node is the reference to the field node. Node *types.Var // Tag is the strug tag of the field. Tag reflect.StructTag // Fields contains all the children fields of the field. A field has // children only if it is a struct. Fields []*Field // Parent is a reference to the parent field. Parent *Field // Model is the reference to the model containing this field. Model *Model // IsPtr reports whether the field is a pointer type or not. IsPtr bool // IsJSON reports whether the field has to be converted to JSON. IsJSON bool // IsAlias reports whether the field is of a type that aliases some other type. IsAlias bool // IsEmbedded reports whether the field is an embedded struct or not. // A struct is considered embedded if and only if the struct was embedded // as defined in Go. IsEmbedded bool }
Field is the representation of a model field.
func (*Field) Address ¶
Address returns the string representation of the code used to get the pointer to the field.
func (*Field) ColumnName ¶
ColumnName returns the SQL valid column name of the field. The struct tag `column` of the field can be use to set the name, otherwise is the field name converted to lower snake case. If the resultant name is a reserved keyword a _ will be prepended to the name.
func (*Field) ForeignKey ¶
ForeignKey returns the name of the foreign keys as specified in the struct tag `fk` or the default foreign key, which is the name of the relationship type in lower snake case with "_id" appended.
func (*Field) Inline ¶
Inline reports whether the field is inline and its children will be in the root of the model. An inline field is the one having the type kallax.Model, one that has a struct tag `kallax` containing `,inline` or an embedded struct field.
func (*Field) IsAutoIncrement ¶ added in v0.13.0
IsAutoIncrement reports whether the field is an autoincrementable primary key.
func (*Field) IsInverse ¶ added in v0.12.0
IsInverse returns whether the field is an inverse relationship.
func (*Field) IsOneToManyRelationship ¶ added in v0.12.0
IsOneToManyRelationship returns whether the field is a one to many relationship.
func (*Field) IsPrimaryKey ¶ added in v0.13.0
IsPrimaryKey reports whether the field is the primary key.
func (*Field) JSONName ¶ added in v0.13.0
JSONName returns the name of the field or its JSON name specified in the JSON struct tag.
func (*Field) SetFields ¶
SetFields sets all the children fields and the current field as a parent of the children.
func (*Field) TypeSchemaName ¶
TypeSchemaName returns the name of the Schema for the field type.
type FieldKind ¶
type FieldKind int
FieldKind is the kind of a field.
const ( // Basic is a field with a basic type. // On top of the Go basic types, we consider Basic as well the following // types: // - time.Time // - time.Duration // - url.URL Basic FieldKind = iota // Array is a field with an array type. Array // Slice is a field with a slice type. Slice // Map is a field with a map type. Map // Interface is a field with an interface type. Interface // Struct is a field with a struct type. Struct // Relationship is a field which is a relationship to other model/s. Relationship // Invalid is an invalid field type. Invalid )
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator is in charge of generating files for packages.
func NewGenerator ¶
NewGenerator creates a new generator that can save on the given filename.
type Model ¶
type Model struct { // Name is the model name. Name string // StoreName is the name of the store for this model. StoreName string // QueryName is the name of the query for this model. QueryName string // ResultSetName is the name of the result set for this model. ResultSetName string // Table is the name of the table, which will be extracted from the `table` // struct tag of the kallax.Model field in the model. // If one is not provided, it will be the model name transformed to lower // snake case. A model with an empty table name is not valid. Table string // Type is the string representation of the type. Type string // Fields contains the list of fields in the model. Fields []*Field // ID contains the identifier field of the model. ID *Field // Events contains the list of events implemented by the model. Events Events // Node is the node where the model was defined. Node *types.Named // CtorFunc is a reference to the model constructor. CtorFunc *types.Func // Package is a reference to the package where the model was defined. Package *types.Package }
Model is the representation of an user-defined model.
func (*Model) Alias ¶
Alias returns the alias of the model, which is the lowercased name preceded by "__".
func (*Model) CtorArgVars ¶
CtorArgVars returns the string representation of the variables to call the scanned constructor in the generated constructor.
func (*Model) CtorArgs ¶
CtorArgs returns the string with the generated constructor arguments, based on the constructor scanned, if any.
func (*Model) CtorRetVars ¶
CtorRetVars returns the string representation of the return variables to receive in the generated constructor based on the ones in the scanned constructor.
func (*Model) CtorReturns ¶
CtorReturns returns the string representation of the return values of the generated constructor based on the ones in the scanned constructor.
func (*Model) HasRelationships ¶ added in v0.12.0
HasRelationships returns whether the model has relationships or not.
func (*Model) Relationships ¶
Relationships returns the fields of a model that are relationships.
func (*Model) SetFields ¶ added in v0.12.0
SetFields sets all the children fields and their model to the current model. It also finds the primary key and sets it in the model. It will return an error if more than one primary key is found.
type Package ¶
type Package struct { // Name is the package name. Name string // Models are all the models found in the package. Models []*Model // contains filtered or unexported fields }
Package is the representation of a scanned package.
func NewPackage ¶ added in v0.13.0
NewPackage creates a new package.
type Processor ¶
type Processor struct { // Path of the package. Path string // Ignore is the list of files to ignore when scanning. Ignore map[string]struct{} // Package is the scanned package. Package *types.Package }
Processor is in charge of processing the package in a patch and scan models from it.
func NewProcessor ¶
NewProcessor creates a new Processor for the given path and ignored files.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template renders the kallax templates using given packages.
Base is the default Template instance with all templates preloaded.
type TemplateData ¶
type TemplateData struct { *Package // Processed is a map to keep track of processed nodes. Processed map[interface{}]string // contains filtered or unexported fields }
TemplateData is the structure passed to fill the templates.
func (*TemplateData) GenColumnAddresses ¶
func (td *TemplateData) GenColumnAddresses(model *Model) string
GenColumnAddresses generates the body of the switch that returns the column address given a column name for the given model.
func (*TemplateData) GenColumnValues ¶
func (td *TemplateData) GenColumnValues(model *Model) string
GenColumnValues generates the body of the switch that returns the column address given a column name for the given model.
func (*TemplateData) GenFindBy ¶ added in v1.0.0
func (td *TemplateData) GenFindBy(model *Model) string
GenFindBy generates FindByPropertyName for all model properties that are valid types or collection of valid types.
func (*TemplateData) GenModelColumns ¶
func (td *TemplateData) GenModelColumns(model *Model) string
GenModelColumns generates the creation of the list of columns in the given model.
func (*TemplateData) GenModelSchema ¶
func (td *TemplateData) GenModelSchema(model *Model) string
GenModelSchema generates generates the fields of the struct definition in the given model.
func (*TemplateData) GenSchemaInit ¶
func (td *TemplateData) GenSchemaInit(model *Model) string
GenSchemaInit generates the code to initialize all fields in the schema of a model.
func (*TemplateData) GenSubSchemas ¶
func (td *TemplateData) GenSubSchemas() string
GenSubSchemas generates the struct definition of all the subschemas in all models. A subschema is the JSON schema of a field that will be stored as JSON.
func (*TemplateData) GenTypeName ¶
func (td *TemplateData) GenTypeName(f *Field) string
GenTypeName generates the name of the type in the field.
func (*TemplateData) IdentifierType ¶ added in v0.13.0
func (td *TemplateData) IdentifierType(f *Field) string
func (*TemplateData) IsPtrSlice ¶ added in v0.12.0
func (td *TemplateData) IsPtrSlice(f *Field) bool
IsPtrSlice returns whether the field is a slice of pointers or not.