Skip to content

Commit 34239fe

Browse files
committed
add counter reset in config
1 parent a33344d commit 34239fe

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

pkg/genlib/config/config.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,55 @@ type Config struct {
3939
}
4040

4141
type ConfigField struct {
42-
Name string `config:"name"`
43-
Fuzziness float64 `config:"fuzziness"`
44-
Range Range `config:"range"`
45-
Cardinality int `config:"cardinality"`
46-
Period time.Duration `config:"period"`
47-
Enum []string `config:"enum"`
48-
ObjectKeys []string `config:"object_keys"`
49-
Value any `config:"value"`
50-
Counter bool `config:"counter"`
42+
Name string `config:"name"`
43+
Fuzziness float64 `config:"fuzziness"`
44+
Range Range `config:"range"`
45+
Cardinality int `config:"cardinality"`
46+
Period time.Duration `config:"period"`
47+
Enum []string `config:"enum"`
48+
ObjectKeys []string `config:"object_keys"`
49+
Value any `config:"value"`
50+
Counter bool `config:"counter"`
51+
CounterReset *CounterReset `config:"counter_reset"`
52+
}
53+
54+
const (
55+
CounterResetStrategyRandom string = "random"
56+
CounterResetStrategyProbabilistic string = "probabilistic"
57+
CounterResetStrategyAfterN string = "after_n"
58+
)
59+
60+
type CounterReset struct {
61+
Strategy string `config:"strategy"`
62+
Probability *uint64 `config:"probability"`
63+
ResetAfterN *uint64 `config:"reset_after_n"`
64+
}
65+
66+
func (cf ConfigField) ValidateCounterResetStrategy() error {
67+
if cf.Counter && cf.CounterReset != nil &&
68+
cf.CounterReset.Strategy != CounterResetStrategyRandom &&
69+
cf.CounterReset.Strategy != CounterResetStrategyProbabilistic &&
70+
cf.CounterReset.Strategy != CounterResetStrategyAfterN {
71+
return errors.New("counter_reset strategy must be one of 'random', 'probabilistic', 'after_n'")
72+
}
73+
74+
return nil
75+
}
76+
77+
func (cf ConfigField) ValidateCounterResetAfterN() error {
78+
if cf.Counter && cf.CounterReset != nil && cf.CounterReset.Strategy == CounterResetStrategyAfterN && cf.CounterReset.ResetAfterN == nil {
79+
return errors.New("counter_reset after_n requires 'reset_after_n' value to be set")
80+
}
81+
82+
return nil
83+
}
84+
85+
func (cf ConfigField) ValidateCounterResetProbabilistic() error {
86+
if cf.Counter && cf.CounterReset != nil && cf.CounterReset.Strategy == CounterResetStrategyProbabilistic && cf.CounterReset.Probability == nil {
87+
return errors.New("counter_reset probabilistic requires 'probability' value to be set")
88+
}
89+
90+
return nil
5191
}
5292

5393
func (cf ConfigField) ValidForDateField() error {

0 commit comments

Comments
 (0)