Skip to content

Commit

Permalink
[knf/united] Add usage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Mar 7, 2024
1 parent 5930c66 commit e7b3ac4
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 12.104.0

* `[knf/united]` Added validation using `knf` validators
* `[knf/united]` Added usage examples

### 12.103.0

Expand Down
1 change: 1 addition & 0 deletions knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ var (
Minute = 60 * Second
Hour = 60 * Minute
Day = 24 * Hour
Week = 7 * Day
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
199 changes: 199 additions & 0 deletions knf/united/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package united

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"

"github.com/essentialkaos/ek/v12/knf"
"github.com/essentialkaos/ek/v12/options"
)

// ////////////////////////////////////////////////////////////////////////////////// //

func ExampleCombine() {
// Load global KNF config
err := knf.Global("/path/to/your/config.knf")

if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

optMap := options.Map{
"O:option-one": {},
"k:option-two": {},
}

// Parse command-line options
_, errs := options.Parse(optMap)

if len(errs) != 0 {
for _, err := range errs {
fmt.Printf("Error: %v\n", err)
}

return
}

// Combine combine KNF configuration, options and environment variables
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

// Read string value
GetS("section:string")

// Read integer value
GetI("section:int")

// Read float value
GetF("section:float")

// Read boolean value
GetB("section:boolean")

// Read file mode value
GetM("section:file-mode")

// Read duration as seconds
GetD("section:duration", Second)

// Read duration as minutes
GetD("section:duration", Minute)

// Read time duration
GetTD("section:time-duration")

// Read timestamp
GetTS("section:timestamp")

// Read timezone
GetTZ("section:timezone")

// Read list
GetL("section:list")
}

func ExampleGetS() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %s\n", GetS("user:name"))
}

func ExampleGetB() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %t\n", GetB("user:is-admin"))
}

func ExampleGetI() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %d\n", GetI("user:uid"))
}

func ExampleGetI64() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %d\n", GetI64("user:uid"))
}

func ExampleGetU() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %d\n", GetU("user:uid"))
}

func ExampleGetU64() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %d\n", GetU64("user:uid"))
}

func ExampleGetF() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %g\n", GetF("user:priority"))
}

func ExampleGetM() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetF("user:default-mode"))
}

func ExampleGetD() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetD("user:timeout", Minute))
}

func ExampleGetTD() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetTD("user:timeout"))
}

func ExampleGetTS() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetTS("user:created"))
}

func ExampleGetTZ() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetTZ("service:timezone"))
}

func ExampleGetL() {
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetL("issue:labels"))
}
15 changes: 12 additions & 3 deletions knf/united/united.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ type DurationMod = knf.DurationMod

// ////////////////////////////////////////////////////////////////////////////////// //

var (
Millisecond = knf.Millisecond
Second = knf.Second
Minute = knf.Minute
Hour = knf.Hour
Day = knf.Day
Week = knf.Week
)

// ////////////////////////////////////////////////////////////////////////////////// //

var global *config

var optionHasFunc = options.Has
Expand All @@ -47,7 +58,7 @@ var optionGetFunc = options.GetS

// Combine applies mappings to combine knf properties, options, and environment
// variables
func Combine(mappings ...Mapping) error {
func Combine(mappings ...Mapping) {
config := &config{
mappings: make(map[string]Mapping),
}
Expand All @@ -57,8 +68,6 @@ func Combine(mappings ...Mapping) error {
}

global = config

return nil
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
16 changes: 4 additions & 12 deletions knf/united/united_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *UnitedSuite) TestKNFOnly(c *C) {
c.Assert(GetTZ("test:timezone").String(), Equals, "Europe/Zurich")
c.Assert(GetL("test:list"), DeepEquals, []string{"Test1", "Test2"})

err := Combine(
Combine(
Mapping{"test:string", "test-string", "TEST_STRING"},
Mapping{"test:integer", "test-integer", "TEST_INTEGER"},
Mapping{"test:float", "test-float", "TEST_FLOAT"},
Expand All @@ -93,8 +93,6 @@ func (s *UnitedSuite) TestKNFOnly(c *C) {
Mapping{"test:list", "test-list", "TEST_LIST"},
)

c.Assert(err, IsNil)

c.Assert(GetS("test:string"), Equals, "Test")
c.Assert(GetI("test:integer"), Equals, 123)
c.Assert(GetI64("test:integer"), Equals, int64(123))
Expand Down Expand Up @@ -143,7 +141,7 @@ func (s *UnitedSuite) TestWithOptions(c *C) {

c.Assert(errs, HasLen, 0)

err := Combine(
Combine(
Mapping{"test:string", "test-string", "TEST_STRING"},
Mapping{"test:integer", "test-integer", "TEST_INTEGER"},
Mapping{"test:float", "test-float", "TEST_FLOAT"},
Expand All @@ -156,8 +154,6 @@ func (s *UnitedSuite) TestWithOptions(c *C) {
Mapping{"test:list", "test-list", "TEST_LIST"},
)

c.Assert(err, IsNil)

optionHasFunc = opts.Has
optionGetFunc = opts.GetS

Expand Down Expand Up @@ -191,7 +187,7 @@ func (s *UnitedSuite) TestWithEnv(c *C) {
os.Setenv("TEST_TIMEZONE", "Europe/Berlin")
os.Setenv("TEST_LIST", "Test1Env,Test2Env")

err := Combine(
Combine(
Mapping{"test:string", "test-string", "TEST_STRING"},
Mapping{"test:integer", "test-integer", "TEST_INTEGER"},
Mapping{"test:float", "test-float", "TEST_FLOAT"},
Expand All @@ -204,8 +200,6 @@ func (s *UnitedSuite) TestWithEnv(c *C) {
Mapping{"test:list", "test-list", "TEST_LIST"},
)

c.Assert(err, IsNil)

c.Assert(GetS("test:string"), Equals, "TestEnv")
c.Assert(GetI("test:integer"), Equals, 789)
c.Assert(GetI64("test:integer"), Equals, int64(789))
Expand All @@ -230,7 +224,7 @@ func (s *UnitedSuite) TestValidation(c *C) {

c.Assert(errs, HasLen, 1)

err := Combine(
Combine(
Mapping{"test:string", "test-string", "TEST_STRING"},
Mapping{"test:integer", "test-integer", "TEST_INTEGER"},
Mapping{"test:float", "test-float", "TEST_FLOAT"},
Expand All @@ -243,8 +237,6 @@ func (s *UnitedSuite) TestValidation(c *C) {
Mapping{"test:list", "test-list", "TEST_LIST"},
)

c.Assert(err, IsNil)

errs = Validate([]*knf.Validator{
{"test:string", knfv.Empty, nil},
{"test:integer", knfv.Greater, 100},
Expand Down

0 comments on commit e7b3ac4

Please sign in to comment.