I am trying to implement abstraction in golang
I am using gorm
orm library and gin
framework
Base Class
type Base struct {
Context *gin.Context // Passing gin request
}
func (b *Base ) Add() {
err := b.Context.BindJSON(b)
if err != nil {
// handling error here
}
gorm.db.Create(b) // Here I am adding base data to database
}
Child Class
type Shopper struct {
Base // Embedding Base struct
Name string,
Address string,
ContactNo int
}
Handler
func handler (c *gin.Context) {
s := new(Shopper)
s.Context = c
s.Add() // Here I am expecting Add() method should bind JSON to shopper struct
// entry to database using gorm
}
Add()
method is not taking any property which shopper
struct has.
Here I just want to avoid code duplication
in each handler
which
just takes json from request body and add to respective database
using gorm