dialect/sql/schema: accept convert from string2enum and enum2stirng (#587)

This commit is contained in:
Ariel Mashraki
2020-07-05 22:22:50 +03:00
committed by GitHub
parent 1a8d98f507
commit cedeef653a
17 changed files with 598 additions and 5 deletions

View File

@@ -33,6 +33,8 @@ const (
FieldBlob = "blob"
// FieldState holds the string denoting the state field in the database.
FieldState = "state"
// FieldStatus holds the string denoting the status field in the database.
FieldStatus = "status"
// EdgeCar holds the string denoting the car edge name in mutations.
EdgeCar = "car"
@@ -73,6 +75,7 @@ var Columns = []string{
FieldNewName,
FieldBlob,
FieldState,
FieldStatus,
}
var (
@@ -111,3 +114,26 @@ func StateValidator(s State) error {
return fmt.Errorf("user: invalid enum value for state field: %q", s)
}
}
// Status defines the type for the status enum field.
type Status string
// Status values.
const (
StatusDone Status = "done"
StatusPending Status = "pending"
)
func (s Status) String() string {
return string(s)
}
// StatusValidator is a validator for the "s" field enum values. It is called by the builders before save.
func StatusValidator(s Status) error {
switch s {
case StatusDone, StatusPending:
return nil
default:
return fmt.Errorf("user: invalid enum value for status field: %q", s)
}
}

View File

@@ -1038,6 +1038,68 @@ func StateNotNil() predicate.User {
})
}
// StatusEQ applies the EQ predicate on the "status" field.
func StatusEQ(v Status) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.EQ(s.C(FieldStatus), v))
})
}
// StatusNEQ applies the NEQ predicate on the "status" field.
func StatusNEQ(v Status) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.NEQ(s.C(FieldStatus), v))
})
}
// StatusIn applies the In predicate on the "status" field.
func StatusIn(vs ...Status) predicate.User {
v := make([]interface{}, len(vs))
for i := range v {
v[i] = vs[i]
}
return predicate.User(func(s *sql.Selector) {
// if not arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
if len(v) == 0 {
s.Where(sql.False())
return
}
s.Where(sql.In(s.C(FieldStatus), v...))
})
}
// StatusNotIn applies the NotIn predicate on the "status" field.
func StatusNotIn(vs ...Status) predicate.User {
v := make([]interface{}, len(vs))
for i := range v {
v[i] = vs[i]
}
return predicate.User(func(s *sql.Selector) {
// if not arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
if len(v) == 0 {
s.Where(sql.False())
return
}
s.Where(sql.NotIn(s.C(FieldStatus), v...))
})
}
// StatusIsNil applies the IsNil predicate on the "status" field.
func StatusIsNil() predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.IsNull(s.C(FieldStatus)))
})
}
// StatusNotNil applies the NotNil predicate on the "status" field.
func StatusNotNil() predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.NotNull(s.C(FieldStatus)))
})
}
// HasCar applies the HasEdge predicate on the "car" edge.
func HasCar() predicate.User {
return predicate.User(func(s *sql.Selector) {