Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch logic maybe not work for the message that contains a enum? #239

Open
saltbo opened this issue Aug 5, 2022 · 0 comments
Open

Patch logic maybe not work for the message that contains a enum? #239

saltbo opened this issue Aug 5, 2022 · 0 comments

Comments

@saltbo
Copy link

saltbo commented Aug 5, 2022

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 * FROMlimiters WHERE (limiters.id = 3) AND (limiters.status= 'STATUS_UNSPECIFIED') ORDER BYlimiters.id ASC LIMIT 1.

the result is err: record not found

How to patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant