Skip to content

Commit

Permalink
Merge pull request #450 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.111.0
  • Loading branch information
andyone authored Mar 24, 2024
2 parents cb9c9aa + 47b6795 commit 3ca60ec
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Changelog

### 12.111.0

* `[knf/united]` Added helper method `AddOptions`
* `[options]` Added methods `V.String` and `Map.String`
* `[support]` Minor UI improvement
* `[options]` Code refactoring

### 12.110.1

* `[support/pkgs]` Improved formatting
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.110.1"
const VERSION = "12.111.0"

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

Expand Down
11 changes: 11 additions & 0 deletions knf/united/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ func ExampleCombine() {
GetL("section:list")
}

func ExampleAddOptions() {
m := options.Map{}

AddOptions(m, "test:option-one", "test:option-two")

fmt.Printf("Map size: %d\n", len(m))

// Output:
// Map size: 2
}

func ExampleSimple() {
m := Simple("test:option-one")

Expand Down
11 changes: 11 additions & 0 deletions knf/united/united.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ func Combine(mappings ...Mapping) {
global = config
}

// AddOptions adds options with knf properties to map
func AddOptions(m options.Map, names ...string) {
if m == nil {
return
}

for _, n := range names {
m.Set(ToOption(n), &options.V{})
}
}

// Simple creates simple mapping for knf property
// section:property → --section-property + SECTION_PROPERTY
func Simple(name string) Mapping {
Expand Down
10 changes: 10 additions & 0 deletions knf/united/united_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,14 @@ func (s *UnitedSuite) TestHelpers(c *C) {
c.Assert(m.Property, Equals, "test:option-one")
c.Assert(m.Option, Equals, "test-option-one")
c.Assert(m.Variable, Equals, "TEST_OPTION_ONE")

var op options.Map

c.Assert(func() { AddOptions(op, "test") }, NotPanics)

op = options.Map{}

AddOptions(op, "test")

c.Assert(op, HasLen, 1)
}
38 changes: 38 additions & 0 deletions options/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,44 @@ func ExampleFormat() {

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

func ExampleV_String() {
v := &V{
Type: INT,
Value: 25,
Alias: "items",
Conflicts: "E:empty",
Bound: "c:create",
Min: 10,
Max: 1000,
}

fmt.Println(v.String())

// Output:
// Int{Value:25 Min:10 Max:1000 Alias:items Conflicts:E:empty Bound:c:create}
}

func ExampleMap_String() {
m := Map{
"s:size": &V{
Type: INT,
Value: 25,
Alias: "items",
Conflicts: "E:empty",
Bound: "c:create",
Min: 10,
Max: 1000,
},
}

fmt.Println(m.String())

// Output:
// options.Map[size:Int{Value:25 Min:10 Max:1000 Alias:items Conflicts:E:empty Bound:c:create}]
}

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

func ExampleOptions_Add() {
opts := NewOptions()

Expand Down
93 changes: 86 additions & 7 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (

// Options types
const (
STRING = iota // String option
INT // Int/Uint option
BOOL // Boolean option
FLOAT // Floating number option
MIXED // String or boolean option
STRING uint8 = iota // String option
INT // Int/Uint option
BOOL // Boolean option
FLOAT // Floating number option
MIXED // String or boolean option
)

// Error codes
Expand All @@ -44,7 +44,7 @@ const (

// V is basic option struct
type V struct {
Type int // option type
Type uint8 // option type
Max float64 // maximum integer option value
Min float64 // minimum integer option value
Alias string // list of aliases
Expand Down Expand Up @@ -553,6 +553,85 @@ func F(opt string) string {

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

// String returns string representation of options map
func (m Map) String() string {
if m == nil {
return "options.Map[Nil]"
}

if len(m) == 0 {
return "options.Map[]"
}

result := "options.Map["

for n, v := range m {
result += parseName(n).Long + ":" + v.String() + " "
}

return strings.TrimRight(result, " ") + "]"
}

// String returns string representation of option
func (v *V) String() string {
if v == nil {
return "Nil{}"
}

var result string

switch v.Type {
case STRING:
result = "String{"
case INT:
result = "Int{"
case BOOL:
result = "Bool{"
case FLOAT:
result = "Float{"
case MIXED:
result = "Mixed{"
default:
result = "Unknown{"
}

if v.Value != nil {
result += fmt.Sprintf("Value:%v ", v.Value)
}

if v.Min != 0 {
result += fmt.Sprintf("Min:%g ", v.Min)
}

if v.Max != 0 {
result += fmt.Sprintf("Max:%g ", v.Max)
}

if v.Alias != "" {
result += fmt.Sprintf("Alias:%s ", v.Alias)
}

if v.Conflicts != "" {
result += fmt.Sprintf("Conflicts:%s ", v.Conflicts)
}

if v.Bound != "" {
result += fmt.Sprintf("Bound:%s ", v.Bound)
}

if v.Mergeble {
result += "Mergeble:Yes "
}

if v.Required {
result += "Required:Yes "
}

return strings.TrimRight(result, " ") + "}"
}

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

// I think it is okay to have such a long and complicated method for parsing data
// because it has a lot of logic which can't be separated into different methods
// without losing code readability
Expand Down Expand Up @@ -918,7 +997,7 @@ func isSupportedType(v any) bool {
return false
}

func guessType(v any) int {
func guessType(v any) uint8 {
switch v.(type) {
case string:
return STRING
Expand Down
46 changes: 46 additions & 0 deletions options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,52 @@ func (s *OptUtilSuite) TestGuessType(c *C) {
c.Assert(guessType(3.3), Equals, FLOAT)
}

func (s *OptUtilSuite) TestVString(c *C) {
var v *V

c.Assert(v.String(), Equals, "Nil{}")

v = &V{
Type: STRING,
Value: "test",
Alias: "test2",
Conflicts: "test3",
Bound: "test4",
Min: 10,
Max: 1000,
Mergeble: true,
Required: true,
}

c.Assert(v.String(), Equals, "String{Value:test Min:10 Max:1000 Alias:test2 Conflicts:test3 Bound:test4 Mergeble:Yes Required:Yes}")

v = &V{Type: INT}
c.Assert(v.String(), Equals, "Int{}")

v = &V{Type: BOOL}
c.Assert(v.String(), Equals, "Bool{}")

v = &V{Type: FLOAT}
c.Assert(v.String(), Equals, "Float{}")

v = &V{Type: MIXED}
c.Assert(v.String(), Equals, "Mixed{}")

v = &V{Type: 10}
c.Assert(v.String(), Equals, "Unknown{}")
}

func (s *OptUtilSuite) TestMapString(c *C) {
var m Map
c.Assert(m.String(), Equals, "options.Map[Nil]")

m = Map{}
c.Assert(m.String(), Equals, "options.Map[]")

m = Map{"t:test": &V{Type: INT, Value: 25}}
c.Assert(m.String(), Equals, "options.Map[test:Int{Value:25}]")
}

func (s *OptUtilSuite) TestArguments(c *C) {
a := Arguments{"A.txt", "b.png", "c.txt", "d.jpg", "e.txte"}

Expand Down
4 changes: 2 additions & 2 deletions support/support_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ func (i *Info) printChecksInfo() {
case CHECK_OK, CHECK_SKIP:
fmtc.Printf(" {s}— {&}%s{!}\n", c.Message)
case CHECK_WARN:
fmtc.Printf(" {y}— {&}%s{!}\n", c.Message)
fmtc.Printf(" {s}— {y}{&}%s{!}\n", c.Message)
case CHECK_ERROR:
fmtc.Printf(" {r}— {&}%s{!}\n", c.Message)
fmtc.Printf(" {s}— {r}{&}%s{!}\n", c.Message)
}
}
}
Expand Down

0 comments on commit 3ca60ec

Please sign in to comment.