Pārlūkot izejas kodu

Move config2 into config

tags/v2.1.0^2
9seconds 4 gadus atpakaļ
vecāks
revīzija
87ed1d1aa7
61 mainītis faili ar 653 papildinājumiem un 2912 dzēšanām
  1. 66
    0
      internal/cli/utils.go
  2. 16
    92
      internal/config/config.go
  3. 1
    1
      internal/config/config_test.go
  4. 1
    1
      internal/config/parse.go
  5. 37
    28
      internal/config/type_blocklist_uri.go
  6. 37
    101
      internal/config/type_blocklist_uri_test.go
  7. 1
    1
      internal/config/type_bool.go
  8. 5
    5
      internal/config/type_bool_test.go
  9. 25
    24
      internal/config/type_bytes.go
  10. 11
    45
      internal/config/type_bytes_test.go
  11. 1
    1
      internal/config/type_concurrency.go
  12. 5
    5
      internal/config/type_concurrency_test.go
  13. 26
    19
      internal/config/type_duration.go
  14. 33
    41
      internal/config/type_duration_test.go
  15. 20
    16
      internal/config/type_error_rate.go
  16. 36
    67
      internal/config/type_error_rate_test.go
  17. 26
    34
      internal/config/type_hostport.go
  18. 21
    48
      internal/config/type_hostport_test.go
  19. 16
    18
      internal/config/type_http_path.go
  20. 24
    48
      internal/config/type_http_path_test.go
  21. 20
    20
      internal/config/type_ip.go
  22. 33
    44
      internal/config/type_ip_test.go
  23. 18
    19
      internal/config/type_metric_prefix.go
  24. 20
    65
      internal/config/type_metric_prefix_test.go
  25. 20
    20
      internal/config/type_port.go
  26. 24
    70
      internal/config/type_port_test.go
  27. 22
    21
      internal/config/type_prefer_ip.go
  28. 28
    57
      internal/config/type_prefer_ip_test.go
  29. 1
    1
      internal/config/type_proxy_url.go
  30. 7
    6
      internal/config/type_proxy_url_test.go
  31. 22
    21
      internal/config/type_statsd_tag_format.go
  32. 30
    58
      internal/config/type_statsd_tag_format_test.go
  33. 0
    71
      internal/config/type_url.go
  34. 0
    107
      internal/config/type_url_test.go
  35. 0
    81
      internal/config2/config.go
  36. 0
    54
      internal/config2/config_test.go
  37. 0
    1
      internal/config2/testdata/broken.toml
  38. 0
    2
      internal/config2/testdata/minimal.toml
  39. 0
    1
      internal/config2/testdata/only_secret.toml
  40. 0
    77
      internal/config2/type_blocklist_uri.go
  41. 0
    113
      internal/config2/type_blocklist_uri_test.go
  42. 0
    55
      internal/config2/type_bytes.go
  43. 0
    86
      internal/config2/type_bytes_test.go
  44. 0
    53
      internal/config2/type_duration.go
  45. 0
    110
      internal/config2/type_duration_test.go
  46. 0
    47
      internal/config2/type_error_rate.go
  47. 0
    94
      internal/config2/type_error_rate_test.go
  48. 0
    59
      internal/config2/type_hostport.go
  49. 0
    88
      internal/config2/type_hostport_test.go
  50. 0
    33
      internal/config2/type_http_path.go
  51. 0
    67
      internal/config2/type_http_path_test.go
  52. 0
    45
      internal/config2/type_ip.go
  53. 0
    104
      internal/config2/type_ip_test.go
  54. 0
    40
      internal/config2/type_metric_prefix.go
  55. 0
    70
      internal/config2/type_metric_prefix_test.go
  56. 0
    45
      internal/config2/type_port.go
  57. 0
    71
      internal/config2/type_port_test.go
  58. 0
    62
      internal/config2/type_prefer_ip.go
  59. 0
    113
      internal/config2/type_prefer_ip_test.go
  60. 0
    58
      internal/config2/type_statsd_tag_format.go
  61. 0
    108
      internal/config2/type_statsd_tag_format_test.go

+ 66
- 0
internal/cli/utils.go Parādīt failu

@@ -0,0 +1,66 @@
1
+package cli
2
+
3
+import (
4
+	"fmt"
5
+	"net"
6
+	"net/url"
7
+	"os"
8
+
9
+	"github.com/9seconds/mtg/v2/internal/config2"
10
+	"github.com/9seconds/mtg/v2/mtglib"
11
+	"github.com/9seconds/mtg/v2/network"
12
+)
13
+
14
+func readTOMLConfig(path string) (*config2.Config, error) {
15
+	content, err := os.ReadFile(path)
16
+	if err != nil {
17
+		return nil, fmt.Errorf("cannot read config file: %w", err)
18
+	}
19
+
20
+	conf, err := config2.Parse(content)
21
+	if err != nil {
22
+		return nil, fmt.Errorf("cannot parse config: %w", err)
23
+	}
24
+
25
+	return conf, nil
26
+}
27
+
28
+func makeNetwork(conf *config2.Config, version string) (mtglib.Network, error) {
29
+	tcpTimeout := conf.Network.Timeout.TCP.Get(network.DefaultTimeout)
30
+	httpTimeout := conf.Network.Timeout.HTTP.Get(network.DefaultHTTPTimeout)
31
+	dohIP := conf.Network.DOHIP.Get(net.ParseIP(network.DefaultDOHHostname)).String()
32
+	bufferSize := conf.TCPBuffer.Get(network.DefaultBufferSize)
33
+	userAgent := "mtg/" + version
34
+
35
+	baseDialer, err := network.NewDefaultDialer(tcpTimeout, int(bufferSize))
36
+	if err != nil {
37
+		return nil, fmt.Errorf("cannot build a default dialer: %w", err)
38
+	}
39
+
40
+	if len(conf.Network.Proxies) == 0 {
41
+		return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
42
+	}
43
+
44
+	proxyURLs := make([]*url.URL, 0, len(conf.Network.Proxies))
45
+	for _, v := range conf.Network.Proxies {
46
+		if value := v.Get(nil); value != nil {
47
+			proxyURLs = append(proxyURLs, value)
48
+		}
49
+	}
50
+
51
+	if len(proxyURLs) == 1 {
52
+		socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
53
+		if err != nil {
54
+			return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
55
+		}
56
+
57
+		return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
58
+	}
59
+
60
+	socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
61
+	if err != nil {
62
+		return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
63
+	}
64
+
65
+	return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
66
+}

+ 16
- 92
internal/config/config.go Parādīt failu

@@ -6,27 +6,26 @@ import (
6 6
 	"fmt"
7 7
 
8 8
 	"github.com/9seconds/mtg/v2/mtglib"
9
-	"github.com/pelletier/go-toml"
10 9
 )
11 10
 
