gorm icon indicating copy to clipboard operation
gorm copied to clipboard

泛型支持

Open helphi opened this issue 2 years ago • 14 comments

Your Question

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

Expected answer

跟随golang1.18发布

helphi avatar Dec 21 '21 12:12 helphi

只能说期待了

FlameMida avatar Mar 28 '22 02:03 FlameMida

有任何消息吗?

zhuying412 avatar Mar 28 '22 07:03 zhuying412

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?

a631807682 avatar Mar 28 '22 10:03 a631807682

eg.

before

var user User
db.First(&user)

after

user := db.First[User]()

helphi avatar Mar 29 '22 10:03 helphi

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.

jinzhu avatar Mar 29 '22 11:03 jinzhu

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

helphi avatar Mar 29 '22 11:03 helphi

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.

jinzhu avatar Mar 29 '22 12:03 jinzhu

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

helphi avatar Mar 29 '22 12:03 helphi

Is there a roadmap or scheme about supporting generics?

zhuying412 avatar Apr 08 '22 06:04 zhuying412

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?

a631807682 avatar Apr 08 '22 07:04 a631807682

大家可以看看我搞的泛型分页封装示例,风格类似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.

yafeng-Soong avatar Apr 10 '22 08:04 yafeng-Soong

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.

a631807682 avatar Apr 10 '22 11:04 a631807682

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.

yafeng-Soong avatar Apr 10 '22 11:04 yafeng-Soong

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

看看这个

cheerego avatar May 16 '22 08:05 cheerego