1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "debug_output.h"
16 
17 #include <string>
18 
19 META_BEGIN_NAMESPACE()
20 namespace Serialization {
21 
Process(const ISerNode::Ptr & tree)22 BASE_NS::string DebugOutput::Process(const ISerNode::Ptr& tree)
23 {
24     tree->Apply(*this);
25     return result_;
26 }
27 
Visit(const IRootNode & n)28 void DebugOutput::Visit(const IRootNode& n)
29 {
30     n.GetObject()->Apply(*this);
31 }
Visit(const INilNode & n)32 void DebugOutput::Visit(const INilNode& n)
33 {
34     Output("<null>");
35 }
Visit(const IObjectNode & n)36 void DebugOutput::Visit(const IObjectNode& n)
37 {
38     IncOutputIndent();
39     Output("Object [name=" + n.GetObjectName() + "]\n");
40     IncOutputIndent();
41     n.GetMembers()->Apply(*this);
42     DecIndentEndLine();
43     DecIndentEndLine();
44 }
Visit(const IArrayNode & n)45 void DebugOutput::Visit(const IArrayNode& n)
46 {
47     bool first = true;
48     for (auto&& v : n.GetMembers()) {
49         if (!first) {
50             NewLine();
51             first = false;
52         }
53         v->Apply(*this);
54     }
55 }
Visit(const IMapNode & n)56 void DebugOutput::Visit(const IMapNode& n)
57 {
58     bool first = true;
59     for (auto&& v : n.GetMembers()) {
60         if (!first) {
61             NewLine();
62             first = false;
63         }
64         Output(v.name + ": ");
65         v.node->Apply(*this);
66     }
67 }
Visit(const IBuiltinValueNode<bool> & n)68 void DebugOutput::Visit(const IBuiltinValueNode<bool>& n)
69 {
70     Output(std::to_string(n.GetValue()).c_str());
71 }
Visit(const IBuiltinValueNode<double> & n)72 void DebugOutput::Visit(const IBuiltinValueNode<double>& n)
73 {
74     Output(std::to_string(n.GetValue()).c_str());
75 }
Visit(const IBuiltinValueNode<int64_t> & n)76 void DebugOutput::Visit(const IBuiltinValueNode<int64_t>& n)
77 {
78     Output(std::to_string(n.GetValue()).c_str());
79 }
Visit(const IBuiltinValueNode<uint64_t> & n)80 void DebugOutput::Visit(const IBuiltinValueNode<uint64_t>& n)
81 {
82     Output(std::to_string(n.GetValue()).c_str());
83 }
Visit(const IBuiltinValueNode<BASE_NS::string> & n)84 void DebugOutput::Visit(const IBuiltinValueNode<BASE_NS::string>& n)
85 {
86     Output(n.GetValue());
87 }
Visit(const IBuiltinValueNode<RefUri> & n)88 void DebugOutput::Visit(const IBuiltinValueNode<RefUri>& n)
89 {
90     Output(n.GetValue().ToString());
91 }
Visit(const ISerNode & n)92 void DebugOutput::Visit(const ISerNode& n)
93 {
94     result_ += "<Unknown node type>\n";
95 }
Output(const BASE_NS::string & v)96 void DebugOutput::Output(const BASE_NS::string& v)
97 {
98     result_ += v;
99 }
NewLine()100 void DebugOutput::NewLine()
101 {
102     result_ += "\n" + BASE_NS::string(indent_, '\t');
103 }
IncOutputIndent()104 void DebugOutput::IncOutputIndent()
105 {
106     ++indent_;
107     result_ += BASE_NS::string(indent_, '\t');
108 }
DecIndentEndLine()109 void DebugOutput::DecIndentEndLine()
110 {
111     --indent_;
112     result_ += "\n";
113 }
114 
115 } // namespace Serialization
116 META_END_NAMESPACE()