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

泛型支持 #4944

Open
helphi opened this issue Dec 21, 2021 · 14 comments
Open

泛型支持 #4944

helphi opened this issue Dec 21, 2021 · 14 comments
Assignees
Labels
type:question general questions

Comments

@helphi
Copy link

helphi commented Dec 21, 2021

Your Question

golang 1.18 泛型beta版已出,请问何时发布 gorm 3.0

Expected answer

跟随golang1.18发布

@helphi helphi added the type:question general questions label Dec 21, 2021
@FlameMida
Copy link

只能说期待了

@zhuying412
Copy link

有任何消息吗?

@a631807682
Copy link
Member

Gorm uses a lot of reflection and uses different logic under different parameter types, it is not suitable for using templates.
So the question is, how should we modify the API so that generics can bring benefits to users?

@helphi
Copy link
Author

helphi commented Mar 29, 2022

eg.

before

var user User
db.First(&user)

after

user := db.First[User]()

@jinzhu
Copy link
Member

jinzhu commented Mar 29, 2022

eg.

before

var user User
db.First(&user)

after

user := db.First[User]()

But with the new style, you can't reuse a variable to reduce GC.

@helphi
Copy link
Author

helphi commented Mar 29, 2022

我觉得这个就要看用户在使用过程中如何取舍了,泛型这么多年才出来官方本来也是在性能与便捷之间进行取舍,gorm支持泛型至少表示我们还是跟着主旋律走的噻 ...

@jinzhu
Copy link
Member

jinzhu commented Mar 29, 2022

There is no doubt, we will support generics, but for the above change, I don't see much value, especially it is a breaking changes.

@helphi
Copy link
Author

helphi commented Mar 29, 2022

我这里只是随便举的一个例子,表示gorm看能不能通过泛型让大家少写点代码,不能当真,最终肯定要根据实际情况来噻。

@zhuying412
Copy link

Is there a roadmap or scheme about supporting generics?

@a631807682
Copy link
Member

I don't think generics are needed from the current api design, like database/sql and encoding/json.
At the moment I haven't figured out how to use generics to make the new api more user-friendly, any suggestions?

@yafeng-Soong
Copy link

大家可以看看我搞的泛型分页封装示例,风格类似MyBatis-plus,博客https://juejin.cn/post/7078279187471679518,代码https://github.com/yafeng-Soong/blog-example/tree/main/gorm_page_generic
希望能有所帮助

Hey Guys,I wrote a simple example using generic, the style looks like Java's MyBatis-plus. blog——https://juejin.cn/post/7078279187471679518, Code——https://github.com/yafeng-Soong/blog-example/tree/main/gorm_page_generic
Hope to help you.

@a631807682
Copy link
Member

a631807682 commented Apr 10, 2022

The reason why pagination can use generics is that they have the same logic.
Take db.Find as an example, the model of each user cannot be the same, so we use reflection, of course we can convert everything into generics like this, but is it better?

// Find
func Find[T interface{}]() ([]T, error) {
	...
}

users, err := Find[User]()

// Unmarshal
type GenericsJSon[T interface{}] struct {
}

func (g GenericsJSon[T]) Unmarshal(data []byte) (*T, error) {
	...
}

user, err := GenericsJSon[User]{}.Unmarshal([]byte(`{"name":"abc"}`))

For me, there are some API that can use generics like Offset/Limit/Count..., but others need to be discussed.

@yafeng-Soong
Copy link

The reason why pagination can use generics is that they have the same logic. Take db.Find as an example, the model of each user cannot be the same, so we use reflection, of course we can convert everything into generics like this, but is it better?

// Find
func Find[T interface{}]() ([]T, error) {
	...
}

users, err := Find[User]()

// Unmarshal
type GenericsJSon[T interface{}] struct {
}

func (g GenericsJSon[T]) Unmarshal(data []byte) (*T, error) {
	...
}

user, err := GenericsJSon[User]{}.Unmarshal([]byte(`{"name":"abc"}`))

For me, there are some API that can use generics like Offset/Limit/Count..., but others need to be discussed.

Thanks for your reply! I got it.

@cheerego
Copy link

https://github.com/cheerego/gorm-generics

看看这个

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

7 participants