From 5c00a8d33b8bd85790fa30a7fbcb48be2aba2bed Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 18 Mar 2024 00:38:52 +0300 Subject: [PATCH 1/2] [knf/united] Add method 'Simple' --- CHANGELOG.md | 4 ++++ ek.go | 2 +- knf/united/example_test.go | 13 ++++++++++++- knf/united/united.go | 6 ++++++ knf/united/united_test.go | 6 ++++++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42963873..bd1e3b65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ### 12.106.0 +* `[knf/united]` Added method `Simple` + +### 12.106.0 + * `[knf/united]` Added method `ToOption` with shortcut `O` * `[knf/united]` Added method `ToEnvVar` with shortcut `E` diff --git a/ek.go b/ek.go index 05356bbf..973cab0a 100644 --- a/ek.go +++ b/ek.go @@ -20,7 +20,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.106.0" +const VERSION = "12.107.0" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/knf/united/example_test.go b/knf/united/example_test.go index b9c4b9a9..1fd4308c 100644 --- a/knf/united/example_test.go +++ b/knf/united/example_test.go @@ -54,8 +54,10 @@ func ExampleCombine() { ) Combine( + // Create mapping manually Mapping{optOne, ToOption(optOne), ToEnvVar(optOne)}, - Mapping{optTwo, ToOption(optTwo), ToEnvVar(optTwo)}, + // Create simple mapping + Simple(optTwo), ) // Read string value @@ -92,6 +94,15 @@ func ExampleCombine() { GetL("section:list") } +func ExampleSimple() { + m := Simple("test:option-one") + + fmt.Printf("%s → --%s + %s\n", m.Property, m.Option, m.Variable) + + // Output: + // test:option-one → --test-option-one + TEST_OPTION_ONE +} + func ExampleToOption() { fmt.Println(ToOption("section:time-duration")) diff --git a/knf/united/united.go b/knf/united/united.go index 7d33a83a..64283ab1 100644 --- a/knf/united/united.go +++ b/knf/united/united.go @@ -72,6 +72,12 @@ func Combine(mappings ...Mapping) { global = config } +// Simple creates simple mapping for knf property +// section:property → --section-property + SECTION_PROPERTY +func Simple(name string) Mapping { + return Mapping{name, O(name), E(name)} +} + // ToOption converts knf property name to option name func ToOption(name string) string { return strutil.ReplaceAll(strings.ToLower(name), "_:", "-") diff --git a/knf/united/united_test.go b/knf/united/united_test.go index f5912a01..969350de 100644 --- a/knf/united/united_test.go +++ b/knf/united/united_test.go @@ -253,4 +253,10 @@ func (s *UnitedSuite) TestHelpers(c *C) { c.Assert(E(""), Equals, "") c.Assert(E("section:prop-one"), Equals, "SECTION_PROP_ONE") c.Assert(E("Section:Prop-Two"), Equals, "SECTION_PROP_TWO") + + m := Simple("test:option-one") + + c.Assert(m.Property, Equals, "test:option-one") + c.Assert(m.Option, Equals, "test-option-one") + c.Assert(m.Variable, Equals, "TEST_OPTION_ONE") } From 3fd3ec40ce7fd68ee31c1d32e3fab9a3bae0a11b Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 18 Mar 2024 00:41:18 +0300 Subject: [PATCH 2/2] Fix version in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd1e3b65..538ef249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## Changelog -### 12.106.0 +### 12.107.0 * `[knf/united]` Added method `Simple`