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

feat: Serialize duration as string, not as nanosecs, in API responses and DB #1542

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions internal/pkg/validator/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/go-playground/validator/v10"
"github.com/umisama/go-regexpcache"

"github.com/keboola/keboola-as-code/internal/pkg/service/common/duration"
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
"github.com/keboola/keboola-as-code/internal/pkg/utils/strhelper"
)
Expand Down Expand Up @@ -139,17 +140,20 @@ func (v *wrapper) registerCustomRules() {
Rule{
Tag: "minDuration",
FuncCtx: func(ctx context.Context, fl validator.FieldLevel) bool {
value, ok := fl.Field().Interface().(time.Duration)
if !ok {
var value time.Duration
if v, ok := fl.Field().Interface().(time.Duration); ok {
value = v
} else if v, ok := fl.Field().Interface().(duration.Duration); ok {
value = v.Duration()
Comment on lines +144 to +147
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I think I need a bit more info on type casting here. 🤔 With duration.Duration being defined as

type Duration time.Duration

I'd expect the first condition to pass even for duration.Duration so the second branch would never happen.

If that's not the case, what am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notes from call:

var value any = duration.Duration(123) // 123ns
var value any = int64(123)

--------------

if v, ok := value.(duration.Duration); ok {
  // ...
}

--------------

swtich v := value.(type) {
  case duration.Duration:
  case int64:
  default:
    panic(....)
}

} else {
panic(errors.Errorf(`unexpected type "%T"`, fl.Field().Interface()))
}

param, err := time.ParseDuration(fl.Param())
if !ok {
if err != nil {
panic(errors.Errorf(`param "%s" is not valid: %w`, fl.Param(), err))
}
if param == 0 {
panic(errors.Errorf(`param "%s" is not valid`, fl.Param()))
}

return value >= param
},
ErrorMsgFunc: func(fe validator.FieldError) string {
Expand All @@ -160,17 +164,20 @@ func (v *wrapper) registerCustomRules() {
Rule{
Tag: "maxDuration",
FuncCtx: func(ctx context.Context, fl validator.FieldLevel) bool {
value, ok := fl.Field().Interface().(time.Duration)
if !ok {
var value time.Duration
if v, ok := fl.Field().Interface().(time.Duration); ok {
value = v
} else if v, ok := fl.Field().Interface().(duration.Duration); ok {
value = v.Duration()
} else {
panic(errors.Errorf(`unexpected type "%T"`, fl.Field().Interface()))
}

param, err := time.ParseDuration(fl.Param())
if !ok {
if err != nil {
panic(errors.Errorf(`param "%s" is not valid: %w`, fl.Param(), err))
}
if param == 0 {
panic(errors.Errorf(`param "%s" is not valid`, fl.Param()))
}

return value <= param
},
ErrorMsgFunc: func(fe validator.FieldError) string {
Expand Down
12 changes: 12 additions & 0 deletions internal/pkg/validator/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import (

"github.com/c2h5oh/datasize"
"github.com/stretchr/testify/assert"

"github.com/keboola/keboola-as-code/internal/pkg/service/common/duration"
)

func TestValidateMinDuration(t *testing.T) {
t.Parallel()

err := New().ValidateValue(10*time.Millisecond, "minDuration=100ms")
assert.Error(t, err)
assert.Equal(t, `must be 100ms or greater`, err.Error())

err = New().ValidateValue(duration.From(10*time.Millisecond), "minDuration=100ms")
assert.Error(t, err)
assert.Equal(t, `must be 100ms or greater`, err.Error())
}

func TestValidateMinBytes(t *testing.T) {
Expand All @@ -27,10 +34,15 @@ func TestValidateMaxDuration(t *testing.T) {
err := New().ValidateValue(200*time.Millisecond, "maxDuration=100ms")
assert.Error(t, err)
assert.Equal(t, `must be 100ms or less`, err.Error())

err = New().ValidateValue(duration.From(200*time.Millisecond), "maxDuration=100ms")
assert.Error(t, err)
assert.Equal(t, `must be 100ms or less`, err.Error())
}

func TestValidateMaxBytes(t *testing.T) {
t.Parallel()

err := New().ValidateValue(datasize.ByteSize(2000), "maxBytes=1KB")
assert.Error(t, err)
assert.Equal(t, `must be 1KB or less`, err.Error())
Expand Down