repogen/internal/spec/models.go

137 lines
2.7 KiB
Go
Raw Normal View History

package spec
2021-01-19 12:26:26 +00:00
import (
"github.com/sunboyy/repogen/internal/code"
)
// QueryMode one or many
type QueryMode string
// query mode constants
const (
QueryModeOne QueryMode = "ONE"
QueryModeMany QueryMode = "MANY"
)
// MethodSpec is a method specification inside repository specification
type MethodSpec struct {
Name string
Params []code.Param
Returns []code.Type
Operation Operation
}
// Operation is an interface for any kind of operation
type Operation interface {
2021-02-14 04:48:09 +00:00
Name() string
}
2021-02-01 14:39:20 +00:00
// InsertOperation is a method specification for insert operations
type InsertOperation struct {
Mode QueryMode
}
2021-02-14 04:48:09 +00:00
// Name returns "Insert" operation name
func (o InsertOperation) Name() string {
return "Insert"
}
// FindOperation is a method specification for find operations
type FindOperation struct {
Mode QueryMode
Query QuerySpec
Sorts []Sort
}
2021-02-14 04:48:09 +00:00
// Name returns "Find" operation name
func (o FindOperation) Name() string {
return "Find"
}
// Sort is a detail of sorting find result
type Sort struct {
FieldName string
Ordering Ordering
}
// Ordering is a sort order
type Ordering string
// Ordering constants
const (
OrderingAscending = "ASC"
OrderingDescending = "DESC"
)
2021-01-27 12:15:25 +00:00
// UpdateOperation is a method specification for update operations
type UpdateOperation struct {
Update Update
2021-01-27 12:15:25 +00:00
Mode QueryMode
Query QuerySpec
}
// Update is an interface of update operation type
type Update interface {
Name() string
NumberOfArguments() int
}
// UpdateFields is a type of update operation that update specific fields
type UpdateFields []UpdateField
// Name returns UpdateFields name 'Fields'
func (u UpdateFields) Name() string {
return "Fields"
}
// NumberOfArguments returns number of update fields
func (u UpdateFields) NumberOfArguments() int {
return len(u)
}
// UpdateModel is a type of update operation that update the whole model
type UpdateModel struct {
}
// Name returns UpdateModel name 'Model'
func (u UpdateModel) Name() string {
return "Model"
}
// NumberOfArguments returns 1
func (u UpdateModel) NumberOfArguments() int {
return 1
}
2021-02-14 04:48:09 +00:00
// Name returns "Update" operation name
func (o UpdateOperation) Name() string {
return "Update"
}
2021-01-27 12:15:25 +00:00
// UpdateField stores mapping between field name in the model and the parameter index
type UpdateField struct {
Name string
ParamIndex int
}
2021-01-26 13:23:52 +00:00
// DeleteOperation is a method specification for delete operations
type DeleteOperation struct {
Mode QueryMode
Query QuerySpec
}
2021-02-14 04:48:09 +00:00
// Name returns "Delete" operation name
func (o DeleteOperation) Name() string {
return "Delete"
}
2021-02-06 11:05:47 +00:00
// CountOperation is a method specification for count operations
type CountOperation struct {
Query QuerySpec
}
2021-02-14 04:48:09 +00:00
// Name returns "Count" operation name
func (o CountOperation) Name() string {
return "Count"
}