Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add header case #15

Merged
merged 6 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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