diff --git a/cmd/tagliatelle/tagliatelle.go b/cmd/tagliatelle/tagliatelle.go index b5acc18..3e35137 100644 --- a/cmd/tagliatelle/tagliatelle.go +++ b/cmd/tagliatelle/tagliatelle.go @@ -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, } diff --git a/readme.md b/readme.md index f58f9ee..7bd728d 100644 --- a/readme.md +++ b/readme.md @@ -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` @@ -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 @@ -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 diff --git a/tagliatelle.go b/tagliatelle.go index 53e77d1..c465376 100644 --- a/tagliatelle.go +++ b/tagliatelle.go @@ -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": @@ -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, '-') +} diff --git a/tagliatelle_test.go b/tagliatelle_test.go index 355cb41..7771a76 100644 --- a/tagliatelle_test.go +++ b/tagliatelle_test.go @@ -18,6 +18,7 @@ func TestAnalyzer(t *testing.T) { "bson": "camel", "avro": "snake", "mapstructure": "kebab", + "header": "header", }, UseFieldName: true, } diff --git a/testdata/src/a/sample.go b/testdata/src/a/sample.go index b35a499..358381d 100644 --- a/testdata/src/a/sample.go +++ b/testdata/src/a/sample.go @@ -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 {