Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Peyton-Spencer/jsontagger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go JSON Tagger

A simple CLI tool to transform JSON struct tags in Go files between snake_case and camelCase formats. This tool uses the caseconv package for case conversion. It can both modify existing JSON tags and generate new tags for fields that don't have them.

Usage

# Convert JSON tags to camelCase (default)
jsontagger -file path/to/your/file.go

# Convert JSON tags to snake_case
jsontagger -file path/to/your/file.go -snake

# Explicitly specify camelCase (same as default)
jsontagger -file path/to/your/file.go -camel

Go Tool

use in your project

go get -tool github.com/peyton-spencer/jsontagger
go tool jsontagger -file path/to/your/file.go

Examples

Modifying existing JSON tags

Original struct with snake_case tags:

type User struct {
    UserID      int    `json:"user_id"`
    FirstName   string `json:"first_name"`
    LastName    string `json:"last_name,omitempty"`
}

After running with -camel:

type User struct {
    UserID      int    `json:"userId"`
    FirstName   string `json:"firstName"`
    LastName    string `json:"lastName,omitempty"`
}

Original struct with camelCase tags:

type Product struct {
    ProductID   int     `json:"productId"`
    Name        string  `json:"name"`
    UnitPrice   float64 `json:"unitPrice,omitempty"`
}

After running with -snake:

type Product struct {
    ProductID   int     `json:"product_id"`
    Name        string  `json:"name"`
    UnitPrice   float64 `json:"unit_price,omitempty"`
}

Generating new JSON tags

Original struct without any tags:

type Person struct {
    ID          int
    FirstName   string
    LastName    string
    DateOfBirth string
}

After running with -camel:

type Person struct {
    ID          int    `json:"id"`
    FirstName   string `json:"firstName"`
    LastName    string `json:"lastName"`
    DateOfBirth string `json:"dateOfBirth"`
}

After running with -snake:

type Person struct {
    ID          int    `json:"id"`
    FirstName   string `json:"first_name"`
    LastName    string `json:"last_name"`
    DateOfBirth string `json:"date_of_birth"`
}

Working with mixed and non-JSON tags

Original struct with mixed tags:

type Project struct {
    ID          int    `db:"project_id"`
    Name        string `validate:"required"`
    Description string
}

After running with -camel:

type Project struct {
    ID          int    `db:"project_id" json:"id"`
    Name        string `validate:"required" json:"name"`
    Description string `json:"description"`
}

Build

go build

Dependencies

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages