modulepackage
0.0.0-20240423093913-12c0f20f5978
Repository: https://github.com/go-shafaq/defcase.git
Documentation: pkg.go.dev
# README
DefCase
Problem
Tagging structs takes time and sometimes it could be annoying (probably because we do such easy thing so many times)
type User struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
}
type Post struct {
Title string `form:"title"`
Description string `form:"description"`
Photo *multipart.FileHeader `form:"photo"`
}
Solution
package main
import (
"github.com/go-shafaq/defcase"
"github.com/go-shafaq/gin"
)
type User struct {
ID int
FirstName string
LastName string
Email string
}
func main() {
// Get Default Case
dcase := defcase.Get()
// Set a case for "json" and "form" tags
dcase.SetCase("json", "*", defcase.Snak_case)
dcase.SetCase("form", "*", defcase.Snak_case)
// Set a Default Case to library
gin.SetDefCase(dcase)
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, User{
ID: 1,
FirstName: "Mahmud",
LastName: "Hasan",
Email: "[email protected]",
})
})
r.Run()
}
Result of localhost:8080 request
Pretty-print
{
"id": 1,
"first_name": "Mahmud",
"last_name": "Hasan",
"email": "[email protected]"
}
Cases
["ID", "Name", "UserId", "IHopeItWillBeHelpful"]
Function | Result |
---|---|
ID Name UserId IHopeItWillBeHelpful | |
snak_case | id name user_id i_hope_it_will_be_helpful |
camelCase | id name userId iHopeItWillBeHelpful |
kebab-case | id name user-id i-hope-it-will-be-helpful |
PascalCase | Id Name UserId IHopeItWillBeHelpful |
SCREAMING_SNAKE_CASE | ID NAME USER_ID I_HOPE_IT_WILL_BE_HELPFUL |
# Constants
id name user_id i_hope_it_will_be_helpful.
id name userId iHopeItWillBeHelpful.
No description provided by the author
id name user-id i-hope-it-will-be-helpful.
Id Name UserId IHopeItWillBeHelpful.
ID Name UserId IHopeItWillBeHelpful.
# Interfaces
No description provided by the author
# Type aliases
No description provided by the author