Skip to content

Commit

Permalink
added more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Fs02 committed Apr 16, 2018
1 parent e806865 commit c5a6c8b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
42 changes: 41 additions & 1 deletion c/condition.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,59 @@
package c

// ConditionType defines enumeration of all supported condition types.
type ConditionType int

const (
// ConditionAnd is condition type for and operator.
ConditionAnd ConditionType = iota
// ConditionOr is condition type for or operator.
ConditionOr
// ConditionNot is condition type for not operator.
ConditionNot

// ConditionEq is condition type for equal comparison.
ConditionEq
// ConditionNe is condition type for not equal comparison.
ConditionNe

// ConditionLt is condition type for less than comparison.
ConditionLt
// ConditionLte is condition type for less than or equal comparison.
ConditionLte
// ConditionGt is condition type for greater than comparison.
ConditionGt
// ConditionGte is condition type for greter than or equal comparison.
ConditionGte

// ConditionNil is condition type for nil check.
ConditionNil
// ConditionNotNil is condition type for not nil check.
ConditionNotNil

// ConditionIn is condition type for inclusion comparison.
ConditionIn
// ConditionNin is condition type for not inclusion comparison.
ConditionNin

// ConditionLike is condition type for like comparison.
ConditionLike
// ConditionNotLike is condition type for not like comparison.
ConditionNotLike

// ConditionFragment is condition type for custom condition.
ConditionFragment
)

// column
// I identifies database variable such as column name or table name.
type I string

// Operand defines information about condition's operand.
type Operand struct {
Column I
Values []interface{}
}

