summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-json-rpc.cpp
blob: 1ca55e9eb8ecb65c991811251cb5fff3c6d4aee7 (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include "slang-json-rpc.h"

#include "../../slang-com-helper.h"

namespace Slang {

// https://www.jsonrpc.org/specification

//    m_sourceManager.initialize(nullptr, nullptr);
//    m_diagnosticSink.init(&m_sourceManager, &JSONLexer::calcLexemeLocation);

static const auto g_jsonRpc = UnownedStringSlice::fromLiteral("jsonrpc");
static const auto g_jsonRpcVersion = UnownedStringSlice::fromLiteral("2.0");
static const auto g_method = UnownedStringSlice::fromLiteral("method");
static const auto g_id = UnownedStringSlice::fromLiteral("id");
static const auto g_params = UnownedStringSlice::fromLiteral("params");
static const auto g_code = UnownedStringSlice::fromLiteral("code");
static const auto g_error = UnownedStringSlice::fromLiteral("error");
static const auto g_message = UnownedStringSlice::fromLiteral("message");
static const auto g_result = UnownedStringSlice::fromLiteral("result");
static const auto g_data = UnownedStringSlice::fromLiteral("data");

// Add the fields.
// TODO(JS): This is a little verbose, and could be improved on with something like
// * Tool that automatically generated from C++ (say via the C++ extractor)
// * Macro magic to simplify the construction
static const StructRttiInfo _makeJSONRPCErrorResponse_ErrorRtti()
{
    JSONRPCErrorResponse::Error obj;
    StructRttiBuilder builder(&obj, "JSONRPCErrorResponse::Error", nullptr);
    builder.addField("code", &obj.code);
    builder.addField("message", &obj.message);
    return builder.make();
}
/* static */const StructRttiInfo JSONRPCErrorResponse::Error::g_rttiInfo = _makeJSONRPCErrorResponse_ErrorRtti();

static const StructRttiInfo _makeJSONRPCErrorResponseRtti()
{
    JSONRPCErrorResponse obj;
    StructRttiBuilder builder(&obj, "JSONRPCErrorResponse", nullptr);

    builder.addField("error", &obj.error);
    builder.addField("data", &obj.data, StructRttiInfo::Flag::Optional);
    builder.addField("id", &obj.id, combine(StructRttiInfo::Flag::Optional, RttiDefaultValue::MinusOne));

    return builder.make();
}
/* static */const StructRttiInfo JSONRPCErrorResponse::g_rttiInfo = _makeJSONRPCErrorResponseRtti();

static const StructRttiInfo _makeJSONRPCCallResponseRtti()
{
    JSONRPCCall obj;
    StructRttiBuilder builder(&obj, "JSONRPCCall", nullptr);

    builder.addField("method", &obj.method);
    builder.addField("params", &obj.params, StructRttiInfo::Flag::Optional);
    builder.addField("id", &obj.id, combine(StructRttiInfo::Flag::Optional, RttiDefaultValue::MinusOne));

    return builder.make();
}
/* static */const StructRttiInfo JSONRPCCall::g_rttiInfo = _makeJSONRPCCallResponseRtti();

static const StructRttiInfo _makeJSONResultResponseResponseRtti()
{
    JSONResultResponse obj;
    StructRttiBuilder builder(&obj, "JSONResultResponse", nullptr);

    builder.addField("result", &obj.result);
    builder.addField("id", &obj.id, combine(StructRttiInfo::Flag::Optional, RttiDefaultValue::MinusOne));

    return builder.make();
}
/* static */const StructRttiInfo JSONResultResponse::g_rttiInfo = _makeJSONResultResponseResponseRtti();


/* static */JSONValue JSONRPCUtil::createCall(JSONContainer* container, const UnownedStringSlice& method, JSONValue params, Int id)
{
    const Index maxPairs = 4;
    JSONKeyValue pairs[maxPairs];

    Index i = 0;

    // Version number is a string
    pairs[i++] = JSONKeyValue::make(container->getKey(g_jsonRpc), container->createString(g_jsonRpcVersion));
    pairs[i++] = JSONKeyValue::make(container->getKey(g_method), container->createString(method));
    pairs[i++] = JSONKeyValue::make(container->getKey(g_params), params);

    if (id >= 0)
    {
        pairs[i++] = JSONKeyValue::make(container->getKey(g_id), JSONValue::makeInt(id));
    }

    return container->createObject(pairs, i);
}

/* static */JSONValue JSONRPCUtil::createCall(JSONContainer* container, const UnownedStringSlice& method, Int id)
{
    const Index maxPairs = 3;
    JSONKeyValue pairs[maxPairs];
    Index i = 0;
    // Version number is a string
    pairs[i++] = JSONKeyValue::make(container->getKey(g_jsonRpc), container->createString(g_jsonRpcVersion));
    pairs[i++] = JSONKeyValue::make(container->getKey(g_method), container->createString(method));

    if (id >= 0)
    {
        pairs[i++] = JSONKeyValue::make(container->getKey(g_id), JSONValue::makeInt(id));
    }

    return container->createObject(pairs, i);
}

/* static */JSONValue JSONRPCUtil::createErrorResponse(JSONContainer* container, Index code, const UnownedStringSlice& message, const JSONValue& data, Int id)
{
    // Set up the error value
    JSONValue errorValue;
    {
        const Index maxPairs = 2;
        JSONKeyValue pairs[maxPairs];
        Index i = 0;

        if (code != 0)
        {
            pairs[i++] = JSONKeyValue::make(container->getKey(g_code), JSONValue::makeInt(code));
        }
        if (message.getLength() > 0)
        {
            pairs[i++] = JSONKeyValue::make(container->getKey(g_message), container->createString(message));
        }
        errorValue = container->createObject(pairs, i);
    }

    const Index maxPairs = 4;
    JSONKeyValue pairs[maxPairs];
    Index i = 0;

    pairs[i++] = JSONKeyValue::make(container->getKey(g_jsonRpc), container->createString(g_jsonRpcVersion));
    pairs[i++] = JSONKeyValue::make(container->getKey(g_error), errorValue);

    if (data.isValid())
    {
        pairs[i++] = JSONKeyValue::make(container->getKey(g_data), data);
    }

    if (id >= 0)
    {
        pairs[i++] = JSONKeyValue::make(container->getKey(g_id), JSONValue::makeInt(id));
    }

    return container->createObject(pairs, i);
}


/* static */JSONValue JSONRPCUtil::createErrorResponse(JSONContainer* container, ErrorCode code, const UnownedStringSlice& message, const JSONValue& data, Int id)
{
    return createErrorResponse(container, Index(code), message, data, id);
}

/* static */JSONValue JSONRPCUtil::createResultResponse(JSONContainer* container, const JSONValue& resultValue, Int id)
{
    const Index maxPairs = 3;
    JSONKeyValue pairs[maxPairs];
    Index i = 0;

    pairs[i++] = JSONKeyValue::make(container->getKey(g_jsonRpc), container->createString(g_jsonRpcVersion));
    pairs[i++] = JSONKeyValue::make(container->getKey(g_result), resultValue);

    if (id >= 0)
    {
        pairs[i++] = JSONKeyValue::make(container->getKey(g_id), JSONValue::makeInt(id));
    }

    return container->createObject(pairs, i);
}

/* static */ JSONRPCUtil::ResponseType JSONRPCUtil::getResponseType(JSONContainer* container, const JSONValue& response)
{
    if (response.getKind() == JSONValue::Kind::Object)
    {
        const JSONKey resultKey = container->findKey(g_result);
        const JSONKey errorKey = container->findKey(g_error);

        auto pairs = container->getObject(response);

        for (const auto& pair : pairs)
        {
            if (pair.key == resultKey)
            {
                return ResponseType::Result;
            }
            else if (pair.key == errorKey)
            {
                return ResponseType::Error;
            }
        }
    }

    return ResponseType::Error;
}

static SlangResult _parseError(JSONContainer* container, const JSONValue& error, JSONRPCUtil::ErrorResponse& out)
{
    if (error.getKind() != JSONValue::Kind::Object)
    {
        return SLANG_FAIL;
    }
    const auto pairs = container->getObject(error);

    const JSONKey messageKey = container->findKey(g_message);
    const JSONKey codeKey = container->findKey(g_code);
    const JSONKey dataKey = container->findKey(g_data);

    Int fieldBits = 0;

    for (auto const& pair : pairs)
    {
        if (pair.key == messageKey)
        {
            if (pair.value.getKind() != JSONValue::Kind::String)
            {
                return SLANG_FAIL;
            }
            out.message = container->getString(pair.value);
            fieldBits |= 0x1;
        }
        else if (pair.key == codeKey)
        {
            if (pair.value.getKind() != JSONValue::Kind::Integer)
            {
                return SLANG_FAIL;
            }
            out.code = Index(pair.value.asInteger());
            fieldBits |= 0x2;
        }
        else if (pair.key == dataKey)
        {
            out.data = pair.value;
            fieldBits |= 0x4;
        }
        else
        {
            return SLANG_FAIL;
        }
    }

    // Check all required fields are set
    return (fieldBits & 0x3) == 0x3 ? SLANG_OK : SLANG_FAIL;
}

/* static */SlangResult JSONRPCUtil::parseError(JSONContainer* container, const JSONValue& response, ErrorResponse& out)
{
    if (response.getKind() != JSONValue::Kind::Object)
    {
        return SLANG_FAIL;
    }

    const auto pairs = container->getObject(response);

    const JSONKey jsonRpcKey = container->findKey(g_jsonRpc);
    const JSONKey errorKey = container->findKey(g_error);
    const JSONKey idKey = container->findKey(g_id);

    Int fieldBits = 0;

    for (auto const& pair : pairs)
    {
        if (pair.key == jsonRpcKey)
        {
            if (!container->areEqual(pair.value, g_jsonRpcVersion))
            {
                return SLANG_FAIL;
            }
            fieldBits |= 0x1;
        }
        else if (pair.key == errorKey)
        {
            // We need to decode the error 
            SLANG_RETURN_ON_FAIL(_parseError(container, pair.value, out));
            fieldBits |= 0x2;
        }
        else if (pair.key == idKey)
        {
            if (pair.value.getKind() != JSONValue::Kind::Integer)
            {
                return SLANG_FAIL;
            }
            out.id = Int(pair.value.asInteger());
            fieldBits |= 0x4;
        }
        else
        {
            // Unknown key
            return SLANG_FAIL;
        }
    }

    // Check all the required bits are set
    return ((fieldBits & 0x3) == 0x3) ? SLANG_OK : SLANG_FAIL;
}

/* static */SlangResult JSONRPCUtil::parseResult(JSONContainer* container, const JSONValue& response, ResultResponse& out)
{
    if (response.getKind() != JSONValue::Kind::Object)
    {
        return SLANG_FAIL;
    }

    const auto pairs = container->getObject(response);

    const JSONKey jsonRpcKey = container->findKey(g_jsonRpc);
    const JSONKey resultKey = container->findKey(g_result);
    const JSONKey idKey = container->findKey(g_id);

    Int fieldBits = 0;

    for (auto const& pair : pairs)
    {
        if (pair.key == jsonRpcKey)
        {
            if (!container->areEqual(pair.value, g_jsonRpcVersion))
            {
                return SLANG_FAIL;
            }
            fieldBits |= 0x1;
        }
        else if (pair.key == resultKey)
        {
            out.result = pair.value;
            fieldBits |= 0x2;
        }
        else if (pair.key == idKey)
        {
            if (pair.value.getKind() != JSONValue::Kind::Integer)
            {
                return SLANG_FAIL;
            }
            out.id = Int(pair.value.asInteger());
            fieldBits |= 0x4;
        }
        else
        {
            // Unknown key
            return SLANG_FAIL;
        }
    }

    // Check all the required bits are set
    return ((fieldBits & 0x3) == 0x3) ? SLANG_OK : SLANG_FAIL;
}

/* static */SlangResult JSONRPCUtil::parseCall(JSONContainer* container, const JSONValue& value, Call& out)
{
    if (value.getKind() != JSONValue::Kind::Object)
    {
        return SLANG_FAIL;
    }

    const auto pairs = container->getObject(value);

    const JSONKey jsonRpcKey = container->findKey(g_jsonRpc);
    const JSONKey methodKey = container->findKey(g_method);
    const JSONKey paramsKey = container->findKey(g_params);
    const JSONKey idKey = container->findKey(g_id);

    Int fieldBits = 0;

    for (auto const& pair : pairs)
    {
        if (pair.key == jsonRpcKey)
        {
            if (!container->areEqual(pair.value, g_jsonRpcVersion))
            {
                return SLANG_FAIL;
            }
            fieldBits |= 0x1;
        }
        else if (pair.key == methodKey)
        {
            if (pair.value.getKind() != JSONValue::Kind::String)
            {
                return SLANG_FAIL;
            }
            out.method = container->getString(pair.value);
            fieldBits |= 0x2;
        }
        else if (pair.key == idKey)
        {
            if (pair.value.getKind() != JSONValue::Kind::Integer)
            {
                return SLANG_FAIL;
            }
            out.id = Int(pair.value.asInteger());
            fieldBits |= 0x4;
        }
        else if (pair.key == paramsKey)
        {
            out.params = pair.value;
            fieldBits |= 0x8;
        }
        else
        {
            // Unknown key
            return SLANG_FAIL;
        }
    }

    // Check all the required bits are set
    return ((fieldBits & 0x3) == 0x3) ? SLANG_OK : SLANG_FAIL;
}


/* static */SlangResult JSONRPCUtil::parseJSON(const UnownedStringSlice& slice, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue)
{
    SourceManager* sourceManager = sink->getSourceManager();

    // Now need to parse as JSON
    String contents(slice);
    SourceFile* sourceFile = sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), contents);
    SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, SourceLoc());

    JSONLexer lexer;
    lexer.init(sourceView, sink);

    JSONBuilder builder(container);

    JSONParser parser;
    SLANG_RETURN_ON_FAIL(parser.parse(&lexer, sourceView, &builder, sink));

    outValue = builder.getRootValue();
    return SLANG_OK;
}

SlangResult JSONRPCUtil::parseJSONAndConsume(HTTPPacketConnection* connection, JSONContainer* container, DiagnosticSink* sink, JSONValue& outValue)
{
    if (!connection->hasContent())
    {
        return SLANG_FAIL;
    }

    auto content = connection->getContent();

    UnownedStringSlice text((const char*)content.begin(), content.getCount());
    SlangResult res = parseJSON(text, container, sink, outValue);

    // Consume the content
    connection->consumeContent();
    return res;
}

} // namespace Slang