-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refreshable v1: Add ToV2() and FromV2() conversion functions (#329)
* refreshable v1: Add ToV2() conversion function * FromV2
- Loading branch information
Showing
12 changed files
with
708 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
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.
Oops, something went wrong.
103 changes: 103 additions & 0 deletions
103
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.
Oops, something went wrong.
Oops, something went wrong.