Skip to content

Commit

Permalink
Remove generics on WrappingUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
jwillp committed Sep 12, 2024
1 parent f0a73ca commit 76c4b58
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
22 changes: 12 additions & 10 deletions pkg/specter/unitloading.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ type Unit interface {
//
// wrapped := Spectre.UnitOf(myValue)
// unwrapped := wrapped.Unwrap()
type WrappingUnit[T any] struct {
type WrappingUnit struct {
id UnitID
kind UnitKind
source Source
wrapped T
wrapped any
}

func UnitOf[T any](v T, id UnitID, kind UnitKind, source Source) *WrappingUnit[T] {
return &WrappingUnit[T]{
func UnitOf(v any, id UnitID, kind UnitKind, source Source) *WrappingUnit {
return &WrappingUnit{
id: id,
kind: kind,
source: source,
Expand All @@ -67,28 +67,30 @@ func UnitOf[T any](v T, id UnitID, kind UnitKind, source Source) *WrappingUnit[T
}

func UnwrapUnit[T any](unit Unit) (value T, ok bool) {
w, ok := unit.(*WrappingUnit[T])
w, ok := unit.(*WrappingUnit)
if !ok {
return value, false
}

return w.wrapped, true
v, ok := w.wrapped.(T)

return v, ok
}

func (w *WrappingUnit[T]) ID() UnitID {
func (w *WrappingUnit) ID() UnitID {
return w.id
}

func (w *WrappingUnit[T]) Kind() UnitKind {
func (w *WrappingUnit) Kind() UnitKind {
return w.kind
}

func (w *WrappingUnit[T]) Source() Source {
func (w *WrappingUnit) Source() Source {
return w.source
}

// Unwrap returns the wrapped value.
func (w *WrappingUnit[T]) Unwrap() T {
func (w *WrappingUnit) Unwrap() any {
return w.wrapped
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/specter/unitloading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,28 +412,28 @@ func TestUnitLoaderAdapter(t *testing.T) {
}

func TestUnwrapUnit(t *testing.T) {
type then[T any] struct {
value T
type then struct {
value any
ok bool
}
type testCase[T any] struct {
type testCase struct {
name string
when specter.Unit
then then[T]
then then
}
tests := []testCase[string]{
tests := []testCase{
{
name: "Unwrap of non wrapped unit should return zero value and false",
when: testutils.NewUnitStub("id", "kind", specter.Source{}),
then: then[string]{
then: then{
value: "",
ok: false,
},
},
{
name: "Unwrap of a wrapped unit should return the value and true",
when: specter.UnitOf("hello", "id", "kind", specter.Source{}),
then: then[string]{
then: then{
value: "hello",
ok: true,
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/testutils/unitproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import (
)

func NewUnitStub(id specter.UnitID, kind specter.UnitKind, source specter.Source) specter.Unit {
return specter.UnitOf[any](nil, id, kind, source)
return specter.UnitOf(nil, id, kind, source)
}

0 comments on commit 76c4b58

Please sign in to comment.