I'm trying to refer Users struct from the models package and trying to access the model from control.But I take following errors.
controllers/user.go:87: user.create_date undefined (cannot refer to unexported field or method create_date)
controllers/user.go:88: user.update_date undefined (cannot refer to unexported field or method update_date)
controllers/user.go:104: user.user_id undefined (cannot refer to unexported field or method user_id)
controllers/user.go:119: user.update_date undefined (cannot refer to unexported field or method update_date)
controllers/user.go:136: user.user_id undefined (cannot refer to unexported field or method user_id)
controllers/user.go:151: user.update_date undefined (cannot refer to unexported field or method update_date)
controllers/user.go:166: user.user_id undefined (cannot refer to unexported field or method user_id)
Models.go
package models
import(
"time"
)
type Users struct {
user_id int `json:"user_id" form:"user_id" gorm:"column:user_id"`
user_login string `json:"user_login" form:"user_login" gorm:"column:user_login"`
user_email string `json:"user_email" form:"user_email" gorm:"column:user_email"`
user_password string `json:"user_password" form:"user_password" gorm:"column:user_password"`
user_password_salt string `json:"user_password_salt" form:"user_password_salt" gorm:"column:user_password_salt"`
user_2factor_secret string `json:"user_2factor_secret" form:"user_2factor_secret" gorm:"column:user_2factor_secret"`
user_fullname string `json:"user_fullname" form:"user_fullname" gorm:"column:user_fullname"`
user_description string `json:"user_description" form:"user_description" gorm:"column:user_description"`
user_enabled string `json:"user_enabled" form:"user_enabled" gorm:"column:user_enabled"`
user_verified string `json:"user_verified" form:"user_verified" gorm:"column:user_verified"`
PublisherInfoID int `json:"PublisherInfoID" form:"PublisherInfoID" gorm:"column:PublisherInfoID"`
DemandCustomerInfoID int `json:"DemandCustomerInfoID" form:"DemandCustomerInfoID" gorm:"column:DemandCustomerInfoID"`
create_date time.Time `json:"create_date" gorm:"column:create_date"`
update_date time.Time `json:"update_date" gorm:"column:update_date"`
user_permission_cache string `json:"user_permission_cache" form:"user_permission_cache" gorm:"column:user_permission_cache"`
user_role int `json:"user_role" form:"user_role" gorm:"column:user_role"`
}
in controllers
package controllers
import (
"time"
"github.com/op/go-logging"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/go-sql-driver/mysql"
"../models"
)
var loguser = logging.MustGetLogger("AdsAPI")
type AdsControllerUser struct {
DB gorm.DB
}
func (ac *AdsControllerUser) SetDB(d gorm.DB) {
ac.DB = d
ac.DB.LogMode(true)
}
func (ac *AdsControllerUser) CreateUsers(c *gin.Context) {
var user models.Users
// This will infer what binder to use depending on the content-type header.
c.Bind(&user)
// Update Timestamps
user.create_date = time.Now()
user.update_date = time.Now()
err := ac.DB.Save(&user)
if err != nil {
loguser.Debugf("Error while creating a user, the error is '%v'", err)
res := gin.H{
"status": "403",
"error": "Unable to create user",
}
c.JSON(404, res)
return
}
content := gin.H{
"status": "201",
"result": "Success",
"UserID": user.user_id,
}
c.Writer.Header().Set("Content-Type", "application/json")
c.JSON(201, content)
}
func (ac *AdsControllerUser) UpdateUsers(c *gin.Context) {
// Grab id
id := c.Params.ByName("id")
var user models.Users
c.Bind(&user)
// Update Timestamps
user.update_date = time.Now()
//err := ac.DB.Model(&models.auth_User).Where("user_id = ?", id).Updates(&cm)
err := ac.DB.Where("user_id = ?", id).Updates(&user)
if err != nil {
loguser.Debugf("Error while updating a user, the error is '%v'", err)
res := gin.H{
"status": "403",
"error": "Unable to update user",
}
c.JSON(403, res)
return
}
content := gin.H{
"status": "201",
"result": "Success",
"UserID": user.user_id,
}
c.Writer.Header().Set("Content-Type", "application/json")
c.JSON(201, content)
}
func (ac *AdsControllerUser) DeleteUsers(c *gin.Context) {
// Grab id
id := c.Params.ByName("id")
var user models.Users
c.Bind(&user)
// Update Timestamps
user.update_date = time.Now()
err := ac.DB.Where("user_id = ?", id).Delete(&user)
if err != nil {
loguser.Debugf("Error while deleting a user, the error is '%v'", err)
res := gin.H{
"status": "403",
"error": "Unable to delete user",
}
c.JSON(403, res)
return
}
content := gin.H {
"result": "Success",
"UserID": user.user_id,
}
c.Writer.Header().Set("Content-Type", "application/json")
c.JSON(201, content)
}
Use Capitals for exported fields in struct, when referring struct in another package.
package models
import (
"time"
)
type Users struct {
ID int `json:"user_id" form:"user_id" gorm:"column:user_id"`
Login string `json:"user_login" form:"user_login" gorm:"column:user_login"`
Email string `json:"user_email" form:"user_email" gorm:"column:user_email"`
Password string `json:"user_password" form:"user_password" gorm:"column:user_password"`
PasswordSalt string `json:"user_password_salt" form:"user_password_salt" gorm:"column:user_password_salt"`
TwoFactorSecret string `json:"user_2factor_secret" form:"user_2factor_secret" gorm:"column:user_2factor_secret"`
Fullname string `json:"user_fullname" form:"user_fullname" gorm:"column:user_fullname"`
Description string `json:"user_description" form:"user_description" gorm:"column:user_description"`
Enabled string `json:"user_enabled" form:"user_enabled" gorm:"column:user_enabled"`
Verified string `json:"user_verified" form:"user_verified" gorm:"column:user_verified"`
PublisherInfoID int `json:"PublisherInfoID" form:"PublisherInfoID" gorm:"column:PublisherInfoID"`
DemandCustomerInfoID int `json:"DemandCustomerInfoID" form:"DemandCustomerInfoID" gorm:"column:DemandCustomerInfoID"`
CreateDate time.Time `json:"create_date" gorm:"column:create_date"`
UpdateDate time.Time `json:"update_date" gorm:"column:update_date"`
PermissionCache string `json:"user_permission_cache" form:"user_permission_cache" gorm:"column:user_permission_cache"`
Role int `json:"user_role" form:"user_role" gorm:"column:user_role"`
}
Now do Users.ID to get fields.
Related
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!
I made a simple post API to store articles in database using Gorm And Go Gin.
problem with API showing when I tried to post the category name instead of the category id because the struct declaring it as int32 type
I made a simple function to getting the id of the category instead of the name but i don't know how to inject it within the incoming request
The article model
package models
import (
"gorm.io/gorm"
)
type News struct {
Id int `gorm:"primary_key;auto_increment;not_null" json:"id"`
Category int32 `gorm:"category" json:"category" binding:"required"`
CountryID int32 `gorm:"country_id" json:"country_id" binding:"required"`
LangID int32 `gorm:"lang_id" json:"lang_id" binding:"required"`
SourceId int32 `gorm:"source_id" json:"source_id" binding:"required"`
HashtagId int32 `gorm:"hashtag_id" json:"hashtag_id" binding:"required"`
Title string `gorm:"title" json:"title" binding:"required"`
Content string `gorm:"content" json:"content" binding:"required"`
Description string `gorm:"description" json:"description" binding:"required"`
Summery string `gorm:"summery" json:"summery" binding:"required"`
ImageUrl string `gorm:"image_url" json:"image_url" binding:"required"`
SourceUrl string `gorm:"source_url" json:"source_url" binding:"required"`
Author string `gorm:"author" json:"author" `
}
func CreateArticle(db *gorm.DB, article *News) (err error) {
err = db.Create(article).Error
if err != nil {
return err
}
return nil
}
Category Model
package models
import "gorm.io/gorm"
type Category struct {
Id int32 `gorm:"primary_key;auto_increment;not_null" json:"id"`
Name string `gorm:"category_name" json:"category" binding:"required"`
Parent int32 `gorm:"parent" json:"parent"`
}
func GetCategoryByName(db *gorm.DB, category *Category, name string) (err error) {
err = db.Where("LOWER(category_name) Like LOWER(?)", "%"+name+"%").First(category).Error
if err != nil {
return err
}
return nil
}
News Controller which implement the models
package controllers
import (
"errors"
"net/http"
"github.com/fouad82/news-app-go/models"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"gorm.io/gorm"
)
type NewsRepo struct {
db *gorm.DB
}
func NewArticle() *NewsRepo {
db := models.InitDb()
return &NewsRepo{db: db}
}
// CreateArticle Create Article
func (repository *NewsRepo) CreateArticle(c *gin.Context) {
var Category models.Category
if err := c.ShouldBindBodyWith(&Category, binding.JSON); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": &Category})
return
}
err := models.GetCategoryByName(repository.db, &Category, Category.Name)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.AbortWithStatus(http.StatusNotFound)
return
}
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err})
return
}
var CategoryId = Category.Id
var Article models.News
Article.Category = CategoryId
if err := c.ShouldBindBodyWith(&Article, binding.JSON); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": err})
return
}
err = models.GetArticleByTitle(repository.db, &Article, Article.Title)
if err == nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.AbortWithStatus(http.StatusNotFound)
return
}
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"status": "error", "error": "Title Created Before"})
return
}
createArticle := models.CreateArticle(repository.db, &Article)
if createArticle != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"status": "error", "error": createArticle})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ok", "result": "Article Created"})
}
in this line i tried to inject the int value within the article struct abut it still getting error that im passing string value not int and this because it still read from the request json not the injected parameter
var CategoryId = Category.Id
var Article models.News
Article.Category = CategoryId
I believe the function you are using is out of request scope, so it's basically can not affect the gin.Context.
What I suggest is to copy the context in order to modify it with your changed data. Article.Category in specific.
...
c.Copy
...
The other thing is about the function - it's evaluating itself - I would suggest to exclude it from CreateArticle function
createArticle := models.CreateArticle(repository.db, &Article)
What I also suggest is to return changed struct and work with it separately from request payload - probably you need to create another function for that:
func (repository *NewsRepo) ChangedArticle(c *gin.Context)News{
changedNews := News{
Id: 0,
Category: 0, // Here is where you change it
CountryID: 0,
LangID: 0,
SourceId: 0,
HashtagId: 0,
Title: "",
Content: "",
Description: "",
Summery: "",
ImageUrl: "",
SourceUrl: "",
Author: "",
}
return changedNews
}
and than in your main function - just use it:
...
createArticle := models.ChangedArticle
...
I could miss some details, but I hope this top level explanation helps.
In PostgreSQL, I have table called surveys.
CREATE TABLE SURVEYS(
SURVEY_ID UUID PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(),
SURVEY_NAME VARCHAR NOT NULL,
SURVEY_DESCRIPTION TEXT,
START_PERIOD TIMESTAMP,
END_PERIOD TIMESTAMP
);
As you can see only SURVEY_ID and SURVEY_NAME columns are NOT NULL.
In Go, I want to create new entry in that table by POST request. I send JSON object like this:
{
"survey_name": "NAME",
"survey_description": "DESCRIPTION",
"start_period": "2019-01-01 00:00:00",
"end_period": "2019-02-28 23:59:59"
}
Unfortunatly it raise strange ERROR:
parsing time ""2019-01-01 00:00:00"" as ""2006-01-02T15:04:05Z07:00"": cannot parse " 00:00:00"" as "T"
Where I make mistake and how to fix my problem?
models/surveys.go:
import (
"database/sql"
"time"
)
type NullTime struct {
time.Time
Valid bool
}
type Survey struct {
ID int `json:"survey_id"`
Name string `json:"survey_name"`
Description sql.NullString `json:"survey_description"`
StartPeriod NullTime `json:"start_period"`
EndPeriod NullTime `json:"end_period"`
}
controllers/surveys.go:
var CreateSurvey = func(responseWriter http.ResponseWriter, request *http.Request) {
// Initialize variables.
survey := models.Survey{}
var err error
// The decoder introduces its own buffering and may read data from argument beyond the JSON values requested.
err = json.NewDecoder(request.Body).Decode(&survey)
if err != nil {
log.Println(err)
utils.ResponseWithError(responseWriter, http.StatusInternalServerError, err.Error())
return
}
defer request.Body.Close()
// Execute INSERT SQL statement.
_, err = database.DB.Exec("INSERT INTO surveys (survey_name, survey_description, start_period, end_period) VALUES ($1, $2, $3, $4);", survey.Name, survey.Description, survey.StartPeriod, survey.EndPeriod)
// Shape the response depending on the result of the previous command.
if err != nil {
log.Println(err)
utils.ResponseWithError(responseWriter, http.StatusInternalServerError, err.Error())
return
}
utils.ResponseWithSuccess(responseWriter, http.StatusCreated, "The new entry successfully created.")
}
The error already says what is wrong:
parsing time ""2019-01-01 00:00:00"" as ""2006-01-02T15:04:05Z07:00"": cannot parse " 00:00:00"" as "T"
You are passing "2019-01-01 00:00:00" while it expects a different time format, namely RFC3339 (UnmarshalJSON's default).
To solve this, you either want to pass the time in the expected format "2019-01-01T00:00:00Z00:00" or define your own type CustomTime like this:
const timeFormat = "2006-01-02 15:04:05"
type CustomTime time.Time
func (ct *CustomTime) UnmarshalJSON(data []byte) error {
newTime, err := time.Parse(timeFormat, strings.Trim(string(data), "\""))
if err != nil {
return err
}
*ct = CustomTime(newTime)
return nil
}
func (ct *CustomTime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", time.Time(*ct).Format(timeFormat))), nil
}
Careful, you might also need to implement the Valuer and the Scanner interfaces for the time to be parsed in and out of the database, something like the following:
func (ct CustomTime) Value() (driver.Value, error) {
return time.Time(ct), nil
}
func (ct *CustomTime) Scan(src interface{}) error {
if val, ok := src.(time.Time); ok {
*ct = CustomTime(val)
} else {
return errors.New("time Scanner passed a non-time object")
}
return nil
}
Go Playground example.
I am new in Golang and need some help! I have several questions.
In PostgreSQL database I have table called surveys.
CREATE TABLE SURVEYS(
SURVEY_ID UUID PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4(),
SURVEY_NAME VARCHAR NOT NULL,
SURVEY_DESCRIPTION TEXT,
START_PERIOD TIMESTAMP,
END_PERIOD TIMESTAMP
);
As you can see SURVEY_ID column is PRIMARY KEY and it's type is UUID4.
In Golang application I create such struct to this table:
type Survey struct {
ID string `json:"survey_id"`
Name string `json:"survey_name"`
Description utils.NullString `json:"survey_description"`
StartPeriod utils.NullTime `json:"start_period"`
EndPeriod utils.NullTime `json:"end_period"`
}
As you can see type of ID field is string. Is it correct? I am not sure that it's best practice.
My second question about strange result which I have when make GET request to specific survey by it's ID.
For example when I make such request:
http://localhost:8000/api/survey/0cf1cf18-d5fd-474e-a8be-754fbdc89720
As response I have this:
{
"survey_id": "0cf1cf18-d5fd-474e-a8be-754fbdc89720",
"survey_name": "NAME",
"survey_description": {
"String": "DESCRIPTION",
"Valid": true
},
"start_period": {
"Time": "2019-01-01T00:00:00Z",
"Valid": false
},
"end_period": {
"Time": "0001-01-01T00:00:00Z",
"Valid": false
}
}
As you can see something wrong with last 3 field: survey_description, start_period and end_period. I want to see key and value in one line. For example as here:
{
"survey_id": "0cf1cf18-d5fd-474e-a8be-754fbdc89720",
"survey_name": "NAME",
"survey_description": "DESCRIPTION",
"start_period": "2019-01-01 00:00:00",
"end_period": null
}
Where exactly I make mistake in my code?
utils.go:
package utils
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/lib/pq"
"time"
)
// NullTime is an alias for pq.NullTime data type.
type NullTime struct {
pq.NullTime
}
// MarshalJSON for NullTime.
func (nt *NullTime) MarshalJSON() ([]byte, error) {
if !nt.Valid {
return []byte("null"), nil
}
val := fmt.Sprintf("\"%s\"", nt.Time.Format(time.RFC3339))
return []byte(val), nil
}
// UnmarshalJSON for NullTime.
func (nt *NullTime) UnmarshalJSON(b []byte) error {
err := json.Unmarshal(b, &nt.Time)
nt.Valid = err == nil
return err
}
// NullInt64 is an alias for sql.NullInt64 data type.
type NullInt64 struct {
sql.NullInt64
}
// MarshalJSON for NullInt64.
func (ni *NullInt64) MarshalJSON() ([]byte, error) {
if !ni.Valid {
return []byte("null"), nil
}
return json.Marshal(ni.Int64)
}
// UnmarshalJSON for NullInt64.
func (ni *NullInt64) UnmarshalJSON(b []byte) error {
err := json.Unmarshal(b, &ni.Int64)
ni.Valid = err == nil
return err
}
// NullString is an alias for sql.NullString data type.
type NullString struct {
sql.NullString
}
// MarshalJSON for NullString.
func (ns *NullString) MarshalJSON() ([]byte, error) {
if !ns.Valid {
return []byte("null"), nil
}
return json.Marshal(ns.String)
}
// UnmarshalJSON for NullString.
func (ns *NullString) UnmarshalJSON(b []byte) error {
err := json.Unmarshal(b, &ns.String)
ns.Valid = err == nil
return err
}
routes.go:
router.HandleFunc("/api/survey/{survey_id:[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}}", controllers.GetSurvey).Methods("GET")
controllers/survey.go:
var GetSurvey = func(responseWriter http.ResponseWriter, request *http.Request) {
// Initialize variables.
survey := models.Survey{}
var err error
vars := mux.Vars(request)
// Execute SQL statement.
err = database.DB.QueryRow("SELECT * FROM surveys WHERE survey_id = $1;", vars["survey_id"]).Scan(&survey.ID, &survey.Name, &survey.Description, &survey.StartPeriod, &survey.EndPeriod)
// Shape the response depending on the result of the previous command.
if err != nil {
log.Println(err)
switch err {
case sql.ErrNoRows:
utils.ResponseWithError(responseWriter, http.StatusNotFound, "The entry not found.")
default:
utils.ResponseWithError(responseWriter, http.StatusInternalServerError, err.Error())
}
return
}
utils.Response(responseWriter, http.StatusOK, survey)
}
Well, finally I found the result.
I changed the struct for the table:
type Survey struct {
ID string `json:"survey_id"`
Name string `json:"survey_name"`
Description *string `json:"survey_description", sql:"index"`
StartPeriod *time.Time `json:"start_period", sql:"index"`
EndPeriod *time.Time `json:"end_period", sql:"index"`
}
I don't see any issue with using a string for a UUID.
As for the MarshalJSON not working, I think I know what's going on. Your null types don't implement MarshalJSON, only the pointers to them. The fix would be to either change the function to use a non-pointer receiver, or make the fields pointers in your struct.
func (ns *NullString) MarshalJSON() ([]byte, error)
If you did make them pointers, then you could just keep them like that, since they're nullable.
I am retrieving form data using postman, but the code is too long. Is there is any method for getting the data in short form? Here's the code I am using:
Customer struct:
type Customer struct {
FirstName string `json:"first_name" bson:"first_name"`
LastName string `json:"last_name" bson:"last_name"`
Email string `json:"email" bson:"email"`
}
type Customers []Customer
type new_user struct {
first_name string
last_name string
email string
}
Function for retrieving the form data called by the route:
function GetData(c *gin.Context){
first_name := c.PostForm("first_name")
last_name := c.PostForm("last_name")
email := c.PostForm("email")
reqBody := new(new_user)
err := c.Bind(reqBody)
if err != nil {
fmt.Println(err)
}
customer.FirstName = first_name
customer.LastName = last_name
customer.Email = email
}
I'm getting 3 form values. Suppose I need to get 50 values, then the function will be much bigger.
You can parse HTTP request body yourself, like to following
option 1:
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/json"
"log"
)
type Customer struct {
FirstName string `json:"first_name" bson:"first_name"`
LastName string `json:"last_name" bson:"last_name"`
Email string `json:"email" bson:"email"`
}
func process(context *gin.Context) {
var customer = &Customer{}
req := context.Request
err := json.NewDecoder(req.Body).Decode(customer)
if err != nil {
log.Fatal()
}
}
option 2:
Encoding to map to decode to struct (not recommended)
import (
"github.com/gin-gonic/gin"
"encoding/json"
"bytes"
"log"
)
type Customer struct {
FirstName string `json:"first_name" bson:"first_name"`
LastName string `json:"last_name" bson:"last_name"`
Email string `json:"email" bson:"email"`
}
func Process(context *gin.Context) {
req := context.Request
var aMap = map[string]interface{}{}
for key, values := range req.PostForm {
aMap[key]=values[0]
}
var buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(aMap)
if err != nil {
log.Fatal(err)
}
var customer = &Customer{}
json.NewDecoder(buf).Decode(customer)
if err != nil {
log.Fatal(err)
}
}
As the mkopriva tells me a short way to do this. I got the answer for it. It can be shorter by doing the following code.
type Customer struct {
FirstName string `form:"first_name" json:"first_name" bson:"first_name"`
LastName string `form:"last_name" json:"last_name" bson:"last_name"`
Email string `form:"email" json:"email" bson:"email"`
}
In the function the code is:-
customer := new(Customer)
if err := c.Bind(customer); err != nil {
return nil, err
}
fmt.Println(customer)
It will print the data from the form-data of the postman.