Skip to content

Commit

Permalink
refreshable v1: Add ToV2() and FromV2() conversion functions (#329)
Browse files Browse the repository at this point in the history
* refreshable v1: Add ToV2() conversion function

* FromV2
  • Loading branch information
bmoylan authored Nov 2, 2023
1 parent d5ee3e8 commit 3ef9bcc
Show file tree
Hide file tree
Showing 12 changed files with 708 additions and 0 deletions.
1 change: 1 addition & 0 deletions refreshable/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/palantir/pkg v1.1.0
github.com/palantir/pkg/refreshable/v2 v2.0.0
github.com/stretchr/testify v1.8.4
)

Expand Down
2 changes: 2 additions & 0 deletions refreshable/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/palantir/pkg v1.1.0 h1:0EhrSUP8oeeh3MUvk7V/UU7WmsN1UiJNTvNj0sN9Cpo=
github.com/palantir/pkg v1.1.0/go.mod h1:KC9srP/9ssWRxBxFCIqhUGC4Jt7OJkWRz0Iqehup1/c=
github.com/palantir/pkg/refreshable/v2 v2.0.0 h1:9uxG2L5nqUOfLg4l9YWjmh2JVW38VNXu0t4/mLkC2is=
github.com/palantir/pkg/refreshable/v2 v2.0.0/go.mod h1:SfgGx/EeOCBJWfhDphHIu7nRl5TeXkKzvlfj8o7x9Mg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
30 changes: 30 additions & 0 deletions refreshable/v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2023 Palantir Technologies. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package refreshable

import (
refreshablev2 "github.com/palantir/pkg/refreshable/v2"
)

// ToV2 converts from a v1 Refreshable created by this package to v2 supporting type safety via generics.
// If v1's value is not of type T, the function panics.
func ToV2[T any](v1 Refreshable) refreshablev2.Refreshable[T] {
v2 := refreshablev2.New[T](v1.Current().(T))
v1.Subscribe(func(i interface{}) {
v2.Update(i.(T))
})
return v2
}

// FromV2 converts from a v1 Refreshable created by this package to v2 supporting type safety via generics.
func FromV2[T any](v2 refreshablev2.Refreshable[T]) Refreshable {
v1 := NewDefaultRefreshable(v2.Current())
v2.Subscribe(func(i T) {
if err := v1.Update(i); err != nil {
panic(err)
}
})
return v1
}
25 changes: 25 additions & 0 deletions refreshable/v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2023 Palantir Technologies. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package refreshable

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestToFromV2(t *testing.T) {
base := NewDefaultRefreshable("original")
v2 := ToV2[string](base)
v1 := FromV2(v2)
assert.Equal(t, base.Current(), "original", "base missing original value")
assert.Equal(t, v2.Current(), "original", "v2 missing original value")
assert.Equal(t, v1.Current(), "original", "v1 missing original value")

assert.NoError(t, base.Update("updated"))
assert.Equal(t, base.Current(), "updated", "base missing updated value")
assert.Equal(t, v2.Current(), "updated", "v2 missing updated value")
assert.Equal(t, v1.Current(), "updated", "v1 missing updated value")
}
29 changes: 29 additions & 0 deletions refreshable/vendor/github.com/palantir/pkg/refreshable/v2/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions refreshable/vendor/github.com/palantir/pkg/refreshable/v2/async.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3ef9bcc

Please sign in to comment.