Skip to content

Commit

Permalink
Allow zero id option
Browse files Browse the repository at this point in the history
  • Loading branch information
evsamsonov committed Nov 3, 2023
1 parent 93cfb1e commit 6d40bff
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions tickseries/tick_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@ var (

// TickSeries represents TickSeries of trading ticks
type TickSeries struct {
ticks []*Tick
ids map[int64]struct{}
ticks []*Tick
ids map[int64]struct{}
allowZeroID bool
}

type Option func(*TickSeries)

func WithAllowZeroIDOption(allowZeroID bool) Option {
return func(ts *TickSeries) {
ts.allowZeroID = allowZeroID
}
}

// New creates and returns new TickSeries
func New() *TickSeries {
return &TickSeries{
func New(opts ...Option) *TickSeries {
ts := &TickSeries{
ticks: make([]*Tick, 0),
ids: make(map[int64]struct{}),
}
for _, opt := range opts {
opt(ts)
}
return ts
}

// Add adds a trading tick to the TickSeries. It allows to add
Expand All @@ -32,8 +45,10 @@ func (t *TickSeries) Add(tick *Tick) error {
return ErrCannotBeNil
}

if _, ok := t.ids[tick.ID]; ok {
return ErrAlreadyExist
if !t.allowZeroID {
if _, ok := t.ids[tick.ID]; ok {
return ErrAlreadyExist
}
}

last := t.Last()
Expand Down

0 comments on commit 6d40bff

Please sign in to comment.