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

@@ -111,6 +111,20 @@ func (uc *UserCreate) SetNillableState(u *user.State) *UserCreate {
return uc
}
// SetStatus sets the status field.
func (uc *UserCreate) SetStatus(u user.Status) *UserCreate {
uc.mutation.SetStatus(u)
return uc
}
// SetNillableStatus sets the status field if the given value is not nil.
func (uc *UserCreate) SetNillableStatus(u *user.Status) *UserCreate {
if u != nil {
uc.SetStatus(*u)
}
return uc
}
// SetID sets the id field.
func (uc *UserCreate) SetID(i int) *UserCreate {
uc.mutation.SetID(i)
@@ -195,6 +209,11 @@ func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
return nil, &ValidationError{Name: "state", err: fmt.Errorf("entv2: validator failed for field \"state\": %w", err)}
}
}
if v, ok := uc.mutation.Status(); ok {
if err := user.StatusValidator(v); err != nil {
return nil, &ValidationError{Name: "status", err: fmt.Errorf("entv2: validator failed for field \"status\": %w", err)}
}
}
var (
err error
node *User
@@ -318,6 +337,14 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
})
u.State = value
}
if value, ok := uc.mutation.Status(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeEnum,
Value: value,
Column: user.FieldStatus,
})
u.Status = value
}
if nodes := uc.mutation.CarIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,