Skip to content

Commit

Permalink
Change ClientDeactivateThreshold type to string value (yorkie-team#471)
Browse files Browse the repository at this point in the history
Change ClientDeactivateThreshold type to string to remove necessary
type conversation in server. This will also ease client to get/update
ClientDeactivateThreshold via time duration string format (ex: 1h30m)
  • Loading branch information
krapie authored Feb 21, 2023
1 parent c5b9a90 commit 923b398
Show file tree
Hide file tree
Showing 29 changed files with 332 additions and 250 deletions.
8 changes: 4 additions & 4 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package converter

import (
"fmt"
gotime "time"

protoTypes "github.com/gogo/protobuf/types"

Expand Down Expand Up @@ -69,12 +68,13 @@ func FromProject(pbProject *api.Project) (*types.Project, error) {
if err != nil {
return nil, fmt.Errorf("convert updatedAt to timestamp: %w", err)
}

return &types.Project{
ID: types.ID(pbProject.Id),
Name: pbProject.Name,
AuthWebhookURL: pbProject.AuthWebhookUrl,
AuthWebhookMethods: pbProject.AuthWebhookMethods,
ClientDeactivateThreshold: gotime.Duration(pbProject.ClientDeactivateThreshold),
ClientDeactivateThreshold: pbProject.ClientDeactivateThreshold,
PublicKey: pbProject.PublicKey,
SecretKey: pbProject.SecretKey,
CreatedAt: createdAt,
Expand Down Expand Up @@ -665,8 +665,8 @@ func FromUpdatableProjectFields(pbProjectFields *api.UpdatableProjectFields) (*t
if pbProjectFields.AuthWebhookMethods != nil {
updatableProjectFields.AuthWebhookMethods = &pbProjectFields.AuthWebhookMethods.Methods
}
if pbProjectFields.ClientDeactivateThreshold != 0 {
updatableProjectFields.ClientDeactivateThreshold = gotime.Duration(pbProjectFields.ClientDeactivateThreshold)
if pbProjectFields.ClientDeactivateThreshold != nil {
updatableProjectFields.ClientDeactivateThreshold = &pbProjectFields.ClientDeactivateThreshold.Value
}

return updatableProjectFields, nil
Expand Down
8 changes: 5 additions & 3 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func ToProject(project *types.Project) (*api.Project, error) {
Name: project.Name,
AuthWebhookUrl: project.AuthWebhookURL,
AuthWebhookMethods: project.AuthWebhookMethods,
ClientDeactivateThreshold: int64(project.ClientDeactivateThreshold),
ClientDeactivateThreshold: project.ClientDeactivateThreshold,
PublicKey: project.PublicKey,
SecretKey: project.SecretKey,
CreatedAt: pbCreatedAt,
Expand Down Expand Up @@ -515,8 +515,10 @@ func ToUpdatableProjectFields(fields *types.UpdatableProjectFields) (*api.Updata
} else {
pbUpdatableProjectFields.AuthWebhookMethods = nil
}
if fields.ClientDeactivateThreshold != 0 {
pbUpdatableProjectFields.ClientDeactivateThreshold = int64(fields.ClientDeactivateThreshold)
if fields.ClientDeactivateThreshold != nil {
pbUpdatableProjectFields.ClientDeactivateThreshold = &protoTypes.StringValue{
Value: *fields.ClientDeactivateThreshold,
}
}
return pbUpdatableProjectFields, nil
}
12 changes: 12 additions & 0 deletions api/types/fields_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
containNumberRegexString = `[0-9]`
containSpecialCharRegexString = `[\{\}\[\]\/?.,;:|\)*~!^\-_+<>@\#$%&\\\=\(\'\"\x60]`
alphaNumberSpecialCharRegexString = `^[a-zA-Z0-9\{\}\[\]\/?.,;:|\)*~!^\-_+<>@\#$%&\\\=\(\'\"\x60]+$`
timeDurationFormatRegexString = `^(\d{1,2}h\s?)?(\d{1,2}m\s?)?(\d{1,2}s)?$`
)

var (
Expand All @@ -44,6 +45,7 @@ var (
containNumberRegex = regexp.MustCompile(containNumberRegexString)
containSpecialCharRegex = regexp.MustCompile(containSpecialCharRegexString)
alphaNumberSpecialCharRegex = regexp.MustCompile(alphaNumberSpecialCharRegexString)
timeDurationFormatRegex = regexp.MustCompile(timeDurationFormatRegexString)
)

func isReservedProjectName(name string) bool {
Expand Down Expand Up @@ -74,6 +76,10 @@ func hasAlphaNumSpecial(str string) bool {
return isValid
}

func isValidTimeDurationStringFormat(str string) bool {
return timeDurationFormatRegex.MatchString(str)
}

func init() {
registerValidation("slug", func(level validator.FieldLevel) bool {
val := level.Field().String()
Expand Down Expand Up @@ -109,4 +115,10 @@ func init() {
return val == ""
})
registerTranslation("url|emptystring", "{0} must be a valid URL")

registerValidation("duration", func(level validator.FieldLevel) bool {
val := level.Field().String()
return isValidTimeDurationStringFormat(val)
})
registerTranslation("duration", "{0} must be a valid time duration string format")
}
2 changes: 1 addition & 1 deletion api/types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Project struct {

// ClientDeactivateThreshold is the time after which clients in
// specific project are considered deactivate for housekeeping.
ClientDeactivateThreshold time.Duration `bson:"client_deactivate_threshold"`
ClientDeactivateThreshold string `bson:"client_deactivate_threshold"`

// PublicKey is the API key of this project.
PublicKey string `json:"public_key"`
Expand Down
5 changes: 2 additions & 3 deletions api/types/updatable_project_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package types

import (
"errors"
"time"

"github.com/go-playground/validator/v10"
)
Expand All @@ -39,12 +38,12 @@ type UpdatableProjectFields struct {
AuthWebhookMethods *[]string `bson:"auth_webhook_methods,omitempty" validate:"omitempty,invalid_webhook_method"`

// ClientDeactivateThreshold is the time after which clients in specific project are considered deactivate.
ClientDeactivateThreshold time.Duration `bson:"client_deactivate_threshold,omitempty" validate:"omitempty"`
ClientDeactivateThreshold *string `bson:"client_deactivate_threshold,omitempty" validate:"omitempty,min=2,duration"`
}

// Validate validates the UpdatableProjectFields.
func (i *UpdatableProjectFields) Validate() error {
if i.Name == nil && i.AuthWebhookURL == nil && i.AuthWebhookMethods == nil && i.ClientDeactivateThreshold == 0 {
if i.Name == nil && i.AuthWebhookURL == nil && i.AuthWebhookMethods == nil && i.ClientDeactivateThreshold == nil {
return ErrEmptyProjectFields
}

Expand Down
9 changes: 4 additions & 5 deletions api/types/updatable_project_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package types_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand All @@ -34,12 +33,12 @@ func TestUpdatableProjectFields(t *testing.T) {
string(types.AttachDocument),
string(types.WatchDocuments),
}
newClientDeactivateThreshold := 1 * time.Hour
newClientDeactivateThreshold := "1h"
fields := &types.UpdatableProjectFields{
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
AuthWebhookMethods: &newAuthWebhookMethods,
ClientDeactivateThreshold: newClientDeactivateThreshold,
ClientDeactivateThreshold: &newClientDeactivateThreshold,
}
assert.NoError(t, fields.Validate())

Expand All @@ -54,7 +53,7 @@ func TestUpdatableProjectFields(t *testing.T) {
fields = &types.UpdatableProjectFields{
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
ClientDeactivateThreshold: newClientDeactivateThreshold,
ClientDeactivateThreshold: &newClientDeactivateThreshold,
}
assert.NoError(t, fields.Validate())

Expand All @@ -66,7 +65,7 @@ func TestUpdatableProjectFields(t *testing.T) {
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
AuthWebhookMethods: &newAuthWebhookMethods,
ClientDeactivateThreshold: newClientDeactivateThreshold,
ClientDeactivateThreshold: &newClientDeactivateThreshold,
}
assert.ErrorAs(t, fields.Validate(), &invalidFieldsError)
})
Expand Down
Loading

0 comments on commit 923b398

Please sign in to comment.