Jens Geyer | 222362c | 2020-02-16 01:54:56 +0100 | [diff] [blame^] | 1 | // 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 | |
| 18 | using System; |
| 19 | using System.Collections; |
| 20 | using System.Collections.Generic; |
| 21 | using System.Text; |
| 22 | using Thrift.Protocol; |
| 23 | |
| 24 | namespace Thrift.Collections |
| 25 | { |
| 26 | |
| 27 | |
| 28 | public static class ToStringExtensions |
| 29 | { |
| 30 | public static void ToString(this object self, StringBuilder sb, bool first = true) |
| 31 | { |
| 32 | if (!first) |
| 33 | sb.Append(", "); |
| 34 | |
| 35 | bool first_child = true; |
| 36 | if (self is string) // string is IEnumerable |
| 37 | { |
| 38 | sb.Append('"'); |
| 39 | sb.Append(self); |
| 40 | sb.Append('"'); |
| 41 | } |
| 42 | else if (self is IDictionary) |
| 43 | { |
| 44 | sb.Append("{ "); |
| 45 | foreach (DictionaryEntry pair in (self as IDictionary)) |
| 46 | { |
| 47 | if (first_child) |
| 48 | first_child = false; |
| 49 | else |
| 50 | sb.Append(","); |
| 51 | |
| 52 | sb.Append("{ "); |
| 53 | pair.Key.ToString(sb); |
| 54 | sb.Append(", "); |
| 55 | pair.Value.ToString(sb); |
| 56 | sb.Append("}"); |
| 57 | } |
| 58 | sb.Append("}"); |
| 59 | } |
| 60 | else if (self is IEnumerable) |
| 61 | { |
| 62 | sb.Append("{ "); |
| 63 | foreach (var elm in (self as IEnumerable)) |
| 64 | { |
| 65 | elm.ToString(sb, first_child); |
| 66 | first_child = false; |
| 67 | } |
| 68 | sb.Append("}"); |
| 69 | } |
| 70 | else if (self is TBase) |
| 71 | { |
| 72 | sb.Append((self as TBase).ToString()); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | sb.Append(self.ToString()); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | } |