Skip to content

Commit

Permalink
bugfix:add cases.NoLower option (#7052)
Browse files Browse the repository at this point in the history
<!--  Thanks for sending a pull request!  Before submitting:

1. Read our CONTRIBUTING.md guide
2. Name your PR as `<Feature Area>: Describe your change`.
a. Do not end the title with punctuation. It will be added in the
changelog.
b. Start with an imperative verb. Example: Fix the latency between
System A and System B.
  c. Use sentence case, not title case.
d. Use a complete phrase or sentence. The PR title will appear in a
changelog, so help other people understand what your change will be.
3. Rebase your PR if it gets out of sync with main
-->

**What this PR does / why we need it**:
see #7048 
if we don't apply cases.NoLower, it will upper first alphabet lower the
others
```go
package main

import (
	"fmt"
	"golang.org/x/text/cases"
	"golang.org/x/text/language"
	"strings"
)

func main() {
	source := "testAPI"
	fmt.Println(cases.Title(language.Und).String(source)) // out: Testapi
	fmt.Println(cases.Title(language.Und, cases.NoLower).String(source)) // out: TestAPI
	fmt.Println(strings.Title(source)) // out: TestAPI
}
```
**Which issue(s) this PR fixes**:


**Special notes for your reviewer**:

<!--
Note about CHANGELOG entries, if a change adds:
* an important feature
* fixes an issue present in a previous release, 
* causes a change in operation that would be useful for an operator of
Loki to know
then please add a CHANGELOG entry.

For documentation changes, build changes, simple fixes etc please skip
this step. We are attempting to curate a changelog of the most relevant
and important changes to be easier to ingest by end users of Loki.

Note about the upgrade guide, if this changes:
* default configuration values
* metric names or label names
* changes existing log lines such as the metrics.go query output line
* configuration parameters 
* anything to do with any API
* any other change that would require special attention or extra steps
to upgrade
Please document clearly what changed AND what needs to be done in the
upgrade guide.
-->
**Checklist**
- [ ] Documentation added
- [ ] Tests updated
- [ ] Is this an important fix or new feature? Add an entry in the
`CHANGELOG.md`.
- [ ] Changes that require user attention or interaction to upgrade are
documented in `docs/sources/upgrading/_index.md`

Signed-off-by: wujw39640 <[email protected]>
Signed-off-by: wujunwei <[email protected]>
Co-authored-by: wujw39640 <[email protected]>
  • Loading branch information
wujunwei and wujw39640 committed Sep 7, 2022
1 parent 30548cc commit eaf7f34
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/util/cfg/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func categorizedUsage(fs *flag.FlagSet) func() {
if name == "" {
continue
}
fmt.Fprintf(fs.Output(), " %s:\n", cases.Title(language.Und).String(name))
fmt.Fprintf(fs.Output(), " %s:\n", cases.Title(language.Und, cases.NoLower).String(name))
for _, u := range categories[name] {
fmt.Fprintln(fs.Output(), u)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/util/cfg/flag_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cfg

import (
"bytes"
"flag"
"testing"
"time"
Expand Down Expand Up @@ -56,3 +57,22 @@ func TestFlags(t *testing.T) {
},
}, data)
}

// TestCategorizedUsage checks that the name of every flag can be "Titled" correctly
func TestCategorizedUsage(t *testing.T) {
output := &bytes.Buffer{}
fs := flag.NewFlagSet(t.Name(), flag.PanicOnError)
fs.SetOutput(output)
// "TestAPI" expected
fs.String("testAPI.one", "", "")
fs.String("testAPI.two", "", "")
// "TESTapi" expected
fs.String("tESTapi.one", "", "")
fs.String("tESTapi.two", "", "")
// "TestAPI" expected
fs.String("TestAPI.one", "", "")
fs.String("TestAPI.two", "", "")
categorizedUsage(fs)()
expected := "Usage of TestCategorizedUsage:\n\n TestAPI:\n -TestAPI.one string:\n \n -TestAPI.two string:\n \n\n TESTapi:\n -tESTapi.one string:\n \n -tESTapi.two string:\n \n\n TestAPI:\n -testAPI.one string:\n \n -testAPI.two string:\n \n\n"
assert.Equal(t, expected, output.String())
}

0 comments on commit eaf7f34

Please sign in to comment.