Files
godoxy-yusing/internal/logging/accesslog/retention.go
yusing 6da7227f9b refactor(errs): migrate from gperr.Error to standard Go error interface
This is a large-scale refactoring across the codebase that replaces the custom
`gperr.Error` type with Go's standard `error` interface. The changes include:

- Replacing `gperr.Error` return types with `error` in function signatures
- Using `errors.New()` and `fmt.Errorf()` instead of `gperr.New()` and `gperr.Errorf()`
- Using `%w` format verb for error wrapping instead of `.With()` method
- Replacing `gperr.Subject()` calls with `gperr.PrependSubject()`
- Converting error logging from `gperr.Log*()` functions to zerolog's `.Err().Msg()` pattern
- Update NewLogger to handle multiline error message
- Updating `goutils` submodule to latest commit

This refactoring aligns with Go idioms and removes the dependency on
custom error handling abstractions in favor of standard library patterns.
2026-02-08 12:07:36 +08:00

95 lines
1.8 KiB
Go

package accesslog
import (
"errors"
"fmt"
"strconv"
strutils "github.com/yusing/goutils/strings"
)
type Retention struct {
Days uint64 `json:"days,omitempty"`
Last uint64 `json:"last,omitempty"`
KeepSize uint64 `json:"keep_size,omitempty"`
} // @name LogRetention
var (
ErrInvalidSyntax = errors.New("invalid syntax")
ErrZeroValue = errors.New("zero value")
)
// see back_scanner_test.go#L210 for benchmarks
var defaultChunkSize = 32 * kilobyte
// Syntax:
//
// <N> days|weeks|months
//
// last <N>
//
// <N> KB|MB|GB|kb|mb|gb
//
// Parse implements strutils.Parser.
func (r *Retention) Parse(v string) (err error) {
split := strutils.SplitSpace(v)
if len(split) != 2 {
return fmt.Errorf("%w: %s", ErrInvalidSyntax, v)
}
switch split[0] {
case "last":
r.Last, err = strconv.ParseUint(split[1], 10, 64)
default: // <N> days|weeks|months
n, err := strconv.ParseUint(split[0], 10, 64)
if err != nil {
return err
}
switch split[1] {
case "day", "days":
r.Days = n
case "week", "weeks":
r.Days = n * 7
case "month", "months":
r.Days = n * 30
case "kb", "Kb":
r.KeepSize = n * kilobits
case "KB":
r.KeepSize = n * kilobyte
case "mb", "Mb":
r.KeepSize = n * megabits
case "MB":
r.KeepSize = n * megabyte
case "gb", "Gb":
r.KeepSize = n * gigabits
case "GB":
r.KeepSize = n * gigabyte
default:
return fmt.Errorf("%w: unit %s", ErrInvalidSyntax, split[1])
}
}
if !r.IsValid() {
return ErrZeroValue
}
return err
}
func (r *Retention) String() string {
if r.Days > 0 {
return fmt.Sprintf("%d days", r.Days)
}
if r.Last > 0 {
return fmt.Sprintf("last %d", r.Last)
}
if r.KeepSize > 0 {
return strutils.FormatByteSize(r.KeepSize)
}
return "<invalid>"
}
func (r *Retention) IsValid() bool {
if r == nil {
return false
}
return r.Days > 0 || r.Last > 0 || r.KeepSize > 0
}