浏览代码

Marshalling of config to string

tags/v2.0.0-rc1
9seconds 5 年前
父节点
当前提交
098a9c411a
共有 1 个文件被更改,包括 57 次插入0 次删除
  1. 57
    0
      config.go

+ 57
- 0
config.go 查看文件

@@ -43,6 +43,10 @@ func (c *configTypeHostPort) UnmarshalText(data []byte) error {
43 43
 	return nil
44 44
 }
45 45
 
46
+func (c configTypeHostPort) MarshalText() ([]byte, error) {
47
+	return []byte(c.String()), nil
48
+}
49
+
46 50
 func (c configTypeHostPort) String() string {
47 51
 	return c.Value(net.IP{}, 0)
48 52
 }
@@ -75,6 +79,10 @@ func (c *configTypePort) UnmarshalJSON(data []byte) error {
75 79
 	return nil
76 80
 }
77 81
 
82
+func (c *configTypePort) MarshalJSON() ([]byte, error) {
83
+	return json.Marshal(c.value)
84
+}
85
+
78 86
 func (c configTypePort) String() string {
79 87
 	return strconv.Itoa(int(c.value))
80 88
 }
@@ -110,6 +118,10 @@ func (c *configTypeBytes) UnmarshalText(data []byte) error {
110 118
 	return nil
111 119
 }
112 120
 
121
+func (c configTypeBytes) MarshalText() ([]byte, error) {
122
+	return []byte(c.String()), nil
123
+}
124
+
113 125
 func (c configTypeBytes) String() string {
114 126
 	return units.ToString(int64(c.value), 1024, "ib", "b")
115 127
 }
@@ -143,6 +155,10 @@ func (c *configTypePreferIP) UnmarshalText(data []byte) error {
143 155
 	return nil
144 156
 }
145 157
 
158
+func (c configTypePreferIP) MarshalText() ([]byte, error) {
159
+	return []byte(c.value), nil
160
+}
161
+
146 162
 func (c *configTypePreferIP) String() string {
147 163
 	return c.value
148 164
 }
@@ -178,6 +194,10 @@ func (c *configTypeDuration) UnmarshalText(data []byte) error {
178 194
 	return nil
179 195
 }
180 196
 
197
+func (c configTypeDuration) MarshalText() ([]byte, error) {
198
+	return []byte(c.value.String()), nil
199
+}
200
+
181 201
 func (c configTypeDuration) String() string {
182 202
 	return c.value.String()
183 203
 }
@@ -209,6 +229,10 @@ func (c *configTypeFloat) UnmarshalJSON(data []byte) error {
209 229
 	return nil
210 230
 }
211 231
 
232
+func (c *configTypeFloat) MarshalText() ([]byte, error) {
233
+	return []byte(c.String()), nil
234
+}
235
+
212 236
 func (c configTypeFloat) String() string {
213 237
 	return strconv.FormatFloat(c.value, 'f', -1, 64)
214 238
 }
@@ -240,7 +264,15 @@ func (c *configTypeIP) UnmarshalText(data []byte) error {
240 264
 	return nil
241 265
 }
242 266
 
267
+func (c *configTypeIP) MarshalText() ([]byte, error) {
268
+	return []byte(c.String()), nil
269
+}
270
+
243 271
 func (c configTypeIP) String() string {
272
+	if c.value == nil {
273
+		return ""
274
+	}
275
+
244 276
 	return c.value.String()
245 277
 }
246 278
 
@@ -271,6 +303,10 @@ func (c *configTypeURL) UnmarshalText(data []byte) error {
271 303
 	return nil
272 304
 }
273 305
 
306
+func (c *configTypeURL) MarshalText() ([]byte, error) {
307
+	return []byte(c.String()), nil
308
+}
309
+
274 310
 func (c configTypeURL) String() string {
275 311
 	if c.value == nil {
276 312
 		return ""
@@ -307,6 +343,10 @@ func (c *configTypeMetricPrefix) UnmarshalText(data []byte) error {
307 343
 	return nil
308 344
 }
309 345
 
346
+func (c configTypeMetricPrefix) MarshalText() ([]byte, error) {
347
+	return []byte(c.String()), nil
348
+}
349
+
310 350
 func (c configTypeMetricPrefix) String() string {
311 351
 	return c.value
312 352
 }
@@ -331,6 +371,10 @@ func (c *configTypeHTTPPath) UnmarshalText(data []byte) error { // nolint: unpar
331 371
 	return nil
332 372
 }
333 373
 
374
+func (c configTypeHTTPPath) MarshalText() ([]byte, error) {
375
+	return []byte(c.String()), nil
376
+}
377
+
334 378
 func (c configTypeHTTPPath) String() string {
335 379
 	return c.value
336 380
 }
@@ -392,6 +436,19 @@ func (c *config) Validate() error {
392 436
 	return nil
393 437
 }
394 438
 
439
+func (c *config) String() string {
440
+	buf := &bytes.Buffer{}
441
+	encoder := json.NewEncoder(buf)
442
+
443
+	encoder.SetEscapeHTML(false)
444
+
445
+	if err := encoder.Encode(c); err != nil {
446
+		panic(err)
447
+	}
448
+
449
+	return buf.String()
450
+}
451
+
395 452
 type configRaw struct {
396 453
 	Debug     bool   `toml:"debug" json:"debug"`
397 454
 	Secret    string `toml:"secret" json:"secret"`

正在加载...
取消
保存