// NewOperand create new operand.
func NewOperand(o ...interface{}) Operand {
if len(o) == 1 {
if c, ok := o[0].(I); ok {
Expand All @@ -45,20 +64,23 @@ func NewOperand(o ...interface{}) Operand {
return Operand{Values: o}
}

// Condition defines details of a coundition type.
type Condition struct {
Type ConditionType
Left Operand
Right Operand
Inner []Condition
}

// None returns true if no condition is specified.
func (c Condition) None() bool {
return (c.Type == ConditionAnd ||
c.Type == ConditionOr ||
c.Type == ConditionNot) &&
len(c.Inner) == 0
}

// And wraps conditions using and.
func (c Condition) And(condition ...Condition) Condition {
if c.None() && len(condition) == 1 {
return condition[0]
Expand All @@ -71,6 +93,7 @@ func (c Condition) And(condition ...Condition) Condition {
return And(inner...)
}

// Or wraps conditions using or.
func (c Condition) Or(condition ...Condition) Condition {
if c.None() && len(condition) == 1 {
return condition[0]
Expand All @@ -84,6 +107,7 @@ func (c Condition) Or(condition ...Condition) Condition {
return Or(inner...)
}

// And compares other conditions using and.
func And(inner ...Condition) Condition {
if len(inner) == 1 {
return inner[0]
Expand All @@ -95,6 +119,7 @@ func And(inner ...Condition) Condition {
}
}

// Or compares other conditions using and.
func Or(inner ...Condition) Condition {
if len(inner) == 1 {
return inner[0]
Expand All @@ -106,6 +131,8 @@ func Or(inner ...Condition) Condition {
}
}

// Not wraps conditions using not.
// It'll negate the condition type if possible.
func Not(inner ...Condition) Condition {
if len(inner) == 1 {
c := inner[0]
Expand Down Expand Up @@ -143,6 +170,7 @@ func Not(inner ...Condition) Condition {
}
}

// Eq compares that left value is equal to right value.
func Eq(left, right interface{}) Condition {
return Condition{
Type: ConditionEq,
Expand All @@ -151,6 +179,7 @@ func Eq(left, right interface{}) Condition {
}
}

// Ne compares that left value is not equal to right value.
func Ne(left, right interface{}) Condition {
return Condition{
Type: ConditionNe,
Expand All @@ -159,6 +188,7 @@ func Ne(left, right interface{}) Condition {
}
}

// Lt compares that left value is less than to right value.
func Lt(left, right interface{}) Condition {
return Condition{
Type: ConditionLt,
Expand All @@ -167,6 +197,7 @@ func Lt(left, right interface{}) Condition {
}
}

// Lte compares that left value is less than or equal to right value.
func Lte(left, right interface{}) Condition {
return Condition{
Type: ConditionLte,
Expand All @@ -175,6 +206,7 @@ func Lte(left, right interface{}) Condition {
}
}

// Gt compares that left value is greater than to right value.
func Gt(left, right interface{}) Condition {
return Condition{
Type: ConditionGt,
Expand All @@ -183,6 +215,7 @@ func Gt(left, right interface{}) Condition {
}
}

// Gte compares that left value is greater than or equal to right value.
func Gte(left, right interface{}) Condition {
return Condition{
Type: ConditionGte,
Expand All @@ -191,20 +224,23 @@ func Gte(left, right interface{}) Condition {
}
}

// Nil check whether column is nil.
func Nil(col I) Condition {
return Condition{
Type: ConditionNil,
Left: NewOperand(col),
}
}

// NotNil check whether column is not nil.
func NotNil(col I) Condition {
return Condition{
Type: ConditionNotNil,
Left: NewOperand(col),
}
}

// In check whethers value of the column is included in values.
func In(col I, values ...interface{}) Condition {
return Condition{
Type: ConditionIn,
Expand All @@ -213,6 +249,7 @@ func In(col I, values ...interface{}) Condition {
}
}

// Nin check whethers value of the column is not included in values.
func Nin(col I, values ...interface{}) Condition {
return Condition{
Type: ConditionNin,
Expand All @@ -221,6 +258,7 @@ func Nin(col I, values ...interface{}) Condition {
}
}

// Like compares value of column to match string pattern.
func Like(col I, pattern string) Condition {
return Condition{
Type: ConditionLike,
Expand All @@ -229,6 +267,7 @@ func Like(col I, pattern string) Condition {
}
}

// NotLike compares value of column to not match string pattern.
func NotLike(col I, pattern string) Condition {
return Condition{
Type: ConditionNotLike,
Expand All @@ -237,6 +276,7 @@ func NotLike(col I, pattern string) Condition {
}
}

// Fragment add custom condition.
func Fragment(expr I, values ...interface{}) Condition {
return Condition{
Type: ConditionFragment,
Expand Down
1 change: 1 addition & 0 deletions c/join.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package c

// Join defines join information in query.
type Join struct {
Mode string
Collection string
Expand Down
5 changes: 5 additions & 0 deletions c/order.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
package c

// Order defines order information of query.
type Order struct {
Field I
Order int
}

// Asc orders field with ascending order.
func Asc(field I) Order {
return Order{
Field: field,
Order: 1,
}
}

// Desc orders field with descending order.
func Desc(field I) Order {
return Order{
Field: field,
Order: -1,
}
}

// Asc returns true if order is ascending.
func (order Order) Asc() bool {
return order.Order >= 0
}

// Desc returns true if order is descending.
func (order Order) Desc() bool {
return order.Order < 0
}
21 changes: 21 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,71 @@
package errors

// UnexpectedErrorCode defines default code for Unexpected Errors.
var UnexpectedErrorCode = 0

// ChangesetErrorCode defines default code for Changeset Errors.
var ChangesetErrorCode = 1

// NotFoundErrorCode defines default code for NotFound Errors.
var NotFoundErrorCode = 2

// DuplicateErrorCode defines default code for Duplicate Errors.
var DuplicateErrorCode = 3

// Error defines information about grimoire's error.
type Error struct {
Message string `json:"message"`
Field string `json:"field,omitempty"`
Code int `json:"json,omitempty"`
}

// Error prints error message.
func (e Error) Error() string {
return e.Message
}

// UnexpectedError returns true if error is an UnexpectedError.
func (e Error) UnexpectedError() bool {
return e.Code == UnexpectedErrorCode
}

// ChangesetError returns true if error is an ChangesetError.
func (e Error) ChangesetError() bool {
return e.Code == ChangesetErrorCode
}

// NotFoundError returns true if error is an NotFoundError.
func (e Error) NotFoundError() bool {
return e.Code == NotFoundErrorCode
}

// DuplicateError returns true if error is an DuplicateError.
func (e Error) DuplicateError() bool {
return e.Code == DuplicateErrorCode
}

// New creates an error with custom image, field and error code.
func New(message string, field string, code int) Error {
return Error{message, field, code}
}

// UnexpectedError creates an unexpected error with custom message.
func UnexpectedError(message string) Error {
return Error{
Message: message,
Code: UnexpectedErrorCode,
}
}

// NotFoundError creates a not found error with custom message.
func NotFoundError(message string) Error {
return Error{
Message: message,
Code: NotFoundErrorCode,
}
}

// ChangesetError creates a changeset error with custom message.
func ChangesetError(message string, field string) Error {
return Error{
Message: message,
Expand All @@ -57,6 +74,7 @@ func ChangesetError(message string, field string) Error {
}
}

// DuplicateError creates a duplicate error with custom message.
func DuplicateError(message string, field string) Error {
return Error{
Message: message,
Expand All @@ -65,6 +83,9 @@ func DuplicateError(message string, field string) Error {
}
}

// Wrap errors as grimoire's error.
// If error is grimoire error, it'll remain as is.
// Otherwise it'll be wrapped as unexpected error.
func Wrap(err error) error {
if err == nil {
return nil
Expand Down
Loading

0 comments on commit c5a6c8b

Please sign in to comment.