Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
schema.h
Go to the documentation of this file.
1#pragma once
2
15
16#include <sonde/global.h>
17
18#include <QtCore/QJsonArray>
19#include <QtCore/QJsonObject>
20#include <QtCore/QStringList>
21
22namespace Sonde {
23
33
35inline QString jsonTypeName(JsonType type)
36{
37 switch (type) {
39 return QStringLiteral("string");
41 return QStringLiteral("integer");
43 return QStringLiteral("number");
45 return QStringLiteral("boolean");
46 case JsonType::Array:
47 return QStringLiteral("array");
49 return QStringLiteral("object");
50 }
51 Q_UNREACHABLE_RETURN(QString());
52}
53
54} // namespace Sonde
55
58namespace Sonde::Schema {
59
62inline QJsonObject described(QJsonObject schema, const QString &description)
63{
64 if (!description.isEmpty())
65 schema.insert(QStringLiteral("description"), description);
66 return schema;
67}
68
71inline QJsonObject property(JsonType type, const QString &description)
72{
73 return described({ { QStringLiteral("type"), jsonTypeName(type) } }, description);
74}
75
77inline QJsonObject anyProperty(const QString &description)
78{
79 return described({ }, description);
80}
81
83inline QJsonObject enumProperty(const QStringList &values, const QString &description)
84{
85 return described({ { QStringLiteral("type"), QStringLiteral("string") },
86 { QStringLiteral("enum"), QJsonArray::fromStringList(values) } },
87 description);
88}
89
91inline QJsonObject arrayProperty(const QJsonObject &items, const QString &description)
92{
93 QJsonObject p = described({ { QStringLiteral("type"), QStringLiteral("array") } }, description);
94 if (!items.isEmpty())
95 p.insert(QStringLiteral("items"), items);
96 return p;
97}
98
103inline QJsonObject object(const QJsonObject &properties, const QStringList &required = { })
104{
105 QJsonObject s{ { QStringLiteral("type"), QStringLiteral("object") },
106 { QStringLiteral("properties"), properties } };
107 if (!required.isEmpty())
108 s.insert(QStringLiteral("required"), QJsonArray::fromStringList(required));
109 return s;
110}
111
112} // namespace Sonde::Schema
What every Sonde header includes: the version, and the deprecation macros (design 0031).
Builders for tool argument schemas (JSON Schema subsets that the server's argument check understands:...
Definition schema.h:58
QJsonObject arrayProperty(const QJsonObject &items, const QString &description)
An array property whose elements match items (an empty object allows anything).
Definition schema.h:91
QJsonObject enumProperty(const QStringList &values, const QString &description)
A string property restricted to values.
Definition schema.h:83
QJsonObject anyProperty(const QString &description)
A property of any JSON type.
Definition schema.h:77
QJsonObject property(JsonType type, const QString &description)
A property of a JSON type.
Definition schema.h:71
QJsonObject object(const QJsonObject &properties, const QStringList &required={ })
The schema of a tool's arguments: an object with properties, of which required must be given.
Definition schema.h:103
QJsonObject described(QJsonObject schema, const QString &description)
schema with "description" set to description, unless that is empty: a description that says nothing (...
Definition schema.h:62
Embeddable Model Context Protocol server for Qt applications.
Definition dispatch.cpp:10
JsonType
The JSON Schema types a property may have.
Definition schema.h:25
@ String
"string"
Definition schema.h:26
@ Boolean
"boolean"
Definition schema.h:29
@ Array
"array"
Definition schema.h:30
@ Object
"object"
Definition schema.h:31
@ Integer
"integer"
Definition schema.h:27
@ Number
"number"
Definition schema.h:28
QString jsonTypeName(JsonType type)
The JSON Schema name of type, such as "string".
Definition schema.h:35