Skip to content

Commit

Permalink
feat: builder 使用常用用法
Browse files Browse the repository at this point in the history
close #13
  • Loading branch information
mohuishou committed Jan 30, 2024
1 parent 05067cb commit 78c39f3
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions 03_builder/builder.go
Expand Up @@ -18,50 +18,63 @@ type ResourcePoolConfig struct {

// ResourcePoolConfigBuilder 用于构建 ResourcePoolConfig
type ResourcePoolConfigBuilder struct {
err error
name string
maxTotal int
maxIdle int
minIdle int
}

// SetName SetName
func (b *ResourcePoolConfigBuilder) SetName(name string) error {
func (b *ResourcePoolConfigBuilder) SetName(name string) {
if b.err != nil {
return
}
if name == "" {
return fmt.Errorf("name can not be empty")
b.err = fmt.Errorf("name can not be empty")
}
b.name = name
return nil
}

// SetMinIdle SetMinIdle
func (b *ResourcePoolConfigBuilder) SetMinIdle(minIdle int) error {
func (b *ResourcePoolConfigBuilder) SetMinIdle(minIdle int) {
if b.err != nil {
return
}
if minIdle < 0 {
return fmt.Errorf("max tatal cannot < 0, input: %d", minIdle)
b.err = fmt.Errorf("max tatal cannot < 0, input: %d", minIdle)
}
b.minIdle = minIdle
return nil
}

// SetMaxIdle SetMaxIdle
func (b *ResourcePoolConfigBuilder) SetMaxIdle(maxIdle int) error {
func (b *ResourcePoolConfigBuilder) SetMaxIdle(maxIdle int) {
if b.err != nil {
return
}
if maxIdle < 0 {
return fmt.Errorf("max tatal cannot < 0, input: %d", maxIdle)
b.err = fmt.Errorf("max tatal cannot < 0, input: %d", maxIdle)
}
b.maxIdle = maxIdle
return nil
}

// SetMaxTotal SetMaxTotal
func (b *ResourcePoolConfigBuilder) SetMaxTotal(maxTotal int) error {
func (b *ResourcePoolConfigBuilder) SetMaxTotal(maxTotal int) {
if b.err != nil {
return
}
if maxTotal <= 0 {
return fmt.Errorf("max tatal cannot <= 0, input: %d", maxTotal)
b.err = fmt.Errorf("max tatal cannot <= 0, input: %d", maxTotal)
}
b.maxTotal = maxTotal
return nil
}

// Build Build
func (b *ResourcePoolConfigBuilder) Build() (*ResourcePoolConfig, error) {
if b.err != nil {
return nil, b.err
}

if b.name == "" {
return nil, fmt.Errorf("name can not be empty")
}
Expand Down

0 comments on commit 78c39f3

Please sign in to comment.