From 098a9c411a1c54e6b1106d42b750370050877b35 Mon Sep 17 00:00:00 2001 From: 9seconds Date: Wed, 10 Mar 2021 15:28:06 +0300 Subject: [PATCH] Marshalling of config to string --- config.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/config.go b/config.go index 5ef956d..c496453 100644 --- a/config.go +++ b/config.go @@ -43,6 +43,10 @@ func (c *configTypeHostPort) UnmarshalText(data []byte) error { return nil } +func (c configTypeHostPort) MarshalText() ([]byte, error) { + return []byte(c.String()), nil +} + func (c configTypeHostPort) String() string { return c.Value(net.IP{}, 0) } @@ -75,6 +79,10 @@ func (c *configTypePort) UnmarshalJSON(data []byte) error { return nil } +func (c *configTypePort) MarshalJSON() ([]byte, error) { + return json.Marshal(c.value) +} + func (c configTypePort) String() string { return strconv.Itoa(int(c.value)) } @@ -110,6 +118,10 @@ func (c *configTypeBytes) UnmarshalText(data []byte) error { return nil } +func (c configTypeBytes) MarshalText() ([]byte, error) { + return []byte(c.String()), nil +} + func (c configTypeBytes) String() string { return units.ToString(int64(c.value), 1024, "ib", "b") } @@ -143,6 +155,10 @@ func (c *configTypePreferIP) UnmarshalText(data []byte) error { return nil } +func (c configTypePreferIP) MarshalText() ([]byte, error) { + return []byte(c.value), nil +} + func (c *configTypePreferIP) String() string { return c.value } @@ -178,6 +194,10 @@ func (c *configTypeDuration) UnmarshalText(data []byte) error { return nil } +func (c configTypeDuration) MarshalText() ([]byte, error) { + return []byte(c.value.String()), nil +} + func (c configTypeDuration) String() string { return c.value.String() } @@ -209,6 +229,10 @@ func (c *configTypeFloat) UnmarshalJSON(data []byte) error { return nil } +func (c *configTypeFloat) MarshalText() ([]byte, error) { + return []byte(c.String()), nil +} + func (c configTypeFloat) String() string { return strconv.FormatFloat(c.value, 'f', -1, 64) } @@ -240,7 +264,15 @@ func (c *configTypeIP) UnmarshalText(data []byte) error { return nil } +func (c *configTypeIP) MarshalText() ([]byte, error) { + return []byte(c.String()), nil +} + func (c configTypeIP) String() string { + if c.value == nil { + return "" + } + return c.value.String() } @@ -271,6 +303,10 @@ func (c *configTypeURL) UnmarshalText(data []byte) error { return nil } +func (c *configTypeURL) MarshalText() ([]byte, error) { + return []byte(c.String()), nil +} + func (c configTypeURL) String() string { if c.value == nil { return "" @@ -307,6 +343,10 @@ func (c *configTypeMetricPrefix) UnmarshalText(data []byte) error { return nil } +func (c configTypeMetricPrefix) MarshalText() ([]byte, error) { + return []byte(c.String()), nil +} + func (c configTypeMetricPrefix) String() string { return c.value } @@ -331,6 +371,10 @@ func (c *configTypeHTTPPath) UnmarshalText(data []byte) error { // nolint: unpar return nil } +func (c configTypeHTTPPath) MarshalText() ([]byte, error) { + return []byte(c.String()), nil +} + func (c configTypeHTTPPath) String() string { return c.value } @@ -392,6 +436,19 @@ func (c *config) Validate() error { return nil } +func (c *config) String() string { + buf := &bytes.Buffer{} + encoder := json.NewEncoder(buf) + + encoder.SetEscapeHTML(false) + + if err := encoder.Encode(c); err != nil { + panic(err) + } + + return buf.String() +} + type configRaw struct { Debug bool `toml:"debug" json:"debug"` Secret string `toml:"secret" json:"secret"`