mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 13:04:02 +03:00
Rework cli
This commit is contained in:
@@ -3,7 +3,6 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TypeBool struct {
|
||||
@@ -11,15 +10,13 @@ type TypeBool struct {
|
||||
}
|
||||
|
||||
func (t *TypeBool) Set(data string) error {
|
||||
switch strings.ToLower(data) {
|
||||
case "1", "y", "yes", "enabled", "true":
|
||||
t.Value = true
|
||||
case "0", "n", "no", "disabled", "false":
|
||||
t.Value = false
|
||||
default:
|
||||
return fmt.Errorf("incorrect bool value %s", data)
|
||||
parsed, err := strconv.ParseBool(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("incorrect bool value: %s", data)
|
||||
}
|
||||
|
||||
t.Value = parsed
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -27,11 +24,11 @@ func (t TypeBool) Get(defaultValue bool) bool {
|
||||
return t.Value || defaultValue
|
||||
}
|
||||
|
||||
func (t *TypeBool) UnmarshalText(data []byte) error {
|
||||
func (t *TypeBool) UnmarshalJSON(data []byte) error {
|
||||
return t.Set(string(data))
|
||||
}
|
||||
|
||||
func (t TypeBool) MarshalText() ([]byte, error) {
|
||||
func (t TypeBool) MarshalJSON() ([]byte, error) {
|
||||
return []byte(t.String()), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -20,53 +20,41 @@ type TypeBoolTestSuite struct {
|
||||
}
|
||||
|
||||
func (suite *TypeBoolTestSuite) TestUnmarshalFail() {
|
||||
testData := []string{
|
||||
testData := []interface{}{
|
||||
"",
|
||||
"np",
|
||||
"нет",
|
||||
int(10),
|
||||
[]int{},
|
||||
}
|
||||
|
||||
for _, v := range testData {
|
||||
data, err := json.Marshal(map[string]string{
|
||||
data, err := json.Marshal(map[string]interface{}{
|
||||
"value": v,
|
||||
})
|
||||
suite.NoError(err)
|
||||
|
||||
suite.T().Run(v, func(t *testing.T) {
|
||||
suite.T().Run(fmt.Sprintf("%v", v), func(t *testing.T) {
|
||||
assert.Error(t, json.Unmarshal(data, &typeBoolTestStruct{}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *TypeBoolTestSuite) TestUnmarshalOk() {
|
||||
testData := map[string]bool{
|
||||
"0": false,
|
||||
"N": false,
|
||||
"nO": false,
|
||||
"no": false,
|
||||
"dISAbLEd": false,
|
||||
"False": false,
|
||||
"false": false,
|
||||
|
||||
"1": true,
|
||||
"y": true,
|
||||
"Yes": true,
|
||||
"yes": true,
|
||||
"enABLED": true,
|
||||
"True": true,
|
||||
"TRUE": true,
|
||||
"true": true,
|
||||
testData := []bool{
|
||||
true,
|
||||
false,
|
||||
}
|
||||
|
||||
for k, v := range testData {
|
||||
for _, v := range testData {
|
||||
value := v
|
||||
|
||||
data, err := json.Marshal(map[string]string{
|
||||
"value": k,
|
||||
data, err := json.Marshal(map[string]bool{
|
||||
"value": v,
|
||||
})
|
||||
suite.NoError(err)
|
||||
|
||||
suite.T().Run(k, func(t *testing.T) {
|
||||
suite.T().Run(strconv.FormatBool(v), func(t *testing.T) {
|
||||
testStruct := &typeBoolTestStruct{}
|
||||
assert.NoError(t, json.Unmarshal(data, testStruct))
|
||||
|
||||
@@ -81,18 +69,24 @@ func (suite *TypeBoolTestSuite) TestUnmarshalOk() {
|
||||
|
||||
func (suite *TypeBoolTestSuite) TestMarshalOk() {
|
||||
for _, v := range []bool{true, false} {
|
||||
name := strconv.FormatBool(v)
|
||||
value := v
|
||||
|
||||
suite.T().Run(name, func(t *testing.T) {
|
||||
suite.T().Run(strconv.FormatBool(v), func(t *testing.T) {
|
||||
testStruct := typeBoolTestStruct{
|
||||
Value: config.TypeBool{
|
||||
Value: v,
|
||||
Value: value,
|
||||
},
|
||||
}
|
||||
|
||||
data, err := json.Marshal(testStruct)
|
||||
encodedJSON, err := json.Marshal(testStruct)
|
||||
assert.NoError(t, err)
|
||||
assert.JSONEq(t, fmt.Sprintf(`{"value": "%s"}`, name), string(data))
|
||||
|
||||
expectedJSON, err := json.Marshal(map[string]bool{
|
||||
"value": value,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,11 +34,11 @@ func (t TypeErrorRate) Get(defaultValue float64) float64 {
|
||||
return t.Value
|
||||
}
|
||||
|
||||
func (t *TypeErrorRate) UnmarshalText(data []byte) error {
|
||||
func (t *TypeErrorRate) UnmarshalJSON(data []byte) error {
|
||||
return t.Set(string(data))
|
||||
}
|
||||
|
||||
func (t TypeErrorRate) MarshalText() ([]byte, error) {
|
||||
func (t TypeErrorRate) MarshalJSON() ([]byte, error) {
|
||||
return []byte(t.String()), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -45,27 +45,14 @@ func (suite *TypeErrorRateTestSuite) TestUnmarshalFail() {
|
||||
}
|
||||
|
||||
func (suite *TypeErrorRateTestSuite) TestUnmarshalOk() {
|
||||
testData := map[string]float64{
|
||||
"1": 1.0,
|
||||
"1.0": 1.0,
|
||||
"0.5": 0.5,
|
||||
".5": 0.5,
|
||||
}
|
||||
data, err := json.Marshal(map[string]float64{
|
||||
"value": 1.0,
|
||||
})
|
||||
suite.NoError(err)
|
||||
|
||||
for k, v := range testData {
|
||||
value := v
|
||||
|
||||
data, err := json.Marshal(map[string]string{
|
||||
"value": k,
|
||||
})
|
||||
suite.NoError(err)
|
||||
|
||||
suite.T().Run(k, func(t *testing.T) {
|
||||
testStruct := &typeErrorRateTestStruct{}
|
||||
assert.NoError(t, json.Unmarshal(data, testStruct))
|
||||
assert.InEpsilon(t, value, testStruct.Value.Value, 1e-10)
|
||||
})
|
||||
}
|
||||
testStruct := &typeErrorRateTestStruct{}
|
||||
suite.NoError(json.Unmarshal(data, testStruct))
|
||||
suite.InEpsilon(1.0, testStruct.Value.Value, 1e-10)
|
||||
}
|
||||
|
||||
func (suite *TypeErrorRateTestSuite) TestMarshalOk() {
|
||||
@@ -77,7 +64,7 @@ func (suite *TypeErrorRateTestSuite) TestMarshalOk() {
|
||||
|
||||
encodedJson, err := json.Marshal(testStruct)
|
||||
suite.NoError(err)
|
||||
suite.JSONEq(`{"value": "1.01"}`, string(encodedJson))
|
||||
suite.JSONEq(`{"value": 1.01}`, string(encodedJson))
|
||||
}
|
||||
|
||||
func (suite *TypeErrorRateTestSuite) TestGet() {
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
|
||||
type TypeHostPort struct {
|
||||
Value string
|
||||
Host string
|
||||
Port uint
|
||||
}
|
||||
|
||||
func (t *TypeHostPort) Set(value string) error {
|
||||
@@ -34,6 +36,8 @@ func (t *TypeHostPort) Set(value string) error {
|
||||
}
|
||||
|
||||
t.Value = net.JoinHostPort(host, port)
|
||||
t.Port = uint(portValue)
|
||||
t.Host = host
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user