How to support Enum value in Gorm - go

Good day guys, I am new to go and Gorm, I am building a simple web service using Postgres, I had an issue getting my model to support enum values, after some research I found a way to to do it. But I began to get new errors which I can't seem to fix after researching resources online.
whenever I try to fetch a user from the database I get this error:
panic: interface conversion: interface {} is string, not []uint8
-> userservice/models.(*RoleAllowed).Scan
-> /Users/Cipher/work/frontendlabs/shopvending/userservice/models/user.model.go:22
database/sql.convertAssignRows
/usr/local/go/src/database/sql/convert.go:385
database/sql.convertAssignRows
/usr/local/go/src/database/sql/convert.go:428
database/sql.(*Rows).Scan
/usr/local/go/src/database/sql/sql.go:3287
gorm.io/gorm.(*DB).scanIntoStruct
/Users/Cipher/go/pkg/mod/gorm.io/gorm#v1.23.8/scan.go:67
gorm.io/gorm.Scan
/Users/Cipher/go/pkg/mod/gorm.io/gorm#v1.23.8/scan.go:293
gorm.io/gorm/callbacks.Query
/Users/Cipher/go/pkg/mod/gorm.io/gorm#v1.23.8/callbacks/query.go:26
gorm.io/gorm.(*processor).Execute
/Users/Cipher/go/pkg/mod/gorm.io/gorm#v1.23.8/callbacks.go:130
gorm.io/gorm.(*DB).First
/Users/Cipher/go/pkg/mod/gorm.io/gorm#v1.23.8/finisher_api.go:129
userservice/services.UserService.FindUserByEmail
/Users/Cipher/work/frontendlabs/shopvending/userservice/services/user.service.go:25
userservice/handlers.ManualLogin
/Users/Cipher/work/frontendlabs/shopvending/userservice/handlers/auth.handler.go:24
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2109
github.com/go-chi/chi/v5.(*Mux).routeHTTP
/Users/Cipher/go/pkg/mod/github.com/go-chi/chi/v5#v5.0.7/mux.go:442
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2109
github.com/go-chi/chi/v5.(*Mux).ServeHTTP
/Users/Cipher/go/pkg/mod/github.com/go-chi/chi/v5#v5.0.7/mux.go:71
github.com/go-chi/chi/v5.(*Mux).Mount.func1
/Users/Cipher/go/pkg/mod/github.com/go-chi/chi/v5#v5.0.7/mux.go:314
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2109
github.com/go-chi/chi/v5.(*Mux).routeHTTP
/Users/Cipher/go/pkg/mod/github.com/go-chi/chi/v5#v5.0.7/mux.go:442
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2109
github.com/go-chi/chi/v5/middleware.Timeout.func1.1
/Users/Cipher/go/pkg/mod/github.com/go-chi/chi/v5#v5.0.7/middleware/timeout.go:45
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2109
userservice/middlewares.CommonMiddleware.func1
/Users/Cipher/work/frontendlabs/shopvending/userservice/middlewares/contentType.go:8
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2109
github.com/go-chi/chi/v5/middleware.Recoverer.func1
/Users/Cipher/go/pkg/mod/github.com/go-chi/chi/v5#v5.0.7/middleware/recoverer.go:38
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2109
github.com/go-chi/chi/v5/middleware.RequestLogger.func1.1
/Users/Cipher/go/pkg/mod/github.com/go-chi/chi/v5#v5.0.7/middleware/logger.go:57
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2109
github.com/go-chi/chi/v5/middleware.RealIP.func1
/Users/Cipher/go/pkg/mod/github.com/go-chi/chi/v5#v5.0.7/middleware/realip.go:35
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2109
github.com/go-chi/chi/v5/middleware.RequestID.func1
/Users/Cipher/go/pkg/mod/github.com/go-chi/chi/v5#v5.0.7/middleware/request_id.go:76
net/http.HandlerFunc.ServeHTTP
/usr/local/go/src/net/http/server.go:2109
github.com/go-chi/chi/v5.(*Mux).ServeHTTP
/Users/Cipher/go/pkg/mod/github.com/go-chi/chi/v5#v5.0.7/mux.go:88
net/http.serverHandler.ServeHTTP
/usr/local/go/src/net/http/server.go:2947
net/http.(*conn).serve
/usr/local/go/src/net/http/server.go:1991
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:3102
I have tried to convert the byte to string, I still get the error.
my user service file:
package services
import (
"github.com/sirupsen/logrus"
"userservice/database"
"userservice/dto"
"userservice/models"
)
type UserService struct{}
//func (u UserService) FindUserById(id int) *models.User {
// user := u.User
// if err := database.Instance.Where("id = ?", id).First(&user); err != nil {
// logrus.Errorf("Can not find user with id %d", id)
// return nil
//
// }
// return user
//
//}
func (u UserService) FindUserByEmail(email string) *models.User {
var user models.User
if err := database.Instance.Where("email = ?", email).First(&user); err.Error != nil {
logrus.Errorf("Can not find user with email %s", err.Error)
return nil
}
return &user
}
func (u UserService) CreateUser(newUser dto.CreateNewUserDTO) *models.User {
createNewUser := models.User{
Email: newUser.Email,
Password: newUser.Password,
}
createNewUser.HashPassword(createNewUser.Password)
err := database.Instance.Create(&createNewUser)
if err.Error != nil {
logrus.Errorf("Error saving food to the database: %s", err.Error)
return nil
}
return &createNewUser
}
the issue is when I call "FindUserByEmail" for some reason it can't serialise.
it was working, until I introduced the enum value integration. now I get the error above.
my user model file:
package models
import (
"database/sql/driver"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"time"
)
type RoleAllowed string
const (
admin RoleAllowed = "admin"
seller RoleAllowed = "seller"
rider RoleAllowed = "rider"
regular RoleAllowed = "regular"
)
func (st *RoleAllowed) Scan(value interface{}) error {
*st = RoleAllowed(value.([]byte))
return nil
}
func (st RoleAllowed) Value() (driver.Value, error) {
return string(st), nil
}
type StatusAllowed string
const (
suspended StatusAllowed = "suspended"
active StatusAllowed = "active"
inactive StatusAllowed = "inactive"
)
func (st *StatusAllowed) Scan(value interface{}) error {
*st = StatusAllowed(value.([]byte))
return nil
}
func (st StatusAllowed) Value() (driver.Value, error) {
return string(st), nil
}
type User struct {
ID uint `json:"id" gorm:"type:bigserial;primaryKey;autoIncrement"`
Email string `json:"email" gorm:"type:varchar(255);unique;not null"`
Password string `json:"password" gorm:"type:varchar(255);unique;not null""`
OTPCode int `json:"otp_code"`
Role RoleAllowed `json:"role" gorm:"type:role_allowed;default:'regular'"`
Status StatusAllowed `json:"status" gorm:"type:status_allowed;default:'active'"`
IsEmailVerified bool `json:"isEmailVerified" gorm:"default:false"`
OTPExpireTime time.Time `json:"otp_expire_time"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (u *User) HashPassword(password string) error {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
if err != nil {
return err
}
u.Password = string(bytes)
return nil
}
func (u *User) CheckPasswordHash(password string) error {
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
if err != nil {
return err
}
return nil
}
please any assistance on how to resolve this issue?

I edited my code below
package models
import (
"database/sql/driver"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"time"
)
type RoleAllowed string
const (
admin RoleAllowed = "admin"
seller RoleAllowed = "seller"
rider RoleAllowed = "rider"
regular RoleAllowed = "regular"
)
func (st *RoleAllowed) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
*st = RoleAllowed(b)
}
return nil
}
func (st RoleAllowed) Value() (driver.Value, error) {
return string(st), nil
}
type StatusAllowed string
const (
suspended StatusAllowed = "suspended"
active StatusAllowed = "active"
inactive StatusAllowed = "inactive"
)
func (st *StatusAllowed) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
*st = StatusAllowed(b)
}
return nil
}
func (st StatusAllowed) Value() (driver.Value, error) {
return string(st), nil
}
type User struct {
ID uint `json:"id" gorm:"type:bigserial;primaryKey;autoIncrement"`
Email string `json:"email" gorm:"type:varchar(255);unique;not null"`
Password string `json:"password" gorm:"type:varchar(255);unique;not null""`
OTPCode int `json:"otp_code"`
Role RoleAllowed `json:"role" gorm:"type:role_allowed;default:'regular'"`
Status StatusAllowed `json:"status" gorm:"type:status_allowed;default:'active'"`
IsEmailVerified bool `json:"isEmailVerified" gorm:"default:false"`
OTPExpireTime time.Time `json:"otp_expire_time"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (u *User) HashPassword(password string) error {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
if err != nil {
return err
}
u.Password = string(bytes)
return nil
}
func (u *User) CheckPasswordHash(password string) error {
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
if err != nil {
return err
}
return nil
}
I had to check the type then assign the right value I need. thanks #mkopriva

Related

How to return nested entities after creating a new object?

Model Account contains nested structures - Currency and User
When I create a new instance of Account in DB, and then return it in my response, nested entities are empty:
type Account struct {
BaseModel
Name string `gorm:"size:64;not null" json:"name"`
Balance decimal.Decimal `gorm:"type:decimal(16, 2);default:0;not null;" json:"balance"`
UserID int `gorm:"not null" json:"-"`
User User `gorm:"foreignKey:UserID" json:"user"`
CurrencyID int `gorm:"not null" json:"-"`
Currency Currency `gorm:"foreignKey:CurrencyID" json:"currency"`
}
type CreateAccountBody struct {
Name string `json:"name" binding:"required"`
Balance decimal.Decimal `json:"balance"`
CurrencyID int `json:"currency_id" binding:"required"`
}
func CreateAccount(ctx *gin.Context) {
body := CreateAccountBody{}
if err := ctx.Bind(&body); err != nil {
log.Println("Error while binding body:", err)
ctx.JSON(
http.StatusBadRequest,
gin.H{"error": "Wrong request parameters"},
)
return
}
account := Account {
Name: body.Name,
Balance: body.Balance,
CurrencyID: body.CurrencyID,
UserID: 1,
}
if result := db.DB.Create(&account); result.Error != nil {
log.Println("Unable to create an account:", result.Error)
}
ctx.JSON(http.StatusCreated, gin.H{"data": account})
}
To avoid this problem, I refresh account variable with separate query:
db.DB.Create(&account)
db.DB.Preload("User").Preload("Currency").Find(&account, account.ID)
ctx.JSON(http.StatusCreated, gin.H{"data": account})
Is this the most effective and correct way to achieve the desired result?
I'm gonna share you how usually I managed this scenario. First, let me share the code.
main.go file
package main
import (
"context"
"gogindemo/handlers"
"github.com/gin-gonic/gin"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var (
db *gorm.DB
ctx *gin.Context
)
func init() {
dsn := "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable"
var err error
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
db.AutoMigrate(&handlers.Currency{})
db.AutoMigrate(&handlers.User{})
db.AutoMigrate(&handlers.Account{})
}
func AddDb() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.Request = ctx.Request.WithContext(context.WithValue(ctx.Request.Context(), "DB", db))
ctx.Next()
}
}
func main() {
db.Create(&handlers.User{Id: 1, Name: "john doe"})
db.Create(&handlers.User{Id: 2, Name: "mary hut"})
db.Create(&handlers.Currency{Id: 1, Name: "EUR"})
db.Create(&handlers.Currency{Id: 2, Name: "USD"})
r := gin.Default()
r.POST("/account", AddDb(), handlers.CreateAccount)
r.Run()
}
Here, I've just added the code for bootstrapping the database objects and add some dummy data to it.
handlers/handlers.go file
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
"gorm.io/gorm"
)
type User struct {
Id int
Name string
}
type Currency struct {
Id int
Name string
}
type Account struct {
Id int
Name string `gorm:"size:64;not null" json:"name"`
Balance decimal.Decimal `gorm:"type:decimal(16, 2);default:0;not null;" json:"balance"`
UserID int `gorm:"not null" json:"-"`
User User `gorm:"foreignKey:UserID" json:"user"`
CurrencyID int `gorm:"not null" json:"-"`
Currency Currency `gorm:"foreignKey:CurrencyID" json:"currency"`
}
type CreateAccountBody struct {
Name string `json:"name" binding:"required"`
Balance decimal.Decimal `json:"balance"`
CurrencyID int `json:"currency_id" binding:"required"`
}
func CreateAccount(c *gin.Context) {
db, ok := c.Request.Context().Value("DB").(*gorm.DB)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
return
}
var accountReq CreateAccountBody
if err := c.BindJSON(&accountReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "wrong request body payload"})
return
}
// create Account & update the "account" variable
account := Account{Name: accountReq.Name, Balance: accountReq.Balance, CurrencyID: accountReq.CurrencyID, UserID: 1}
db.Create(&account).Preload("Currency").Preload("User").Find(&account, account.Id)
c.IndentedJSON(http.StatusCreated, account)
}
Within this file, I actually talk with the database through the DB passed in the context. Now, back to your question.
If the relationship between the Currency/Account and User/Account is of type 1:1, then, you should rely on the Preload clause. This will load the related entity in a separate query instead of adding it in an INNER JOIN clause.
Let me know if this solves your issue, thanks!

Receiving inconsistent data in Go api

I was learning the Go language and tested Google Cloud Functions with go + Google Firestore as the database.
While I was testing the response I got inconsistent responses.
I have used the json Marshaller to convert Firebase data to Json object to return from the API, this API is hosted in the Google Cloud Functions.
// Package p contains an HTTP Cloud Function.
package p
import (
"context"
"fmt"
"log"
"net/http"
s "strings"
"encoding/json"
"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
)
func HelloWorld(w http.ResponseWriter, r *http.Request) {
path := s.Replace(r.URL.Path, "/", "", -1)
ctx := context.Background()
client := createClient(ctx)
iter := client.Collection("profile").Where("publicUrl", "==", path).Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
var publicDTO PublicDTO
var Profile Profile
doc.DataTo(&Profile)
publicDTO.Profile = Profile
b, err := json.Marshal(publicDTO)
if err != nil {
fmt.Println(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
}
func createClient(ctx context.Context) *firestore.Client {
projectID := "projectId"
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
return client
}
type PublicDTO struct {
Profile Profile `json:"profile"`
}
type Profile struct {
Id string `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
FullName string `json:"fullName"`
Email string `json:"email"`
ImageUrl string `json:"imageUrl"`
CoverPic string `json:"coverPic"`
Experience int `json:"experience"`
PhoneNumber string `json:"phoneNumber"`
Description string `json:"description"`
Address string `json:"address"`
State string `json:"state"`
Country string `json:"country"`
Dob map[string]string `json:"dob"`
Website string `json:"website"`
Reputation int `json:"reputation"`
MemberFrom map[string]int `json:"memberFrom"`
Title string `json:"title"`
Organization string `json:"organization"`
Status string `json:"status"`
Setup int `json:"setup"`
Social map[string]string `json:"social"`
PublicUrl string `json:"publicUrl"`
Language []string `json:"language"`
Interests []string `json:"interests"`
}
but each time the response i'm getting is inconsistent, some of the values are missing.
The solution i got after marshal and unmarshal, it works as expected.
package p
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
s "strings"
"time"
"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
)
var ctx context.Context
var client *firestore.Client
func PublicApi(w http.ResponseWriter, r *http.Request) {
path := s.Replace(r.URL.Path, "/", "", -1)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
newFsConfigBytes, _ := json.Marshal(getPublicDTO(path))
w.Write(newFsConfigBytes)
}
func getPublicDTO(path string) (publicDTO publicDTO) {
ctx = context.Background()
client = createClient()
profile, id := getProfiles(path)
publicDTO.Profile = profile
publicDTO.MilestoneDTOS = getMilestone(id)
return
}
func getProfiles(link string) (profile, string) {
var retVal profile
var id string
iter := client.Collection("profile").Where("publicUrl", "==", link).Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
id = doc.Ref.ID
b, _ := json.Marshal(doc.Data())
json.Unmarshal(b, &retVal)
}
return retVal, id
}
func getMilestone(id string) []milestoneDTOS {
var retVal []milestoneDTOS
iter := client.Collection("milestone").Where("userId", "==", id).Documents(ctx)
for {
var milestoneDTO milestoneDTOS
doc, err := iter.Next()
if err == iterator.Done {
break
}
b, _ := json.Marshal(doc.Data())
err = json.Unmarshal(b, &milestoneDTO)
if err != nil {
fmt.Println(err)
}
retVal = append(retVal, milestoneDTO)
}
return retVal
}
func createClient() *firestore.Client {
projectID := "app_id_asda"
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
return client
}
type profile struct {
Address string `json:"address"`
City string `json:"city"`
Country string `json:"country"`
CoverPic string `json:"coverPic"`
CreatedBy string `json:"createdBy"`
CreatedDate int `json:"createdDate"`
Description string `json:"description"`
Dob int64 `json:"dob"`
Email string `json:"email"`
Enabled bool `json:"enabled"`
Experience int `json:"experience"`
FirstName string `json:"firstName"`
FullName string `json:"fullName"`
FullNameNoSpace string `json:"fullNameNoSpace"`
ImageURL string `json:"imageUrl"`
Interests []string `json:"interests"`
IsEnabled bool `json:"isEnabled"`
Language string `json:"language"`
LastModifiedDate int `json:"lastModifiedDate"`
LastName string `json:"lastName"`
LatLng string `json:"latLng"`
MemberFrom time.Time `json:"memberFrom"`
ObjectID string `json:"objectID"`
Organization string `json:"organization"`
PhoneNumber string `json:"phoneNumber"`
PlanID string `json:"planId"`
PublicURL string `json:"publicUrl"`
Reputation int `json:"reputation"`
Setup int `json:"setup"`
Social string `json:"social"`
State string `json:"state"`
Status string `json:"status"`
Title string `json:"title"`
Website string `json:"website"`
}
type milestoneDTOS struct {
Category string `json:"category"`
CreatedBy string `json:"createdBy"`
CreatedDate int `json:"createdDate"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
EndDate time.Time `json:"endDate"`
IsCurrentPosition bool `json:"isCurrentPosition"`
IsEnabled bool `json:"isEnabled"`
LastModifiedBy time.Time `json:"lastModifiedBy"`
LastModifiedDate int `json:"lastModifiedDate"`
ObjectID string `json:"objectID"`
Organization string `json:"organization"`
PictureURL string `json:"pictureURL"`
Profile string `json:"profile"`
Score float64 `json:"score"`
StartDate time.Time `json:"startDate"`
Tags []string `json:"tags"`
Title string `json:"title"`
URL string `json:"url"`
UserID string `json:"userId"`
}
type publicDTO struct {
Profile profile `json:"profile"`
MilestoneDTOS []milestoneDTOS `json:"milestoneDTOS"`
}

Gorm BeforeCreate hook is not working to generate a UUID

I am trying to generate a UUID every time I create a company. I thought about doing it in the hook function, but it is not working. I tried to panic the program if it is executed, but the hook is not responding.
I followed the documentation on how I can implement the hook, but it does not work for me.
Hooks documentation: https://gorm.io/docs/hooks.html
import (
"time"
"github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid"
)
type Base struct {
ID uuid.UUID `gorm:"type:UUID;" json:"_id"`
CreatedAt time.Time `gorm:"type:Date;" json:"created_at"`
UpdatedAt time.Time `gorm:"type:Date;" json:"updated_at"`
DeletedAt *time.Time `gorm:"type:Date;" json:"deleted_at"`
}
type CompanyModel struct {
Base
// Departments []DepartmentModel `gorm:"foreignKey:id" json:"departments"`
Cvr int `gorm:"not null" json:"cvr"`
Name string `gorm:"not null" json:"name"`
}
// BeforeCreate creates
func (u *Base) BeforeCreate(tx *gorm.DB) (err error) {
u.ID = uuid.NewV4()
panic(u.ID)
return
}
// BeforeSave creates
func (u *Base) BeforeSave(tx *gorm.DB) (err error) {
u.ID = uuid.NewV4()
panic(u.ID)
return
}
I am creating a company by the following
tx := db.Session(&gorm.Session{SkipHooks: false}).Create(&models.CompanyModel{
Cvr: 12333,
Name: "test,
// Departments: company.Departments,
},
)
The saved value when I execute is always:
[1.479ms] [rows:0] INSERT INTO "company_models" ("id","created_at","updated_at","deleted_at","cvr","name") VALUES ('00000000-0000-0000-0000-000000000000','2021-04-21 18:00:51.879','2021-04-21 18:00:51.879',NULL,12333,'test')
It looks like you are using gorm v1, and I think you need gorm v2. The import is "gorm.io/gorm".
Minimum working example:
package main
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type User struct {
Id int
Name string
}
func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
u.Name = "Steve"
return
}
func (u *User) BeforeSave(tx *gorm.DB) (err error) {
u.Name = "Sally"
return
}
func main() {
db, err := gorm.Open(sqlite.Open("hooks.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&User{})
user := User{
Name: "Blake",
}
db.Create(&user)
fmt.Println(user.Name)
user.Name = "Tyler"
db.Save(&user)
fmt.Println(user.Name)
}
Prints:
Steve
Sally

Serialization of struct with pointers

Having a struct heirarchy like:
type DomainStore struct {
Domains []*Domain
Users []*User
}
type Domain struct {
Name string
Records []*Record
Owner *User
}
type User struct {
Name string
Email string
Domains []*Domain
}
type Record struct {
Name string
Host string
}
With a single DomainStore having a list of Domain and Users with pointer between Domain and User.
I'm looking for a way to serialize/deserialize to/from file. I have been trying to use gob, but the pointers is not (by design) serialized correct (its flattened).
Thinking about giving each object a unique id and making a func to serialize/deserialize each type, but it seems much work/boilerplate. Any suggestions for a strategy?
I would like to keep the whole DomainStore in memory, and just serialize to file on user request.
The main problem: How to serialise/deserialize and keep the pointers pointing to the same object and not different copies of the same object
Both gob and json seems to "just" copy the value of the object and afted deserializasion I end up with multiple independent copies of objects.
Using gob ang json this is what happens:
Before, A & C both points to B:
A -> B <- C
After deserialization with json/gob:
A -> B1 , C -> B2
A & C points to to different object, with the same values. But, if i change B1 it's not changed in B2.
--- Update ---
When marshalling i can obtain the memory location of the object and use it as an ID:
func (u *User) MarshalJSON() ([]byte, error) {
return json.Marshal(&JsonUser{
ID: fmt.Sprintf("%p", u),
Name: u.Name,
Email: u.Email,
})
}
And when marshalling the Domain I can replace the
func (d *Domain) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
ID string `json:"id"`
Name string `json:"name"`
User string `json:"user"`
}{
ID: fmt.Sprintf("%p", d),
Name: d.Name,
User: fmt.Sprintf("%p", d.User),
})
}
Now I just need to be able to unmarshal this which gives me a problem in the UnmarshalJSON need to access a map of id's and their respective objects.
func (u *User) UnmarshalJSON(data []byte) error {
// need acces to a map shared by all UnmarshalJSON functions
}
It can be done using the following method:
All the objects are placed in maps in a State object.
When the objects in a State object is marshalled, all objects refered to using pointers is replaced with the memory location of the object.
When unmarshalled pointers are restored using a global list of previously read objects.
The code will run, and is just to illustrate the method, I'm new to Go, so bear with me.
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"
)
type User struct {
Name string
Email string
}
type JsonUser struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
func (u *User) Print(level int) {
ident := strings.Repeat("-", level)
log.Println(ident, "Username:", u.Name, u.Email)
}
func (u *User) Id() string {
return fmt.Sprintf("%p", u)
}
func (u *User) MarshalJSON() ([]byte, error) {
return json.Marshal(&JsonUser{
ID: u.Id(),
Name: u.Name,
Email: u.Email,
})
}
func (u *User) UnmarshalJSON(data []byte) error {
aux := &JsonUser{}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
u.Name = aux.Name
u.Email = aux.Email
load_helper[aux.ID] = u
log.Println("Added user with id ", aux.ID, u.Name)
return nil
}
type Record struct {
Type string // MX / A / CNAME / TXT / REDIR / SVR
Name string // # / www
Host string // IP / address
Priority int // Used for MX
Port int // Used for SVR
}
type JsonRecord struct {
ID string
Type string
Name string
Host string
Priority int
Port int
}
func (r *Record) Print(level int) {
ident := strings.Repeat("-", level)
log.Println(ident, "", r.Type, r.Name, r.Host)
}
func (r *Record) Id() string {
return fmt.Sprintf("%p", r)
}
func (r *Record) MarshalJSON() ([]byte, error) {
return json.Marshal(&JsonRecord{
ID: r.Id(),
Name: r.Name,
Type: r.Type,
Host: r.Host,
Priority: r.Priority,
Port: r.Port,
})
}
func (r *Record) UnmarshalJSON(data []byte) error {
aux := &JsonRecord{}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
r.Name = aux.Name
r.Type = aux.Type
r.Host = aux.Host
r.Priority = aux.Priority
r.Port = aux.Port
load_helper[aux.ID] = r
log.Println("Added record with id ", aux.ID, r.Name)
return nil
}
type Domain struct {
Name string
User *User // User ID
Records []*Record // Record ID's
}
type JsonDomain struct {
ID string `json:"id"`
Name string `json:"name"`
User string `json:"user"`
Records []string `json:"records"`
}
func (d *Domain) Print(level int) {
ident := strings.Repeat("-", level)
log.Println(ident, "Domain:", d.Name)
d.User.Print(level + 1)
log.Println(ident, " Records:")
for _, r := range d.Records {
r.Print(level + 2)
}
}
func (d *Domain) Id() string {
return fmt.Sprintf("%p", d)
}
func (d *Domain) MarshalJSON() ([]byte, error) {
var record_ids []string
for _, r := range d.Records {
record_ids = append(record_ids, r.Id())
}
return json.Marshal(JsonDomain{
ID: d.Id(),
Name: d.Name,
User: d.User.Id(),
Records: record_ids,
})
}
func (d *Domain) UnmarshalJSON(data []byte) error {
log.Println("UnmarshalJSON domain")
aux := &JsonDomain{}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
d.Name = aux.Name
d.User = load_helper[aux.User].(*User) // restore pointer to domains user
for _, record_id := range aux.Records {
d.Records = append(d.Records, load_helper[record_id].(*Record))
}
return nil
}
type State struct {
Users map[string]*User
Records map[string]*Record
Domains map[string]*Domain
}
func NewState() *State {
s := &State{}
s.Users = make(map[string]*User)
s.Domains = make(map[string]*Domain)
s.Records = make(map[string]*Record)
return s
}
func (s *State) Print() {
log.Println("State:")
log.Println("Users:")
for _, u := range s.Users {
u.Print(1)
}
log.Println("Domains:")
for _, d := range s.Domains {
d.Print(1)
}
}
func (s *State) NewUser(name string, email string) *User {
u := &User{Name: name, Email: email}
id := fmt.Sprintf("%p", u)
s.Users[id] = u
return u
}
func (s *State) NewDomain(user *User, name string) *Domain {
d := &Domain{Name: name, User: user}
s.Domains[d.Id()] = d
return d
}
func (s *State) NewMxRecord(d *Domain, rtype string, name string, host string, priority int) *Record {
r := &Record{Type: rtype, Name: name, Host: host, Priority: priority}
d.Records = append(d.Records, r)
s.Records[r.Id()] = r
return r
}
func (s *State) FindDomain(name string) (*Domain, error) {
for _, v := range s.Domains {
if v.Name == name {
return v, nil
}
}
return nil, errors.New("Not found")
}
func Save(s *State) (string, error) {
b, err := json.MarshalIndent(s, "", " ")
if err == nil {
return string(b), nil
} else {
log.Println(err)
return "", err
}
}
var load_helper map[string]interface{}
func Load(s *State, blob string) {
load_helper = make(map[string]interface{})
if err := json.Unmarshal([]byte(blob), s); err != nil {
log.Println(err)
} else {
log.Println("OK")
}
}
func test_state() {
s := NewState()
u := s.NewUser("Ownername", "some#email.com")
d := s.NewDomain(u, "somedomain.com")
s.NewMxRecord(d, "MX", "#", "192.168.1.1", 10)
s.NewMxRecord(d, "A", "www", "192.168.1.1", 0)
s.Print()
x, _ := Save(s) // Saved to json string
log.Println("State saved, the json string is:")
log.Println(x)
s2 := NewState() // Create a new empty State
Load(s2, x)
s2.Print()
d, err := s2.FindDomain("somedomain.com")
if err == nil {
d.User.Name = "Changed"
} else {
log.Println("Error:", err)
}
s2.Print()
}
func main() {
test_state()
}
This is quite a lot of code and there are to much coupling between the objects and the serialization. Also the global var load_helper is bad. Ideas to improve will be appreciated.
Another approch would be to use reflection to make a more generic solution. Here is an example using this method:
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
"reflect"
)
func pprint(x interface{}) {
b, err := json.MarshalIndent(x, "", " ")
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(string(b))
}
var typeRegistry = make(map[string]reflect.Type)
// Register a type to make it possible for the Save/Load functions
// to serialize it.
func Register(v interface{}) {
t := reflect.TypeOf(v)
n := t.Name()
fmt.Println("Register type",n)
typeRegistry[n] = reflect.TypeOf(v)
}
// Make an instance of a type from the string name of the type.
func makeInstance(name string) reflect.Value {
v := reflect.New(typeRegistry[name]).Elem()
return v
}
// Translate a string type name tpo a real type.
func getTypeFromString(name string) reflect.Type {
return typeRegistry[name]
}
// Serializeable interface must be supported by all objects passed to the Load / Save functions.
type Serializeable interface {
Id() string
}
// GenericSave saves the object d
func GenericSave(d interface{}) (string, error) {
r := make(map[string]interface{})
v := reflect.ValueOf(d)
t := reflect.TypeOf(d)
if t.Kind()==reflect.Ptr {
t=t.Elem()
v=v.Elem()
}
r["_TYPE"]=t.Name()
r["_ID"]=fmt.Sprintf("%p", d)
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
name := f.Name
vf := v.FieldByName(name)
// fmt.Println("Field", i+1, "name is", name, "type is", f.Type.Name(), "and kind is", f.Type.Kind())
// fmt.Println("V:", vf)
if f.Tag != "" {
store:=strings.Split(f.Tag.Get("store"),",")
switch store[1] {
case "v":
switch t.Field(i).Type.Name() {
case "string":
r[store[0]]=vf.String()
case "int":
r[store[0]]=vf.Int()
}
case "p":
vals:=vf.MethodByName("Id").Call([]reflect.Value{})
r[store[0]]=vals[0].String()
case "lp":
tr:=[]string{}
for j := 0; j < vf.Len(); j++ {
vals:=vf.Index(j).MethodByName("Id").Call([]reflect.Value{})
tr=append(tr,vals[0].String())
}
r[store[0]]=tr
}
}
}
m,_:=json.Marshal(r)
return string(m),nil
}
// Save saves the list of objects.
func Save(objects []Serializeable) []byte {
lst:=[]string{}
for _,o := range(objects) {
os,_:= GenericSave(o) // o.Save()
lst=append(lst,os)
}
m,_:=json.Marshal(lst)
return m
}
func toStructPtr(obj interface{}) interface{} {
vp := reflect.New(reflect.TypeOf(obj))
vp.Elem().Set(reflect.ValueOf(obj))
return vp.Interface()
}
// Load creates a list of serializeable objects from json blob
func Load(blob []byte) []Serializeable {
objects := []Serializeable{}
loadHelper := make(map[string]interface{})
var olist []interface{}
if err := json.Unmarshal(blob, &olist); err != nil {
log.Println(err)
} else {
for _,o := range(olist) {
var omap map[string]interface{}
json.Unmarshal([]byte(o.(string)), &omap)
t:= getTypeFromString(omap["_TYPE"].(string))
obj := reflect.New(t).Elem()
for i := 0; i < t.NumField(); i++ {
// n:=t.Field(i).Name
// fmt.Println(i,n,t.Field(i).Type.Name())
if t.Field(i).Tag != "" {
store:=strings.Split(t.Field(i).Tag.Get("store"),",")
// fmt.Println(store)
switch store[1] {
case "v":
switch t.Field(i).Type.Name() {
case "string":
obj.FieldByIndex([]int{i}).SetString(omap[store[0]].(string))
case "int":
obj.FieldByIndex([]int{i}).SetInt(int64(omap[store[0]].(float64)))
}
case "p":
nObj:=loadHelper[omap[store[0]].(string)]
obj.FieldByIndex([]int{i}).Set(reflect.ValueOf(nObj.(*User)))
case "lp":
ptrItemType:=t.Field(i).Type.Elem()
slice := reflect.Zero(reflect.SliceOf( ptrItemType /* reflect.TypeOf( &Record{} ) */ ))//.Interface()
for _, pID := range(omap[store[0]].([]interface{})) {
nObj:=loadHelper[pID.(string)]
slice=reflect.Append(slice, reflect.ValueOf(nObj) )
}
obj.FieldByIndex([]int{i}).Set(slice)
}
}
}
oi:=toStructPtr(obj.Interface())
oip:=oi.(Serializeable)
objects=append(objects,oip)
loadHelper[omap["_ID"].(string)]=oip
}
}
return objects
}
/* Application data structures */
type User struct {
Name string `store:"name,v"`
Email string `store:"email,v"`
}
func (u *User) Id() string {
return fmt.Sprintf("%p", u)
}
func (u *User) Save() (string, error) {
return GenericSave(u)
}
func (u *User) Print() {
fmt.Println("User:",u.Name)
}
type Record struct {
Type string `store:"type,v"`// MX / A / CNAME / TXT / REDIR / SVR
Name string `store:"name,v"`// # / www
Host string `store:"host,v"`// IP / address
Priority int `store:"priority,v"`// Used for MX
Port int `store:"port,v"`// Used for SVR
}
func (r *Record) Id() string {
return fmt.Sprintf("%p", r)
}
func (r *Record) Save() (string, error) {
return GenericSave(r)
}
func (r *Record) Print() {
fmt.Println("Record:",r.Type,r.Name,r.Host)
}
type Domain struct {
Name string `store:"name,v"`
User *User `store:"user,p"` // User ID
Records []*Record `store:"record,lp"` // Record ID's
}
func (d *Domain) Id() string {
return fmt.Sprintf("%p", d)
}
func (d *Domain) Save() (string, error) {
return GenericSave(d)
}
func (d *Domain) Print() {
fmt.Println("Domain:",d.Name)
d.User.Print()
fmt.Println("Records:")
for _, r := range d.Records {
r.Print()
}
}
type DBM struct {
Domains []*Domain
Users []*User
Records []*Record
}
func (dbm *DBM) AddDomain(d *Domain) {
dbm.Domains=append(dbm.Domains,d)
}
func (dbm *DBM) AddUser(u *User) {
dbm.Users=append(dbm.Users,u)
}
func (dbm *DBM) AddRecord(r *Record) {
dbm.Records=append(dbm.Records,r)
}
func (dbm *DBM) GetObjects() []Serializeable {
objects:=[]Serializeable{}
for _,r := range(dbm.Records) {
objects=append(objects, r)
}
for _,u := range(dbm.Users) {
objects=append(objects, u)
}
for _,d := range(dbm.Domains) {
objects=append(objects, d)
}
return objects
}
func (dbm *DBM) SetObjects(objects []Serializeable) {
for _,o := range(objects) {
switch o.(type) {
case *Record:
fmt.Println("record")
dbm.AddRecord(o.(*Record))
case *User:
fmt.Println("record")
dbm.AddUser(o.(*User))
case *Domain:
fmt.Println("record")
dbm.AddDomain(o.(*Domain))
}
}
}
func testState() {
Register(User{})
Register(Domain{})
Register(Record{})
dbm:=DBM{}
u := &User{Name: "Martin", Email: "some#email.com"}
dbm.AddUser(u)
r1 := &Record{Name: "#", Type: "MX", Host: "mail.ishost.dk"}
r2 := &Record{Name: "#", Type: "MX", Host: "mail.infoserv.dk"}
dbm.AddRecord(r1)
dbm.AddRecord(r2)
d := &Domain{User:u, Name: "Martin", Records: []*Record{r1, r2}}
dbm.AddDomain(d)
x:=Save(dbm.GetObjects())
fmt.Println("== Saved objects")
// fmt.Println(string(x))
fmt.Println("== Loading")
dbm2:=DBM{}
dbm2.SetObjects(Load(x))
u2:=dbm2.Users[0]
u2.Print()
u2.Name="KURT"
u2.Print()
d2:=dbm2.Domains[0]
d2.Print()
d2.User.Name="ZIG"
u2.Print()
}
func main() {
testState()
}
Use encoding/json package
to marshal:
// Marshal is a function that marshals the object into an
// io.Reader.
// By default, it uses the JSON marshaller.
var Marshal = func(v interface{}) (io.Reader, error) {
b, err := json.MarshalIndent(v, "", "\t")
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
to unmarshal:
// Unmarshal is a function that unmarshals the data from the
// reader into the specified value.
// By default, it uses the JSON unmarshaller.
var Unmarshal = func(r io.Reader, v interface{}) error {
return json.NewDecoder(r).Decode(v)
}
Not sure there's more to this,
Another thing you can do is, store all these as json formatted strings.

golang - Save enum type to SQL database "panic: sql: converting Exec argument #1's type: non-Value type int returned from Value"

In my current go project (~5K LOC), I am using sqlite3 as my underlying database layer, and I am using gorm as my ORM engine. One of the models is a Platform with a field of PlatformType enum type. Here's a code snippet to demonstrate my problem.
package main
import (
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/jinzhu/gorm"
"database/sql/driver"
"fmt"
)
/****************************\
Object Layer
\****************************/
// Platform ID
type PlatformID string
func (u *PlatformID) Scan(value interface{}) error { *u = PlatformID(value.([]byte)); return nil }
func (u PlatformID) Value() (driver.Value, error) { return string(u), nil }
// Platform Type enumeration
type PlatformType int
const (
PLATFORM_TYPE_NOT_A_VALUE PlatformType = iota
PLATFORM_TYPE_TYPE1
PLATFORM_TYPE_TYPE2
)
var types = [...]string {
"Not a type",
"Type1",
"Type2",
}
func (platform_type PlatformType) String() string {
return types[platform_type]
}
func (u *PlatformType) Scan(value interface{}) error { *u = PlatformType(value.(int)); return nil }
func (u PlatformType) Value() (driver.Value, error) { return int(u), nil }
// Platform descriptor.
type Platform struct {
ID PlatformID `json:"ID" gorm:"type:varchar(100);unique;not null"` // Assigned by LCBO.
Type PlatformType `json:"Type" gorm:"type:integer"`
}
type PlatformStore interface {
Init() error
Save(platform *Platform) error
}
/****************************\
Persist Layer
\****************************/
func NewSqlite3Store(dbname string) *gorm.DB {
db, err := gorm.Open("sqlite3", dbname)
if err != nil {
panic("failed to connect database")
}
return db
}
type DBPlatformStore struct {
db *gorm.DB
}
func NewDBPlatformStore(db *gorm.DB) PlatformStore {
return &DBPlatformStore{
db: db,
}
}
func (store *DBPlatformStore) Init() error {
err := store.db.AutoMigrate(&Platform{}).Error
if err != nil {
panic(err)
}
return err
}
func (store *DBPlatformStore) Save(platform *Platform) error {
err := store.db.Create(platform).Error
if err != nil {
panic(err)
}
return err
}
/****************************\
Application Layer
\****************************/
func main() {
db := NewSqlite3Store("enum_test.db")
platformStore := NewDBPlatformStore(db)
fmt.Println("Initialize Database")
err := platformStore.Init()
if err != nil {
panic(err)
}
platform := new(Platform)
platform.ID = "12345"
platform.Type = PLATFORM_TYPE_TYPE1
platformStore.Save(platform)
}
After running the code above, I got a runtime error "sql: converting Exec argument #1's type: non-Value type int returned from Value"
]# go run enumtest.go
Initialize Database
panic: sql: converting Exec argument #1's type: non-Value type int returned from Value
goroutine 1 [running]:
panic(0x66d380, 0xc8203ae350)
/*/panic.go:481 +0x3e6
main.(*DBPlatformStore).Save(0xc820020b20, 0xc820304500, 0x0, 0x0)
/*/enumtest.go:84 +0x9f
main.main()
/*/enumtest.go:106 +0x247
exit status 2
And I checked my database, the platforms table has been created successfully.
]# sqlite3 enum_test.db
sqlite> .schema platforms
CREATE TABLE "platforms" ("id" varchar(100) NOT NULL UNIQUE,"type" integer , PRIMARY KEY ("id"));
The (not-so) trivial question is how do I modify my code so that I can correctly save the entry to database.
My bigger question is: How to save a customized GO enum type to a sql database?(with a ORM engine hopefully)
According to current database/sql docs, the sql has four builtin functions that returns driver.Value, and the underlying types are int64, float64, string and bool. So I guess that's the only four types supported.
I just changed the underlying type of my enum from int to int64 and things are working.
The problematic section is updated to the following snippet:
// Platform Type enumeration
type PlatformType int64
const (
PLATFORM_TYPE_NOT_A_VALUE PlatformType = iota
PLATFORM_TYPE_TYPE1
PLATFORM_TYPE_TYPE2
)
var types = [...]string {
"Not a type",
"Type1",
"Type2",
}
func (platform_type PlatformType) String() string {
return types[platform_type]
}
func (u *PlatformType) Scan(value interface{}) error { *u = PlatformType(value.(int64)); return nil }
func (u PlatformType) Value() (driver.Value, error) { return int64(u), nil }

Resources