-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathutil.cc
More file actions
159 lines (144 loc) · 4.49 KB
/
Copy pathutil.cc
File metadata and controls
159 lines (144 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <thrift/compiler/generate/csharp/util.h>
#include <fmt/format.h>
#include <thrift/compiler/ast/t_container.h>
#include <thrift/compiler/ast/t_primitive_type.h>
#include <thrift/compiler/ast/t_structured.h>
#include <thrift/compiler/diagnostic.h>
namespace apache::thrift::compiler::csharp {
std::string get_csharp_property_name(const std::string& name) {
return name;
}
std::string get_csharp_namespace(
const t_program& program, diagnostics_engine& diags) {
const std::string& csharp_ns = program.get_namespace("csharp");
if (!csharp_ns.empty()) {
return csharp_ns;
}
// For included programs (e.g., annotation files) that lack an explicit
// namespace csharp, derive a fallback from the package or program name.
// The root program is validated separately by the generator entry point.
const t_package& pkg = program.package();
if (pkg.empty()) {
diags.error(
program,
"No namespace 'csharp' in `{}`. Please add "
"'namespace csharp <YourNamespace>' to the .thrift file.",
program.name());
return program.name();
}
// Convert package path (e.g., "facebook.com/thrift/annotation/hack")
// to C# namespace (e.g., "facebook.thrift.annotation.hack")
// Strip the TLD (last domain component) consistent with other language
// generators (C++, Python, Hack).
std::string ns;
const auto& domain = pkg.domain();
for (auto it = domain.begin(); it != domain.end(); ++it) {
if (std::next(it) == domain.end()) {
break; // Skip TLD (last component, e.g. "com", "dev")
}
if (!ns.empty()) {
ns += ".";
}
ns += *it;
}
for (std::string_view component : pkg.path()) {
if (!ns.empty()) {
ns += ".";
}
ns += component;
}
if (!ns.empty()) {
return ns;
}
diags.error(
program,
"No namespace 'csharp' in `{}`. Please add "
"'namespace csharp <YourNamespace>' to the .thrift file.",
program.name());
return program.name();
}
std::string escape_csharp_string(const std::string& value) {
std::string escaped;
escaped.reserve(value.size());
for (unsigned char c : value) {
switch (c) {
case '\\':
escaped += "\\\\";
break;
case '"':
escaped += "\\\"";
break;
case '\n':
escaped += "\\n";
break;
case '\r':
escaped += "\\r";
break;
case '\t':
escaped += "\\t";
break;
case '\0':
escaped += "\\0";
break;
default:
if (c < 0x20) {
// Escape control characters (except those handled above)
escaped += fmt::format("\\u{:04x}", static_cast<int>(c));
} else {
escaped += c;
}
break;
}
}
return escaped;
}
std::string quote_csharp_string(const std::string& value) {
return "\"" + escape_csharp_string(value) + "\"";
}
bool is_csharp_nullable_type(const t_type* type) {
type = type->get_true_type();
// Reference types are nullable
if (dynamic_cast<const t_structured*>(type)) {
return true;
}
if (const auto* prim_type = dynamic_cast<const t_primitive_type*>(type)) {
switch (prim_type->primitive_type()) {
case t_primitive_type::type::t_string:
case t_primitive_type::type::t_binary:
return true;
case t_primitive_type::type::t_void:
case t_primitive_type::type::t_bool:
case t_primitive_type::type::t_byte:
case t_primitive_type::type::t_i16:
case t_primitive_type::type::t_i32:
case t_primitive_type::type::t_i64:
case t_primitive_type::type::t_float:
case t_primitive_type::type::t_double:
return false;
default:
return false;
}
}
// Collections are reference types
if (dynamic_cast<const t_list*>(type) || dynamic_cast<const t_set*>(type) ||
dynamic_cast<const t_map*>(type)) {
return true;
}
return false;
}
} // namespace apache::thrift::compiler::csharp