Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add utilities for applying options and copying protobufs #809

Merged
merged 1 commit into from
Sep 18, 2024
Merged

Conversation

paulwe
Copy link
Contributor

@paulwe paulwe commented Sep 18, 2024

No description provided.

@paulwe paulwe requested a review from a team September 18, 2024 06:03
Copy link

changeset-bot bot commented Sep 18, 2024

⚠️ No Changeset found

Latest commit: 3a2d712

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

💥 An error occurred when fetching the changed packages and changesets in this PR
Some errors occurred when validating the changesets config:
The package or glob expression "github.com/livekit/protocol" specified in the `fixed` option does not match any package in the project. You may have misspelled the package name or provided an invalid glob expression. Note that glob expressions must be defined according to https://www.npmjs.com/package/micromatch.


package options

func Apply[T any, F ~func(T)](o T, opts []F) T {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry Paul, I already asked you this once, but I forgot the answer :-(. What does the ~ mean there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~ means the type constraint permits any type with the same underlying type. so for example

type Foo func()

var f0 = func() {}
var f1 Foo = func() {}

func a[T ~func()](f T) {}
func b[T func()](f T)  {}

a(f0) // ok
b(f0) // ok
a(f1) // ok
b(f1) // not ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, got it. Thank you Paul. Let me commit this to my poor memory and hope it survives till the next time I see this pattern :-)

Copy link
Contributor Author

@paulwe paulwe Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed here because we have functional options like

type FooOption func(x *FooOptions)

// several implementations of FooOption

func SomeFoo(opts ...FooOption) {
    _ := options.Apply(&FooOptions{}, opts)
    // without ~ this wouldn't work because []FooOption is not []func(*FooOptions)
}

import "google.golang.org/protobuf/proto"

func CloneProto[T proto.Message](m T) T {
return proto.Clone(m).(T)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!!!

@paulwe paulwe merged commit a7b1ab8 into main Sep 18, 2024
3 checks passed
@paulwe paulwe deleted the helpers branch September 18, 2024 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants