forked from egret135/DevKit-Pro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-protobuf.html
More file actions
255 lines (222 loc) · 7.53 KB
/
Copy pathtest-protobuf.html
File metadata and controls
255 lines (222 loc) · 7.53 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Protocol Buffer Generator Test</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 1400px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 20px;
}
.panel {
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
h2 {
margin-top: 0;
color: #333;
}
textarea {
width: 100%;
height: 300px;
font-family: 'Courier New', monospace;
font-size: 13px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
resize: vertical;
}
pre {
background: #23241f;
color: #f8f8f2;
padding: 15px;
border-radius: 4px;
overflow-x: auto;
font-size: 13px;
line-height: 1.5;
}
button {
background: #3B82F6;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
margin-right: 10px;
margin-top: 10px;
}
button:hover {
background: #2563EB;
}
.controls {
margin: 15px 0;
}
label {
display: inline-block;
margin-right: 20px;
font-weight: 500;
}
select {
padding: 5px 10px;
border-radius: 4px;
border: 1px solid #ddd;
}
.error {
color: #EF4444;
margin-top: 10px;
font-weight: 500;
}
.success {
color: #10B981;
margin-top: 10px;
font-weight: 500;
}
</style>
</head>
<body>
<h1>🧪 Protocol Buffer Generator 测试</h1>
<div class="container">
<div class="panel">
<h2>输入 JSON</h2>
<textarea id="jsonInput" placeholder="粘贴 JSON 数据...">{
"id": 1001,
"username": "alice",
"email": "alice@example.com",
"age": 28,
"balance": 99.99,
"is_active": true,
"tags": ["vip", "verified"],
"profile": {
"avatar": "https://example.com/avatar.png",
"bio": "Software Engineer"
}
}</textarea>
<div class="controls">
<label>
嵌套模式:
<select id="nestedMode">
<option value="separate">独立声明</option>
<option value="inline">嵌套message</option>
</select>
</label>
<label>
Message名称:
<input type="text" id="messageName" value="User"
style="padding: 5px; border-radius: 4px; border: 1px solid #ddd;">
</label>
</div>
<button onclick="testConvert()">🔄 转换</button>
<button onclick="loadExample('simple')">📝 简单示例</button>
<button onclick="loadExample('nested')">🗂️ 嵌套示例</button>
<button onclick="loadExample('complex')">🎯 复杂示例</button>
<div id="status"></div>
</div>
<div class="panel">
<h2>输出 Protocol Buffer</h2>
<pre id="output">// 点击"转换"按钮生成 Protocol Buffer message</pre>
</div>
</div>
<!-- Scripts -->
<script src="parsers/json-parser.js"></script>
<script src="parsers/protobuf-parser.js"></script>
<script src="utils/protobuf-type-mapper.js"></script>
<script src="generators/protobuf-generator.js"></script>
<script>
const examples = {
simple: {
"name": "test",
"count": 10,
"active": true
},
nested: {
"id": 1,
"username": "bob",
"profile": {
"avatar": "https://example.com/avatar.png",
"bio": "Developer"
},
"settings": {
"theme": "dark",
"notifications": true
}
},
complex: {
"user_id": 12345,
"username": "charlie",
"email": "charlie@example.com",
"age": 30,
"balance": 1234.56,
"is_premium": true,
"tags": ["admin", "moderator", "vip"],
"profile": {
"avatar_url": "https://cdn.example.com/avatars/charlie.jpg",
"bio": "Full-stack developer with 10 years experience",
"location": "San Francisco",
"social_links": {
"twitter": "@charlie",
"github": "charlie-dev"
}
},
"metadata": {
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-12-30T00:00:00Z",
"version": 2
}
}
};
function loadExample(type) {
const jsonInput = document.getElementById('jsonInput');
jsonInput.value = JSON.stringify(examples[type], null, 2);
document.getElementById('status').innerHTML = `<div class="success">✅ 已加载 ${type} 示例</div>`;
}
function testConvert() {
const jsonInput = document.getElementById('jsonInput');
const output = document.getElementById('output');
const status = document.getElementById('status');
const nestedMode = document.getElementById('nestedMode').value;
const messageName = document.getElementById('messageName').value || 'Message';
try {
const jsonStr = jsonInput.value.trim();
if (!jsonStr) {
throw new Error('请输入 JSON 数据');
}
// Parse JSON for Protocol Buffer
const protoParsedData = parseJSONForProtobuf(jsonStr, messageName);
if (protoParsedData.error) {
throw new Error(protoParsedData.error);
}
// Generate Protocol Buffer message
const protoOptions = {
messageName: messageName,
nestedMode: nestedMode,
packageName: 'model',
syntax: 'proto3',
numericIntType: 'int32',
numericFloatType: 'float'
};
const protoCode = generateProtoMessage(protoParsedData, protoOptions);
output.textContent = protoCode;
status.innerHTML = '<div class="success">✅ 转换成功!</div>';
} catch (error) {
output.textContent = `// 错误: ${error.message}`;
status.innerHTML = `<div class="error">❌ ${error.message}</div>`;
}
}
// Auto-convert on load
window.addEventListener('DOMContentLoaded', testConvert);
</script>
</body>
</html>