blob: ae69d1cb9502b70ec89ef3effa11280249511776 [file] [log] [blame]
Jens Geyer222362c2020-02-16 01:54:56 +01001// Licensed to the Apache Software Foundation(ASF) under one
2// or more contributor license agreements.See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18using System;
19using System.Collections;
20using System.Collections.Generic;
Jens Geyer443412d2022-09-23 16:25:48 +020021using System.Globalization;
Jens Geyer222362c2020-02-16 01:54:56 +010022using System.Text;
23using Thrift.Protocol;
24
Jens Geyeraffea7b2020-05-22 17:28:30 +020025namespace Thrift.Protocol
Jens Geyer222362c2020-02-16 01:54:56 +010026{
27
28
29 public static class ToStringExtensions
30 {
31 public static void ToString(this object self, StringBuilder sb, bool first = true)
32 {
33 if (!first)
34 sb.Append(", ");
35
36 bool first_child = true;
37 if (self is string) // string is IEnumerable
38 {
39 sb.Append('"');
40 sb.Append(self);
41 sb.Append('"');
42 }
43 else if (self is IDictionary)
44 {
45 sb.Append("{ ");
46 foreach (DictionaryEntry pair in (self as IDictionary))
47 {
48 if (first_child)
49 first_child = false;
50 else
Jens Geyer561bc9a2022-01-26 22:38:04 +010051 sb.Append(',');
Jens Geyer222362c2020-02-16 01:54:56 +010052
53 sb.Append("{ ");
54 pair.Key.ToString(sb);
55 sb.Append(", ");
56 pair.Value.ToString(sb);
Jens Geyer561bc9a2022-01-26 22:38:04 +010057 sb.Append('}');
Jens Geyer222362c2020-02-16 01:54:56 +010058 }
Jens Geyer561bc9a2022-01-26 22:38:04 +010059 sb.Append('}');
Jens Geyer222362c2020-02-16 01:54:56 +010060 }
61 else if (self is IEnumerable)
62 {
63 sb.Append("{ ");
64 foreach (var elm in (self as IEnumerable))
65 {
66 elm.ToString(sb, first_child);
67 first_child = false;
68 }
Jens Geyer561bc9a2022-01-26 22:38:04 +010069 sb.Append('}');
Jens Geyer222362c2020-02-16 01:54:56 +010070 }
71 else if (self is TBase)
72 {
73 sb.Append((self as TBase).ToString());
74 }
Jens Geyer443412d2022-09-23 16:25:48 +020075 else if (self is double)
76 {
77 sb.Append(((double)self).ToString(CultureInfo.InvariantCulture));
78 }
Jens Geyer222362c2020-02-16 01:54:56 +010079 else
80 {
Jens Geyer443412d2022-09-23 16:25:48 +020081 sb.Append(self != null ? self.ToString() : "<null>");
Jens Geyer222362c2020-02-16 01:54:56 +010082 }
83 }
84 }
85
86
87}