Home

Awesome

Tagliatelle

Sponsor Build Status

A linter that handles struct tags.

Supported string casing:

SourceCamel CaseGo Camel Case
GooIDgooIdgooID
HTTPStatusCodehttpStatusCodehttpStatusCode
FooBARfooBarfooBar
URLurlurl
IDidid
hostIPhostIphostIP
JSONjsonjson
JSONNamejsonNamejsonName
NameJSONnameJsonnameJSON
UneTêteuneTêteuneTête
SourcePascal CaseGo Pascal Case
GooIDGooIdGooID
HTTPStatusCodeHttpStatusCodeHTTPStatusCode
FooBARFooBarFooBar
URLUrlURL
IDIdID
hostIPHostIpHostIP
JSONJsonJSON
JSONNameJsonNameJSONName
NameJSONNameJsonNameJSON
UneTêteUneTêteUneTête
SourceSnake CaseUpper Snake CaseGo Snake Case
GooIDgoo_idGOO_IDgoo_ID
HTTPStatusCodehttp_status_codeHTTP_STATUS_CODEHTTP_status_code
FooBARfoo_barFOO_BARfoo_bar
URLurlURLURL
IDidIDID
hostIPhost_ipHOST_IPhost_IP
JSONjsonJSONJSON
JSONNamejson_nameJSON_NAMEJSON_name
NameJSONname_jsonNAME_JSONname_JSON
UneTêteune_têteUNE_TÊTEune_tête
SourceKebab CaseGo KebabCase
GooIDgoo-idgoo-ID
HTTPStatusCodehttp-status-codeHTTP-status-code
FooBARfoo-barfoo-bar
URLurlURL
IDidID
hostIPhost-iphost-IP
JSONjsonJSON
JSONNamejson-nameJSON-name
NameJSONname-jsonname-JSON
UneTêteune-têteune-tête
SourceHeader Case
GooIDGoo-Id
HTTPStatusCodeHttp-Status-Code
FooBARFoo-Bar
URLUrl
IDId
hostIPHost-Ip
JSONJson
JSONNameJson-Name
NameJSONName-Json
UneTêteUne-Tête

Examples

// json and camel case
type Foo struct {
    ID     string `json:"ID"` // must be "id"
    UserID string `json:"UserID"`// must be "userId"
    Name   string `json:"name"`
    Value  string `json:"val,omitempty"`// must be "value"
}

What this tool is about

This tool is about validating tags according to rules you define. The tool also allows to fix tags according to the rules you defined.

This tool is not intended to validate the fact a tag in valid or not. To do that, you can use go vet, or use golangci-lint "go vet" linter.

How to use the tool

As a golangci-lint linter

Define the rules, you want via your golangci-lint configuration file:

linters-settings:
  tagliatelle:
    # Check the struck tag name case.
    case:
      # Use the struct field name to check the name of the struct tag.
      # Default: false
      use-field-name: true
      rules:
        # Any struct tag type can be used.
        # Support string case: `camel`, `pascal`, `kebab`, `snake`, `upperSnake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`, `header`.
        json: camel
        yaml: camel
        xml: camel
        toml: camel
        bson: camel
        avro: snake
        mapstructure: kebab
        env: upperSnake
        envconfig: upperSnake

More information here https://golangci-lint.run/usage/linters/#tagliatelle

Install and run it from the binary

Not recommended.

go install github.com/ldez/tagliatelle/cmd/tagliatelle@latest

then launch it manually.

Rules

Here are the default rules for the well known and used tags, when using tagliatelle as a binary or golangci-lint linter:

Custom Rules

The tool is not limited to the tags used in example, you can use it to validate any tag.

You can add your own tag, for example whatever and tells the tool you want to use kebab.

This option is only available via golangci-lint.

linters-settings:
  tagliatelle:
    # Check the struck tag name case.
    case:
      # Use the struct field name to check the name of the struct tag.
      # Default: false
      use-field-name: true
      rules:
        # Any struct tag type can be used.
        # Support string case: `camel`, `pascal`, `kebab`, `snake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`
        json: camel
        yaml: camel
        xml: camel
        toml: camel
        whatever: kebab