Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃 How can sql NullString types be handled in c.BodyParser ? #1426

Closed
VictorVargas opened this issue Jul 7, 2021 · 2 comments 路 Fixed by #1100
Closed

馃 How can sql NullString types be handled in c.BodyParser ? #1426

VictorVargas opened this issue Jul 7, 2021 · 2 comments 路 Fixed by #1100

Comments

@VictorVargas
Copy link

Question description
Hello I have this problem when trying to use the BodyParse in my structure with the special types of SQL to be able to receive null values, BodyParse apparently cannot handle them

Code snippet

package main

import (
	"fmt"
	"time"

	"github.com/google/uuid"
        "github.com/gofiber/fiber/v2"
)

type NullString sql.NullString

type Catalog struct {
	ID       uuid.UUID  `json:"id"`
	ParentID uuid.UUID  `json:"parent_id"`
	Name     string     `json:"name"`
	Addon    NullString `json:"addon"`
	Type     string     `json:"type"`
	CreateAt time.Time  `json:"create_at"`
	UpdateAt time.Time  `json:"update_at"`
}

func CatalogInsert(catalog *Catalog) Catalog {
	db := Connect()
	defer db.Close()

	var newCatalog Catalog
	id := uuid.New()
	fields := "id,name,addon,type"
	values := "UUID_TO_BIN(?),?,?,?"

	if catalog.ParentID != uuid.Nil {
		fields += ",parent_id"
		values += ",UUID_TO_BIN(?)"
	}

	query := fmt.Sprintf(`INSERT INTO catalogs (%s) VALUES (%s)`, fields, values)
	insert, err := db.Prepare(query)

	if err != nil {
		panic(err.Error()) // proper error handling instead of panic in your app
	}

	if catalog.ParentID != uuid.Nil {
		_, err = insert.Exec(
			id,
			catalog.Name,
			catalog.Addon,
			catalog.Type,
			catalog.ParentID,
		)
	} else {
		_, err = insert.Exec(
			id,
			catalog.Name,
			catalog.Addon,
			catalog.Type,
		)
	}

	if err != nil {
		panic(err.Error()) // proper error handling instead of panic in your app
	}

	defer insert.Close()

	results, err := db.Prepare(`SELECT
		BIN_TO_UUID(id) AS id,
		BIN_TO_UUID(parent_id) AS parent_id,
		name,
		addon,
		type
	FROM catalogs WHERE id = UUID_TO_BIN(?)`)

	if err != nil {
		panic(err.Error()) // proper error handling instead of panic in your app
	}

	err = results.QueryRow(id).Scan(
		newCatalog.ID,
		newCatalog.ParentID,
		newCatalog.Name,
		newCatalog.Addon,
		newCatalog.Type,
	)

	if err != nil {
		panic(err.Error()) // proper error handling instead of panic in your app
	}

	return newCatalog
}

func CatalogAdd(c *fiber.Ctx) error {
	catalog := new(Catalog)
	if err := c.BodyParser(catalog); err != nil {
		return err
	}

	newCatalog := CatalogInsert(catalog)

	return c.JSON(fiber.Map{
		"item": newCatalog,
	})
}

When I do the post, it throws me the following error
json: cannot unmarshal ""USA","type":"country"}" into Go struct field Catalog.addon of type NullString

How could I solve it.

Thank you for your answers.

@welcome
Copy link

welcome bot commented Jul 7, 2021

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@Itshardtopickanusername

I had similar issue, and ended up writing custom UnmarshalJSON. since BodyParser uses Unmarshaler interface (for JSON) you can handle it in your own way. I created new struct for the request body, and implemented the Unmarshaler + Marshaler interfaces to handle incoming and outgoing JSONs.

Hope this helped, if you still have the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants