protoc-gen-gorm
protoc-gen-gorm copied to clipboard
Patch logic maybe not work for the message that contains a enum?
PATCH localhost:8081/api/v1/limiters/3
Content-Type: application/json
{
"status": "STATUS_ACTIVE"
}
func DefaultPatchLimiter(ctx context.Context, in *Limiter, updateMask *field_mask.FieldMask, db *gorm.DB) (*Limiter, error) {
if in == nil {
return nil, errors.NilArgumentError
}
var pbObj Limiter
var err error
if hook, ok := interface{}(&pbObj).(LimiterWithBeforePatchRead); ok {
if db, err = hook.BeforePatchRead(ctx, in, updateMask, db); err != nil {
return nil, err
}
}
pbReadRes, err := DefaultReadLimiter(ctx, &Limiter{Id: in.GetId()}, db)
if err != nil {
return nil, err
}
func DefaultReadLimiter(ctx context.Context, in *Limiter, db *gorm.DB) (*Limiter, error) {
if in == nil {
return nil, errors.NilArgumentError
}
ormObj, err := in.ToORM(ctx)
if err != nil {
return nil, err
}
if ormObj.Id == 0 {
return nil, errors.EmptyIdError
}
if hook, ok := interface{}(&ormObj).(LimiterORMWithBeforeReadApplyQuery); ok {
if db, err = hook.BeforeReadApplyQuery(ctx, db); err != nil {
return nil, err
}
}
if db, err = gorm1.ApplyFieldSelection(ctx, db, nil, &LimiterORM{}); err != nil {
return nil, err
}
if hook, ok := interface{}(&ormObj).(LimiterORMWithBeforeReadFind); ok {
if db, err = hook.BeforeReadFind(ctx, db); err != nil {
return nil, err
}
}
ormResponse := LimiterORM{}
if err = db.Where(&ormObj).First(&ormResponse).Error; err != nil {
return nil, err
}
DefaultPatch call the DefaultRead, and DefaultRead use the returns from ToORM as where condition, and the ToORM logic is: enum value must have a value. eg:
func (m *Limiter) ToORM(ctx context.Context) (LimiterORM, error) {
to := LimiterORM{}
var err error
if prehook, ok := interface{}(m).(LimiterWithBeforeToORM); ok {
if err = prehook.BeforeToORM(ctx, &to); err != nil {
return to, err
}
}
to.Id = m.Id
to.MatchMode = m.MatchMode
to.MatchValue = m.MatchValue
to.Query = m.Query
to.Headers = m.Headers
to.LimitQps = m.LimitQps
to.Alias = m.Alias
to.ClusterIdentifier = m.ClusterIdentifier
to.Status = Status_name[int32(m.Status)]
finnaly, the sql is SELECT * FROM
limiters WHERE (
limiters.
id = 3) AND (
limiters.
status= 'STATUS_UNSPECIFIED') ORDER BY
limiters.
id ASC LIMIT 1
.
the result is err: record not found
How to patch?