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 linter is about

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

This linter is not intended to validate the fact a tag in valid or not.

How to use the linter

As a golangci-lint linter

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

linters-settings:
  tagliatelle:
    # Checks the struct tag name case.
    case:
      # Defines the association between tag name and case.
      # Any struct tag name can be used.
      # Supported string cases:
      # - `camel`
      # - `pascal`
      # - `kebab`
      # - `snake`
      # - `upperSnake`
      # - `goCamel`
      # - `goPascal`
      # - `goKebab`
      # - `goSnake`
      # - `upper`
      # - `lower`
      # - `header`
      rules:
        json: camel
        yaml: camel
        xml: camel
        toml: camel
        bson: camel
        avro: snake
        mapstructure: kebab
        env: upperSnake
        envconfig: upperSnake
        whatever: snake
      # Defines the association between tag name and case.
      # Important: the `extended-rules` overrides `rules`.
      # Default: empty
      extended-rules:
        json:
          # Supported string cases:
          # - `camel`
          # - `pascal`
          # - `kebab`
          # - `snake`
          # - `upperSnake`
          # - `goCamel`
          # - `goPascal`
          # - `goKebab`
          # - `goSnake`
          # - `header`
          # - `lower`
          # - `header`
          #
          # Required
          case: camel
          # Adds 'AMQP', 'DB', 'GID', 'RTP', 'SIP', 'TS' to initialisms,
          # and removes 'LHS', 'RHS' from initialisms.
          # Default: false
          extra-initialisms: true
          # Defines initialism additions and overrides.
          # Default: empty
          initialism-overrides:
            DB: true # add a new initialism
            LHS: false # disable a default initialism.
            # ...
      # Uses the struct field name to check the name of the struct tag.
      # Default: false
      use-field-name: true
      # The field names to ignore.
      # Default: []
      ignored-fields:
        - Bar
        - Foo
      # Overrides the default/root configuration.
      # Default: []
      overrides:
        -
          # The package path (uses `/` only as a separator).
          # Required
          pkg: foo/bar
          # Default: empty or the same as the default/root configuration.
          rules:
            json: snake
            xml: pascal
          # Default: empty or the same as the default/root configuration.
          extended-rules:
            # same options as the base `extended-rules`.
          # Default: false (WARNING: it doesn't follow the default/root configuration)
          use-field-name: true
          # The field names to ignore.
          # Default: [] or the same as the default/root configuration.
          ignored-fields:
            - Bar
            - Foo
          # Ignore the package (takes precedence over all other configurations).
          # Default: false
          ignore: true

Examples

Overrides case rules for the package foo/bar:

linters-settings:
  tagliatelle:
    case:
      rules:
        json: camel
        yaml: camel
        xml: camel
      overrides:
        - pkg: foo/bar
          rules:
            json: snake
            xml: pascal

Ignore fields inside the package foo/bar:

linters-settings:
  tagliatelle:
    case:
      rules:
        json: camel
        yaml: camel
        xml: camel
      overrides:
        - pkg: foo/bar
          ignored-fields:
            - Bar
            - Foo

Ignore the package foo/bar:

linters-settings:
  tagliatelle:
    case:
      rules:
        json: camel
        yaml: camel
        xml: camel
      overrides:
        - pkg: foo/bar
          ignore: true

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 linter 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 linter you want to use kebab.

This option is only available via golangci-lint.

linters-settings:
  tagliatelle:
    # Check the struck tag name case.
    case:
      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
      # Use the struct field name to check the name of the struct tag.
      # Default: false
      use-field-name: true