12 11
 type Config struct {
13
-	Debug                bool          `json:"debug"`
14
-	Secret               mtglib.Secret `json:"secret"`
15
-	BindTo               TypeHostPort  `json:"bindTo"`
16
-	TCPBuffer            TypeBytes     `json:"tcpBuffer"`
17
-	PreferIP             TypePreferIP  `json:"preferIp"`
18
-	DomainFrontingPort   TypePort      `json:"domainFrontingPort"`
19
-	TolerateTimeSkewness TypeDuration  `json:"tolerateTimeSkewness"`
20
-	Concurrency          uint          `json:"concurrency"`
12
+	Debug                TypeBool        `json:"debug"`
13
+	Secret               mtglib.Secret   `json:"secret"`
14
+	BindTo               TypeHostPort    `json:"bindTo"`
15
+	TCPBuffer            TypeBytes       `json:"tcpBuffer"`
16
+	PreferIP             TypePreferIP    `json:"preferIp"`
17
+	DomainFrontingPort   TypePort        `json:"domainFrontingPort"`
18
+	TolerateTimeSkewness TypeDuration    `json:"tolerateTimeSkewness"`
19
+	Concurrency          TypeConcurrency `json:"concurrency"`
21 20
 	Defense              struct {
22 21
 		AntiReplay struct {
23
-			Enabled   bool          `json:"enabled"`
22
+			Enabled   TypeBool      `json:"enabled"`
24 23
 			MaxSize   TypeBytes     `json:"maxSize"`
25 24
 			ErrorRate TypeErrorRate `json:"errorRate"`
26 25
 		} `json:"antiReplay"`
27 26
 		Blocklist struct {
28
-			Enabled             bool               `json:"enabled"`
29
-			DownloadConcurrency uint               `json:"downloadConcurrency"`
27
+			Enabled             TypeBool           `json:"enabled"`
28
+			DownloadConcurrency TypeConcurrency    `json:"downloadConcurrency"`
30 29
 			URLs                []TypeBlocklistURI `json:"urls"`
31 30
 			UpdateEach          TypeDuration       `json:"updateEach"`
32 31
 		} `json:"blocklist"`
@@ -37,18 +36,18 @@ type Config struct {
37 36
 			HTTP TypeDuration `json:"http"`
38 37
 			Idle TypeDuration `json:"idle"`
39 38
 		} `json:"timeout"`
40
-		DOHIP   TypeIP    `json:"dohIp"`
41
-		Proxies []TypeURL `json:"proxies"`
39
+		DOHIP   TypeIP         `json:"dohIp"`
40
+		Proxies []TypeProxyURL `json:"proxies"`
42 41
 	} `json:"network"`
43 42
 	Stats struct {
44 43
 		StatsD struct {
45
-			Enabled      bool                `json:"enabled"`
44
+			Enabled      TypeBool            `json:"enabled"`
46 45
 			Address      TypeHostPort        `json:"address"`
47 46
 			MetricPrefix TypeMetricPrefix    `json:"metricPrefix"`
48 47
 			TagFormat    TypeStatsdTagFormat `json:"tagFormat"`
49 48
 		} `json:"statsd"`
50 49
 		Prometheus struct {
51
-			Enabled      bool             `json:"enabled"`
50
+			Enabled      TypeBool         `json:"enabled"`
52 51
 			BindTo       TypeHostPort     `json:"bindTo"`
53 52
 			HTTPPath     TypeHTTPPath     `json:"httpPath"`
54 53
 			MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
@@ -61,7 +60,7 @@ func (c *Config) Validate() error {
61 60
 		return fmt.Errorf("invalid secret %s", c.Secret.String())
62 61
 	}
63 62
 
64
-	if len(c.BindTo.HostValue(nil)) == 0 || c.BindTo.PortValue(0) == 0 {
63
+    if c.BindTo.Get("") == "" {
65 64
 		return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
66 65
 	}
67 66
 
@@ -80,78 +79,3 @@ func (c *Config) String() string {
80 79
 
81 80
 	return buf.String()
82 81
 }
83
-
84
-type configRaw struct {
85
-	Debug                bool   `toml:"debug" json:"debug,omitempty"`
86
-	Secret               string `toml:"secret" json:"secret"`
87
-	BindTo               string `toml:"bind-to" json:"bindTo"`
88
-	TCPBuffer            string `toml:"tcp-buffer" json:"tcpBuffer,omitempty"`
89
-	PreferIP             string `toml:"prefer-ip" json:"preferIp,omitempty"`
90
-	DomainFrontingPort   uint   `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
91
-	TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
92
-	Concurrency          uint   `toml:"concurrency" json:"concurrency,omitempty"`
93
-	Defense              struct {
94
-		AntiReplay struct {
95
-			Enabled   bool    `toml:"enabled" json:"enabled,omitempty"`
96
-			MaxSize   string  `toml:"max-size" json:"maxSize,omitempty"`
97
-			ErrorRate float64 `toml:"error-rate" json:"errorRate,omitempty"`
98
-		} `toml:"anti-replay" json:"antiReplay,omitempty"`
99
-		Blocklist struct {
100
-			Enabled             bool     `toml:"enabled" json:"enabled,omitempty"`
101
-			DownloadConcurrency uint     `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
102
-			URLs                []string `toml:"urls" json:"urls,omitempty"`
103
-			UpdateEach          string   `toml:"update-each" json:"updateEach,omitempty"`
104
-		} `toml:"blocklist" json:"blocklist,omitempty"`
105
-	} `toml:"defense" json:"defense,omitempty"`
106
-	Network struct {
107
-		Timeout struct {
108
-			TCP  string `toml:"tcp" json:"tcp,omitempty"`
109
-			HTTP string `toml:"http" json:"http,omitempty"`
110
-			Idle string `toml:"idle" json:"idle,omitempty"`
111
-		} `toml:"timeout" json:"timeout,omitempty"`
112
-		DOHIP   string   `toml:"doh-ip" json:"dohIp,omitempty"`
113
-		Proxies []string `toml:"proxies" json:"proxies,omitempty"`
114
-	} `toml:"network" json:"network,omitempty"`
115
-	Stats struct {
116
-		StatsD struct {
117
-			Enabled      bool   `toml:"enabled" json:"enabled,omitempty"`
118
-			Address      string `toml:"address" json:"address,omitempty"`
119
-			MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
120
-			TagFormat    string `toml:"tag-format" json:"tagFormat,omitempty"`
121
-		} `toml:"statsd" json:"statsd,omitempty"`
122
-		Prometheus struct {
123
-			Enabled      bool   `toml:"enabled" json:"enabled,omitempty"`
124
-			BindTo       string `toml:"bind-to" json:"bindTo,omitempty"`
125
-			HTTPPath     string `toml:"http-path" json:"httpPath,omitempty"`
126
-			MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
127
-		} `toml:"prometheus" json:"prometheus,omitempty"`
128
-	} `toml:"stats" json:"stats,omitempty"`
129
-}
130
-
131
-func Parse(rawData []byte) (*Config, error) {
132
-	rawConf := &configRaw{}
133
-	jsonBuf := &bytes.Buffer{}
134
-	conf := &Config{}
135
-
136
-	jsonEncoder := json.NewEncoder(jsonBuf)
137
-	jsonEncoder.SetEscapeHTML(false)
138
-	jsonEncoder.SetIndent("", "")
139
-
140
-	if err := toml.Unmarshal(rawData, rawConf); err != nil {
141
-		return nil, fmt.Errorf("cannot parse toml config: %w", err)
142
-	}
143
-
144
-	if err := jsonEncoder.Encode(rawConf); err != nil {
145
-		panic(err)
146
-	}
147
-
148
-	if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
149
-		return nil, fmt.Errorf("cannot parse a config: %w", err)
150
-	}
151
-
152
-	if err := conf.Validate(); err != nil {
153
-		return nil, fmt.Errorf("cannot validate config: %w", err)
154
-	}
155
-
156
-	return conf, nil
157
-}

+ 1
- 1
internal/config/config_test.go Parādīt failu

@@ -10,7 +10,7 @@ import (
10 10
 )
11 11
 
12 12
 type ConfigTestSuite struct {
13
-	suite.Suite
13
+    suite.Suite
14 14
 }
15 15
 
16 16
 func (suite *ConfigTestSuite) ReadConfig(filename string) []byte {

internal/config2/parse.go → internal/config/parse.go Parādīt failu

@@ -1,4 +1,4 @@
1
-package config2
1
+package config
2 2
 
3 3
 import (
4 4
 	"bytes"

+ 37
- 28
internal/config/type_blocklist_uri.go Parādīt failu

@@ -8,61 +8,70 @@ import (
8 8
 )
9 9
 
10 10
 type TypeBlocklistURI struct {
11
-	value string
11
+	Value string
12 12
 }
13 13
 
14
-func (c *TypeBlocklistURI) UnmarshalText(data []byte) error {
15
-	if len(data) == 0 {
16
-		return nil
17
-	}
18
-
19
-	text := string(data)
20
-	if filepath.IsAbs(text) {
21
-		if _, err := os.Stat(text); os.IsNotExist(err) {
22
-			return fmt.Errorf("filepath %s does not exist", text)
14
+func (t *TypeBlocklistURI) Set(value string) error {
15
+	if stat, err := os.Stat(value); err == nil || os.IsExist(err) {
16
+        switch {
17
+        case stat.IsDir():
18
+            return fmt.Errorf("value is correct filepath but directory")
19
+        case stat.Mode().Perm() & 0o400 == 0:
20
+            return fmt.Errorf("value is correct filepath but not readable")
21
+        }
22
+
23
+		value, err = filepath.Abs(value)
24
+		if err != nil {
25
+			return fmt.Errorf(
26
+				"value is correct filepath but cannot resolve absolute (%s): %w",
27
+				value, err)
23 28
 		}
24 29
 
25
-		c.value = text
30
+		t.Value = value
26 31
 
27 32
 		return nil
28 33
 	}
29 34
 
30
-	parsedURL, err := url.Parse(text)
35
+	parsedURL, err := url.Parse(value)
31 36
 	if err != nil {
32
-		return fmt.Errorf("incorrect url: %w", err)
37
+		return fmt.Errorf("incorrect url (%s): %w", value, err)
33 38
 	}
34 39
 
35 40
 	switch parsedURL.Scheme {
36
-	case "http", "https": // nolint: goconst
41
+	case "http", "https":
37 42
 	default:
38
-		return fmt.Errorf("unknown schema %s", parsedURL.Scheme)
43
+		return fmt.Errorf("unknown schema %s (%s)", parsedURL.Scheme, value)
39 44
 	}
40 45
 
41 46
 	if parsedURL.Host == "" {
42
-		return fmt.Errorf("incorrect url %s", text)
47
+		return fmt.Errorf("incorrect url %s", value)
43 48
 	}
44 49
 
45
-	c.value = parsedURL.String()
50
+	t.Value = parsedURL.String()
46 51
 
47 52
 	return nil
48 53
 }
49 54
 
50
-func (c TypeBlocklistURI) MarshalText() ([]byte, error) {
51
-	return []byte(c.value), nil
55
+func (t TypeBlocklistURI) Get(defaultValue string) string {
56
+	if t.Value == "" {
57
+		return defaultValue
58
+	}
59
+
60
+	return t.Value
52 61
 }
53 62
 
54
-func (c TypeBlocklistURI) String() string {
55
-	return c.value
63
+func (t TypeBlocklistURI) IsRemote() bool {
64
+	return !filepath.IsAbs(t.Value)
56 65
 }
57 66
 
58
-func (c TypeBlocklistURI) IsRemote() bool {
59
-	return !filepath.IsAbs(c.value)
67
+func (t *TypeBlocklistURI) UnmarshalText(data []byte) error {
68
+	return t.Set(string(data))
60 69
 }
61 70
 
62
-func (c TypeBlocklistURI) Value(defaultValue string) string {
63
-	if c.value == "" {
64
-		return defaultValue
65
-	}
71
+func (t TypeBlocklistURI) MarshalText() ([]byte, error) {
72
+	return []byte(t.String()), nil
73
+}
66 74
 
67
-	return c.value
75
+func (t TypeBlocklistURI) String() string {
76
+	return t.Value
68 77
 }

+ 37
- 101
internal/config/type_blocklist_uri_test.go Parādīt failu

@@ -1,12 +1,10 @@
1 1
 package config_test
2 2
 
3 3
 import (
4
-	"crypto/rand"
5
-	"encoding/base64"
6 4
 	"encoding/json"
7 5
 	"os"
8 6
 	"path/filepath"
9
-	"strconv"
7
+	"strings"
10 8
 	"testing"
11 9
 
12 10
 	"github.com/9seconds/mtg/v2/internal/config"
@@ -20,42 +18,28 @@ type typeBlocklistURITestStruct struct {
20 18
 
21 19
 type TypeBlocklistURITestSuite struct {
22 20
 	suite.Suite
23
-}
24
-
25
-func (suite *TypeBlocklistURITestSuite) TestUnmarshalNil() {
26
-	typ := &config.TypeBlocklistURI{}
27
-	suite.NoError(typ.UnmarshalText(nil))
28
-	suite.Empty(typ.String())
29
-}
30 21
 
31
-func (suite *TypeBlocklistURITestSuite) TestUnknownSchema() {
32
-	typ := &config.TypeBlocklistURI{}
33
-	suite.Error(typ.UnmarshalText([]byte("gopher://lalala")))
22
+	directory    string
23
+	absDirectory string
34 24
 }
35 25
 
36
-func (suite *TypeBlocklistURITestSuite) TestEmptyHost() {
37
-	typ := &config.TypeBlocklistURI{}
38
-	suite.Error(typ.UnmarshalText([]byte("https:///path")))
39
-}
26
+func (suite *TypeBlocklistURITestSuite) SetupSuite() {
27
+	dir, _ := os.Getwd()
28
+	absDir, _ := filepath.Abs(dir)
40 29
 
41
-func (suite *TypeBlocklistURITestSuite) TestIncorrectURL() {
42
-	typ := &config.TypeBlocklistURI{}
43
-	suite.Error(typ.UnmarshalText([]byte("h:/--")))
30
+	suite.directory = dir
31
+	suite.absDirectory = absDir
44 32
 }
45 33
 
46 34
 func (suite *TypeBlocklistURITestSuite) TestUnmarshalFail() {
47
-	rnd := make([]byte, 48)
48
-
49
-	rand.Read(rnd) // nolint: errcheck
50
-
51
-	unknownPath := base64.StdEncoding.EncodeToString(rnd)
52
-
53 35
 	testData := []string{
54
-		"1",
55
-		unknownPath,
56
-		"/" + unknownPath,
57
-		"http:/",
58
-		"gopher://lalalal",
36
+		"gopher://lalala",
37
+		"https:///paths",
38
+		"h:/=",
39
+		filepath.Join(suite.directory, "___"),
40
+		filepath.Join(suite.absDirectory, "___"),
41
+		suite.directory,
42
+		suite.absDirectory,
59 43
 	}
60 44
 
61 45
 	for _, v := range testData {
@@ -71,13 +55,12 @@ func (suite *TypeBlocklistURITestSuite) TestUnmarshalFail() {
71 55
 }
72 56
 
73 57
 func (suite *TypeBlocklistURITestSuite) TestUnmarshalOk() {
74
-	dir, _ := os.Getwd()
75
-	dir, _ = filepath.Abs(dir)
76
-
77 58
 	testData := []string{
78 59
 		"http://lalala",
79
-		filepath.Join(dir, "config.go"),
80 60
 		"https://lalala",
61
+		"https://lalala/path",
62
+		filepath.Join(suite.directory, "config.go"),
63
+		filepath.Join(suite.absDirectory, "config.go"),
81 64
 	}
82 65
 
83 66
 	for _, v := range testData {
@@ -92,83 +75,36 @@ func (suite *TypeBlocklistURITestSuite) TestUnmarshalOk() {
92 75
 			testStruct := &typeBlocklistURITestStruct{}
93 76
 
94 77
 			assert.NoError(t, json.Unmarshal(data, testStruct))
95
-			assert.EqualValues(t, value, testStruct.Value.Value(""))
78
+			assert.EqualValues(t, value, testStruct.Value.Get(""))
79
+
80
+			if strings.HasPrefix(value, "http") {
81
+				assert.True(t, testStruct.Value.IsRemote())
82
+			} else {
83
+				assert.False(t, testStruct.Value.IsRemote())
84
+			}
96 85
 		})
97 86
 	}
98 87
 }
99 88
 
100 89
 func (suite *TypeBlocklistURITestSuite) TestMarshalOk() {
101
-	dir, _ := os.Getwd()
102
-	dir, _ = filepath.Abs(dir)
103
-
104
-	testData := []string{
105
-		"http://lalalal",
106
-		filepath.Join(dir, "config.go"),
90
+	testStruct := &typeBlocklistURITestStruct{
91
+		Value: config.TypeBlocklistURI{
92
+			Value: "http://some.url/with/path",
93
+		},
107 94
 	}
108 95
 
109
-	for _, v := range testData {
110
-		name := v
111
-
112
-		data, err := json.Marshal(map[string]string{
113
-			"value": name,
114
-		})
115
-		suite.NoError(err)
116
-
117
-		suite.T().Run(name, func(t *testing.T) {
118
-			testStruct := &typeBlocklistURITestStruct{}
119
-
120
-			assert.NoError(t, json.Unmarshal(data, testStruct))
121
-			assert.Equal(t, name, testStruct.Value.String())
122
-
123
-			marshalled, err := testStruct.Value.MarshalText()
124
-			assert.NoError(t, err)
125
-			assert.Equal(t, name, string(marshalled))
126
-		})
127
-	}
128
-}
129
-
130
-func (suite *TypeBlocklistURITestSuite) TestValue() {
131
-	testStruct := &typeBlocklistURITestStruct{}
132
-
133
-	suite.Equal("http://lalala", testStruct.Value.Value("http://lalala"))
134
-
135
-	data, err := json.Marshal(map[string]string{
136
-		"value": "http://blablabla",
137
-	})
96
+	data, err := json.Marshal(testStruct)
138 97
 	suite.NoError(err)
139
-	suite.NoError(json.Unmarshal(data, testStruct))
140
-
141
-	suite.Equal("http://blablabla", testStruct.Value.Value(""))
98
+	suite.JSONEq(`{"value": "http://some.url/with/path"}`, string(data))
142 99
 }
143 100
 
144
-func (suite *TypeBlocklistURITestSuite) TestIsRemote() {
145
-	dir, _ := os.Getwd()
146
-	dir, _ = filepath.Abs(dir)
147
-
148
-	testData := map[bool]string{
149
-		true:  "http://lalalal",
150
-		false: filepath.Join(dir, "config.go"),
151
-	}
152
-
153
-	for k, v := range testData {
154
-		ok := k
155
-
156
-		data, err := json.Marshal(map[string]string{
157
-			"value": v,
158
-		})
159
-		suite.NoError(err)
160
-
161
-		suite.T().Run(strconv.FormatBool(ok), func(t *testing.T) {
162
-			testStruct := &typeBlocklistURITestStruct{}
163
-			assert.NoError(t, json.Unmarshal(data, testStruct))
101
+func (suite *TypeBlocklistURITestSuite) TestGet() {
102
+	value := config.TypeBlocklistURI{}
103
+	suite.Equal("/path", value.Get("/path"))
164 104
 
165
-			if ok {
166
-				assert.True(t, testStruct.Value.IsRemote())
167
-			} else {
168
-				assert.False(t, testStruct.Value.IsRemote())
169
-			}
170
-		})
171
-	}
105
+    suite.NoError(value.Set("http://lalala.ru"))
106
+    suite.Equal("http://lalala.ru", value.Get("/path"))
107
+    suite.Equal("http://lalala.ru", value.Get(""))
172 108
 }
173 109
 
174 110
 func TestTypeBlocklistURI(t *testing.T) {

internal/config2/type_bool.go → internal/config/type_bool.go Parādīt failu

@@ -1,4 +1,4 @@
1
-package config2
1
+package config
2 2
 
3 3
 import (
4 4
 	"fmt"

internal/config2/type_bool_test.go → internal/config/type_bool_test.go Parādīt failu

@@ -1,4 +1,4 @@
1
-package config2_test
1
+package config_test
2 2
 
3 3
 import (
4 4
 	"encoding/json"
@@ -6,13 +6,13 @@ import (
6 6
 	"strconv"
7 7
 	"testing"
8 8
 
9
-	"github.com/9seconds/mtg/v2/internal/config2"
9
+	"github.com/9seconds/mtg/v2/internal/config"
10 10
 	"github.com/stretchr/testify/assert"
11 11
 	"github.com/stretchr/testify/suite"
12 12
 )
13 13
 
14 14
 type typeBoolTestStruct struct {
15
-	Value config2.TypeBool `json:"value"`
15
+	Value config.TypeBool `json:"value"`
16 16
 }
17 17
 
18 18
 type TypeBoolTestSuite struct {
@@ -85,7 +85,7 @@ func (suite *TypeBoolTestSuite) TestMarshalOk() {
85 85
 
86 86
 		suite.T().Run(name, func(t *testing.T) {
87 87
 			testStruct := typeBoolTestStruct{
88
-				Value: config2.TypeBool{
88
+				Value: config.TypeBool{
89 89
 					Value: v,
90 90
 				},
91 91
 			}
@@ -98,7 +98,7 @@ func (suite *TypeBoolTestSuite) TestMarshalOk() {
98 98
 }
99 99
 
100 100
 func (suite *TypeBoolTestSuite) TestGet() {
101
-	value := config2.TypeBool{}
101
+	value := config.TypeBool{}
102 102
 	suite.False(value.Get(false))
103 103
 	suite.True(value.Get(true))
104 104
 

+ 25
- 24
internal/config/type_bytes.go Parādīt failu

@@ -7,48 +7,49 @@ import (
7 7
 	"github.com/alecthomas/units"
8 8
 )
9 9
 
10
+var typeBytesStringCleaner = strings.NewReplacer(" ", "", "\t", "", "IB", "iB")
11
+
10 12
 type TypeBytes struct {
11
-	value units.Base2Bytes
13
+	Value units.Base2Bytes
12 14
 }
13 15
 
14
-func (c *TypeBytes) UnmarshalText(data []byte) error {
15
-	if len(data) == 0 {
16
-		return nil
17
-	}
18
-
19
-	normalizedData := strings.ToUpper(string(data))
20
-	normalizedData = strings.ReplaceAll(normalizedData, "IB", "iB")
16
+func (t *TypeBytes) Set(value string) error {
17
+    normalizedValue := typeBytesStringCleaner.Replace(strings.ToUpper(value))
21 18
 
22
-	value, err := units.ParseBase2Bytes(normalizedData)
19
+	parsedValue, err := units.ParseBase2Bytes(normalizedValue)
23 20
 	if err != nil {
24
-		return fmt.Errorf("incorrect bytes value: %w", err)
21
+		return fmt.Errorf("incorrect bytes value (%v): %w", value, err)
25 22
 	}
26 23
 
27
-	if value < 0 {
28
-		return fmt.Errorf("%d should be positive number", value)
24
+	if parsedValue < 0 {
25
+		return fmt.Errorf("bytes should be positive (%s)", value)
29 26
 	}
30 27
 
31
-	c.value = value
28
+	t.Value = parsedValue
32 29
 
33 30
 	return nil
34 31
 }
35 32
 
36
-func (c TypeBytes) MarshalText() ([]byte, error) {
37
-	return []byte(c.String()), nil
33
+func (t TypeBytes) Get(defaultValue uint) uint {
34
+	if t.Value == 0 {
35
+		return defaultValue
36
+	}
37
+
38
+	return uint(t.Value)
38 39
 }
39 40
 
40
-func (c TypeBytes) String() string {
41
-	if c.value == 0 {
42
-		return ""
43
-	}
41
+func (t *TypeBytes) UnmarshalText(data []byte) error {
42
+	return t.Set(string(data))
43
+}
44 44
 
45
-	return strings.ToLower(c.value.String())
45
+func (t TypeBytes) MarshalText() ([]byte, error) {
46
+	return []byte(t.String()), nil
46 47
 }
47 48
 
48
-func (c TypeBytes) Value(defaultValue uint) uint {
49
-	if c.value == 0 {
50
-		return defaultValue
49
+func (t TypeBytes) String() string {
50
+	if t.Value == 0 {
51
+		return ""
51 52
 	}
52 53
 
53
-	return uint(c.value)
54
+	return strings.ToLower(t.Value.String())
54 55
 }

+ 11
- 45
internal/config/type_bytes_test.go Parādīt failu

@@ -17,19 +17,12 @@ type TypeBytesTestSuite struct {
17 17
 	suite.Suite
18 18
 }
19 19
 
20
-func (suite *TypeBytesTestSuite) TestUnmarshalNil() {
21
-	typ := &config.TypeBytes{}
22
-	suite.NoError(typ.UnmarshalText(nil))
23
-	suite.Empty(typ.String())
24
-}
25
-
26 20
 func (suite *TypeBytesTestSuite) TestUnmarshalFail() {
27 21
 	testData := []string{
28 22
 		"1m",
29 23
 		"1",
30 24
 		"-1kb",
31 25
 		"-1kib",
32
-		"-1QB",
33 26
 	}
34 27
 
35 28
 	for _, v := range testData {
@@ -65,53 +58,26 @@ func (suite *TypeBytesTestSuite) TestUnmarshalOk() {
65 58
 			testStruct := &typeBytesTestStruct{}
66 59
 
67 60
 			assert.NoError(t, json.Unmarshal(data, testStruct))
68
-			assert.EqualValues(t, value, testStruct.Value.Value(0))
61
+			assert.EqualValues(t, value, testStruct.Value.Get(0))
69 62
 		})
70 63
 	}
71 64
 }
72 65
 
73 66
 func (suite *TypeBytesTestSuite) TestMarshalOk() {
74
-	testData := []string{
75
-		"1b",
76
-		"1kib",
77
-		"2mib",
78
-	}
79
-
80
-	for _, v := range testData {
81
-		name := v
82
-
83
-		data, err := json.Marshal(map[string]string{
84
-			"value": name,
85
-		})
86
-		suite.NoError(err)
87
-
88
-		suite.T().Run(name, func(t *testing.T) {
89
-			testStruct := &typeBytesTestStruct{}
90
-
91
-			assert.NoError(t, json.Unmarshal(data, testStruct))
92
-			assert.Equal(t, name, testStruct.Value.String())
67
+    value := typeBytesTestStruct{}
68
+    suite.NoError(value.Value.Set("1kib"))
93 69
 
94
-			marshalled, err := testStruct.Value.MarshalText()
95
-			assert.NoError(t, err)
96
-			assert.Equal(t, name, string(marshalled))
97
-		})
98
-	}
70
+    data, err := json.Marshal(value)
71
+    suite.NoError(err)
72
+    suite.JSONEq(`{"value": "1kib"}`, string(data))
99 73
 }
100 74
 
101
-func (suite *TypeBytesTestSuite) TestValue() {
102
-	testStruct := &typeBytesTestStruct{}
103
-
104
-	suite.EqualValues(0, testStruct.Value.Value(0))
105
-	suite.EqualValues(1, testStruct.Value.Value(1))
106
-
107
-	data, err := json.Marshal(map[string]string{
108
-		"value": "1kb",
109
-	})
110
-	suite.NoError(err)
111
-	suite.NoError(json.Unmarshal(data, testStruct))
75
+func (suite *TypeBytesTestSuite) TestGet() {
76
+    value := config.TypeBytes{}
77
+    suite.EqualValues(1000, value.Get(1000))
112 78
 
113
-	suite.EqualValues(1024, testStruct.Value.Value(0))
114
-	suite.EqualValues(1024, testStruct.Value.Value(1))
79
+    suite.NoError(value.Set("1mib"))
80
+    suite.EqualValues(1048576, value.Get(1000))
115 81
 }
116 82
 
117 83
 func TestTypeBytes(t *testing.T) {

internal/config2/type_concurrency.go → internal/config/type_concurrency.go Parādīt failu

@@ -1,4 +1,4 @@
1
-package config2
1
+package config
2 2
 
3 3
 import (
4 4
 	"fmt"

internal/config2/type_concurrency_test.go → internal/config/type_concurrency_test.go Parādīt failu

@@ -1,16 +1,16 @@
1
-package config2_test
1
+package config_test
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5 5
 	"testing"
6 6
 
7
-	"github.com/9seconds/mtg/v2/internal/config2"
7
+	"github.com/9seconds/mtg/v2/internal/config"
8 8
 	"github.com/stretchr/testify/assert"
9 9
 	"github.com/stretchr/testify/suite"
10 10
 )
11 11
 
12 12
 type typeConcurrencyTestStruct struct {
13
-	Value config2.TypeConcurrency `json:"value"`
13
+	Value config.TypeConcurrency `json:"value"`
14 14
 }
15 15
 
16 16
 type TypeConcurrencyTestSuite struct {
@@ -49,7 +49,7 @@ func (suite *TypeConcurrencyTestSuite) TestUnmarshalOk() {
49 49
 
50 50
 func (suite *TypeConcurrencyTestSuite) TestMarshalOk() {
51 51
 	testStruct := &typeConcurrencyTestStruct{
52
-		Value: config2.TypeConcurrency{
52
+		Value: config.TypeConcurrency{
53 53
 			Value: 2,
54 54
 		},
55 55
 	}
@@ -60,7 +60,7 @@ func (suite *TypeConcurrencyTestSuite) TestMarshalOk() {
60 60
 }
61 61
 
62 62
 func (suite *TypeConcurrencyTestSuite) TestGet() {
63
-	value := config2.TypeConcurrency{}
63
+	value := config.TypeConcurrency{}
64 64
 	suite.EqualValues(1, value.Get(1))
65 65
 
66 66
 	value.Value = 3

+ 26
- 19
internal/config/type_duration.go Parādīt failu

@@ -6,41 +6,48 @@ import (
6 6
 	"time"
7 7
 )
8 8
 
9
+var typeDurationStringCleaner = strings.NewReplacer(" ", "", "\t", "")
10
+
9 11
 type TypeDuration struct {
10
-	value time.Duration
12
+	Value time.Duration
11 13
 }
12 14
 
13
-func (c *TypeDuration) UnmarshalText(data []byte) error {
14
-	if len(data) == 0 {
15
-		return nil
16
-	}
17
-
18
-	dur, err := time.ParseDuration(strings.ToLower(string(data)))
15
+func (t *TypeDuration) Set(value string) error {
16
+	parsedValue, err := time.ParseDuration(
17
+		typeDurationStringCleaner.Replace(strings.ToLower(value)))
19 18
 	if err != nil {
20
-		return fmt.Errorf("incorrect duration: %w", err)
19
+		return fmt.Errorf("incorrect duration (%s): %w", value, err)
21 20
 	}
22 21
 
23
-	if dur < 0 {
24
-		return fmt.Errorf("%s should be positive duration", dur)
22
+	if parsedValue < 0 {
23
+		return fmt.Errorf("duration has to be a positive: %s", value)
25 24
 	}
26 25
 
27
-	c.value = dur
26
+	t.Value = parsedValue
28 27
 
29 28
 	return nil
30 29
 }
31 30
 
32
-func (c TypeDuration) MarshalText() ([]byte, error) {
33
-	return []byte(c.value.String()), nil
31
+func (t TypeDuration) Get(defaultValue time.Duration) time.Duration {
32
+	if t.Value == 0 {
33
+		return defaultValue
34
+	}
35
+
36
+	return t.Value
34 37
 }
35 38
 
36
-func (c TypeDuration) String() string {
37
-	return c.value.String()
39
+func (t *TypeDuration) UnmarshalText(data []byte) error {
40
+	return t.Set(string(data))
38 41
 }
39 42
 
40
-func (c TypeDuration) Value(defaultValue time.Duration) time.Duration {
41
-	if c.value == 0 {
42
-		return defaultValue
43
+func (t TypeDuration) MarshalText() ([]byte, error) {
44
+	return []byte(t.String()), nil
45
+}
46
+
47
+func (t TypeDuration) String() string {
48
+	if t.Value == 0 {
49
+		return ""
43 50
 	}
44 51
 
45
-	return c.value
52
+	return t.Value.String()
46 53
 }

+ 33
- 41
internal/config/type_duration_test.go Parādīt failu

@@ -18,18 +18,12 @@ type TypeDurationTestSuite struct {
18 18
 	suite.Suite
19 19
 }
20 20
 
21
-func (suite *TypeDurationTestSuite) TestUnmarshalNil() {
22
-	typ := &config.TypeDuration{}
23
-	suite.NoError(typ.UnmarshalText(nil))
24
-	suite.EqualValues(0, typ.Value(0))
25
-}
26
-
27 21
 func (suite *TypeDurationTestSuite) TestUnmarshalFail() {
28 22
 	testData := []string{
29
-		"1t",
30
-		"1",
31 23
 		"-1s",
32
-		"-1h",
24
+		"1 seconds ago",
25
+		"1s ago",
26
+		"",
33 27
 	}
34 28
 
35 29
 	for _, v := range testData {
@@ -47,8 +41,11 @@ func (suite *TypeDurationTestSuite) TestUnmarshalFail() {
47 41
 func (suite *TypeDurationTestSuite) TestUnmarshalOk() {
48 42
 	testData := map[string]time.Duration{
49 43
 		"1s":   time.Second,
50
-		"1m":   time.Minute,
51
-		"2h1s": 2*time.Hour + time.Second,
44
+		"0":    0 * time.Second,
45
+		"0s":   0 * time.Second,
46
+		"1\tM": time.Minute,
47
+		"1H":   time.Hour,
48
+		"1 h":  time.Hour,
52 49
 	}
53 50
 
54 51
 	for k, v := range testData {
@@ -63,53 +60,48 @@ func (suite *TypeDurationTestSuite) TestUnmarshalOk() {
63 60
 			testStruct := &typeDurationTestStruct{}
64 61
 
65 62
 			assert.NoError(t, json.Unmarshal(data, testStruct))
66
-			assert.Equal(t, value, testStruct.Value.Value(0))
63
+			assert.Equal(t, value, testStruct.Value.Value)
67 64
 		})
68 65
 	}
69 66
 }
70 67
 
71 68
 func (suite *TypeDurationTestSuite) TestMarshalOk() {
72
-	testData := []string{
73
-		"1s",
74
-		"1m0s",
75
-		"2h0m1s",
69
+	testData := map[string]string{
70
+		"1s":  "1s",
71
+		"0":   "",
72
+		"0s":  "",
73
+		"0ms": "",
74
+		"1 H": "1h0m0s",
76 75
 	}
77 76
 
78
-	for _, v := range testData {
79
-		name := v
80
-
81
-		data, err := json.Marshal(map[string]string{
82
-			"value": name,
83
-		})
84
-		suite.NoError(err)
77
+	for k, v := range testData {
78
+		value := k
79
+		expected := v
85 80
 
86
-		suite.T().Run(name, func(t *testing.T) {
81
+		suite.T().Run(value, func(t *testing.T) {
87 82
 			testStruct := &typeDurationTestStruct{}
88 83
 
89
-			assert.NoError(t, json.Unmarshal(data, testStruct))
90
-			assert.Equal(t, name, testStruct.Value.String())
84
+			assert.NoError(t, testStruct.Value.Set(value))
85
+
86
+			data, err := json.Marshal(testStruct)
87
+			assert.NoError(t, err)
91 88
 
92
-			marshalled, err := testStruct.Value.MarshalText()
89
+			expectedJson, err := json.Marshal(map[string]string{
90
+				"value": expected,
91
+			})
93 92
 			assert.NoError(t, err)
94
-			assert.Equal(t, name, string(marshalled))
93
+
94
+			assert.JSONEq(t, string(expectedJson), string(data))
95 95
 		})
96 96
 	}
97 97
 }
98 98
 
99
-func (suite *TypeDurationTestSuite) TestValue() {
100
-	testStruct := &typeDurationTestStruct{}
101
-
102
-	suite.EqualValues(0, testStruct.Value.Value(0))
103
-	suite.Equal(time.Second, testStruct.Value.Value(time.Second))
104
-
105
-	data, err := json.Marshal(map[string]string{
106
-		"value": "1s",
107
-	})
108
-	suite.NoError(err)
109
-	suite.NoError(json.Unmarshal(data, testStruct))
99
+func (suite *TypeDurationTestSuite) TestGet() {
100
+	value := config.TypeDuration{}
101
+	suite.Equal(time.Second, value.Get(time.Second))
110 102
 
111
-	suite.Equal(time.Second, testStruct.Value.Value(0))
112
-	suite.Equal(time.Second, testStruct.Value.Value(time.Minute))
103
+	value.Value = 3 * time.Second
104
+	suite.Equal(3*time.Second, value.Get(time.Hour))
113 105
 }
114 106
 
115 107
 func TestTypeDuration(t *testing.T) {

+ 20
- 16
internal/config/type_error_rate.go Parādīt failu

@@ -8,36 +8,40 @@ import (
8 8
 const typeErrorRateIgnoreLess = 1e-8
9 9
 
10 10
 type TypeErrorRate struct {
11
-	value float64
11
+	Value float64
12 12
 }
13 13
 
14
-func (c *TypeErrorRate) UnmarshalJSON(data []byte) error {
15
-	value, err := strconv.ParseFloat(string(data), 64)
14
+func (t *TypeErrorRate) Set(value string) error {
15
+	parsedValue, err := strconv.ParseFloat(value, 64)
16 16
 	if err != nil {
17
-		return fmt.Errorf("incorrect float value: %w", err)
17
+		return fmt.Errorf("Value is not a float (%s): %w", value, err)
18 18
 	}
19 19
 
20
-	if value <= 0 || value >= 100 {
21
-		return fmt.Errorf("%f should be 0 < x < 100", value)
20
+	if parsedValue <= 0.0 || parsedValue >= 100.0 {
21
+		return fmt.Errorf("Value should be 0 < x < 100 (%s)", value)
22 22
 	}
23 23
 
24
-	c.value = value
24
+	t.Value = parsedValue
25 25
 
26 26
 	return nil
27 27
 }
28 28
 
29
-func (c *TypeErrorRate) MarshalText() ([]byte, error) {
30
-	return []byte(c.String()), nil
29
+func (t TypeErrorRate) Get(defaultValue float64) float64 {
30
+	if t.Value < typeErrorRateIgnoreLess {
31
+		return defaultValue
32
+	}
33
+
34
+	return t.Value
31 35
 }
32 36
 
33
-func (c TypeErrorRate) String() string {
34
-	return strconv.FormatFloat(c.value, 'f', -1, 64)
37
+func (t *TypeErrorRate) UnmarshalText(data []byte) error {
38
+	return t.Set(string(data))
35 39
 }
36 40
 
37
-func (c TypeErrorRate) Value(defaultValue float64) float64 {
38
-	if c.value < typeErrorRateIgnoreLess {
39
-		return defaultValue
40
-	}
41
+func (t TypeErrorRate) MarshalText() ([]byte, error) {
42
+	return []byte(t.String()), nil
43
+}
41 44
 
42
-	return c.value
45
+func (t TypeErrorRate) String() string {
46
+	return strconv.FormatFloat(t.Value, 'f', -1, 64)
43 47
 }

+ 36
- 67
internal/config/type_error_rate_test.go Parādīt failu

@@ -2,7 +2,6 @@ package config_test
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"strconv"
6 5
 	"testing"
7 6
 
8 7
 	"github.com/9seconds/mtg/v2/internal/config"
@@ -19,104 +18,74 @@ type TypeErrorRateTestSuite struct {
19 18
 }
20 19
 
21 20
 func (suite *TypeErrorRateTestSuite) TestUnmarshalFail() {
22
-	testData := []float64{
23
-		1000,
24
-		-100,
25
-		-0.0001,
21
+	testData := []string{
22
+		"",
23
+		"1s",
24
+		"1,",
25
+		"1,2",
26
+		".",
27
+		"3.4.5",
28
+		"3.5.",
29
+		".3.5",
30
+		"some word",
31
+		"1e2",
32
+		"-1.0",
26 33
 	}
27 34
 
28 35
 	for _, v := range testData {
29
-		data, err := json.Marshal(map[string]float64{
36
+		data, err := json.Marshal(map[string]string{
30 37
 			"value": v,
31 38
 		})
32 39
 		suite.NoError(err)
33 40
 
34
-		suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
41
+		suite.T().Run(v, func(t *testing.T) {
35 42
 			assert.Error(t, json.Unmarshal(data, &typeErrorRateTestStruct{}))
36 43
 		})
37 44
 	}
38
-
39
-	data, err := json.Marshal(map[string]string{
40
-		"value": "hello",
41
-	})
42
-	suite.NoError(err)
43
-	suite.Error(json.Unmarshal(data, &typeErrorRateTestStruct{}))
44 45
 }
45 46
 
46 47
 func (suite *TypeErrorRateTestSuite) TestUnmarshalOk() {
47
-	testData := []float64{
48
-		1,
49
-		55.5,
50
-		0.0001,
51
-		1e-6,
48
+	testData := map[string]float64{
49
+		"1":   1.0,
50
+		"1.0": 1.0,
51
+		"0.5": 0.5,
52
+		".5":  0.5,
52 53
 	}
53 54
 
54
-	for _, v := range testData {
55
+	for k, v := range testData {
55 56
 		value := v
56 57
 
57
-		data, err := json.Marshal(map[string]float64{
58
-			"value": v,
58
+		data, err := json.Marshal(map[string]string{
59
+			"value": k,
59 60
 		})
60 61
 		suite.NoError(err)
61 62
 
62
-		suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
63
+		suite.T().Run(k, func(t *testing.T) {
63 64
 			testStruct := &typeErrorRateTestStruct{}
64
-
65 65
 			assert.NoError(t, json.Unmarshal(data, testStruct))
66
-			assert.InEpsilon(t, value, testStruct.Value.Value(0), 1e-10)
66
+			assert.InEpsilon(t, value, testStruct.Value.Value, 1e-10)
67 67
 		})
68 68
 	}
69 69
 }
70 70
 
71 71
 func (suite *TypeErrorRateTestSuite) TestMarshalOk() {
72
-	testData := []float64{
73
-		1,
74
-		55.5,
75
-		0.0001,
76
-		1e-6,
72
+	testStruct := typeErrorRateTestStruct{
73
+		Value: config.TypeErrorRate{
74
+			Value: 1.01,
75
+		},
77 76
 	}
78 77
 
79
-	for _, v := range testData {
80
-		value := v
81
-
82
-		data, err := json.Marshal(map[string]float64{
83
-			"value": v,
84
-		})
85
-		suite.NoError(err)
86
-
87
-		suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
88
-			testStruct := &typeErrorRateTestStruct{}
89
-
90
-			assert.NoError(t, json.Unmarshal(data, testStruct))
91
-
92
-			parsed, err := strconv.ParseFloat(testStruct.Value.String(), 64)
93
-			assert.NoError(t, err)
94
-			assert.InEpsilon(t, value, parsed, 1e-10)
95
-
96
-			marshalled, err := testStruct.Value.MarshalText()
97
-			assert.NoError(t, err)
98
-
99
-			parsed, err = strconv.ParseFloat(string(marshalled), 64)
100
-			assert.NoError(t, err)
101
-			assert.InEpsilon(t, value, parsed, 1e-10)
102
-		})
103
-	}
78
+	encodedJson, err := json.Marshal(testStruct)
79
+	suite.NoError(err)
80
+	suite.JSONEq(`{"value": "1.01"}`, string(encodedJson))
104 81
 }
105 82
 
106
-func (suite *TypeErrorRateTestSuite) TestValue() {
107
-	testStruct := &typeErrorRateTestStruct{}
108
-
109
-	suite.InEpsilon(1, testStruct.Value.Value(1), 1e-10)
110
-	suite.InEpsilon(2, testStruct.Value.Value(2), 1e-10)
111
-
112
-	data, err := json.Marshal(map[string]float64{
113
-		"value": 1,
114
-	})
115
-	suite.NoError(err)
116
-	suite.NoError(json.Unmarshal(data, testStruct))
83
+func (suite *TypeErrorRateTestSuite) TestGet() {
84
+	value := config.TypeErrorRate{}
85
+	suite.InEpsilon(1.0, value.Get(1.0), 1e-10)
117 86
 
118
-	suite.InEpsilon(1, testStruct.Value.Value(2), 1e-10)
119
-	suite.InEpsilon(1, testStruct.Value.Value(3), 1e-10)
87
+	value.Value = 5.0
88
+	suite.InEpsilon(5.0, value.Get(1.0), 1e-10)
120 89
 }
121 90
 
122 91
 func TestTypeErrorRate(t *testing.T) {

+ 26
- 34
internal/config/type_hostport.go Parādīt failu

@@ -7,61 +7,53 @@ import (
7 7
 )
8 8
 
9 9
 type TypeHostPort struct {
10
-	host TypeIP
11
-	port TypePort
10
+	Value string
12 11
 }
13 12
 
14
-func (c *TypeHostPort) UnmarshalText(data []byte) error {
15
-	if len(data) == 0 {
16
-		return nil
13
+func (t *TypeHostPort) Set(value string) error {
14
+	host, port, err := net.SplitHostPort(value)
15
+	if err != nil {
16
+		return fmt.Errorf("incorrect host:port value (%v): %w", value, err)
17 17
 	}
18 18
 
19
-	text := string(data)
20
-
21
-	host, port, err := net.SplitHostPort(text)
19
+	portValue, err := strconv.ParseUint(port, 10, 16)
22 20
 	if err != nil {
23
-		return fmt.Errorf("incorrect host:port syntax: %w", err)
21
+		return fmt.Errorf("incorrect port number (%v): %w", value, err)
24 22
 	}
25 23
 
26
-	if port == "" {
27
-		return fmt.Errorf("port in %s host:port pair cannot be empty", text)
24
+	if portValue == 0 {
25
+		return fmt.Errorf("incorrect port number (%s)", value)
28 26
 	}
29 27
 
30
-	if err := c.port.UnmarshalJSON([]byte(port)); err != nil {
31
-		return fmt.Errorf("incorrect port in host:port: %w", err)
28
+	if host == "" {
29
+		return fmt.Errorf("empty host: %s", value)
32 30
 	}
33 31
 
34
-	if err := c.host.UnmarshalText([]byte(host)); err != nil {
35
-		return fmt.Errorf("incorrect host: %w", err)
32
+	if net.ParseIP(host) == nil {
33
+		return fmt.Errorf("host is not an IP address: %s", value)
36 34
 	}
37 35
 
36
+	t.Value = net.JoinHostPort(host, port)
37
+
38 38
 	return nil
39 39
 }
40 40
 
41
-func (c TypeHostPort) MarshalText() ([]byte, error) {
42
-	return []byte(c.String()), nil
43
-}
41
+func (t TypeHostPort) Get(defaultValue string) string {
42
+	if t.Value == "" {
43
+		return defaultValue
44
+	}
44 45
 
45
-func (c TypeHostPort) String() string {
46
-	return c.Value(net.IP{}, 0)
46
+	return t.Value
47 47
 }
48 48
 
49
-func (c TypeHostPort) HostValue(defaultValue net.IP) net.IP {
50
-	return c.host.Value(defaultValue)
49
+func (t *TypeHostPort) UnmarshalText(data []byte) error {
50
+	return t.Set(string(data))
51 51
 }
52 52
 
53
-func (c TypeHostPort) PortValue(defaultValue uint) uint {
54
-	return c.port.Value(defaultValue)
53
+func (t TypeHostPort) MarshalText() ([]byte, error) {
54
+	return []byte(t.String()), nil
55 55
 }
56 56
 
57
-func (c TypeHostPort) Value(defaultHostValue net.IP, defaultPortValue uint) string {
58
-	host := c.HostValue(defaultHostValue)
59
-	port := c.PortValue(defaultPortValue)
60
-
61
-	hostStr := ""
62
-	if len(host) > 0 {
63
-		hostStr = host.String()
64
-	}
65
-
66
-	return net.JoinHostPort(hostStr, strconv.Itoa(int(port)))
57
+func (t TypeHostPort) String() string {
58
+	return t.Value
67 59
 }

+ 21
- 48
internal/config/type_hostport_test.go Parādīt failu

@@ -2,7 +2,6 @@ package config_test
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"net"
6 5
 	"testing"
7 6
 
8 7
 	"github.com/9seconds/mtg/v2/internal/config"
@@ -20,11 +19,13 @@ type TypeHostPortTestSuite struct {
20 19
 
21 20
 func (suite *TypeHostPortTestSuite) TestUnmarshalFail() {
22 21
 	testData := []string{
23
-		"10.0.0.10:aaa",
24
-		"10.0.0.10:",
25 22
 		":",
26
-		"xxx",
27
-		"xxx:80",
23
+		":800",
24
+		"127.0.0.1:8000000",
25
+		"12...:80",
26
+		"",
27
+		"localhost",
28
+		"google.com:",
28 29
 	}
29 30
 
30 31
 	for _, v := range testData {
@@ -41,9 +42,8 @@ func (suite *TypeHostPortTestSuite) TestUnmarshalFail() {
41 42
 
42 43
 func (suite *TypeHostPortTestSuite) TestUnmarshalOk() {
43 44
 	testData := []string{
44
-		"10.0.0.10:80",
45
-		"0.0.0.0:80",
46
-		":8000",
45
+		"127.0.0.1:80",
46
+		"10.0.0.10:6553",
47 47
 	}
48 48
 
49 49
 	for _, v := range testData {
@@ -56,57 +56,30 @@ func (suite *TypeHostPortTestSuite) TestUnmarshalOk() {
56 56
 
57 57
 		suite.T().Run(v, func(t *testing.T) {
58 58
 			testStruct := &typeHostPortTestStruct{}
59
-
60 59
 			assert.NoError(t, json.Unmarshal(data, testStruct))
61
-			assert.EqualValues(t, value, testStruct.Value.Value(nil, 0))
60
+			assert.Equal(t, value, testStruct.Value.Value)
62 61
 		})
63 62
 	}
64 63
 }
65 64
 
66 65
 func (suite *TypeHostPortTestSuite) TestMarshalOk() {
67
-	testData := []string{
68
-		"10.0.0.10:80",
69
-		"0.0.0.0:80",
70
-		":8000",
66
+	testStruct := typeHostPortTestStruct{
67
+		Value: config.TypeHostPort{
68
+			Value: "127.0.0.1:8000",
69
+		},
71 70
 	}
72 71
 
73
-	for _, v := range testData {
74
-		value := v
75
-
76
-		data, err := json.Marshal(map[string]string{
77
-			"value": v,
78
-		})
79
-		suite.NoError(err)
80
-
81
-		suite.T().Run(v, func(t *testing.T) {
82
-			testStruct := &typeHostPortTestStruct{}
83
-
84
-			assert.NoError(t, json.Unmarshal(data, testStruct))
85
-			assert.Equal(t, value, testStruct.Value.String())
86
-
87
-			marshalled, err := testStruct.Value.MarshalText()
88
-			assert.NoError(t, err)
89
-			assert.Equal(t, value, string(marshalled))
90
-		})
91
-	}
72
+	data, err := json.Marshal(testStruct)
73
+	suite.NoError(err)
74
+	suite.JSONEq(`{"value": "127.0.0.1:8000"}`, string(data))
92 75
 }
93 76
 
94
-func (suite *TypeHostPortTestSuite) TestValue() {
95
-	testStruct := &typeHostPortTestStruct{}
96
-
97
-	suite.EqualValues("127.0.0.1:80",
98
-		testStruct.Value.Value(net.ParseIP("127.0.0.1"), 80))
99
-	suite.EqualValues("127.1.0.1:80",
100
-		testStruct.Value.Value(net.ParseIP("127.1.0.1"), 80))
101
-
102
-	data, err := json.Marshal(map[string]string{
103
-		"value": "127.0.0.1:80",
104
-	})
105
-	suite.NoError(err)
106
-	suite.NoError(json.Unmarshal(data, testStruct))
77
+func (suite *TypeHostPortTestSuite) TestGet() {
78
+	value := config.TypeHostPort{}
79
+	suite.Equal("127.0.0.1:9000", value.Get("127.0.0.1:9000"))
107 80
 
108
-	suite.EqualValues("127.0.0.1:80", testStruct.Value.Value(nil, 0))
109
-	suite.EqualValues("127.0.0.1:80", testStruct.Value.Value(net.ParseIP("10.0.0.10"), 3000))
81
+	value.Value = "127.0.0.1:80"
82
+	suite.Equal("127.0.0.1:80", value.Get("127.0.0.1:9000"))
110 83
 }
111 84
 
112 85
 func TestTypeHostPort(t *testing.T) {

+ 16
- 18
internal/config/type_http_path.go Parādīt failu

@@ -3,33 +3,31 @@ package config
3 3
 import "strings"
4 4
 
5 5
 type TypeHTTPPath struct {
6
-	value string
6
+	Value string
7 7
 }
8 8
 
9
-func (c *TypeHTTPPath) UnmarshalText(data []byte) error {
10
-	if len(data) > 0 {
11
-		c.value = "/" + strings.Trim(string(data), "/")
12
-	}
9
+func (t *TypeHTTPPath) Set(value string) error {
10
+	t.Value = "/" + strings.Trim(value, "/")
13 11
 
14 12
 	return nil
15 13
 }
16 14
 
17
-func (c TypeHTTPPath) MarshalText() ([]byte, error) {
18
-	return []byte(c.String()), nil
19
-}
20
-
21
-func (c TypeHTTPPath) String() string {
22
-	if c.value == "" {
23
-		return "/"
15
+func (t TypeHTTPPath) Get(defaultValue string) string {
16
+	if t.Value == "" {
17
+		return defaultValue
24 18
 	}
25 19
 
26
-	return c.value
20
+	return t.Value
27 21
 }
28 22
 
29
-func (c TypeHTTPPath) Value(defaultValue string) string {
30
-	if c.value == "" {
31
-		return defaultValue
32
-	}
23
+func (t *TypeHTTPPath) UnmarshalText(data []byte) error {
24
+	return t.Set(string(data))
25
+}
26
+
27
+func (t TypeHTTPPath) MarshalText() ([]byte, error) {
28
+	return []byte(t.String()), nil
29
+}
33 30
 
34
-	return c.value
31
+func (t TypeHTTPPath) String() string {
32
+	return t.Value
35 33
 }

+ 24
- 48
internal/config/type_http_path_test.go Parādīt failu

@@ -17,72 +17,48 @@ type TypeHTTPPathTestSuite struct {
17 17
 	suite.Suite
18 18
 }
19 19
 
20
-func (suite *TypeHTTPPathTestSuite) TestUnmarshal() {
21
-	testData := []string{
22
-		"/hello",
23
-		"hello",
24
-		"hello/",
25
-		"/hello/",
26
-	}
27
-
28
-	for _, v := range testData {
29
-		data, err := json.Marshal(map[string]string{
30
-			"value": v,
31
-		})
32
-		suite.NoError(err)
33
-
34
-		suite.T().Run(v, func(t *testing.T) {
35
-			testStruct := &typeHTTPPathTestStruct{}
36
-
37
-			assert.NoError(t, json.Unmarshal(data, testStruct))
38
-			assert.Equal(t, "/hello", testStruct.Value.Value(""))
39
-		})
40
-	}
41
-}
42
-
43
-func (suite *TypeHTTPPathTestSuite) TestMarshalOk() {
20
+func (suite *TypeHTTPPathTestSuite) TestUnmarshalOk() {
44 21
 	testData := map[string]string{
45
-		"":        "/",
46
-		"/hello":  "/hello",
47
-		"/hello/": "/hello",
48
-		"hello/":  "/hello",
49
-		"hello":   "/hello",
22
+		"":      "/",
23
+		"/":     "/",
24
+		"/path": "/path",
25
+		"path":  "/path",
50 26
 	}
51 27
 
52 28
 	for k, v := range testData {
53
-		toPass := k
54
-		compareWith := v
29
+		value := v
55 30
 
56 31
 		data, err := json.Marshal(map[string]string{
57
-			"value": toPass,
32
+			"value": k,
58 33
 		})
59 34
 		suite.NoError(err)
60 35
 
61
-		suite.T().Run(toPass, func(t *testing.T) {
36
+		suite.T().Run(k, func(t *testing.T) {
62 37
 			testStruct := &typeHTTPPathTestStruct{}
63
-
64 38
 			assert.NoError(t, json.Unmarshal(data, testStruct))
65
-			assert.Equal(t, compareWith, testStruct.Value.String())
66
-
67
-			marshalled, err := testStruct.Value.MarshalText()
68
-			assert.NoError(t, err)
69
-			assert.Equal(t, compareWith, string(marshalled))
39
+			assert.Equal(t, value, testStruct.Value.Get(""))
70 40
 		})
71 41
 	}
72 42
 }
73 43
 
74
-func (suite *TypeHTTPPathTestSuite) TestValue() {
75
-	testStruct := &typeHTTPPathTestStruct{}
76
-
77
-	suite.Equal("/hello", testStruct.Value.Value("/hello"))
44
+func (suite *TypeHTTPPathTestSuite) TestMarshalOk() {
45
+	value := typeHTTPPathTestStruct{
46
+		Value: config.TypeHTTPPath{
47
+			Value: "/path",
48
+		},
49
+	}
78 50
 
79
-	data, err := json.Marshal(map[string]string{
80
-		"value": "/map",
81
-	})
51
+	data, err := json.Marshal(value)
82 52
 	suite.NoError(err)
83
-	suite.NoError(json.Unmarshal(data, testStruct))
53
+	suite.JSONEq(`{"value": "/path"}`, string(data))
54
+}
55
+
56
+func (suite *TypeHTTPPathTestSuite) TestGet() {
57
+	value := config.TypeHTTPPath{}
58
+	suite.Equal("/hello", value.Get("/hello"))
84 59
 
85
-	suite.Equal("/map", testStruct.Value.Value("/hello"))
60
+	suite.NoError(value.Set("/lalala"))
61
+	suite.Equal("/lalala", value.Get("/hello"))
86 62
 }
87 63
 
88 64
 func TestTypeHTTPPath(t *testing.T) {

+ 20
- 20
internal/config/type_ip.go Parādīt failu

@@ -6,40 +6,40 @@ import (
6 6
 )
7 7
 
8 8
 type TypeIP struct {
9
-	value net.IP
9
+	Value net.IP
10 10
 }
11 11
 
12
-func (c *TypeIP) UnmarshalText(data []byte) error {
13
-	if len(data) == 0 {
14
-		return nil
15
-	}
16
-
17
-	ip := net.ParseIP(string(data))
12
+func (t *TypeIP) Set(value string) error {
13
+	ip := net.ParseIP(value)
18 14
 	if ip == nil {
19
-		return fmt.Errorf("incorrect ip address: %s", string(data))
15
+		return fmt.Errorf("incorret ip %s", value)
20 16
 	}
21 17
 
22
-	c.value = ip
18
+    t.Value = ip
23 19
 
24 20
 	return nil
25 21
 }
26 22
 
27
-func (c *TypeIP) MarshalText() ([]byte, error) {
28
-	return []byte(c.String()), nil
23
+func (t *TypeIP) Get(defaultValue net.IP) net.IP {
24
+	if len(t.Value) == 0 {
25
+		return defaultValue
26
+	}
27
+
28
+	return t.Value
29 29
 }
30 30
 
31
-func (c TypeIP) String() string {
32
-	if len(c.value) > 0 {
33
-		return c.value.String()
34
-	}
31
+func (t *TypeIP) UnmarshalText(data []byte) error {
32
+	return t.Set(string(data))
33
+}
35 34
 
36
-	return ""
35
+func (t TypeIP) MarshalText() ([]byte, error) {
36
+	return []byte(t.String()), nil
37 37
 }
38 38
 
39
-func (c TypeIP) Value(defaultValue net.IP) net.IP {
40
-	if c.value == nil {
41
-		return defaultValue
39
+func (t TypeIP) String() string {
40
+	if len(t.Value) == 0 {
41
+		return ""
42 42
 	}
43 43
 
44
-	return c.value
44
+	return t.Value.String()
45 45
 }

+ 33
- 44
internal/config/type_ip_test.go Parādīt failu

@@ -20,10 +20,11 @@ type TypeIPTestSuite struct {
20 20
 
21 21
 func (suite *TypeIPTestSuite) TestUnmarshalFail() {
22 22
 	testData := []string{
23
-		"0.0.10",
24
-		"10.0.0.10:",
25
-		"xxx:80",
26
-		"2001:0db8:85a3:0000:0000:8a2e:4",
23
+		"",
24
+		"....",
25
+		"0...",
26
+		"300.200.200.800",
27
+		"[]",
27 28
 	}
28 29
 
29 30
 	for _, v := range testData {
@@ -39,74 +40,62 @@ func (suite *TypeIPTestSuite) TestUnmarshalFail() {
39 40
 }
40 41
 
41 42
 func (suite *TypeIPTestSuite) TestUnmarshalOk() {
42
-	testData := []string{
43
-		"0.0.0.0",
44
-		"10.0.0.10",
45
-		"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
43
+	testData := map[string]string{
44
+		"2001:0db8:85a3:0000:0000:8a2e:0370:7334": "2001:db8:85a3::8a2e:370:7334",
45
+		"127.0.0.1": "127.0.0.1",
46 46
 	}
47 47
 
48
-	for _, v := range testData {
49
-		value := v
48
+	for k, v := range testData {
49
+		expected := v
50 50
 
51 51
 		data, err := json.Marshal(map[string]string{
52
-			"value": v,
52
+			"value": k,
53 53
 		})
54 54
 		suite.NoError(err)
55 55
 
56
-		suite.T().Run(v, func(t *testing.T) {
56
+		suite.T().Run(k, func(t *testing.T) {
57 57
 			testStruct := &typeIPTestStruct{}
58
-
59 58
 			assert.NoError(t, json.Unmarshal(data, testStruct))
60
-			assert.Equal(t,
61
-				net.ParseIP(value).String(),
62
-				testStruct.Value.Value(nil).String())
59
+			assert.Equal(t, expected, testStruct.Value.Get(nil).String())
63 60
 		})
64 61
 	}
65 62
 }
66 63
 
67 64
 func (suite *TypeIPTestSuite) TestMarshalOk() {
68 65
 	testData := []string{
69
-		"0.0.0.0",
70
-		"10.0.0.10",
71
-		"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
66
+		"2001:db8:85a3::8a2e:370:7334",
67
+		"127.0.0.1",
72 68
 	}
73 69
 
74 70
 	for _, v := range testData {
75
-		value := net.ParseIP(v).String()
76
-
77
-		data, err := json.Marshal(map[string]string{
78
-			"value": v,
79
-		})
80
-		suite.NoError(err)
71
+		value := v
81 72
 
82 73
 		suite.T().Run(v, func(t *testing.T) {
83
-			testStruct := &typeIPTestStruct{}
74
+			testStruct := &typeIPTestStruct{
75
+				Value: config.TypeIP{
76
+					Value: net.ParseIP(value),
77
+				},
78
+			}
84 79
 
85
-			assert.NoError(t, json.Unmarshal(data, testStruct))
86
-			assert.Equal(t, value, testStruct.Value.String())
80
+			encodedJSON, err := json.Marshal(testStruct)
81
+			assert.NoError(t, err)
87 82
 
88
-			marshalled, err := testStruct.Value.MarshalText()
83
+			expectedJSON, err := json.Marshal(map[string]string{
84
+				"value": value,
85
+			})
89 86
 			assert.NoError(t, err)
90
-			assert.Equal(t, value, string(marshalled))
87
+
88
+			assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
91 89
 		})
92 90
 	}
93 91
 }
94 92
 
95
-func (suite *TypeIPTestSuite) TestValue() {
96
-	testStruct := &typeIPTestStruct{}
97
-	suite.Empty(testStruct.Value.String())
98
-
99
-	suite.Nil(testStruct.Value.Value(nil))
100
-	suite.Equal("127.1.0.1", testStruct.Value.Value(net.ParseIP("127.1.0.1")).String())
101
-
102
-	data, err := json.Marshal(map[string]string{
103
-		"value": "127.0.0.1",
104
-	})
105
-	suite.NoError(err)
106
-	suite.NoError(json.Unmarshal(data, testStruct))
93
+func (suite *TypeIPTestSuite) TestGet() {
94
+	value := config.TypeIP{}
95
+	suite.Equal("127.0.0.1", value.Get(net.ParseIP("127.0.0.1")).String())
107 96
 
108
-	suite.Equal("127.0.0.1", testStruct.Value.Value(nil).String())
109
-	suite.Equal("127.0.0.1", testStruct.Value.Value(net.ParseIP("10.0.0.10")).String())
97
+	suite.NoError(value.Set("127.0.0.2"))
98
+	suite.Equal("127.0.0.2", value.Get(net.ParseIP("127.0.0.1")).String())
110 99
 }
111 100
 
112 101
 func TestTypeIP(t *testing.T) {

+ 18
- 19
internal/config/type_metric_prefix.go Parādīt failu

@@ -6,36 +6,35 @@ import (
6 6
 )
7 7
 
8 8
 type TypeMetricPrefix struct {
9
-	value string
9
+	Value string
10 10
 }
11 11
 
12
-func (c *TypeMetricPrefix) UnmarshalText(data []byte) error {
13
-	if len(data) == 0 {
14
-		return nil
12
+func (t *TypeMetricPrefix) Set(value string) error {
13
+	if ok, err := regexp.MatchString("^[a-z0-9]+$", value); !ok || err != nil {
14
+        return fmt.Errorf("incorrect metric prefix %s: %w", value, err)
15 15
 	}
16 16
 
17
-	prefix := string(data)
18
-	if ok, err := regexp.MatchString("^[a-z0-9]+$", prefix); !ok || err != nil {
19
-		return fmt.Errorf("incorrect metric prefix: %s", prefix)
20
-	}
21
-
22
-	c.value = prefix
17
+    t.Value = value
23 18
 
24 19
 	return nil
25 20
 }
26 21
 
27
-func (c TypeMetricPrefix) MarshalText() ([]byte, error) {
28
-	return []byte(c.String()), nil
22
+func (t TypeMetricPrefix) Get(defaultValue string) string {
23
+	if t.Value == "" {
24
+		return defaultValue
25
+	}
26
+
27
+	return t.Value
29 28
 }
30 29
 
31
-func (c TypeMetricPrefix) String() string {
32
-	return c.value
30
+func (t *TypeMetricPrefix) UnmarshalText(data []byte) error {
31
+	return t.Set(string(data))
33 32
 }
34 33
 
35
-func (c TypeMetricPrefix) Value(defaultValue string) string {
36
-	if c.value == "" {
37
-		return defaultValue
38
-	}
34
+func (t TypeMetricPrefix) MarshalText() ([]byte, error) {
35
+	return []byte(t.String()), nil
36
+}
39 37
 
40
-	return c.value
38
+func (t TypeMetricPrefix) String() string {
39
+	return t.Value
41 40
 }

+ 20
- 65
internal/config/type_metric_prefix_test.go Parādīt failu

@@ -17,18 +17,13 @@ type TypeMetricPrefixTestSuite struct {
17 17
 	suite.Suite
18 18
 }
19 19
 
20
-func (suite *TypeMetricPrefixTestSuite) TestUnmarshalNil() {
21
-	typ := &config.TypeMetricPrefix{}
22
-	suite.NoError(typ.UnmarshalText(nil))
23
-	suite.Empty(typ.String())
24
-}
25
-
26 20
 func (suite *TypeMetricPrefixTestSuite) TestUnmarshalFail() {
27 21
 	testData := []string{
28
-		"aaa.aaa",
29
-		"aaa-bbb",
30
-		"aaa:ccc",
31
-		"metric prefix",
22
+		"",
23
+		"-",
24
+		"hello/world",
25
+		"lala*",
26
+		"++sdf++",
32 27
 	}
33 28
 
34 29
 	for _, v := range testData {
@@ -44,69 +39,29 @@ func (suite *TypeMetricPrefixTestSuite) TestUnmarshalFail() {
44 39
 }
45 40
 
46 41
 func (suite *TypeMetricPrefixTestSuite) TestUnmarshalOk() {
47
-	testData := []string{
48
-		"mtg",
49
-		"mtg111",
50
-	}
51
-
52
-	for _, v := range testData {
53
-		value := v
54
-
55
-		data, err := json.Marshal(map[string]string{
56
-			"value": v,
57
-		})
58
-		suite.NoError(err)
59
-
60
-		suite.T().Run(v, func(t *testing.T) {
61
-			testStruct := &typeMetricPrefixTestStruct{}
62
-
63
-			assert.NoError(t, json.Unmarshal(data, testStruct))
64
-			assert.Equal(t, value, testStruct.Value.Value(""))
65
-		})
66
-	}
42
+	testStruct := &typeMetricPrefixTestStruct{}
43
+	suite.NoError(json.Unmarshal([]byte(`{"value": "mtg"}`), testStruct))
44
+	suite.Equal("mtg", testStruct.Value.Get("lalala"))
67 45
 }
68 46
 
69 47
 func (suite *TypeMetricPrefixTestSuite) TestMarshalOk() {
70
-	testData := []string{
71
-		"mtg",
72
-		"mtg111",
48
+	testStruct := &typeMetricPrefixTestStruct{
49
+		Value: config.TypeMetricPrefix{
50
+			Value: "mtg",
51
+		},
73 52
 	}
74 53
 
75
-	for _, v := range testData {
76
-		value := v
77
-
78
-		data, err := json.Marshal(map[string]string{
79
-			"value": v,
80
-		})
81
-		suite.NoError(err)
82
-
83
-		suite.T().Run(v, func(t *testing.T) {
84
-			testStruct := &typeMetricPrefixTestStruct{}
85
-
86
-			assert.NoError(t, json.Unmarshal(data, testStruct))
87
-			assert.Equal(t, value, testStruct.Value.String())
88
-
89
-			marshalled, err := testStruct.Value.MarshalText()
90
-			assert.NoError(t, err)
91
-			assert.Equal(t, value, string(marshalled))
92
-		})
93
-	}
54
+	data, err := json.Marshal(testStruct)
55
+	suite.NoError(err)
56
+	suite.JSONEq(`{"value": "mtg"}`, string(data))
94 57
 }
95 58
 
96
-func (suite *TypeMetricPrefixTestSuite) TestValue() {
97
-	testStruct := &typeMetricPrefixTestStruct{}
98
-
99
-	suite.Equal("mtg", testStruct.Value.Value("mtg"))
100
-	suite.Equal("vvv", testStruct.Value.Value("vvv"))
101
-
102
-	data, err := json.Marshal(map[string]string{
103
-		"value": "aaa",
104
-	})
105
-	suite.NoError(err)
106
-	suite.NoError(json.Unmarshal(data, testStruct))
59
+func (suite *TypeMetricPrefixTestSuite) TestGet() {
60
+	value := config.TypeMetricPrefix{}
61
+	suite.Equal("lalala", value.Get("lalala"))
107 62
 
108
-	suite.Equal("aaa", testStruct.Value.Value("mtg"))
109
-	suite.Equal("aaa", testStruct.Value.Value("vvv"))
63
+	value.Value = "mtg"
64
+	suite.Equal("mtg", value.Get("lalala"))
110 65
 }
111 66
 
112 67
 func TestTypeMetricPrefix(t *testing.T) {

+ 20
- 20
internal/config/type_port.go Parādīt failu

@@ -6,40 +6,40 @@ import (
6 6
 )
7 7
 
8 8
 type TypePort struct {
9
-	value uint
9
+	Value uint16
10 10
 }
11 11
 
12
-func (c *TypePort) UnmarshalJSON(data []byte) error {
13
-	if len(data) == 0 {
14
-		return nil
15
-	}
16
-
17
-	intValue, err := strconv.ParseUint(string(data), 10, 64)
12
+func (t *TypePort) Set(value string) error {
13
+	portValue, err := strconv.ParseUint(value, 10, 16)
18 14
 	if err != nil {
19
-		return fmt.Errorf("port number is not a number: %w", err)
15
+		return fmt.Errorf("incorrect port number (%v): %w", value, err)
20 16
 	}
21 17
 
22
-	if intValue == 0 || intValue >= 65536 {
23
-		return fmt.Errorf("port number should be 0 < portNo < 65536: %d", intValue)
18
+	if portValue == 0 {
19
+		return fmt.Errorf("incorrect port number (%s)", value)
24 20
 	}
25 21
 
26
-	c.value = uint(intValue)
22
+    t.Value = uint16(portValue)
27 23
 
28 24
 	return nil
29 25
 }
30 26
 
31
-func (c *TypePort) MarshalJSON() ([]byte, error) {
32
-	return []byte(c.String()), nil
27
+func (t TypePort) Get(defaultValue uint16) uint16 {
28
+	if t.Value == 0 {
29
+		return defaultValue
30
+	}
31
+
32
+	return t.Value
33 33
 }
34 34
 
35
-func (c TypePort) String() string {
36
-	return strconv.Itoa(int(c.value))
35
+func (t *TypePort) UnmarshalJSON(data []byte) error {
36
+	return t.Set(string(data))
37 37
 }
38 38
 
39
-func (c TypePort) Value(defaultValue uint) uint {
40
-	if c.value == 0 {
41
-		return defaultValue
42
-	}
39
+func (t TypePort) MarshalJSON() ([]byte, error) {
40
+	return []byte(t.String()), nil
41
+}
43 42
 
44
-	return c.value
43
+func (t TypePort) String() string {
44
+	return strconv.Itoa(int(t.Value))
45 45
 }

+ 24
- 70
internal/config/type_port_test.go Parādīt failu

@@ -2,7 +2,6 @@ package config_test
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
-	"strconv"
6 5
 	"testing"
7 6
 
8 7
 	"github.com/9seconds/mtg/v2/internal/config"
@@ -18,97 +17,52 @@ type TypePortTestSuite struct {
18 17
 	suite.Suite
19 18
 }
20 19
 
21
-func (suite *TypePortTestSuite) TestUnmarshalNil() {
22
-	typ := &config.TypePort{}
23
-	suite.NoError(typ.UnmarshalJSON(nil))
24
-	suite.Equal("0", typ.String())
25
-}
26
-
27 20
 func (suite *TypePortTestSuite) TestUnmarshalFail() {
28
-	testData := []int{
29
-		-1,
30
-		1_000_000,
21
+	testData := []string{
22
+		"",
23
+		"port",
24
+		"0",
25
+		"-1",
26
+		"1.5",
27
+		"70000",
31 28
 	}
32 29
 
33 30
 	for _, v := range testData {
34
-		data, err := json.Marshal(map[string]int{
31
+		data, err := json.Marshal(map[string]string{
35 32
 			"value": v,
36 33
 		})
37 34
 		suite.NoError(err)
38 35
 
39
-		suite.T().Run(strconv.Itoa(v), func(t *testing.T) {
36
+		suite.T().Run(v, func(t *testing.T) {
40 37
 			assert.Error(t, json.Unmarshal(data, &typePortTestStruct{}))
41 38
 		})
42 39
 	}
43 40
 }
44 41
 
45 42
 func (suite *TypePortTestSuite) TestUnmarshalOk() {
46
-	testData := []int{
47
-		1,
48
-		1_000,
49
-		65535,
50
-	}
51
-
52
-	for _, v := range testData {
53
-		value := v
54
-
55
-		data, err := json.Marshal(map[string]int{
56
-			"value": v,
57
-		})
58
-		suite.NoError(err)
59
-
60
-		suite.T().Run(strconv.Itoa(v), func(t *testing.T) {
61
-			testStruct := &typePortTestStruct{}
62
-
63
-			assert.NoError(t, json.Unmarshal(data, testStruct))
64
-			assert.EqualValues(t, value, testStruct.Value.Value(0))
65
-		})
66
-	}
43
+	testStruct := &typePortTestStruct{}
44
+	suite.NoError(json.Unmarshal([]byte(`{"value": 5}`), testStruct))
45
+	suite.EqualValues(5, testStruct.Value.Value)
67 46
 }
68 47
 
69 48
 func (suite *TypePortTestSuite) TestMarshalOk() {
70
-	testData := map[string]int{
71
-		"1":     1,
72
-		"1000":  1000,
73
-		"65535": 65535,
49
+	testStruct := &typePortTestStruct{
50
+		Value: config.TypePort{
51
+			Value: 10,
52
+		},
74 53
 	}
75 54
 
76
-	for k, v := range testData {
77
-		name := k
78
-		value := v
79
-
80
-		data, err := json.Marshal(map[string]int{
81
-			"value": value,
82
-		})
83
-		suite.NoError(err)
84
-
85
-		suite.T().Run(name, func(t *testing.T) {
86
-			testStruct := &typePortTestStruct{}
87
-
88
-			assert.NoError(t, json.Unmarshal(data, testStruct))
89
-			assert.Equal(t, name, testStruct.Value.String())
90
-
91
-			marshalled, err := testStruct.Value.MarshalJSON()
92
-			assert.NoError(t, err)
93
-			assert.Equal(t, name, string(marshalled))
94
-		})
95
-	}
55
+	data, err := json.Marshal(testStruct)
56
+	suite.NoError(err)
57
+	suite.JSONEq(`{"value":10}`, string(data))
96 58
 }
97 59
 
98
-func (suite *TypePortTestSuite) TestValue() {
99
-	testStruct := &typePortTestStruct{}
100
-
101
-	suite.EqualValues(0, testStruct.Value.Value(0))
102
-	suite.EqualValues(1, testStruct.Value.Value(1))
103
-
104
-	data, err := json.Marshal(map[string]int{
105
-		"value": 5,
106
-	})
107
-	suite.NoError(err)
108
-	suite.NoError(json.Unmarshal(data, testStruct))
60
+func (suite *TypePortTestSuite) TestGet() {
61
+	value := config.TypePort{}
62
+	suite.EqualValues(10, value.Get(10))
109 63
 
110
-	suite.EqualValues(5, testStruct.Value.Value(0))
111
-	suite.EqualValues(5, testStruct.Value.Value(1))
64
+	value.Value = 100
65
+	suite.EqualValues(100, value.Get(10))
112 66
 }
113 67
 
114 68
 func TestTypePort(t *testing.T) {

+ 22
- 21
internal/config/type_prefer_ip.go Parādīt failu

@@ -24,38 +24,39 @@ const (
24 24
 )
25 25
 
26 26
 type TypePreferIP struct {
27
-	value string
27
+	Value string
28 28
 }
29 29
 
30
-func (c *TypePreferIP) UnmarshalText(data []byte) error {
31
-	if len(data) == 0 {
32
-		return nil
33
-	}
30
+func (t *TypePreferIP) Set(value string) error {
31
+	value = strings.ToLower(value)
34 32
 
35
-	text := strings.ToLower(string(data))
33
+	switch value {
34
+	case TypePreferIPPreferIPv4, TypePreferIPPreferIPv6,
35
+		TypePreferOnlyIPv4, TypePreferOnlyIPv6:
36
+        t.Value = value
36 37
 
37
-	switch text {
38
-	case TypePreferIPPreferIPv4, TypePreferIPPreferIPv6, TypePreferOnlyIPv4, TypePreferOnlyIPv6:
39
-		c.value = text
38
+		return nil
40 39
 	default:
41
-		return fmt.Errorf("incorrect prefer-ip value: %s", string(data))
40
+		return fmt.Errorf("unsupported ip preference: %s", value)
42 41
 	}
43
-
44
-	return nil
45 42
 }
46 43
 
47
-func (c TypePreferIP) MarshalText() ([]byte, error) {
48
-	return []byte(c.value), nil
44
+func (t *TypePreferIP) Get(defaultValue string) string {
45
+	if t.Value == "" {
46
+		return defaultValue
47
+	}
48
+
49
+	return t.Value
49 50
 }
50 51
 
51
-func (c *TypePreferIP) String() string {
52
-	return c.value
52
+func (t *TypePreferIP) UnmarshalText(data []byte) error {
53
+	return t.Set(string(data))
53 54
 }
54 55
 
55
-func (c *TypePreferIP) Value(defaultValue string) string {
56
-	if c.value == "" {
57
-		return defaultValue
58
-	}
56
+func (t TypePreferIP) MarshalText() ([]byte, error) {
57
+	return []byte(t.String()), nil
58
+}
59 59
 
60
-	return c.value
60
+func (t TypePreferIP) String() string {
61
+	return t.Value
61 62
 }

+ 28
- 57
internal/config/type_prefer_ip_test.go Parādīt failu

@@ -18,18 +18,12 @@ type TypePreferIPTestSuite struct {
18 18
 	suite.Suite
19 19
 }
20 20
 
21
-func (suite *TypePreferIPTestSuite) TestUnmarshalNil() {
22
-	typ := &config.TypePreferIP{}
23
-	suite.NoError(typ.UnmarshalText(nil))
24
-	suite.Empty(typ.String())
25
-}
26
-
27 21
 func (suite *TypePreferIPTestSuite) TestUnmarshalFail() {
28 22
 	testData := []string{
29
-		"p",
30
-		"ipv4",
31
-		"onlyipv4",
32
-		"ipv6prefer",
23
+		"",
24
+		"prefer",
25
+		"preferipv4",
26
+		config.TypePreferIPPreferIPv4 + "_",
33 27
 	}
34 28
 
35 29
 	for _, v := range testData {
@@ -50,14 +44,10 @@ func (suite *TypePreferIPTestSuite) TestUnmarshalOk() {
50 44
 		config.TypePreferIPPreferIPv6,
51 45
 		config.TypePreferOnlyIPv4,
52 46
 		config.TypePreferOnlyIPv6,
53
-		strings.ToUpper(config.TypePreferIPPreferIPv4),
54
-		strings.ToUpper(config.TypePreferIPPreferIPv6),
55
-		strings.ToUpper(config.TypePreferOnlyIPv4),
56
-		strings.ToUpper(config.TypePreferOnlyIPv6),
57
-		strings.ToLower(config.TypePreferIPPreferIPv4),
58
-		strings.ToLower(config.TypePreferIPPreferIPv6),
59
-		strings.ToLower(config.TypePreferOnlyIPv4),
60
-		strings.ToLower(config.TypePreferOnlyIPv6),
47
+		strings.ToTitle(config.TypePreferOnlyIPv4),
48
+		strings.ToTitle(config.TypePreferOnlyIPv6),
49
+		strings.ToTitle(config.TypePreferIPPreferIPv4),
50
+		strings.ToTitle(config.TypePreferIPPreferIPv6),
61 51
 	}
62 52
 
63 53
 	for _, v := range testData {
@@ -70,11 +60,8 @@ func (suite *TypePreferIPTestSuite) TestUnmarshalOk() {
70 60
 
71 61
 		suite.T().Run(v, func(t *testing.T) {
72 62
 			testStruct := &typePreferIPTestStruct{}
73
-
74 63
 			assert.NoError(t, json.Unmarshal(data, testStruct))
75
-			assert.EqualValues(t,
76
-				strings.ToLower(value),
77
-				testStruct.Value.Value(config.TypePreferIPPreferIPv4))
64
+			assert.Equal(t, strings.ToLower(value), testStruct.Value.Value)
78 65
 		})
79 66
 	}
80 67
 }
@@ -85,55 +72,39 @@ func (suite *TypePreferIPTestSuite) TestMarshalOk() {
85 72
 		config.TypePreferIPPreferIPv6,
86 73
 		config.TypePreferOnlyIPv4,
87 74
 		config.TypePreferOnlyIPv6,
88
-		strings.ToUpper(config.TypePreferIPPreferIPv4),
89
-		strings.ToUpper(config.TypePreferIPPreferIPv6),
90
-		strings.ToUpper(config.TypePreferOnlyIPv4),
91
-		strings.ToUpper(config.TypePreferOnlyIPv6),
92
-		strings.ToLower(config.TypePreferIPPreferIPv4),
93
-		strings.ToLower(config.TypePreferIPPreferIPv6),
94
-		strings.ToLower(config.TypePreferOnlyIPv4),
95
-		strings.ToLower(config.TypePreferOnlyIPv6),
96 75
 	}
97 76
 
98 77
 	for _, v := range testData {
99 78
 		value := v
100 79
 
101
-		data, err := json.Marshal(map[string]string{
102
-			"value": v,
103
-		})
104
-		suite.NoError(err)
105
-
106 80
 		suite.T().Run(v, func(t *testing.T) {
107
-			testStruct := &typePreferIPTestStruct{}
81
+			testStruct := &typePreferIPTestStruct{
82
+				Value: config.TypePreferIP{
83
+					Value: value,
84
+				},
85
+			}
108 86
 
109
-			assert.NoError(t, json.Unmarshal(data, testStruct))
110
-			assert.Equal(t, strings.ToLower(value), testStruct.Value.String())
87
+			encodedJSON, err := json.Marshal(testStruct)
88
+			assert.NoError(t, err)
111 89
 
112
-			marshalled, err := testStruct.Value.MarshalText()
90
+			expectedJSON, err := json.Marshal(map[string]string{
91
+				"value": value,
92
+			})
113 93
 			assert.NoError(t, err)
114
-			assert.Equal(t, strings.ToLower(value), string(marshalled))
94
+
95
+			assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
115 96
 		})
116 97
 	}
117 98
 }
118 99
 
119
-func (suite *TypePreferIPTestSuite) TestValue() {
120
-	testStruct := &typePreferIPTestStruct{}
121
-
122
-	suite.EqualValues(config.TypePreferIPPreferIPv4,
123
-		testStruct.Value.Value(config.TypePreferIPPreferIPv4))
124
-	suite.EqualValues(config.TypePreferIPPreferIPv6,
125
-		testStruct.Value.Value(config.TypePreferIPPreferIPv6))
126
-
127
-	data, err := json.Marshal(map[string]string{
128
-		"value": config.TypePreferOnlyIPv4,
129
-	})
130
-	suite.NoError(err)
131
-	suite.NoError(json.Unmarshal(data, testStruct))
100
+func (suite *TypePreferIPTestSuite) TestGet() {
101
+	value := config.TypePreferIP{}
102
+	suite.Equal(config.TypePreferIPPreferIPv4,
103
+		value.Get(config.TypePreferIPPreferIPv4))
132 104
 
133
-	suite.EqualValues(config.TypePreferOnlyIPv4,
134
-		testStruct.Value.Value(config.TypePreferOnlyIPv6))
135
-	suite.EqualValues(config.TypePreferOnlyIPv4,
136
-		testStruct.Value.Value(config.TypePreferIPPreferIPv6))
105
+	suite.NoError(value.Set(config.TypePreferIPPreferIPv6))
106
+	suite.Equal(config.TypePreferIPPreferIPv6,
107
+		value.Get(config.TypePreferIPPreferIPv4))
137 108
 }
138 109
 
139 110
 func TestTypePreferIP(t *testing.T) {

internal/config2/type_proxy_url.go → internal/config/type_proxy_url.go Parādīt failu

@@ -1,4 +1,4 @@
1
-package config2
1
+package config
2 2
 
3 3
 import (
4 4
 	"fmt"

internal/config2/type_proxy_url_test.go → internal/config/type_proxy_url_test.go Parādīt failu

@@ -1,17 +1,17 @@
1
-package config2_test
1
+package config_test
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5 5
 	"net/url"
6 6
 	"testing"
7 7
 
8
-	"github.com/9seconds/mtg/v2/internal/config2"
8
+	"github.com/9seconds/mtg/v2/internal/config"
9 9
 	"github.com/stretchr/testify/assert"
10 10
 	"github.com/stretchr/testify/suite"
11 11
 )
12 12
 
13 13
 type typeProxyURLTestStruct struct {
14
-	Value config2.TypeProxyURL `json:"value"`
14
+	Value config.TypeProxyURL `json:"value"`
15 15
 }
16 16
 
17 17
 type ProxyURLTestSuite struct {
@@ -69,20 +69,21 @@ func (suite *ProxyURLTestSuite) TestUnmarshalOk() {
69 69
 func (suite *ProxyURLTestSuite) TestMarshalOk() {
70 70
 	parsed, _ := url.Parse("socks5://127.0.0.1:1080?open_threshold=1")
71 71
 	testStruct := &typeProxyURLTestStruct{
72
-		Value: config2.TypeProxyURL{
72
+		Value: config.TypeProxyURL{
73 73
 			Value: parsed,
74 74
 		},
75 75
 	}
76 76
 
77 77
 	encodedJSON, err := json.Marshal(testStruct)
78 78
 	suite.NoError(err)
79
-	suite.JSONEq(`{"value": "socks5://127.0.0.1:1080?open_threshold=1"}`, string(encodedJSON))
79
+	suite.JSONEq(`{"value": "socks5://127.0.0.1:1080?open_threshold=1"}`,
80
+		string(encodedJSON))
80 81
 }
81 82
 
82 83
 func (suite *ProxyURLTestSuite) TestGet() {
83 84
 	emptyURL := &url.URL{}
84 85
 
85
-	value := config2.TypeProxyURL{}
86
+	value := config.TypeProxyURL{}
86 87
 	suite.Equal(emptyURL, value.Get(emptyURL))
87 88
 
88 89
 	value.Value = &url.URL{}

+ 22
- 21
internal/config/type_statsd_tag_format.go Parādīt failu

@@ -20,38 +20,39 @@ const (
20 20
 )
21 21
 
22 22
 type TypeStatsdTagFormat struct {
23
-	value string
23
+	Value string
24 24
 }
25 25
 
26
-func (c *TypeStatsdTagFormat) UnmarshalText(data []byte) error {
27
-	if len(data) == 0 {
28
-		return nil
29
-	}
26
+func (t *TypeStatsdTagFormat) Set(value string) error {
27
+	lowercasedValue := strings.ToLower(value)
30 28
 
31
-	text := strings.ToLower(string(data))
29
+	switch lowercasedValue {
30
+	case TypeStatsdTagFormatDatadog, TypeStatsdTagFormatInfluxdb,
31
+		TypeStatsdTagFormatGraphite:
32
+        t.Value = lowercasedValue
32 33
 
33
-	switch text {
34
-	case TypeStatsdTagFormatInfluxdb, TypeStatsdTagFormatDatadog, TypeStatsdTagFormatGraphite:
35
-		c.value = text
34
+		return nil
36 35
 	default:
37
-		return fmt.Errorf("incorrect tag format value: %s", string(data))
36
+		return fmt.Errorf("unknown tag format %s", value)
38 37
 	}
39
-
40
-	return nil
41 38
 }
42 39
 
43
-func (c TypeStatsdTagFormat) MarshalText() ([]byte, error) {
44
-	return []byte(c.value), nil
40
+func (t TypeStatsdTagFormat) Get(defaultValue string) string {
41
+	if t.Value == "" {
42
+		return defaultValue
43
+	}
44
+
45
+	return t.Value
45 46
 }
46 47
 
47
-func (c *TypeStatsdTagFormat) String() string {
48
-	return c.value
48
+func (t *TypeStatsdTagFormat) UnmarshalText(data []byte) error {
49
+	return t.Set(string(data))
49 50
 }
50 51
 
51
-func (c *TypeStatsdTagFormat) Value(defaultValue string) string {
52
-	if c.value == "" {
53
-		return defaultValue
54
-	}
52
+func (t *TypeStatsdTagFormat) MarshalText() ([]byte, error) {
53
+	return []byte(t.String()), nil
54
+}
55 55
 
56
-	return c.value
56
+func (t *TypeStatsdTagFormat) String() string {
57
+	return t.Value
57 58
 }

+ 30
- 58
internal/config/type_statsd_tag_format_test.go Parādīt failu

@@ -14,22 +14,14 @@ type typeStatsdTagFormatTestStruct struct {
14 14
 	Value config.TypeStatsdTagFormat `json:"value"`
15 15
 }
16 16
 
17
-type TypeStatsdTagFormatTestSuite struct {
17
+type StatsdTagFormatTestSuite struct {
18 18
 	suite.Suite
19 19
 }
20 20
 
21
-func (suite *TypeStatsdTagFormatTestSuite) TestUnmarshalNil() {
22
-	typ := &config.TypeStatsdTagFormat{}
23
-	suite.NoError(typ.UnmarshalText(nil))
24
-	suite.Equal("lalala", typ.Value("lalala"))
25
-}
26
-
27
-func (suite *TypeStatsdTagFormatTestSuite) TestUnmarshalFail() {
21
+func (suite *StatsdTagFormatTestSuite) TestUnmarshalFail() {
28 22
 	testData := []string{
29
-		"p",
30
-		"ipv4",
31
-		"onlyipv4",
32
-		"ipv6prefer",
23
+		"",
24
+		"dogdog",
33 25
 	}
34 26
 
35 27
 	for _, v := range testData {
@@ -44,17 +36,14 @@ func (suite *TypeStatsdTagFormatTestSuite) TestUnmarshalFail() {
44 36
 	}
45 37
 }
46 38
 
47
-func (suite *TypeStatsdTagFormatTestSuite) TestUnmarshalOk() {
39
+func (suite *StatsdTagFormatTestSuite) TestUnmarshalOk() {
48 40
 	testData := []string{
49
-		config.TypeStatsdTagFormatDatadog,
50 41
 		config.TypeStatsdTagFormatInfluxdb,
51 42
 		config.TypeStatsdTagFormatGraphite,
52
-		strings.ToUpper(config.TypeStatsdTagFormatDatadog),
43
+		config.TypeStatsdTagFormatDatadog,
53 44
 		strings.ToUpper(config.TypeStatsdTagFormatInfluxdb),
54 45
 		strings.ToUpper(config.TypeStatsdTagFormatGraphite),
55
-		strings.ToLower(config.TypeStatsdTagFormatDatadog),
56
-		strings.ToLower(config.TypeStatsdTagFormatInfluxdb),
57
-		strings.ToLower(config.TypeStatsdTagFormatGraphite),
46
+		strings.ToUpper(config.TypeStatsdTagFormatDatadog),
58 47
 	}
59 48
 
60 49
 	for _, v := range testData {
@@ -67,70 +56,53 @@ func (suite *TypeStatsdTagFormatTestSuite) TestUnmarshalOk() {
67 56
 
68 57
 		suite.T().Run(v, func(t *testing.T) {
69 58
 			testStruct := &typeStatsdTagFormatTestStruct{}
70
-
71 59
 			assert.NoError(t, json.Unmarshal(data, testStruct))
72
-			assert.EqualValues(t,
73
-				strings.ToLower(value),
74
-				testStruct.Value.Value(config.TypeStatsdTagFormatDatadog))
60
+			assert.Equal(t, strings.ToLower(value), testStruct.Value.Value)
75 61
 		})
76 62
 	}
77 63
 }
78 64
 
79
-func (suite *TypeStatsdTagFormatTestSuite) TestMarshalOk() {
65
+func (suite *StatsdTagFormatTestSuite) TestMarshalOk() {
80 66
 	testData := []string{
81
-		config.TypeStatsdTagFormatDatadog,
82 67
 		config.TypeStatsdTagFormatInfluxdb,
83 68
 		config.TypeStatsdTagFormatGraphite,
84
-		strings.ToUpper(config.TypeStatsdTagFormatDatadog),
85
-		strings.ToUpper(config.TypeStatsdTagFormatInfluxdb),
86
-		strings.ToUpper(config.TypeStatsdTagFormatGraphite),
87
-		strings.ToLower(config.TypeStatsdTagFormatDatadog),
88
-		strings.ToLower(config.TypeStatsdTagFormatInfluxdb),
89
-		strings.ToLower(config.TypeStatsdTagFormatGraphite),
69
+		config.TypeStatsdTagFormatDatadog,
90 70
 	}
91 71
 
92 72
 	for _, v := range testData {
93 73
 		value := v
94 74
 
95
-		data, err := json.Marshal(map[string]string{
96
-			"value": v,
97
-		})
98
-		suite.NoError(err)
99
-
100 75
 		suite.T().Run(v, func(t *testing.T) {
101
-			testStruct := &typeStatsdTagFormatTestStruct{}
76
+			testStruct := &typeStatsdTagFormatTestStruct{
77
+				Value: config.TypeStatsdTagFormat{
78
+					Value: value,
79
+				},
80
+			}
102 81
 
103
-			assert.NoError(t, json.Unmarshal(data, testStruct))
104
-			assert.Equal(t, strings.ToLower(value), testStruct.Value.String())
82
+			encodedJSON, err := json.Marshal(testStruct)
83
+			assert.NoError(t, err)
105 84
 
106
-			marshalled, err := testStruct.Value.MarshalText()
85
+			expectedJSON, err := json.Marshal(map[string]string{
86
+				"value": value,
87
+			})
107 88
 			assert.NoError(t, err)
108
-			assert.Equal(t, strings.ToLower(value), string(marshalled))
89
+
90
+			assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
109 91
 		})
110 92
 	}
111 93
 }
112 94
 
113
-func (suite *TypeStatsdTagFormatTestSuite) TestValue() {
114
-	testStruct := &typePreferIPTestStruct{}
115
-
116
-	suite.EqualValues(config.TypePreferIPPreferIPv4,
117
-		testStruct.Value.Value(config.TypePreferIPPreferIPv4))
118
-	suite.EqualValues(config.TypePreferIPPreferIPv6,
119
-		testStruct.Value.Value(config.TypePreferIPPreferIPv6))
120
-
121
-	data, err := json.Marshal(map[string]string{
122
-		"value": config.TypePreferOnlyIPv4,
123
-	})
124
-	suite.NoError(err)
125
-	suite.NoError(json.Unmarshal(data, testStruct))
95
+func (suite *StatsdTagFormatTestSuite) TestGet() {
96
+	value := config.TypeStatsdTagFormat{}
97
+	suite.Equal(config.TypeStatsdTagFormatDatadog,
98
+		value.Get(config.TypeStatsdTagFormatDatadog))
126 99
 
127
-	suite.EqualValues(config.TypePreferOnlyIPv4,
128
-		testStruct.Value.Value(config.TypePreferOnlyIPv6))
129
-	suite.EqualValues(config.TypePreferOnlyIPv4,
130
-		testStruct.Value.Value(config.TypePreferIPPreferIPv6))
100
+	suite.NoError(value.Set(config.TypeStatsdTagFormatInfluxdb))
101
+	suite.Equal(config.TypeStatsdTagFormatInfluxdb,
102
+		value.Get(config.TypeStatsdTagFormatDatadog))
131 103
 }
132 104
 
133 105
 func TestTypeStatsdTagFormat(t *testing.T) {
134 106
 	t.Parallel()
135
-	suite.Run(t, &TypeStatsdTagFormatTestSuite{})
107
+	suite.Run(t, &StatsdTagFormatTestSuite{})
136 108
 }

+ 0
- 71
internal/config/type_url.go Parādīt failu

@@ -1,71 +0,0 @@
1
-package config
2
-
3
-import (
4
-	"fmt"
5
-	"net"
6
-	"net/url"
7
-)
8
-
9
-type TypeURL struct {
10
-	value *url.URL
11
-}
12
-
13
-func (c *TypeURL) UnmarshalText(data []byte) error { // nolint: cyclop
14
-	if len(data) == 0 {
15
-		return nil
16
-	}
17
-
18
-	value, err := url.Parse(string(data))
19
-	if err != nil {
20
-		return fmt.Errorf("incorrect URL: %w", err)
21
-	}
22
-
23
-	switch value.Scheme {
24
-	case "http", "https", "socks5":
25
-	case "":
26
-		return fmt.Errorf("url %s has to have a schema", value)
27
-	default:
28
-		return fmt.Errorf("unsupported schema %s", value.Scheme)
29
-	}
30
-
31
-	if value.Host == "" {
32
-		return fmt.Errorf("url %s has to have a host", value)
33
-	}
34
-
35
-	if _, _, err := net.SplitHostPort(value.Host); err != nil {
36
-		switch value.Scheme {
37
-		case "http":
38
-			value.Host = net.JoinHostPort(value.Host, "80")
39
-		case "https":
40
-			value.Host = net.JoinHostPort(value.Host, "443")
41
-		case "socks5":
42
-			value.Host = net.JoinHostPort(value.Host, "1080")
43
-		default:
44
-			return fmt.Errorf("cannot set a default port for %s", value)
45
-		}
46
-	}
47
-
48
-	c.value = value
49
-
50
-	return nil
51
-}
52
-
53
-func (c *TypeURL) MarshalText() ([]byte, error) {
54
-	return []byte(c.String()), nil
55
-}
56
-
57
-func (c TypeURL) String() string {
58
-	if c.value == nil {
59
-		return ""
60
-	}
61
-
62
-	return c.value.String()
63
-}
64
-
65
-func (c TypeURL) Value(defaultValue *url.URL) *url.URL {
66
-	if c.value == nil {
67
-		return defaultValue
68
-	}
69
-
70
-	return c.value
71
-}

+ 0
- 107
internal/config/type_url_test.go Parādīt failu

@@ -1,107 +0,0 @@
1
-package config_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"net/url"
6
-	"testing"
7
-
8
-	"github.com/9seconds/mtg/v2/internal/config"
9
-	"github.com/stretchr/testify/assert"
10
-	"github.com/stretchr/testify/suite"
11
-)
12
-
13
-type typeURLTestStruct struct {
14
-	Value config.TypeURL `json:"value"`
15
-}
16
-
17
-type TypeURLTestSuite struct {
18
-	suite.Suite
19
-}
20
-
21
-func (suite *TypeURLTestSuite) TestUnmarshalNil() {
22
-	u, _ := url.Parse("https://google.com")
23
-
24
-	typ := &config.TypeURL{}
25
-	suite.NoError(typ.UnmarshalText(nil))
26
-	suite.Empty(typ.String())
27
-	suite.Equal("https://google.com", typ.Value(u).String())
28
-}
29
-
30
-func (suite *TypeURLTestSuite) TestUnmarshalFail() {
31
-	testData := []string{
32
-		"http:/aaa.com",
33
-		"ipv4",
34
-		"111",
35
-		"://111",
36
-		"http://aaa.com:xxx",
37
-		"gopher://aaa.com:888",
38
-		"gopher://aaa.com",
39
-	}
40
-
41
-	for _, v := range testData {
42
-		data, err := json.Marshal(map[string]string{
43
-			"value": v,
44
-		})
45
-		suite.NoError(err)
46
-
47
-		suite.T().Run(v, func(t *testing.T) {
48
-			assert.Error(t, json.Unmarshal(data, &typeURLTestStruct{}))
49
-		})
50
-	}
51
-}
52
-
53
-func (suite *TypeURLTestSuite) TestUnmarshalOk() {
54
-	testData := map[string]string{
55
-		"https://10.0.0.10:80":    "https://10.0.0.10:80",
56
-		"https://10.0.0.10:443":   "https://10.0.0.10",
57
-		"http://10.0.0.10:8":      "http://10.0.0.10:8",
58
-		"http://10.0.0.10:80":     "http://10.0.0.10",
59
-		"socks5://10.0.0.10:1080": "socks5://10.0.0.10",
60
-		"socks5://10.0.0.10:888":  "socks5://10.0.0.10:888",
61
-	}
62
-
63
-	for k, v := range testData {
64
-		expected := k
65
-		actual := v
66
-
67
-		data, err := json.Marshal(map[string]string{
68
-			"value": actual,
69
-		})
70
-		suite.NoError(err)
71
-
72
-		suite.T().Run(actual, func(t *testing.T) {
73
-			testStruct := &typeURLTestStruct{}
74
-
75
-			assert.NoError(t, json.Unmarshal(data, testStruct))
76
-			assert.Equal(t, expected, testStruct.Value.Value(nil).String())
77
-
78
-			marshalled, err := testStruct.Value.MarshalText()
79
-			assert.NoError(t, err)
80
-			assert.Equal(t, expected, string(marshalled))
81
-		})
82
-	}
83
-}
84
-
85
-func (suite *TypeURLTestSuite) TestValue() {
86
-	testStruct := &typeURLTestStruct{}
87
-
88
-	u1, _ := url.Parse("https://10.0.0.10:80")
89
-	u2, _ := url.Parse("https://10.1.0.10:80")
90
-
91
-	suite.Equal("https://10.0.0.10:80", testStruct.Value.Value(u1).String())
92
-	suite.Equal("https://10.1.0.10:80", testStruct.Value.Value(u2).String())
93
-
94
-	data, err := json.Marshal(map[string]string{
95
-		"value": "http://127.0.0.1:80",
96
-	})
97
-	suite.NoError(err)
98
-	suite.NoError(json.Unmarshal(data, testStruct))
99
-
100
-	suite.Equal("http://127.0.0.1:80", testStruct.Value.Value(u1).String())
101
-	suite.Equal("http://127.0.0.1:80", testStruct.Value.Value(u2).String())
102
-}
103
-
104
-func TestTypeURL(t *testing.T) {
105
-	t.Parallel()
106
-	suite.Run(t, &TypeURLTestSuite{})
107
-}

+ 0
- 81
internal/config2/config.go Parādīt failu

@@ -1,81 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"bytes"
5
-	"encoding/json"
6
-	"fmt"
7
-
8
-	"github.com/9seconds/mtg/v2/mtglib"
9
-)
10
-
11
-type Config struct {
12
-	Debug                TypeBool        `json:"debug"`
13
-	Secret               mtglib.Secret   `json:"secret"`
14
-	BindTo               TypeHostPort    `json:"bindTo"`
15
-	TCPBuffer            TypeBytes       `json:"tcpBuffer"`
16
-	PreferIP             TypePreferIP    `json:"preferIp"`
17
-	DomainFrontingPort   TypePort        `json:"domainFrontingPort"`
18
-	TolerateTimeSkewness TypeDuration    `json:"tolerateTimeSkewness"`
19
-	Concurrency          TypeConcurrency `json:"concurrency"`
20
-	Defense              struct {
21
-		AntiReplay struct {
22
-			Enabled   TypeBool      `json:"enabled"`
23
-			MaxSize   TypeBytes     `json:"maxSize"`
24
-			ErrorRate TypeErrorRate `json:"errorRate"`
25
-		} `json:"antiReplay"`
26
-		Blocklist struct {
27
-			Enabled             TypeBool           `json:"enabled"`
28
-			DownloadConcurrency TypeConcurrency    `json:"downloadConcurrency"`
29
-			URLs                []TypeBlocklistURI `json:"urls"`
30
-			UpdateEach          TypeDuration       `json:"updateEach"`
31
-		} `json:"blocklist"`
32
-	} `json:"defense"`
33
-	Network struct {
34
-		Timeout struct {
35
-			TCP  TypeDuration `json:"tcp"`
36
-			HTTP TypeDuration `json:"http"`
37
-			Idle TypeDuration `json:"idle"`
38
-		} `json:"timeout"`
39
-		DOHIP   TypeIP         `json:"dohIp"`
40
-		Proxies []TypeProxyURL `json:"proxies"`
41
-	} `json:"network"`
42
-	Stats struct {
43
-		StatsD struct {
44
-			Enabled      TypeBool            `json:"enabled"`
45
-			Address      TypeHostPort        `json:"address"`
46
-			MetricPrefix TypeMetricPrefix    `json:"metricPrefix"`
47
-			TagFormat    TypeStatsdTagFormat `json:"tagFormat"`
48
-		} `json:"statsd"`
49
-		Prometheus struct {
50
-			Enabled      TypeBool         `json:"enabled"`
51
-			BindTo       TypeHostPort     `json:"bindTo"`
52
-			HTTPPath     TypeHTTPPath     `json:"httpPath"`
53
-			MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
54
-		} `json:"prometheus"`
55
-	} `json:"stats"`
56
-}
57
-
58
-func (c *Config) Validate() error {
59
-	if !c.Secret.Valid() {
60
-		return fmt.Errorf("invalid secret %s", c.Secret.String())
61
-	}
62
-
63
-    if c.BindTo.Get("") == "" {
64
-		return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
65
-	}
66
-
67
-	return nil
68
-}
69
-
70
-func (c *Config) String() string {
71
-	buf := &bytes.Buffer{}
72
-	encoder := json.NewEncoder(buf)
73
-
74
-	encoder.SetEscapeHTML(false)
75
-
76
-	if err := encoder.Encode(c); err != nil {
77
-		panic(err)
78
-	}
79
-
80
-	return buf.String()
81
-}

+ 0
- 54
internal/config2/config_test.go Parādīt failu

@@ -1,54 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"os"
5
-	"path/filepath"
6
-	"testing"
7
-
8
-	"github.com/9seconds/mtg/v2/internal/config2"
9
-	"github.com/stretchr/testify/suite"
10
-)
11
-
12
-type ConfigTestSuite struct {
13
-    suite.Suite
14
-}
15
-
16
-func (suite *ConfigTestSuite) ReadConfig(filename string) []byte {
17
-	data, err := os.ReadFile(filepath.Join("testdata", filename))
18
-	suite.NoError(err)
19
-
20
-	return data
21
-}
22
-
23
-func (suite *ConfigTestSuite) TestParseEmpty() {
24
-	_, err := config2.Parse([]byte{})
25
-	suite.Error(err)
26
-}
27
-
28
-func (suite *ConfigTestSuite) TestParseBrokenToml() {
29
-	_, err := config2.Parse(suite.ReadConfig("broken.toml"))
30
-	suite.Error(err)
31
-}
32
-
33
-func (suite *ConfigTestSuite) TestParseOnlySecret() {
34
-	_, err := config2.Parse(suite.ReadConfig("only_secret.toml"))
35
-	suite.Error(err)
36
-}
37
-
38
-func (suite *ConfigTestSuite) TestParseMinimalConfig() {
39
-	conf, err := config2.Parse(suite.ReadConfig("minimal.toml"))
40
-	suite.NoError(err)
41
-	suite.Equal("7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t", conf.Secret.Base64())
42
-	suite.Equal("0.0.0.0:3128", conf.BindTo.String())
43
-}
44
-
45
-func (suite *ConfigTestSuite) TestString() {
46
-	conf, err := config2.Parse(suite.ReadConfig("minimal.toml"))
47
-	suite.NoError(err)
48
-	suite.NotEmpty(conf.String())
49
-}
50
-
51
-func TestConfig(t *testing.T) {
52
-	t.Parallel()
53
-	suite.Run(t, &ConfigTestSuite{})
54
-}

+ 0
- 1
internal/config2/testdata/broken.toml Parādīt failu

@@ -1 +0,0 @@
1
-s = sdfsdfds

+ 0
- 2
internal/config2/testdata/minimal.toml Parādīt failu

@@ -1,2 +0,0 @@
1
-secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"
2
-bind-to = "0.0.0.0:3128"

+ 0
- 1
internal/config2/testdata/only_secret.toml Parādīt failu

@@ -1 +0,0 @@
1
-secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"

+ 0
- 77
internal/config2/type_blocklist_uri.go Parādīt failu

@@ -1,77 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"fmt"
5
-	"net/url"
6
-	"os"
7
-	"path/filepath"
8
-)
9
-
10
-type TypeBlocklistURI struct {
11
-	Value string
12
-}
13
-
14
-func (t *TypeBlocklistURI) Set(value string) error {
15
-	if stat, err := os.Stat(value); err == nil || os.IsExist(err) {
16
-        switch {
17
-        case stat.IsDir():
18
-            return fmt.Errorf("value is correct filepath but directory")
19
-        case stat.Mode().Perm() & 0o400 == 0:
20
-            return fmt.Errorf("value is correct filepath but not readable")
21
-        }
22
-
23
-		value, err = filepath.Abs(value)
24
-		if err != nil {
25
-			return fmt.Errorf(
26
-				"value is correct filepath but cannot resolve absolute (%s): %w",
27
-				value, err)
28
-		}
29
-
30
-		t.Value = value
31
-
32
-		return nil
33
-	}
34
-
35
-	parsedURL, err := url.Parse(value)
36
-	if err != nil {
37
-		return fmt.Errorf("incorrect url (%s): %w", value, err)
38
-	}
39
-
40
-	switch parsedURL.Scheme {
41
-	case "http", "https":
42
-	default:
43
-		return fmt.Errorf("unknown schema %s (%s)", parsedURL.Scheme, value)
44
-	}
45
-
46
-	if parsedURL.Host == "" {
47
-		return fmt.Errorf("incorrect url %s", value)
48
-	}
49
-
50
-	t.Value = parsedURL.String()
51
-
52
-	return nil
53
-}
54
-
55
-func (t TypeBlocklistURI) Get(defaultValue string) string {
56
-	if t.Value == "" {
57
-		return defaultValue
58
-	}
59
-
60
-	return t.Value
61
-}
62
-
63
-func (t TypeBlocklistURI) IsRemote() bool {
64
-	return !filepath.IsAbs(t.Value)
65
-}
66
-
67
-func (t *TypeBlocklistURI) UnmarshalText(data []byte) error {
68
-	return t.Set(string(data))
69
-}
70
-
71
-func (t TypeBlocklistURI) MarshalText() ([]byte, error) {
72
-	return []byte(t.String()), nil
73
-}
74
-
75
-func (t TypeBlocklistURI) String() string {
76
-	return t.Value
77
-}

+ 0
- 113
internal/config2/type_blocklist_uri_test.go Parādīt failu

@@ -1,113 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"os"
6
-	"path/filepath"
7
-	"strings"
8
-	"testing"
9
-
10
-	"github.com/9seconds/mtg/v2/internal/config2"
11
-	"github.com/stretchr/testify/assert"
12
-	"github.com/stretchr/testify/suite"
13
-)
14
-
15
-type typeBlocklistURITestStruct struct {
16
-	Value config2.TypeBlocklistURI `json:"value"`
17
-}
18
-
19
-type TypeBlocklistURITestSuite struct {
20
-	suite.Suite
21
-
22
-	directory    string
23
-	absDirectory string
24
-}
25
-
26
-func (suite *TypeBlocklistURITestSuite) SetupSuite() {
27
-	dir, _ := os.Getwd()
28
-	absDir, _ := filepath.Abs(dir)
29
-
30
-	suite.directory = dir
31
-	suite.absDirectory = absDir
32
-}
33
-
34
-func (suite *TypeBlocklistURITestSuite) TestUnmarshalFail() {
35
-	testData := []string{
36
-		"gopher://lalala",
37
-		"https:///paths",
38
-		"h:/=",
39
-		filepath.Join(suite.directory, "___"),
40
-		filepath.Join(suite.absDirectory, "___"),
41
-		suite.directory,
42
-		suite.absDirectory,
43
-	}
44
-
45
-	for _, v := range testData {
46
-		data, err := json.Marshal(map[string]string{
47
-			"value": v,
48
-		})
49
-		suite.NoError(err)
50
-
51
-		suite.T().Run(v, func(t *testing.T) {
52
-			assert.Error(t, json.Unmarshal(data, &typeBlocklistURITestStruct{}))
53
-		})
54
-	}
55
-}
56
-
57
-func (suite *TypeBlocklistURITestSuite) TestUnmarshalOk() {
58
-	testData := []string{
59
-		"http://lalala",
60
-		"https://lalala",
61
-		"https://lalala/path",
62
-		filepath.Join(suite.directory, "config.go"),
63
-		filepath.Join(suite.absDirectory, "config.go"),
64
-	}
65
-
66
-	for _, v := range testData {
67
-		value := v
68
-
69
-		data, err := json.Marshal(map[string]string{
70
-			"value": v,
71
-		})
72
-		suite.NoError(err)
73
-
74
-		suite.T().Run(v, func(t *testing.T) {
75
-			testStruct := &typeBlocklistURITestStruct{}
76
-
77
-			assert.NoError(t, json.Unmarshal(data, testStruct))
78
-			assert.EqualValues(t, value, testStruct.Value.Get(""))
79
-
80
-			if strings.HasPrefix(value, "http") {
81
-				assert.True(t, testStruct.Value.IsRemote())
82
-			} else {
83
-				assert.False(t, testStruct.Value.IsRemote())
84
-			}
85
-		})
86
-	}
87
-}
88
-
89
-func (suite *TypeBlocklistURITestSuite) TestMarshalOk() {
90
-	testStruct := &typeBlocklistURITestStruct{
91
-		Value: config2.TypeBlocklistURI{
92
-			Value: "http://some.url/with/path",
93
-		},
94
-	}
95
-
96
-	data, err := json.Marshal(testStruct)
97
-	suite.NoError(err)
98
-	suite.JSONEq(`{"value": "http://some.url/with/path"}`, string(data))
99
-}
100
-
101
-func (suite *TypeBlocklistURITestSuite) TestGet() {
102
-	value := config2.TypeBlocklistURI{}
103
-	suite.Equal("/path", value.Get("/path"))
104
-
105
-    suite.NoError(value.Set("http://lalala.ru"))
106
-    suite.Equal("http://lalala.ru", value.Get("/path"))
107
-    suite.Equal("http://lalala.ru", value.Get(""))
108
-}
109
-
110
-func TestTypeBlocklistURI(t *testing.T) {
111
-	t.Parallel()
112
-	suite.Run(t, &TypeBlocklistURITestSuite{})
113
-}

+ 0
- 55
internal/config2/type_bytes.go Parādīt failu

@@ -1,55 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"fmt"
5
-	"strings"
6
-
7
-	"github.com/alecthomas/units"
8
-)
9
-
10
-var typeBytesStringCleaner = strings.NewReplacer(" ", "", "\t", "", "IB", "iB")
11
-
12
-type TypeBytes struct {
13
-	Value units.Base2Bytes
14
-}
15
-
16
-func (t *TypeBytes) Set(value string) error {
17
-    normalizedValue := typeBytesStringCleaner.Replace(strings.ToUpper(value))
18
-
19
-	parsedValue, err := units.ParseBase2Bytes(normalizedValue)
20
-	if err != nil {
21
-		return fmt.Errorf("incorrect bytes value (%v): %w", value, err)
22
-	}
23
-
24
-	if parsedValue < 0 {
25
-		return fmt.Errorf("bytes should be positive (%s)", value)
26
-	}
27
-
28
-	t.Value = parsedValue
29
-
30
-	return nil
31
-}
32
-
33
-func (t TypeBytes) Get(defaultValue uint) uint {
34
-	if t.Value == 0 {
35
-		return defaultValue
36
-	}
37
-
38
-	return uint(t.Value)
39
-}
40
-
41
-func (t *TypeBytes) UnmarshalText(data []byte) error {
42
-	return t.Set(string(data))
43
-}
44
-
45
-func (t TypeBytes) MarshalText() ([]byte, error) {
46
-	return []byte(t.String()), nil
47
-}
48
-
49
-func (t TypeBytes) String() string {
50
-	if t.Value == 0 {
51
-		return ""
52
-	}
53
-
54
-	return strings.ToLower(t.Value.String())
55
-}

+ 0
- 86
internal/config2/type_bytes_test.go Parādīt failu

@@ -1,86 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"testing"
6
-
7
-	"github.com/9seconds/mtg/v2/internal/config2"
8
-	"github.com/stretchr/testify/assert"
9
-	"github.com/stretchr/testify/suite"
10
-)
11
-
12
-type typeBytesTestStruct struct {
13
-	Value config2.TypeBytes `json:"value"`
14
-}
15
-
16
-type TypeBytesTestSuite struct {
17
-	suite.Suite
18
-}
19
-
20
-func (suite *TypeBytesTestSuite) TestUnmarshalFail() {
21
-	testData := []string{
22
-		"1m",
23
-		"1",
24
-		"-1kb",
25
-		"-1kib",
26
-	}
27
-
28
-	for _, v := range testData {
29
-		data, err := json.Marshal(map[string]string{
30
-			"value": v,
31
-		})
32
-		suite.NoError(err)
33
-
34
-		suite.T().Run(v, func(t *testing.T) {
35
-			assert.Error(t, json.Unmarshal(data, &typeBytesTestStruct{}))
36
-		})
37
-	}
38
-}
39
-
40
-func (suite *TypeBytesTestSuite) TestUnmarshalOk() {
41
-	testData := map[string]uint{
42
-		"1b":   1,
43
-		"1kb":  1024,
44
-		"1kib": 1024,
45
-		"2mb":  2 * 1024 * 1024,
46
-		"2mib": 2 * 1024 * 1024,
47
-	}
48
-
49
-	for k, v := range testData {
50
-		value := v
51
-
52
-		data, err := json.Marshal(map[string]string{
53
-			"value": k,
54
-		})
55
-		suite.NoError(err)
56
-
57
-		suite.T().Run(k, func(t *testing.T) {
58
-			testStruct := &typeBytesTestStruct{}
59
-
60
-			assert.NoError(t, json.Unmarshal(data, testStruct))
61
-			assert.EqualValues(t, value, testStruct.Value.Get(0))
62
-		})
63
-	}
64
-}
65
-
66
-func (suite *TypeBytesTestSuite) TestMarshalOk() {
67
-    value := typeBytesTestStruct{}
68
-    suite.NoError(value.Value.Set("1kib"))
69
-
70
-    data, err := json.Marshal(value)
71
-    suite.NoError(err)
72
-    suite.JSONEq(`{"value": "1kib"}`, string(data))
73
-}
74
-
75
-func (suite *TypeBytesTestSuite) TestGet() {
76
-    value := config2.TypeBytes{}
77
-    suite.EqualValues(1000, value.Get(1000))
78
-
79
-    suite.NoError(value.Set("1mib"))
80
-    suite.EqualValues(1048576, value.Get(1000))
81
-}
82
-
83
-func TestTypeBytes(t *testing.T) {
84
-	t.Parallel()
85
-	suite.Run(t, &TypeBytesTestSuite{})
86
-}

+ 0
- 53
internal/config2/type_duration.go Parādīt failu

@@ -1,53 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"fmt"
5
-	"strings"
6
-	"time"
7
-)
8
-
9
-var typeDurationStringCleaner = strings.NewReplacer(" ", "", "\t", "")
10
-
11
-type TypeDuration struct {
12
-	Value time.Duration
13
-}
14
-
15
-func (t *TypeDuration) Set(value string) error {
16
-	parsedValue, err := time.ParseDuration(
17
-		typeDurationStringCleaner.Replace(strings.ToLower(value)))
18
-	if err != nil {
19
-		return fmt.Errorf("incorrect duration (%s): %w", value, err)
20
-	}
21
-
22
-	if parsedValue < 0 {
23
-		return fmt.Errorf("duration has to be a positive: %s", value)
24
-	}
25
-
26
-	t.Value = parsedValue
27
-
28
-	return nil
29
-}
30
-
31
-func (t TypeDuration) Get(defaultValue time.Duration) time.Duration {
32
-	if t.Value == 0 {
33
-		return defaultValue
34
-	}
35
-
36
-	return t.Value
37
-}
38
-
39
-func (t *TypeDuration) UnmarshalText(data []byte) error {
40
-	return t.Set(string(data))
41
-}
42
-
43
-func (t TypeDuration) MarshalText() ([]byte, error) {
44
-	return []byte(t.String()), nil
45
-}
46
-
47
-func (t TypeDuration) String() string {
48
-	if t.Value == 0 {
49
-		return ""
50
-	}
51
-
52
-	return t.Value.String()
53
-}

+ 0
- 110
internal/config2/type_duration_test.go Parādīt failu

@@ -1,110 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"testing"
6
-	"time"
7
-
8
-	"github.com/9seconds/mtg/v2/internal/config2"
9
-	"github.com/stretchr/testify/assert"
10
-	"github.com/stretchr/testify/suite"
11
-)
12
-
13
-type typeDurationTestStruct struct {
14
-	Value config2.TypeDuration `json:"value"`
15
-}
16
-
17
-type TypeDurationTestSuite struct {
18
-	suite.Suite
19
-}
20
-
21
-func (suite *TypeDurationTestSuite) TestUnmarshalFail() {
22
-	testData := []string{
23
-		"-1s",
24
-		"1 seconds ago",
25
-		"1s ago",
26
-		"",
27
-	}
28
-
29
-	for _, v := range testData {
30
-		data, err := json.Marshal(map[string]string{
31
-			"value": v,
32
-		})
33
-		suite.NoError(err)
34
-
35
-		suite.T().Run(v, func(t *testing.T) {
36
-			assert.Error(t, json.Unmarshal(data, &typeDurationTestStruct{}))
37
-		})
38
-	}
39
-}
40
-
41
-func (suite *TypeDurationTestSuite) TestUnmarshalOk() {
42
-	testData := map[string]time.Duration{
43
-		"1s":   time.Second,
44
-		"0":    0 * time.Second,
45
-		"0s":   0 * time.Second,
46
-		"1\tM": time.Minute,
47
-		"1H":   time.Hour,
48
-		"1 h":  time.Hour,
49
-	}
50
-
51
-	for k, v := range testData {
52
-		value := v
53
-
54
-		data, err := json.Marshal(map[string]string{
55
-			"value": k,
56
-		})
57
-		suite.NoError(err)
58
-
59
-		suite.T().Run(k, func(t *testing.T) {
60
-			testStruct := &typeDurationTestStruct{}
61
-
62
-			assert.NoError(t, json.Unmarshal(data, testStruct))
63
-			assert.Equal(t, value, testStruct.Value.Value)
64
-		})
65
-	}
66
-}
67
-
68
-func (suite *TypeDurationTestSuite) TestMarshalOk() {
69
-	testData := map[string]string{
70
-		"1s":  "1s",
71
-		"0":   "",
72
-		"0s":  "",
73
-		"0ms": "",
74
-		"1 H": "1h0m0s",
75
-	}
76
-
77
-	for k, v := range testData {
78
-		value := k
79
-		expected := v
80
-
81
-		suite.T().Run(value, func(t *testing.T) {
82
-			testStruct := &typeDurationTestStruct{}
83
-
84
-			assert.NoError(t, testStruct.Value.Set(value))
85
-
86
-			data, err := json.Marshal(testStruct)
87
-			assert.NoError(t, err)
88
-
89
-			expectedJson, err := json.Marshal(map[string]string{
90
-				"value": expected,
91
-			})
92
-			assert.NoError(t, err)
93
-
94
-			assert.JSONEq(t, string(expectedJson), string(data))
95
-		})
96
-	}
97
-}
98
-
99
-func (suite *TypeDurationTestSuite) TestGet() {
100
-	value := config2.TypeDuration{}
101
-	suite.Equal(time.Second, value.Get(time.Second))
102
-
103
-	value.Value = 3 * time.Second
104
-	suite.Equal(3*time.Second, value.Get(time.Hour))
105
-}
106
-
107
-func TestTypeDuration(t *testing.T) {
108
-	t.Parallel()
109
-	suite.Run(t, &TypeDurationTestSuite{})
110
-}

+ 0
- 47
internal/config2/type_error_rate.go Parādīt failu

@@ -1,47 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"fmt"
5
-	"strconv"
6
-)
7
-
8
-const typeErrorRateIgnoreLess = 1e-8
9
-
10
-type TypeErrorRate struct {
11
-	Value float64
12
-}
13
-
14
-func (t *TypeErrorRate) Set(value string) error {
15
-	parsedValue, err := strconv.ParseFloat(value, 64)
16
-	if err != nil {
17
-		return fmt.Errorf("Value is not a float (%s): %w", value, err)
18
-	}
19
-
20
-	if parsedValue <= 0.0 || parsedValue >= 100.0 {
21
-		return fmt.Errorf("Value should be 0 < x < 100 (%s)", value)
22
-	}
23
-
24
-	t.Value = parsedValue
25
-
26
-	return nil
27
-}
28
-
29
-func (t TypeErrorRate) Get(defaultValue float64) float64 {
30
-	if t.Value < typeErrorRateIgnoreLess {
31
-		return defaultValue
32
-	}
33
-
34
-	return t.Value
35
-}
36
-
37
-func (t *TypeErrorRate) UnmarshalText(data []byte) error {
38
-	return t.Set(string(data))
39
-}
40
-
41
-func (t TypeErrorRate) MarshalText() ([]byte, error) {
42
-	return []byte(t.String()), nil
43
-}
44
-
45
-func (t TypeErrorRate) String() string {
46
-	return strconv.FormatFloat(t.Value, 'f', -1, 64)
47
-}

+ 0
- 94
internal/config2/type_error_rate_test.go Parādīt failu

@@ -1,94 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"testing"
6
-
7
-	"github.com/9seconds/mtg/v2/internal/config2"
8
-	"github.com/stretchr/testify/assert"
9
-	"github.com/stretchr/testify/suite"
10
-)
11
-
12
-type typeErrorRateTestStruct struct {
13
-	Value config2.TypeErrorRate `json:"value"`
14
-}
15
-
16
-type TypeErrorRateTestSuite struct {
17
-	suite.Suite
18
-}
19
-
20
-func (suite *TypeErrorRateTestSuite) TestUnmarshalFail() {
21
-	testData := []string{
22
-		"",
23
-		"1s",
24
-		"1,",
25
-		"1,2",
26
-		".",
27
-		"3.4.5",
28
-		"3.5.",
29
-		".3.5",
30
-		"some word",
31
-		"1e2",
32
-		"-1.0",
33
-	}
34
-
35
-	for _, v := range testData {
36
-		data, err := json.Marshal(map[string]string{
37
-			"value": v,
38
-		})
39
-		suite.NoError(err)
40
-
41
-		suite.T().Run(v, func(t *testing.T) {
42
-			assert.Error(t, json.Unmarshal(data, &typeErrorRateTestStruct{}))
43
-		})
44
-	}
45
-}
46
-
47
-func (suite *TypeErrorRateTestSuite) TestUnmarshalOk() {
48
-	testData := map[string]float64{
49
-		"1":   1.0,
50
-		"1.0": 1.0,
51
-		"0.5": 0.5,
52
-		".5":  0.5,
53
-	}
54
-
55
-	for k, v := range testData {
56
-		value := v
57
-
58
-		data, err := json.Marshal(map[string]string{
59
-			"value": k,
60
-		})
61
-		suite.NoError(err)
62
-
63
-		suite.T().Run(k, func(t *testing.T) {
64
-			testStruct := &typeErrorRateTestStruct{}
65
-			assert.NoError(t, json.Unmarshal(data, testStruct))
66
-			assert.InEpsilon(t, value, testStruct.Value.Value, 1e-10)
67
-		})
68
-	}
69
-}
70
-
71
-func (suite *TypeErrorRateTestSuite) TestMarshalOk() {
72
-	testStruct := typeErrorRateTestStruct{
73
-		Value: config2.TypeErrorRate{
74
-			Value: 1.01,
75
-		},
76
-	}
77
-
78
-	encodedJson, err := json.Marshal(testStruct)
79
-	suite.NoError(err)
80
-	suite.JSONEq(`{"value": "1.01"}`, string(encodedJson))
81
-}
82
-
83
-func (suite *TypeErrorRateTestSuite) TestGet() {
84
-	value := config2.TypeErrorRate{}
85
-	suite.InEpsilon(1.0, value.Get(1.0), 1e-10)
86
-
87
-	value.Value = 5.0
88
-	suite.InEpsilon(5.0, value.Get(1.0), 1e-10)
89
-}
90
-
91
-func TestTypeErrorRate(t *testing.T) {
92
-	t.Parallel()
93
-	suite.Run(t, &TypeErrorRateTestSuite{})
94
-}

+ 0
- 59
internal/config2/type_hostport.go Parādīt failu

@@ -1,59 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"fmt"
5
-	"net"
6
-	"strconv"
7
-)
8
-
9
-type TypeHostPort struct {
10
-	Value string
11
-}
12
-
13
-func (t *TypeHostPort) Set(value string) error {
14
-	host, port, err := net.SplitHostPort(value)
15
-	if err != nil {
16
-		return fmt.Errorf("incorrect host:port value (%v): %w", value, err)
17
-	}
18
-
19
-	portValue, err := strconv.ParseUint(port, 10, 16)
20
-	if err != nil {
21
-		return fmt.Errorf("incorrect port number (%v): %w", value, err)
22
-	}
23
-
24
-	if portValue == 0 {
25
-		return fmt.Errorf("incorrect port number (%s)", value)
26
-	}
27
-
28
-	if host == "" {
29
-		return fmt.Errorf("empty host: %s", value)
30
-	}
31
-
32
-	if net.ParseIP(host) == nil {
33
-		return fmt.Errorf("host is not an IP address: %s", value)
34
-	}
35
-
36
-	t.Value = net.JoinHostPort(host, port)
37
-
38
-	return nil
39
-}
40
-
41
-func (t TypeHostPort) Get(defaultValue string) string {
42
-	if t.Value == "" {
43
-		return defaultValue
44
-	}
45
-
46
-	return t.Value
47
-}
48
-
49
-func (t *TypeHostPort) UnmarshalText(data []byte) error {
50
-	return t.Set(string(data))
51
-}
52
-
53
-func (t TypeHostPort) MarshalText() ([]byte, error) {
54
-	return []byte(t.String()), nil
55
-}
56
-
57
-func (t TypeHostPort) String() string {
58
-	return t.Value
59
-}

+ 0
- 88
internal/config2/type_hostport_test.go Parādīt failu

@@ -1,88 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"testing"
6
-
7
-	"github.com/9seconds/mtg/v2/internal/config2"
8
-	"github.com/stretchr/testify/assert"
9
-	"github.com/stretchr/testify/suite"
10
-)
11
-
12
-type typeHostPortTestStruct struct {
13
-	Value config2.TypeHostPort `json:"value"`
14
-}
15
-
16
-type TypeHostPortTestSuite struct {
17
-	suite.Suite
18
-}
19
-
20
-func (suite *TypeHostPortTestSuite) TestUnmarshalFail() {
21
-	testData := []string{
22
-		":",
23
-		":800",
24
-		"127.0.0.1:8000000",
25
-		"12...:80",
26
-		"",
27
-		"localhost",
28
-		"google.com:",
29
-	}
30
-
31
-	for _, v := range testData {
32
-		data, err := json.Marshal(map[string]string{
33
-			"value": v,
34
-		})
35
-		suite.NoError(err)
36
-
37
-		suite.T().Run(v, func(t *testing.T) {
38
-			assert.Error(t, json.Unmarshal(data, &typeHostPortTestStruct{}))
39
-		})
40
-	}
41
-}
42
-
43
-func (suite *TypeHostPortTestSuite) TestUnmarshalOk() {
44
-	testData := []string{
45
-		"127.0.0.1:80",
46
-		"10.0.0.10:6553",
47
-	}
48
-
49
-	for _, v := range testData {
50
-		value := v
51
-
52
-		data, err := json.Marshal(map[string]string{
53
-			"value": v,
54
-		})
55
-		suite.NoError(err)
56
-
57
-		suite.T().Run(v, func(t *testing.T) {
58
-			testStruct := &typeHostPortTestStruct{}
59
-			assert.NoError(t, json.Unmarshal(data, testStruct))
60
-			assert.Equal(t, value, testStruct.Value.Value)
61
-		})
62
-	}
63
-}
64
-
65
-func (suite *TypeHostPortTestSuite) TestMarshalOk() {
66
-	testStruct := typeHostPortTestStruct{
67
-		Value: config2.TypeHostPort{
68
-			Value: "127.0.0.1:8000",
69
-		},
70
-	}
71
-
72
-	data, err := json.Marshal(testStruct)
73
-	suite.NoError(err)
74
-	suite.JSONEq(`{"value": "127.0.0.1:8000"}`, string(data))
75
-}
76
-
77
-func (suite *TypeHostPortTestSuite) TestGet() {
78
-	value := config2.TypeHostPort{}
79
-	suite.Equal("127.0.0.1:9000", value.Get("127.0.0.1:9000"))
80
-
81
-	value.Value = "127.0.0.1:80"
82
-	suite.Equal("127.0.0.1:80", value.Get("127.0.0.1:9000"))
83
-}
84
-
85
-func TestTypeHostPort(t *testing.T) {
86
-	t.Parallel()
87
-	suite.Run(t, &TypeHostPortTestSuite{})
88
-}

+ 0
- 33
internal/config2/type_http_path.go Parādīt failu

@@ -1,33 +0,0 @@
1
-package config2
2
-
3
-import "strings"
4
-
5
-type TypeHTTPPath struct {
6
-	Value string
7
-}
8
-
9
-func (t *TypeHTTPPath) Set(value string) error {
10
-	t.Value = "/" + strings.Trim(value, "/")
11
-
12
-	return nil
13
-}
14
-
15
-func (t TypeHTTPPath) Get(defaultValue string) string {
16
-	if t.Value == "" {
17
-		return defaultValue
18
-	}
19
-
20
-	return t.Value
21
-}
22
-
23
-func (t *TypeHTTPPath) UnmarshalText(data []byte) error {
24
-	return t.Set(string(data))
25
-}
26
-
27
-func (t TypeHTTPPath) MarshalText() ([]byte, error) {
28
-	return []byte(t.String()), nil
29
-}
30
-
31
-func (t TypeHTTPPath) String() string {
32
-	return t.Value
33
-}

+ 0
- 67
internal/config2/type_http_path_test.go Parādīt failu

@@ -1,67 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"testing"
6
-
7
-	"github.com/9seconds/mtg/v2/internal/config2"
8
-	"github.com/stretchr/testify/assert"
9
-	"github.com/stretchr/testify/suite"
10
-)
11
-
12
-type typeHTTPPathTestStruct struct {
13
-	Value config2.TypeHTTPPath `json:"value"`
14
-}
15
-
16
-type TypeHTTPPathTestSuite struct {
17
-	suite.Suite
18
-}
19
-
20
-func (suite *TypeHTTPPathTestSuite) TestUnmarshalOk() {
21
-	testData := map[string]string{
22
-		"":      "/",
23
-		"/":     "/",
24
-		"/path": "/path",
25
-		"path":  "/path",
26
-	}
27
-
28
-	for k, v := range testData {
29
-		value := v
30
-
31
-		data, err := json.Marshal(map[string]string{
32
-			"value": k,
33
-		})
34
-		suite.NoError(err)
35
-
36
-		suite.T().Run(k, func(t *testing.T) {
37
-			testStruct := &typeHTTPPathTestStruct{}
38
-			assert.NoError(t, json.Unmarshal(data, testStruct))
39
-			assert.Equal(t, value, testStruct.Value.Get(""))
40
-		})
41
-	}
42
-}
43
-
44
-func (suite *TypeHTTPPathTestSuite) TestMarshalOk() {
45
-	value := typeHTTPPathTestStruct{
46
-		Value: config2.TypeHTTPPath{
47
-			Value: "/path",
48
-		},
49
-	}
50
-
51
-	data, err := json.Marshal(value)
52
-	suite.NoError(err)
53
-	suite.JSONEq(`{"value": "/path"}`, string(data))
54
-}
55
-
56
-func (suite *TypeHTTPPathTestSuite) TestGet() {
57
-	value := config2.TypeHTTPPath{}
58
-	suite.Equal("/hello", value.Get("/hello"))
59
-
60
-	suite.NoError(value.Set("/lalala"))
61
-	suite.Equal("/lalala", value.Get("/hello"))
62
-}
63
-
64
-func TestTypeHTTPPath(t *testing.T) {
65
-	t.Parallel()
66
-	suite.Run(t, &TypeHTTPPathTestSuite{})
67
-}

+ 0
- 45
internal/config2/type_ip.go Parādīt failu

@@ -1,45 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"fmt"
5
-	"net"
6
-)
7
-
8
-type TypeIP struct {
9
-	Value net.IP
10
-}
11
-
12
-func (t *TypeIP) Set(value string) error {
13
-	ip := net.ParseIP(value)
14
-	if ip == nil {
15
-		return fmt.Errorf("incorret ip %s", value)
16
-	}
17
-
18
-    t.Value = ip
19
-
20
-	return nil
21
-}
22
-
23
-func (t *TypeIP) Get(defaultValue net.IP) net.IP {
24
-	if len(t.Value) == 0 {
25
-		return defaultValue
26
-	}
27
-
28
-	return t.Value
29
-}
30
-
31
-func (t *TypeIP) UnmarshalText(data []byte) error {
32
-	return t.Set(string(data))
33
-}
34
-
35
-func (t TypeIP) MarshalText() ([]byte, error) {
36
-	return []byte(t.String()), nil
37
-}
38
-
39
-func (t TypeIP) String() string {
40
-	if len(t.Value) == 0 {
41
-		return ""
42
-	}
43
-
44
-	return t.Value.String()
45
-}

+ 0
- 104
internal/config2/type_ip_test.go Parādīt failu

@@ -1,104 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"net"
6
-	"testing"
7
-
8
-	"github.com/9seconds/mtg/v2/internal/config2"
9
-	"github.com/stretchr/testify/assert"
10
-	"github.com/stretchr/testify/suite"
11
-)
12
-
13
-type typeIPTestStruct struct {
14
-	Value config2.TypeIP `json:"value"`
15
-}
16
-
17
-type TypeIPTestSuite struct {
18
-	suite.Suite
19
-}
20
-
21
-func (suite *TypeIPTestSuite) TestUnmarshalFail() {
22
-	testData := []string{
23
-		"",
24
-		"....",
25
-		"0...",
26
-		"300.200.200.800",
27
-		"[]",
28
-	}
29
-
30
-	for _, v := range testData {
31
-		data, err := json.Marshal(map[string]string{
32
-			"value": v,
33
-		})
34
-		suite.NoError(err)
35
-
36
-		suite.T().Run(v, func(t *testing.T) {
37
-			assert.Error(t, json.Unmarshal(data, &typeIPTestStruct{}))
38
-		})
39
-	}
40
-}
41
-
42
-func (suite *TypeIPTestSuite) TestUnmarshalOk() {
43
-	testData := map[string]string{
44
-		"2001:0db8:85a3:0000:0000:8a2e:0370:7334": "2001:db8:85a3::8a2e:370:7334",
45
-		"127.0.0.1": "127.0.0.1",
46
-	}
47
-
48
-	for k, v := range testData {
49
-		expected := v
50
-
51
-		data, err := json.Marshal(map[string]string{
52
-			"value": k,
53
-		})
54
-		suite.NoError(err)
55
-
56
-		suite.T().Run(k, func(t *testing.T) {
57
-			testStruct := &typeIPTestStruct{}
58
-			assert.NoError(t, json.Unmarshal(data, testStruct))
59
-			assert.Equal(t, expected, testStruct.Value.Get(nil).String())
60
-		})
61
-	}
62
-}
63
-
64
-func (suite *TypeIPTestSuite) TestMarshalOk() {
65
-	testData := []string{
66
-		"2001:db8:85a3::8a2e:370:7334",
67
-		"127.0.0.1",
68
-	}
69
-
70
-	for _, v := range testData {
71
-		value := v
72
-
73
-		suite.T().Run(v, func(t *testing.T) {
74
-			testStruct := &typeIPTestStruct{
75
-				Value: config2.TypeIP{
76
-					Value: net.ParseIP(value),
77
-				},
78
-			}
79
-
80
-			encodedJSON, err := json.Marshal(testStruct)
81
-			assert.NoError(t, err)
82
-
83
-			expectedJSON, err := json.Marshal(map[string]string{
84
-				"value": value,
85
-			})
86
-			assert.NoError(t, err)
87
-
88
-			assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
89
-		})
90
-	}
91
-}
92
-
93
-func (suite *TypeIPTestSuite) TestGet() {
94
-	value := config2.TypeIP{}
95
-	suite.Equal("127.0.0.1", value.Get(net.ParseIP("127.0.0.1")).String())
96
-
97
-	suite.NoError(value.Set("127.0.0.2"))
98
-	suite.Equal("127.0.0.2", value.Get(net.ParseIP("127.0.0.1")).String())
99
-}
100
-
101
-func TestTypeIP(t *testing.T) {
102
-	t.Parallel()
103
-	suite.Run(t, &TypeIPTestSuite{})
104
-}

+ 0
- 40
internal/config2/type_metric_prefix.go Parādīt failu

@@ -1,40 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"fmt"
5
-	"regexp"
6
-)
7
-
8
-type TypeMetricPrefix struct {
9
-	Value string
10
-}
11
-
12
-func (t *TypeMetricPrefix) Set(value string) error {
13
-	if ok, err := regexp.MatchString("^[a-z0-9]+$", value); !ok || err != nil {
14
-        return fmt.Errorf("incorrect metric prefix %s: %w", value, err)
15
-	}
16
-
17
-    t.Value = value
18
-
19
-	return nil
20
-}
21
-
22
-func (t TypeMetricPrefix) Get(defaultValue string) string {
23
-	if t.Value == "" {
24
-		return defaultValue
25
-	}
26
-
27
-	return t.Value
28
-}
29
-
30
-func (t *TypeMetricPrefix) UnmarshalText(data []byte) error {
31
-	return t.Set(string(data))
32
-}
33
-
34
-func (t TypeMetricPrefix) MarshalText() ([]byte, error) {
35
-	return []byte(t.String()), nil
36
-}
37
-
38
-func (t TypeMetricPrefix) String() string {
39
-	return t.Value
40
-}

+ 0
- 70
internal/config2/type_metric_prefix_test.go Parādīt failu

@@ -1,70 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"testing"
6
-
7
-	"github.com/9seconds/mtg/v2/internal/config2"
8
-	"github.com/stretchr/testify/assert"
9
-	"github.com/stretchr/testify/suite"
10
-)
11
-
12
-type typeMetricPrefixTestStruct struct {
13
-	Value config2.TypeMetricPrefix `json:"value"`
14
-}
15
-
16
-type TypeMetricPrefixTestSuite struct {
17
-	suite.Suite
18
-}
19
-
20
-func (suite *TypeMetricPrefixTestSuite) TestUnmarshalFail() {
21
-	testData := []string{
22
-		"",
23
-		"-",
24
-		"hello/world",
25
-		"lala*",
26
-		"++sdf++",
27
-	}
28
-
29
-	for _, v := range testData {
30
-		data, err := json.Marshal(map[string]string{
31
-			"value": v,
32
-		})
33
-		suite.NoError(err)
34
-
35
-		suite.T().Run(v, func(t *testing.T) {
36
-			assert.Error(t, json.Unmarshal(data, &typeMetricPrefixTestStruct{}))
37
-		})
38
-	}
39
-}
40
-
41
-func (suite *TypeMetricPrefixTestSuite) TestUnmarshalOk() {
42
-	testStruct := &typeMetricPrefixTestStruct{}
43
-	suite.NoError(json.Unmarshal([]byte(`{"value": "mtg"}`), testStruct))
44
-	suite.Equal("mtg", testStruct.Value.Get("lalala"))
45
-}
46
-
47
-func (suite *TypeMetricPrefixTestSuite) TestMarshalOk() {
48
-	testStruct := &typeMetricPrefixTestStruct{
49
-		Value: config2.TypeMetricPrefix{
50
-			Value: "mtg",
51
-		},
52
-	}
53
-
54
-	data, err := json.Marshal(testStruct)
55
-	suite.NoError(err)
56
-	suite.JSONEq(`{"value": "mtg"}`, string(data))
57
-}
58
-
59
-func (suite *TypeMetricPrefixTestSuite) TestGet() {
60
-	value := config2.TypeMetricPrefix{}
61
-	suite.Equal("lalala", value.Get("lalala"))
62
-
63
-	value.Value = "mtg"
64
-	suite.Equal("mtg", value.Get("lalala"))
65
-}
66
-
67
-func TestTypeMetricPrefix(t *testing.T) {
68
-	t.Parallel()
69
-	suite.Run(t, &TypeMetricPrefixTestSuite{})
70
-}

+ 0
- 45
internal/config2/type_port.go Parādīt failu

@@ -1,45 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"fmt"
5
-	"strconv"
6
-)
7
-
8
-type TypePort struct {
9
-	Value uint16
10
-}
11
-
12
-func (t *TypePort) Set(value string) error {
13
-	portValue, err := strconv.ParseUint(value, 10, 16)
14
-	if err != nil {
15
-		return fmt.Errorf("incorrect port number (%v): %w", value, err)
16
-	}
17
-
18
-	if portValue == 0 {
19
-		return fmt.Errorf("incorrect port number (%s)", value)
20
-	}
21
-
22
-    t.Value = uint16(portValue)
23
-
24
-	return nil
25
-}
26
-
27
-func (t TypePort) Get(defaultValue uint16) uint16 {
28
-	if t.Value == 0 {
29
-		return defaultValue
30
-	}
31
-
32
-	return t.Value
33
-}
34
-
35
-func (t *TypePort) UnmarshalJSON(data []byte) error {
36
-	return t.Set(string(data))
37
-}
38
-
39
-func (t TypePort) MarshalJSON() ([]byte, error) {
40
-	return []byte(t.String()), nil
41
-}
42
-
43
-func (t TypePort) String() string {
44
-	return strconv.Itoa(int(t.Value))
45
-}

+ 0
- 71
internal/config2/type_port_test.go Parādīt failu

@@ -1,71 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"testing"
6
-
7
-	"github.com/9seconds/mtg/v2/internal/config2"
8
-	"github.com/stretchr/testify/assert"
9
-	"github.com/stretchr/testify/suite"
10
-)
11
-
12
-type typePortTestStruct struct {
13
-	Value config2.TypePort `json:"value"`
14
-}
15
-
16
-type TypePortTestSuite struct {
17
-	suite.Suite
18
-}
19
-
20
-func (suite *TypePortTestSuite) TestUnmarshalFail() {
21
-	testData := []string{
22
-		"",
23
-		"port",
24
-		"0",
25
-		"-1",
26
-		"1.5",
27
-		"70000",
28
-	}
29
-
30
-	for _, v := range testData {
31
-		data, err := json.Marshal(map[string]string{
32
-			"value": v,
33
-		})
34
-		suite.NoError(err)
35
-
36
-		suite.T().Run(v, func(t *testing.T) {
37
-			assert.Error(t, json.Unmarshal(data, &typePortTestStruct{}))
38
-		})
39
-	}
40
-}
41
-
42
-func (suite *TypePortTestSuite) TestUnmarshalOk() {
43
-	testStruct := &typePortTestStruct{}
44
-	suite.NoError(json.Unmarshal([]byte(`{"value": 5}`), testStruct))
45
-	suite.EqualValues(5, testStruct.Value.Value)
46
-}
47
-
48
-func (suite *TypePortTestSuite) TestMarshalOk() {
49
-	testStruct := &typePortTestStruct{
50
-		Value: config2.TypePort{
51
-			Value: 10,
52
-		},
53
-	}
54
-
55
-	data, err := json.Marshal(testStruct)
56
-	suite.NoError(err)
57
-	suite.JSONEq(`{"value":10}`, string(data))
58
-}
59
-
60
-func (suite *TypePortTestSuite) TestGet() {
61
-	value := config2.TypePort{}
62
-	suite.EqualValues(10, value.Get(10))
63
-
64
-	value.Value = 100
65
-	suite.EqualValues(100, value.Get(10))
66
-}
67
-
68
-func TestTypePort(t *testing.T) {
69
-	t.Parallel()
70
-	suite.Run(t, &TypePortTestSuite{})
71
-}

+ 0
- 62
internal/config2/type_prefer_ip.go Parādīt failu

@@ -1,62 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"fmt"
5
-	"strings"
6
-)
7
-
8
-const (
9
-	// TypePreferIPPreferIPv4 states that you prefer to use IPv4 addresses
10
-	// but IPv6 is also possible.
11
-	TypePreferIPPreferIPv4 = "prefer-ipv4"
12
-
13
-	// TypePreferIPPreferIPv6 states that you prefer to use IPv6 addresses
14
-	// but IPv4 is also possible.
15
-	TypePreferIPPreferIPv6 = "prefer-ipv6"
16
-
17
-	// TypePreferOnlyIPv4 states that you prefer to use IPv4 addresses
18
-	// only.
19
-	TypePreferOnlyIPv4 = "only-ipv4"
20
-
21
-	// TypePreferOnlyIPv6 states that you prefer to use IPv6 addresses
22
-	// only.
23
-	TypePreferOnlyIPv6 = "only-ipv6"
24
-)
25
-
26
-type TypePreferIP struct {
27
-	Value string
28
-}
29
-
30
-func (t *TypePreferIP) Set(value string) error {
31
-	value = strings.ToLower(value)
32
-
33
-	switch value {
34
-	case TypePreferIPPreferIPv4, TypePreferIPPreferIPv6,
35
-		TypePreferOnlyIPv4, TypePreferOnlyIPv6:
36
-        t.Value = value
37
-
38
-		return nil
39
-	default:
40
-		return fmt.Errorf("unsupported ip preference: %s", value)
41
-	}
42
-}
43
-
44
-func (t *TypePreferIP) Get(defaultValue string) string {
45
-	if t.Value == "" {
46
-		return defaultValue
47
-	}
48
-
49
-	return t.Value
50
-}
51
-
52
-func (t *TypePreferIP) UnmarshalText(data []byte) error {
53
-	return t.Set(string(data))
54
-}
55
-
56
-func (t TypePreferIP) MarshalText() ([]byte, error) {
57
-	return []byte(t.String()), nil
58
-}
59
-
60
-func (t TypePreferIP) String() string {
61
-	return t.Value
62
-}

+ 0
- 113
internal/config2/type_prefer_ip_test.go Parādīt failu

@@ -1,113 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"strings"
6
-	"testing"
7
-
8
-	"github.com/9seconds/mtg/v2/internal/config2"
9
-	"github.com/stretchr/testify/assert"
10
-	"github.com/stretchr/testify/suite"
11
-)
12
-
13
-type typePreferIPTestStruct struct {
14
-	Value config2.TypePreferIP `json:"value"`
15
-}
16
-
17
-type TypePreferIPTestSuite struct {
18
-	suite.Suite
19
-}
20
-
21
-func (suite *TypePreferIPTestSuite) TestUnmarshalFail() {
22
-	testData := []string{
23
-		"",
24
-		"prefer",
25
-		"preferipv4",
26
-		config2.TypePreferIPPreferIPv4 + "_",
27
-	}
28
-
29
-	for _, v := range testData {
30
-		data, err := json.Marshal(map[string]string{
31
-			"value": v,
32
-		})
33
-		suite.NoError(err)
34
-
35
-		suite.T().Run(v, func(t *testing.T) {
36
-			assert.Error(t, json.Unmarshal(data, &typePreferIPTestStruct{}))
37
-		})
38
-	}
39
-}
40
-
41
-func (suite *TypePreferIPTestSuite) TestUnmarshalOk() {
42
-	testData := []string{
43
-		config2.TypePreferIPPreferIPv4,
44
-		config2.TypePreferIPPreferIPv6,
45
-		config2.TypePreferOnlyIPv4,
46
-		config2.TypePreferOnlyIPv6,
47
-		strings.ToTitle(config2.TypePreferOnlyIPv4),
48
-		strings.ToTitle(config2.TypePreferOnlyIPv6),
49
-		strings.ToTitle(config2.TypePreferIPPreferIPv4),
50
-		strings.ToTitle(config2.TypePreferIPPreferIPv6),
51
-	}
52
-
53
-	for _, v := range testData {
54
-		value := v
55
-
56
-		data, err := json.Marshal(map[string]string{
57
-			"value": v,
58
-		})
59
-		suite.NoError(err)
60
-
61
-		suite.T().Run(v, func(t *testing.T) {
62
-			testStruct := &typePreferIPTestStruct{}
63
-			assert.NoError(t, json.Unmarshal(data, testStruct))
64
-			assert.Equal(t, strings.ToLower(value), testStruct.Value.Value)
65
-		})
66
-	}
67
-}
68
-
69
-func (suite *TypePreferIPTestSuite) TestMarshalOk() {
70
-	testData := []string{
71
-		config2.TypePreferIPPreferIPv4,
72
-		config2.TypePreferIPPreferIPv6,
73
-		config2.TypePreferOnlyIPv4,
74
-		config2.TypePreferOnlyIPv6,
75
-	}
76
-
77
-	for _, v := range testData {
78
-		value := v
79
-
80
-		suite.T().Run(v, func(t *testing.T) {
81
-			testStruct := &typePreferIPTestStruct{
82
-				Value: config2.TypePreferIP{
83
-					Value: value,
84
-				},
85
-			}
86
-
87
-			encodedJSON, err := json.Marshal(testStruct)
88
-			assert.NoError(t, err)
89
-
90
-			expectedJSON, err := json.Marshal(map[string]string{
91
-				"value": value,
92
-			})
93
-			assert.NoError(t, err)
94
-
95
-			assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
96
-		})
97
-	}
98
-}
99
-
100
-func (suite *TypePreferIPTestSuite) TestGet() {
101
-	value := config2.TypePreferIP{}
102
-	suite.Equal(config2.TypePreferIPPreferIPv4,
103
-		value.Get(config2.TypePreferIPPreferIPv4))
104
-
105
-	suite.NoError(value.Set(config2.TypePreferIPPreferIPv6))
106
-	suite.Equal(config2.TypePreferIPPreferIPv6,
107
-		value.Get(config2.TypePreferIPPreferIPv4))
108
-}
109
-
110
-func TestTypePreferIP(t *testing.T) {
111
-	t.Parallel()
112
-	suite.Run(t, &TypePreferIPTestSuite{})
113
-}

+ 0
- 58
internal/config2/type_statsd_tag_format.go Parādīt failu

@@ -1,58 +0,0 @@
1
-package config2
2
-
3
-import (
4
-	"fmt"
5
-	"strings"
6
-)
7
-
8
-const (
9
-	// TypeStatsdTagFormatInfluxdb defines a tag format compatible with
10
-	// InfluxDB.
11
-	TypeStatsdTagFormatInfluxdb = "influxdb"
12
-
13
-	// TypeStatsdTagFormatDatadog defines a tag format compatible with
14
-	// DataDog.
15
-	TypeStatsdTagFormatDatadog = "datadog"
16
-
17
-	// TypeStatsdTagFormatGraphite defines a tag format compatible with
18
-	// Graphite.
19
-	TypeStatsdTagFormatGraphite = "graphite"
20
-)
21
-
22
-type TypeStatsdTagFormat struct {
23
-	Value string
24
-}
25
-
26
-func (t *TypeStatsdTagFormat) Set(value string) error {
27
-	lowercasedValue := strings.ToLower(value)
28
-
29
-	switch lowercasedValue {
30
-	case TypeStatsdTagFormatDatadog, TypeStatsdTagFormatInfluxdb,
31
-		TypeStatsdTagFormatGraphite:
32
-        t.Value = lowercasedValue
33
-
34
-		return nil
35
-	default:
36
-		return fmt.Errorf("unknown tag format %s", value)
37
-	}
38
-}
39
-
40
-func (t TypeStatsdTagFormat) Get(defaultValue string) string {
41
-	if t.Value == "" {
42
-		return defaultValue
43
-	}
44
-
45
-	return t.Value
46
-}
47
-
48
-func (t *TypeStatsdTagFormat) UnmarshalText(data []byte) error {
49
-	return t.Set(string(data))
50
-}
51
-
52
-func (t *TypeStatsdTagFormat) MarshalText() ([]byte, error) {
53
-	return []byte(t.String()), nil
54
-}
55
-
56
-func (t *TypeStatsdTagFormat) String() string {
57
-	return t.Value
58
-}

+ 0
- 108
internal/config2/type_statsd_tag_format_test.go Parādīt failu

@@ -1,108 +0,0 @@
1
-package config2_test
2
-
3
-import (
4
-	"encoding/json"
5
-	"strings"
6
-	"testing"
7
-
8
-	"github.com/9seconds/mtg/v2/internal/config2"
9
-	"github.com/stretchr/testify/assert"
10
-	"github.com/stretchr/testify/suite"
11
-)
12
-
13
-type typeStatsdTagFormatTestStruct struct {
14
-	Value config2.TypeStatsdTagFormat `json:"value"`
15
-}
16
-
17
-type StatsdTagFormatTestSuite struct {
18
-	suite.Suite
19
-}
20
-
21
-func (suite *StatsdTagFormatTestSuite) TestUnmarshalFail() {
22
-	testData := []string{
23
-		"",
24
-		"dogdog",
25
-	}
26
-
27
-	for _, v := range testData {
28
-		data, err := json.Marshal(map[string]string{
29
-			"value": v,
30
-		})
31
-		suite.NoError(err)
32
-
33
-		suite.T().Run(v, func(t *testing.T) {
34
-			assert.Error(t, json.Unmarshal(data, &typeStatsdTagFormatTestStruct{}))
35
-		})
36
-	}
37
-}
38
-
39
-func (suite *StatsdTagFormatTestSuite) TestUnmarshalOk() {
40
-	testData := []string{
41
-		config2.TypeStatsdTagFormatInfluxdb,
42
-		config2.TypeStatsdTagFormatGraphite,
43
-		config2.TypeStatsdTagFormatDatadog,
44
-		strings.ToUpper(config2.TypeStatsdTagFormatInfluxdb),
45
-		strings.ToUpper(config2.TypeStatsdTagFormatGraphite),
46
-		strings.ToUpper(config2.TypeStatsdTagFormatDatadog),
47
-	}
48
-
49
-	for _, v := range testData {
50
-		value := v
51
-
52
-		data, err := json.Marshal(map[string]string{
53
-			"value": v,
54
-		})
55
-		suite.NoError(err)
56
-
57
-		suite.T().Run(v, func(t *testing.T) {
58
-			testStruct := &typeStatsdTagFormatTestStruct{}
59
-			assert.NoError(t, json.Unmarshal(data, testStruct))
60
-			assert.Equal(t, strings.ToLower(value), testStruct.Value.Value)
61
-		})
62
-	}
63
-}
64
-
65
-func (suite *StatsdTagFormatTestSuite) TestMarshalOk() {
66
-	testData := []string{
67
-		config2.TypeStatsdTagFormatInfluxdb,
68
-		config2.TypeStatsdTagFormatGraphite,
69
-		config2.TypeStatsdTagFormatDatadog,
70
-	}
71
-
72
-	for _, v := range testData {
73
-		value := v
74
-
75
-		suite.T().Run(v, func(t *testing.T) {
76
-			testStruct := &typeStatsdTagFormatTestStruct{
77
-				Value: config2.TypeStatsdTagFormat{
78
-					Value: value,
79
-				},
80
-			}
81
-
82
-			encodedJSON, err := json.Marshal(testStruct)
83
-			assert.NoError(t, err)
84
-
85
-			expectedJSON, err := json.Marshal(map[string]string{
86
-				"value": value,
87
-			})
88
-			assert.NoError(t, err)
89
-
90
-			assert.JSONEq(t, string(expectedJSON), string(encodedJSON))
91
-		})
92
-	}
93
-}
94
-
95
-func (suite *StatsdTagFormatTestSuite) TestGet() {
96
-	value := config2.TypeStatsdTagFormat{}
97
-	suite.Equal(config2.TypeStatsdTagFormatDatadog,
98
-		value.Get(config2.TypeStatsdTagFormatDatadog))
99
-
100
-	suite.NoError(value.Set(config2.TypeStatsdTagFormatInfluxdb))
101
-	suite.Equal(config2.TypeStatsdTagFormatInfluxdb,
102
-		value.Get(config2.TypeStatsdTagFormatDatadog))
103
-}
104
-
105
-func TestTypeStatsdTagFormat(t *testing.T) {
106
-	t.Parallel()
107
-	suite.Run(t, &StatsdTagFormatTestSuite{})
108
-}

Notiek ielāde…
Atcelt
Saglabāt