generator

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2017 License: MIT Imports: 19 Imported by: 18

Documentation

Overview

Package generator implements the processor of source code and generator of kallax models based on Go source code.

Index

Constants

View Source
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"
)
View Source
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 Events

type Events []Event

Events is a collection of events.

func (Events) Has

func (s Events) Has(e Event) bool

Has reports whether the event is in the collection.

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 NewField

func NewField(n, t string, tag reflect.StructTag) *Field

NewField creates a new field with its name, type and struct tag.

func (*Field) Address

func (f *Field) Address() string

Address returns the string representation of the code used to get the pointer to the field.

func (*Field) ColumnName

func (f *Field) ColumnName() string

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

func (f *Field) ForeignKey() string

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

func (f *Field) Inline() bool

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

func (f *Field) IsAutoIncrement() bool

IsAutoIncrement reports whether the field is an autoincrementable primary key.

func (*Field) IsInverse

func (f *Field) IsInverse() bool

IsInverse returns whether the field is an inverse relationship.

func (*Field) IsOneToManyRelationship

func (f *Field) IsOneToManyRelationship() bool

IsOneToManyRelationship returns whether the field is a one to many relationship.

func (*Field) IsPrimaryKey

func (f *Field) IsPrimaryKey() bool

IsPrimaryKey reports whether the field is the primary key.

func (*Field) JSONName

func (f *Field) JSONName() string

JSONName returns the name of the field or its JSON name specified in the JSON struct tag.

func (*Field) SetFields

func (f *Field) SetFields(sf []*Field)

SetFields sets all the children fields and the current field as a parent of the children.

func (*Field) String

func (f *Field) String() string

func (*Field) TypeSchemaName

func (f *Field) TypeSchemaName() string

TypeSchemaName returns the name of the Schema for the field type.

func (*Field) Value

func (f *Field) Value() string

Value is the string representation of the code needed to get the value of the field in a way that SQL drivers can process.

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
)

func (FieldKind) String

func (t FieldKind) String() string

String returns the constant name of the FieldKind

type Generator

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

Generator is in charge of generating files for packages.

func NewGenerator

func NewGenerator(filename string) *Generator

NewGenerator creates a new generator that can save on the given filename.

func (*Generator) Generate

func (g *Generator) Generate(pkg *Package) error

Generate writes the file with the contents of the given package.

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 NewModel

func NewModel(n string) *Model

NewModel creates a new model with the given name.

func (*Model) Alias

func (m *Model) Alias() string

Alias returns the alias of the model, which is the lowercased name preceded by "__".

func (*Model) CtorArgVars

func (m *Model) CtorArgVars() string

CtorArgVars returns the string representation of the variables to call the scanned constructor in the generated constructor.

func (*Model) CtorArgs

func (m *Model) CtorArgs() string

CtorArgs returns the string with the generated constructor arguments, based on the constructor scanned, if any.

func (*Model) CtorRetVars

func (m *Model) CtorRetVars() string

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

func (m *Model) CtorReturns() string

CtorReturns returns the string representation of the return values of the generated constructor based on the ones in the scanned constructor.

func (*Model) HasRelationships

func (m *Model) HasRelationships() bool

HasRelationships returns whether the model has relationships or not.

func (*Model) Relationships

func (m *Model) Relationships() []*Field

Relationships returns the fields of a model that are relationships.

func (*Model) SetFields

func (m *Model) SetFields(fields []*Field) error

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.

func (*Model) String

func (m *Model) String() string

String prints the representation of the model.

func (*Model) Validate

func (m *Model) Validate() error

Validate returns an error if the model is not valid. To be valid, a model needs a non-empty table name, a non-repeated set of fields.

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

func NewPackage(pkg *types.Package) *Package

NewPackage creates a new package.

func (*Package) FindModel

func (p *Package) FindModel(name string) *Model

FindModel finds the model with the given name.

func (*Package) SetModels

func (p *Package) SetModels(models []*Model)

SetModels sets the models of the packages and indexes them.

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

func NewProcessor(path string, ignore []string) *Processor

NewProcessor creates a new Processor for the given path and ignored files.

func (*Processor) Do

func (p *Processor) Do() (*Package, error)

Do performs all the processing and returns the scanned package.

type Template

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

Template renders the kallax templates using given packages.

var Base *Template = &Template{template: base}

Base is the default Template instance with all templates preloaded.

func (*Template) Execute

func (t *Template) Execute(wr io.Writer, data *Package) error

Execute writes the processed template to the given writer.

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

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

func (td *TemplateData) IdentifierType(f *Field) string

func (*TemplateData) IsPtrSlice

func (td *TemplateData) IsPtrSlice(f *Field) bool

IsPtrSlice returns whether the field is a slice of pointers or not.

Directories

Path Synopsis
cli

Jump to

Keyboard shortcuts

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