Skip to content

Commit

Permalink
feat: add header case (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
abemedia and ldez authored Jan 10, 2023
1 parent 5fed40b commit 3d2aa28
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
11 changes: 6 additions & 5 deletions cmd/tagliatelle/tagliatelle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
func main() {
cfg := tagliatelle.Config{
Rules: map[string]string{
"json": "camel",
"yaml": "camel",
"xml": "camel",
"bson": "camel",
"avro": "snake",
"json": "camel",
"yaml": "camel",
"xml": "camel",
"bson": "camel",
"avro": "snake",
"header": "header",
},
UseFieldName: true,
}
Expand Down
17 changes: 16 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Supported string casing:
- `goPascal` Respects [Go's common initialisms](https://github.com/golang/lint/blob/83fdc39ff7b56453e3793356bcff3070b9b96445/lint.go#L770-L809) (e.g. HttpResponse -> HTTPResponse).
- `goKebab` Respects [Go's common initialisms](https://github.com/golang/lint/blob/83fdc39ff7b56453e3793356bcff3070b9b96445/lint.go#L770-L809) (e.g. HttpResponse -> HTTPResponse).
- `goSnake` Respects [Go's common initialisms](https://github.com/golang/lint/blob/83fdc39ff7b56453e3793356bcff3070b9b96445/lint.go#L770-L809) (e.g. HttpResponse -> HTTPResponse).
- `header`
- `upper`
- `lower`

Expand Down Expand Up @@ -70,6 +71,19 @@ Supported string casing:
| NameJSON | name-json | name-JSON |
| UneTête | une-tête | une-tête |

| Source | Header Case |
|----------------|------------------|
| GooID | Goo-Id |
| HTTPStatusCode | Http-Status-Code |
| FooBAR | Foo-Bar |
| URL | Url |
| ID | Id |
| hostIP | Host-Ip |
| JSON | Json |
| JSONName | Json-Name |
| NameJSON | Name-Json |
| UneTête | Une-Tête |

## Examples

```go
Expand Down Expand Up @@ -130,9 +144,10 @@ Here are the default rules for the well known and used tags, when using tagliate

- `json`: `camel`
- `yaml`: `camel`
- `xml`: `camel`
- `xml`: `camel`
- `bson`: `camel`
- `avro`: `snake`
- `header`: `header`

### Custom Rules

Expand Down
6 changes: 6 additions & 0 deletions tagliatelle.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ func getConverter(c string) (func(s string) string, error) {
return strcase.ToGoKebab, nil
case "goSnake":
return strcase.ToGoSnake, nil
case "header":
return toHeader, nil
case "upper":
return strings.ToUpper, nil
case "lower":
Expand All @@ -208,3 +210,7 @@ func getConverter(c string) (func(s string) string, error) {
return nil, fmt.Errorf("unsupported case: %s", c)
}
}

func toHeader(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '-')
}
1 change: 1 addition & 0 deletions tagliatelle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestAnalyzer(t *testing.T) {
"bson": "camel",
"avro": "snake",
"mapstructure": "kebab",
"header": "header",
},
UseFieldName: true,
}
Expand Down
12 changes: 7 additions & 5 deletions testdata/src/a/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ type Bir struct {
}

type Bur struct {
Name string
Value string `yaml:"Value"` // want `yaml\(camel\): got 'Value' want 'value'`
More string `json:"-"`
Also string `json:",omitempty"` // want `json\(camel\): got 'Also' want 'also'`
ReqPerS string `avro:"req_per_s"`
Name string
Value string `yaml:"Value"` // want `yaml\(camel\): got 'Value' want 'value'`
More string `json:"-"`
Also string `json:",omitempty"` // want `json\(camel\): got 'Also' want 'also'`
ReqPerS string `avro:"req_per_s"`
HeaderValue string `header:"Header-Value"`
WrongHeaderValue string `header:"Header_Value"` // want `header\(header\): got 'Header_Value' want 'Wrong-Header-Value'`
}

type Quux struct {
Expand Down

0 comments on commit 3d2aa28

Please sign in to comment.