-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(varlogtest): add functional options to varlogtest
- Loading branch information
Showing
3 changed files
with
97 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package varlogtest | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kakao/varlog/pkg/types" | ||
) | ||
|
||
type config struct { | ||
clusterID types.ClusterID | ||
replicationFactor int | ||
} | ||
|
||
func newConfig(opts []Option) (config, error) { | ||
cfg := config{} | ||
for _, opt := range opts { | ||
opt.apply(&cfg) | ||
} | ||
if err := cfg.validate(); err != nil { | ||
return config{}, err | ||
} | ||
return cfg, nil | ||
} | ||
|
||
func (cfg *config) validate() error { | ||
if cfg.clusterID.Invalid() { | ||
return fmt.Errorf("invalid cluster id %d", cfg.clusterID) | ||
} | ||
if cfg.replicationFactor < 1 { | ||
return fmt.Errorf("invalid replication factor %d", cfg.replicationFactor) | ||
} | ||
return nil | ||
} | ||
|
||
type Option interface { | ||
apply(*config) | ||
} | ||
|
||
type funcOption struct { | ||
f func(*config) | ||
} | ||
|
||
func newFuncOption(f func(*config)) *funcOption { | ||
return &funcOption{f: f} | ||
} | ||
|
||
func (fo *funcOption) apply(cfg *config) { | ||
fo.f(cfg) | ||
} | ||
|
||
func WithClusterID(cid types.ClusterID) Option { | ||
return newFuncOption(func(cfg *config) { | ||
cfg.clusterID = cid | ||
}) | ||
} | ||
|
||
func WithReplicationFactor(repfactor int) Option { | ||
return newFuncOption(func(cfg *config) { | ||
cfg.replicationFactor = repfactor | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters