Skip to content

Commit

Permalink
style: optimize code style
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed May 13, 2022
1 parent 8643c3d commit 7d226a0
Show file tree
Hide file tree
Showing 15 changed files with 629 additions and 74 deletions.
34 changes: 34 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
run:
deadline: 30s
tests: false
skip-dirs-use-default: true
skip-dirs:
- test

linters-settings:
gofmt:
simplify: true
govet:
check-shadowing: true
goimports:
local-prefixes: gorm.io,gorm.io/gen
unused:
check-exported: false
revive:
min-confidence: 0.8

linters:
presets:
- unused
enable:
- govet
- revive
- bodyclose
- errcheck
- exportloopref
- staticcheck
disable:
- gofumpt

issues:
exclude-use-default: false
7 changes: 0 additions & 7 deletions do.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ import (
"gorm.io/gen/helper"
)

var (
createClauses = []string{"INSERT", "VALUES", "ON CONFLICT"}
queryClauses = []string{"SELECT", "FROM", "WHERE", "GROUP BY", "ORDER BY", "LIMIT", "FOR"}
updateClauses = []string{"UPDATE", "SET", "WHERE"}
deleteClauses = []string{"DELETE", "FROM", "WHERE"}
)

// ResultInfo query/execute info
type ResultInfo struct {
RowsAffected int64
Expand Down
7 changes: 7 additions & 0 deletions do_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import (
"gorm.io/gen/field"
)

var (
createClauses = []string{"INSERT", "VALUES", "ON CONFLICT"}
queryClauses = []string{"SELECT", "FROM", "WHERE", "GROUP BY", "ORDER BY", "LIMIT", "FOR"}
updateClauses = []string{"UPDATE", "SET", "WHERE"}
deleteClauses = []string{"DELETE", "FROM", "WHERE"}
)

type stmtOpt func(*gorm.Statement) *gorm.Statement

var (
Expand Down
52 changes: 46 additions & 6 deletions field/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ import (
"gorm.io/gorm/schema"
)

// RelationshipType table relationship
type RelationshipType schema.RelationshipType

const (
HasOne RelationshipType = RelationshipType(schema.HasOne) // HasOneRel has one relationship
HasMany RelationshipType = RelationshipType(schema.HasMany) // HasManyRel has many relationships
// HasOne a has one association sets up a one-to-one connection with another model. Reference https://gorm.io/docs/has_one.html
HasOne RelationshipType = RelationshipType(schema.HasOne) // HasOneRel has one relationship
// HasMany a has many association sets up a one-to-many connection with another model. Reference https://gorm.io/docs/has_many.html
HasMany RelationshipType = RelationshipType(schema.HasMany) // HasManyRel has many relationships
// BelongsTo A belongs to association sets up a one-to-one connection with another model. Reference https://gorm.io/docs/belongs_to.html
BelongsTo RelationshipType = RelationshipType(schema.BelongsTo) // BelongsToRel belongs to relationship
// Many2Many Many to Many add a join table between two models. Reference https://gorm.io/docs/many2many.html
Many2Many RelationshipType = RelationshipType(schema.Many2Many) // Many2ManyRel many to many relationship
)

var ns = schema.NamingStrategy{}

// RelationField interface for relation field
type RelationField interface {
Name() string
Path() string
Expand All @@ -38,6 +44,7 @@ type RelationField interface {
GetPage() (offset, limit int)
}

// Relation relation meta info
type Relation struct {
relationship RelationshipType

Expand All @@ -55,69 +62,100 @@ type Relation struct {
limit, offset int
}

// Name relation field' name
func (r Relation) Name() string { return r.fieldName }

// Path relation field's path
func (r Relation) Path() string { return r.fieldPath }

// Type relation field's type
func (r Relation) Type() string { return r.fieldType }

// Model relation field's model
func (r Relation) Model() interface{} { return r.fieldModel }

// Relationship relationship between field and table struct
func (r Relation) Relationship() RelationshipType { return r.relationship }

// RelationshipName relationship's name
func (r Relation) RelationshipName() string { return ns.SchemaName(string(r.relationship)) }

// ChildRelations return child relations
func (r Relation) ChildRelations() []Relation { return r.childRelations }

// Field build field
func (r Relation) Field(fields ...string) Expr {
if len(fields) > 0 {
return NewString("", r.fieldName+"."+strings.Join(fields, ".")).appendBuildOpts(WithoutQuote)
}
return NewString("", r.fieldName).appendBuildOpts(WithoutQuote)
}

// AppendChildRelation append child relationship
func (r *Relation) AppendChildRelation(relations ...Relation) {
r.childRelations = append(r.childRelations, wrapPath(r.fieldPath, relations)...)
}

// On relation condition
func (r Relation) On(conds ...Expr) RelationField {
r.conds = append(r.conds, conds...)
return &r
}

// Select relation select columns
func (r Relation) Select(columns ...Expr) RelationField {
r.selects = append(r.selects, columns...)
return &r
}

// Order relation order columns
func (r Relation) Order(columns ...Expr) RelationField {
r.order = append(r.order, columns...)
return &r
}

// Clauses set relation clauses
func (r Relation) Clauses(hints ...clause.Expression) RelationField {
r.clauses = append(r.clauses, hints...)
return &r
}

// Offset set relation offset
func (r Relation) Offset(offset int) RelationField {
r.offset = offset
return &r
}

// Limit set relation limit
func (r Relation) Limit(limit int) RelationField {
r.limit = limit
return &r
}

func (r *Relation) GetConds() []Expr { return r.conds }
func (r *Relation) GetSelects() []Expr { return r.selects }
func (r *Relation) GetOrderCol() []Expr { return r.order }
// GetConds get query conditions
func (r *Relation) GetConds() []Expr { return r.conds }

// GetSelects get select columns
func (r *Relation) GetSelects() []Expr { return r.selects }

// GetOrderCol get order columns
func (r *Relation) GetOrderCol() []Expr { return r.order }

// GetClauses get clauses
func (r *Relation) GetClauses() []clause.Expression { return r.clauses }
func (r *Relation) GetPage() (offset, limit int) { return r.offset, r.limit }

// GetPage get offset and limit
func (r *Relation) GetPage() (offset, limit int) { return r.offset, r.limit }

// StructField return struct field code
func (r *Relation) StructField() (fieldStr string) {
for _, relation := range r.childRelations {
fieldStr += relation.fieldName + " struct {\nfield.RelationField\n" + relation.StructField() + "}\n"
}
return fieldStr
}

// StructFieldInit return field initialize code
func (r *Relation) StructFieldInit() string {
initStr := fmt.Sprintf("RelationField: field.NewRelation(%q, %q),\n", r.fieldPath, r.fieldType)
for _, relation := range r.childRelations {
Expand All @@ -144,6 +182,7 @@ var defaultRelationshipPrefix = map[RelationshipType]string{
Many2Many: "[]",
}

// RelateConfig config for relationship
type RelateConfig struct {
RelatePointer bool
RelateSlice bool
Expand All @@ -155,6 +194,7 @@ type RelateConfig struct {
OverwriteTag string
}

// RelateFieldPrefix return generated relation field's type
func (c *RelateConfig) RelateFieldPrefix(relationshipType RelationshipType) string {
switch {
case c.RelatePointer:
Expand Down
11 changes: 11 additions & 0 deletions field/bool.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
package field

// Bool boolean type field
type Bool Field

// Not ...
func (field Bool) Not() Bool {
return Bool{field.not()}
}

// Is ...
func (field Bool) Is(value bool) Expr {
return field.is(value)
}

// And boolean and
func (field Bool) And(value bool) Expr {
return Bool{field.and(value)}
}

// Or boolean or
func (field Bool) Or(value bool) Expr {
return Bool{field.or(value)}
}

// Xor ...
func (field Bool) Xor(value bool) Expr {
return Bool{field.xor(value)}
}

// BitXor ...
func (field Bool) BitXor(value bool) Expr {
return Bool{field.bitXor(value)}
}

// BitAnd ...
func (field Bool) BitAnd(value bool) Expr {
return Bool{field.bitAnd(value)}
}

// BitOr ...
func (field Bool) BitOr(value bool) Expr {
return Bool{field.bitOr(value)}
}

// Value ...
func (field Bool) Value(value bool) AssignExpr {
return field.value(value)
}

// Zero ...
func (field Bool) Zero() AssignExpr {
return field.value(false)
}
2 changes: 1 addition & 1 deletion field/doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// package field implement all type field and method
// Package field implement all type field and method
package field
Loading

0 comments on commit 7d226a0

Please sign in to comment.