Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang decode JSON request in nested struct and insert in DB as blob

Tags:

json

mysql

go

I have a nested struct which I am using to decode JSON request.

type Service struct {
    ID       string `json:"id,omitempty" db:"id"`
    Name     string `json:"name" db:"name"`
    Contract struct {
        ServiceTime int    `json:"service_time"`
        Region      string `json:"region"`
    } `json:"contract" db:"contract"`
}

I am using blob type to store Contract in MySQL. To make it work , I would be needing to create a different struct with Contract as string to insert in DB. Can this be done in better way by using single struct only or is there any other elegant way ?

like image 716
nebi Avatar asked Nov 16 '17 17:11

nebi


1 Answers

This depends on what you use to talk to the database, but if you're using database/sql and a driver that provides support for this you can have your Contract type implement the Valuer interface.

type Service struct {
    ID       string    `json:"id,omitempty" db:"id"`
    Name     string    `json:"name" db:"name"`
    Contract Contract `json:"contract" db:"contract"`
}

type Contract struct {
    ServiceTime int    `json:"service_time"`
    Region      string `json:"region"`
}

func (c *Contract) Value() (driver.Value, error) {
    if c != nil {
        b, err := json.Marshal(c)
        if err != nil {
            return nil, err
        }
        return string(b), nil
    }
    return nil, nil
}

And then when you're executing the query just pass in the Service.Contract field and the driver should call the Value method.

sql := // INSERT INTO ...
svc := // &Service{ ...
db.Exec(sql, svc.ID, svc.Name, svc.Contract)

And to be able to retrieve the blob back from db and unmarshal it back into Contract you can have it implement the Scanner interface, again, if a type implements this the driver is expected to call it.

func (c *Contract) Scan(src interface{}) error {
    var data []byte
    if b, ok := src.([]byte); ok {
        data = b
    } else if s, ok := src.(string); ok {
        data = []byte(s)
    }
    return json.Unmarshal(data, c)
}

// ...

sql := // SELECT * FROM ...
svc := // &Service{ ...
db.QueryRow(sql, 123).Scan(&svc.ID, &svc.Name, &svc.Contract)
like image 173
mkopriva Avatar answered Sep 30 '22 13:09

mkopriva