Skip to content

Commit

Permalink
feat(server/config): 添加 Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed May 31, 2024
1 parent 1f01461 commit c791170
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
25 changes: 14 additions & 11 deletions server/config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ type (

tlsConfig *tls.Config

ReadTimeout duration `yaml:"readTimeout,omitempty" json:"readTimeout,omitempty" xml:"readTimeout,attr,omitempty"`
WriteTimeout duration `yaml:"writeTimeout,omitempty" json:"writeTimeout,omitempty" xml:"writeTimeout,attr,omitempty"`
IdleTimeout duration `yaml:"idleTimeout,omitempty" json:"idleTimeout,omitempty" xml:"idleTimeout,attr,omitempty"`
ReadHeaderTimeout duration `yaml:"readHeaderTimeout,omitempty" json:"readHeaderTimeout,omitempty" xml:"readHeaderTimeout,attr,omitempty"`
ReadTimeout Duration `yaml:"readTimeout,omitempty" json:"readTimeout,omitempty" xml:"readTimeout,attr,omitempty"`
WriteTimeout Duration `yaml:"writeTimeout,omitempty" json:"writeTimeout,omitempty" xml:"writeTimeout,attr,omitempty"`
IdleTimeout Duration `yaml:"idleTimeout,omitempty" json:"idleTimeout,omitempty" xml:"idleTimeout,attr,omitempty"`
ReadHeaderTimeout Duration `yaml:"readHeaderTimeout,omitempty" json:"readHeaderTimeout,omitempty" xml:"readHeaderTimeout,attr,omitempty"`
MaxHeaderBytes int `yaml:"maxHeaderBytes,omitempty" json:"maxHeaderBytes,omitempty" xml:"maxHeaderBytes,attr,omitempty"`

// Recovery 拦截 panic 时反馈给客户端的状态码
Expand Down Expand Up @@ -90,8 +90,10 @@ type (
httpServer *http.Server
}

// 表示时间段,等同于 [time.Duration]
duration time.Duration // 封装 [time.Duration] 以实现对 JSON、XML 和 YAML 的解析
// Duration 表示时间段
//
// 封装 [time.Duration] 以实现对 JSON、XML 和 YAML 的解析
Duration time.Duration

headerConfig struct {
// 报头名称
Expand Down Expand Up @@ -168,7 +170,7 @@ var (
return err == nil || errors.Is(err, fs.ErrExist)
}, locales.NotFound)

durShouldGreatThan0 = filter.V(func(v duration) bool { return v >= 0 }, locales.ShouldGreatThan(0))
durShouldGreatThan0 = filter.V(func(v Duration) bool { return v >= 0 }, locales.ShouldGreatThan(0))
)

func (cert *certificateConfig) sanitize() *web.FieldError {
Expand Down Expand Up @@ -311,14 +313,15 @@ func (l *acmeConfig) sanitize() *web.FieldError {
)
}

func (d duration) Duration() time.Duration { return time.Duration(d) }
// Duration 转换为标准库的 [time.Duration]
func (d Duration) Duration() time.Duration { return time.Duration(d) }

func (d duration) MarshalText() ([]byte, error) { return []byte(time.Duration(d).String()), nil }
func (d Duration) MarshalText() ([]byte, error) { return []byte(time.Duration(d).String()), nil }

func (d *duration) UnmarshalText(b []byte) error {
func (d *Duration) UnmarshalText(b []byte) error {
v, err := time.ParseDuration(string(b))
if err == nil {
*d = duration(v)
*d = Duration(v)
}
return err
}
18 changes: 9 additions & 9 deletions server/config/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,27 @@ func TestACME_sanitize(t *testing.T) {
var (
dur time.Duration

_ encoding.TextMarshaler = duration(1)
_ encoding.TextUnmarshaler = (*duration)(&dur)
_ encoding.TextMarshaler = Duration(1)
_ encoding.TextUnmarshaler = (*Duration)(&dur)
)

type testDuration struct {
Duration duration `xml:"dur" json:"dur" yaml:"dur"`
Duration Duration `xml:"dur" json:"dur" yaml:"dur"`
}

func TestDuration_Duration(t *testing.T) {
a := assert.New(t, false)

dur := time.Second * 2

a.Equal(dur, duration(dur).Duration())
a.Equal(dur, Duration(dur).Duration())
}

func TestDuration_YAML(t *testing.T) {
a := assert.New(t, false)

m := &testDuration{
Duration: duration(time.Nanosecond * 5),
Duration: Duration(time.Nanosecond * 5),
}

bs, err := yaml.Marshal(m)
Expand All @@ -143,7 +143,7 @@ func TestDuration_XML(t *testing.T) {
a := assert.New(t, false)

m := &testDuration{
Duration: duration(time.Nanosecond * 5),
Duration: Duration(time.Nanosecond * 5),
}

bs, err := xml.MarshalIndent(m, "", " ")
Expand All @@ -160,10 +160,10 @@ func TestDuration_XMLAttr(t *testing.T) {
a := assert.New(t, false)

type obj struct {
D duration `xml:"d,attr"`
D Duration `xml:"d,attr"`
}
m := &obj{
D: duration(time.Nanosecond * 5),
D: Duration(time.Nanosecond * 5),
}

bs, err := xml.MarshalIndent(m, "", " ")
Expand All @@ -177,7 +177,7 @@ func TestDuration_JSON(t *testing.T) {
a := assert.New(t, false)

m := &testDuration{
Duration: duration(time.Nanosecond * 5),
Duration: Duration(time.Nanosecond * 5),
}

bs, err := json.Marshal(m)
Expand Down

0 comments on commit c791170

Please sign in to comment.