Skip to content

Commit

Permalink
feat(rx): tidy type and variable names (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Apr 24, 2024
1 parent 90b7788 commit ce1d0a1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 55 deletions.
4 changes: 2 additions & 2 deletions rx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ func Range[T Numeric](iterator RangeIterator[T], opts ...Option[T]) Observable[T
}

// RangeNF creates an Observable that emits count sequential integers beginning
// at start, for non numeric types, which do contain a nominated Numeric member
func RangeNF[T NominatedField[T, O], O Numeric](iterator RangeIteratorNF[T, O],
// at start, for non numeric types, which do contain a nominated proxy Numeric member
func RangeNF[T ProxyField[T, O], O Numeric](iterator RangeIteratorPF[T, O],
opts ...Option[T],
) Observable[T] {
if err := iterator.Init(); err != nil {
Expand Down
22 changes: 11 additions & 11 deletions rx/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ var _ = Describe("Factory", func() {
})
})

When("missing StepBy", func() {
When("missing By", func() {
It("🧪 should: default to 1", func() {
defer leaktest.Check(GinkgoT())()

Expand All @@ -894,13 +894,13 @@ var _ = Describe("Factory", func() {
})
})

When("StepBy 2", func() {
When("By 2", func() {
It("🧪 should: create observable", func() {
defer leaktest.Check(GinkgoT())()

obs := rx.Range(&rx.NumericRangeIterator[int]{
StartAt: 5,
StepBy: 2,
By: 2,
Whilst: rx.LessThan(12),
})

Expand All @@ -912,13 +912,13 @@ var _ = Describe("Factory", func() {
})
})

When("StepBy 2 and reverse StepBy", func() {
When("By 2 and reverse By", func() {
It("🧪 should: create observable", func() {
defer leaktest.Check(GinkgoT())()

obs := rx.Range(&rx.NumericRangeIterator[int]{
StartAt: 11,
StepBy: -2,
By: -2,
Whilst: rx.MoreThan(4),
})

Expand Down Expand Up @@ -1012,15 +1012,15 @@ var _ = Describe("Factory", func() {
})
})

Context("custom range iterator with nominated field", func() {
Context("custom range iterator with proxy field", func() {
When("positive count", func() {
It("🧪 should: create observable", func() {
// Test_Range
defer leaktest.Check(GinkgoT())()

obs := rx.RangeNF(&widgetByIDRangeIterator{
StartAt: widget{id: 5},
StepBy: widget{id: 1},
By: widget{id: 1},
Whilst: widgetLessThan(widget{id: 8}),
})

Expand All @@ -1037,16 +1037,16 @@ var _ = Describe("Factory", func() {
})
})

Context("NominatedRangeIterator", func() {
Context("ProxyRangeIterator", func() {
When("positive count", func() {
It("🧪 should: create observable", func() {
// Test_Range
defer leaktest.Check(GinkgoT())()

obs := rx.RangeNF(&rx.NominatedRangeIterator[widget, int]{
obs := rx.RangeNF(&rx.ProxyRangeIterator[widget, int]{
StartAt: widget{id: 5},
StepBy: widget{id: 1},
Whilst: rx.LessThanNF(widget{id: 8}),
By: widget{id: 1},
Whilst: rx.LessThanPF(widget{id: 8}),
})

rx.Assert(context.Background(), obs,
Expand Down
55 changes: 25 additions & 30 deletions rx/iterable-range.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ package rx
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// type rangeIterableL[T any] struct {
// start, count NumVal
// opts []Option[T]
// }

type rangeIterable[T any] struct {
start, count NumVal
iterator RangeIterator[T]
Expand Down Expand Up @@ -59,21 +54,21 @@ func (i *rangeIterable[T]) Observe(opts ...Option[T]) <-chan Item[T] {
return next
}

type rangeIterableNF[T NominatedField[T, O], O Numeric] struct {
iterator RangeIteratorNF[T, O]
type rangeIterablePF[T ProxyField[T, O], O Numeric] struct {
iterator RangeIteratorPF[T, O]
opts []Option[T]
}

func newRangeIterableNF[T NominatedField[T, O], O Numeric](iterator RangeIteratorNF[T, O],
func newRangeIterableNF[T ProxyField[T, O], O Numeric](iterator RangeIteratorPF[T, O],
opts ...Option[T],
) Iterable[T] {
return &rangeIterableNF[T, O]{
return &rangeIterablePF[T, O]{
iterator: iterator,
opts: opts,
}
}

func (i *rangeIterableNF[T, O]) Observe(opts ...Option[T]) <-chan Item[T] {
func (i *rangeIterablePF[T, O]) Observe(opts ...Option[T]) <-chan Item[T] {
option := parseOptions(append(i.opts, opts...)...)
ctx := option.buildContext(emptyContext)
next := option.buildChannel()
Expand Down Expand Up @@ -113,7 +108,7 @@ func Count[T Numeric](count T) WhilstFunc[T] {

type NumericRangeIterator[T Numeric] struct {
StartAt T
StepBy T
By T
Whilst WhilstFunc[T]
zero T
}
Expand All @@ -126,11 +121,11 @@ func (i *NumericRangeIterator[T]) Init() error {
return nil
}

// Start should return the initial index value. If the StepBy has
// Start should return the initial index value. If the By value has
// not been set, it will default to 1.
func (i *NumericRangeIterator[T]) Start() (*T, error) {
if i.StepBy == 0 {
i.StepBy = 1
if i.By == 0 {
i.By = 1
}

if i.Whilst == nil {
Expand All @@ -141,12 +136,12 @@ func (i *NumericRangeIterator[T]) Start() (*T, error) {
}

func (i *NumericRangeIterator[T]) Step() T {
return i.StepBy
return i.By
}

// Increment increments the index value
func (i *NumericRangeIterator[T]) Increment(index *T) T {
*(index) += i.StepBy
*(index) += i.By

return *(index)
}
Expand All @@ -157,26 +152,26 @@ func (i *NumericRangeIterator[T]) While(current T) bool {
return i.Whilst(current)
}

type NominatedRangeIterator[T NominatedField[T, O], O Numeric] struct {
type ProxyRangeIterator[T ProxyField[T, O], O Numeric] struct {
StartAt T
StepBy T
By T
Whilst WhilstFunc[T]
zero T
}

func (i *NominatedRangeIterator[T, O]) Init() error {
func (i *ProxyRangeIterator[T, O]) Init() error {
if i.Whilst == nil {
return RangeMissingWhilstError
}

return nil
}

// Start should return the initial index value. If the StepBy has
// Start should return the initial index value. If By has
// not been set, a panic occurs
func (i *NominatedRangeIterator[T, O]) Start() (*T, error) {
if i.StepBy.Field() == 0 {
panic("bad step-by, can't be zero")
func (i *ProxyRangeIterator[T, O]) Start() (*T, error) {
if i.By.Field() == 0 {
panic("bad by value, can't be zero")
}

if i.Whilst == nil {
Expand All @@ -188,12 +183,12 @@ func (i *NominatedRangeIterator[T, O]) Start() (*T, error) {
return &index, nil
}

func (i *NominatedRangeIterator[T, O]) Step() O {
return i.StepBy.Field()
func (i *ProxyRangeIterator[T, O]) Step() O {
return i.By.Field()
}

// Increment increments index value
func (i *NominatedRangeIterator[T, O]) Increment(index *T) *T {
func (i *ProxyRangeIterator[T, O]) Increment(index *T) *T {
// This does look a bit strange but its a work around
// for the fact that the instance of T is implemented with
// non-pointer receivers and therefore can't make modifications
Expand All @@ -205,24 +200,24 @@ func (i *NominatedRangeIterator[T, O]) Increment(index *T) *T {
// index receives a pointer to a copy of itself, via Increment
// and increments the copy.
//
(*index).Inc(index, i.StepBy)
(*index).Inc(index, i.By)

return index
}

// While defines a condition that must be true for the loop to
// continue iterating.
func (i *NominatedRangeIterator[T, O]) While(current T) bool {
func (i *ProxyRangeIterator[T, O]) While(current T) bool {
return i.Whilst(current)
}

func LessThanNF[T NominatedField[T, O], O Numeric](until T) WhilstFunc[T] {
func LessThanPF[T ProxyField[T, O], O Numeric](until T) WhilstFunc[T] {
return func(current T) bool {
return current.Field() < until.Field()
}
}

func MoreThanNF[T NominatedField[T, O], O Numeric](until T) WhilstFunc[T] {
func MoreThanPF[T ProxyField[T, O], O Numeric](until T) WhilstFunc[T] {
return func(current T) bool {
return current.Field() > until.Field()
}
Expand Down
6 changes: 3 additions & 3 deletions rx/observable-operator-groupby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _ = Describe("Observable operator", func() {
count := 11
obs := rx.Range(&rx.NumericRangeIterator[int]{
StartAt: 0,
StepBy: 1,
By: 1,
Whilst: rx.LessThan(count),
}).GroupBy(length, func(item rx.Item[int]) int {
return item.V % length
Expand Down Expand Up @@ -96,7 +96,7 @@ var _ = Describe("Observable operator", func() {

obs := rx.Range(&rx.NumericRangeIterator[int]{
StartAt: 0,
StepBy: 1,
By: 1,
Whilst: rx.LessThan(count),
}).GroupByDynamic(func(item rx.Item[int]) string {
if item.V == 10 {
Expand Down Expand Up @@ -163,7 +163,7 @@ var _ = Describe("Observable operator", func() {

obs := rx.Range(&rx.NumericRangeIterator[int]{
StartAt: 0,
StepBy: 1,
By: 1,
Whilst: rx.LessThan(count),
}).GroupBy(length, func(_ rx.Item[int]) int {
return 4
Expand Down
4 changes: 2 additions & 2 deletions rx/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ type (
While(current T) bool
}

NominatedField[T any, O Numeric] interface {
ProxyField[T any, O Numeric] interface {
Field() O
Inc(index *T, by T) *T
Index(int) *T
}

RangeIteratorNF[T NominatedField[T, O], O Numeric] interface {
RangeIteratorPF[T ProxyField[T, O], O Numeric] interface {
Init() error
// Start should return the initial index value
Start() (*T, error)
Expand Down
15 changes: 8 additions & 7 deletions rx/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,24 @@ func (w widget) Index(i int) *widget {

type widgetByIDRangeIterator struct {
StartAt widget
StepBy widget
By widget
Whilst func(current widget) bool
zero widget
}

func (i *widgetByIDRangeIterator) Init() error {
return nil
}

// Start should return the initial index value. If the StepBy has
// Start should return the initial index value. If the By value has
// not been set, it will default to 1.
func (i *widgetByIDRangeIterator) Start() (*widget, error) {
if i.StepBy.id == 0 {
i.StepBy = widget{id: 1}
if i.By.id == 0 {
i.By = widget{id: 1}
}

if i.Whilst == nil {
return &widget{}, rx.BadRangeIteratorError{}
return &i.zero, rx.BadRangeIteratorError{}
}

index := i.StartAt
Expand All @@ -144,12 +145,12 @@ func (i *widgetByIDRangeIterator) Start() (*widget, error) {
}

func (i *widgetByIDRangeIterator) Step() int {
return i.StepBy.id
return i.By.id
}

// Increment returns a pointer to a new instance of with incremented index value
func (i *widgetByIDRangeIterator) Increment(index *widget) *widget {
index.id += i.StepBy.id
index.id += i.By.id

return index
}
Expand Down

0 comments on commit ce1d0a1

Please sign in to comment.