mirror of
https://github.com/logos-co/logos-cpp-sdk.git
synced 2026-08-31 17:51:07 +00:00
* feat(optional): ?T is two-state, and the generators finally read it
No generator in any language read the optional flag — it had never been
implemented. `?T` was a HARD REJECT on the cdylib backend ("module not
cdylib-eligible"), `std::optional<T>` in an impl header fell through to the
opaque `any` with no diagnostic, and a `? name: T` field was emitted as a
required `T`. Three real contracts in the workspace already declare optionals
and were silently getting one of those three answers.
`?T` is TWO-state: a value of T, or empty. Never three — "one LIDL type <-> one
type per language" leaves nowhere for a third state, because every target has
exactly one empty inhabitant.
ONE MEANING, TWO SPELLINGS. `? name: T` (the field flag) and `name: ?T` (the
type kind) are the same declaration. Backends no longer answer that themselves:
logos-lidl's fieldIsOptional/fieldValueType are re-exported from lidl_compat.h
and every site THIS COMMIT TOUCHES reads them, so the two spellings emit
byte-identical code on the cdylib and client backends. That
caught a live drift on the way in — lidlRecordCollidesWithBytesTag read `f.type`
and so refused `? _bytes: tstr` while letting `_bytes: ?tstr` straight through,
one declaration with two answers.
THE WIRE RULE DEPENDS ON THE SLOT. Absent and explicit null are the SAME state
on decode and DIFFERENT on encode:
- decode is liberal, by exactly one inhabitant: in an optional slot absent and
null both mean empty; in a required slot both stay errors. A present value
goes through the decoder a required T would get, so a wrong type still fails
at the same path — optional widens the domain, it does not switch checking
off. `?bstr` therefore keeps the LENIENT bytes decode a bare `bstr` gets,
rather than silently becoming stricter in the optional slot.
- encode has one canonical form: empty OMITS the key where the slot is NAMED
(a record field) and is spelled null where it is POSITIONAL (an argument, a
return, an event parameter — no key to omit, and arity must not change). Key
omission lives in the record emitter because a Codec only ever sees a value,
never the slot it sits in. A round trip therefore canonicalises.
- `?any` collapses onto `any`: nlohmann::json already carries null, so
std::optional<LogosMap> would give the slot two spellings of empty.
The dispatch gate now admits a missing trailing optional argument and
materialises it as null, exactly the way a missing record field already was. A
method with no optional parameter emits the byte-identical gate it always did.
Header-first: `std::optional<T>` <-> `?T`, composing with records and
containers. `std::optional<std::optional<T>>` has NO LIDL type (three C++ states
over a two-state wire), so it maps down to `?T` — which makes the author's own
declaration stop compiling against the generated codec, deliberately — and says
so at derivation time instead of leaving a conversion error in generated code.
The Qt/Lp consumer surface is NOT fixed and does not pretend to be. The wrappers
real modules get come from legacy/main.cpp, where the AST is flattened to a
single Qt type-name string per slot before optionality could be seen; Qt has no
optional metatype, so `?T` lands on QVariant — the right shape (an invalid
QVariant is Qt's empty inhabitant) with no type. The generator now prints a Note
naming every flattened slot so an affected build is never silent, and
docs/project.md records exactly what a Qt consumer will still do with an
optional field.
Verified by output equivalence, not by a green build: the generator was built
before and after and run over every .lidl in the workspace plus the impl-header
fixtures, in cdylib, consumer-qt, consumer-lp, client and header-first modes.
428 of 465 artefacts are byte-identical; all 37 that differ belong to one of the
four contracts that declare an optional (the 38th path is the manifest). The
harness's sensitivity is pinned by a negative control: qt vs lp output differs
in 45 files. The emitted codec was additionally compiled under -Wall -Wextra and
run against the rules above — omission, absent==null, required-still-rejects,
present-but-wrong-still-fails, and canonicalising round trip.
Tests: 199 pass, 0 fail (180 before, 19 new).
Requires logos-lidl's optionality accessors and logos-protocol's
Codec<std::optional<T>>.
NOT FIXED, AND IT IS THE PATH THAT MATTERS MOST. The legacy interface-wrapper
path is untouched, and it is the one every real module builds through
(buildPlugin.nix:145 -> logos-cpp-generator --general-only). There the two
spellings still diverge:
? maybe: tstr -> QString maybe{}; __m.value("maybe").toString()
maybe: ?tstr -> QVariant maybe{}; __m.value("maybe")
and --api-style lp diverges too, neither side being std::optional. So R3 holds
on the backends below and NOT on the Qt consumer a shipping module actually
gets. logos-chat-module -- the contract that prompted this work -- uses the
field-flag spelling, so it lands on the branch that silently defaults.
The cause is upstream of codegen: legacy/main.cpp's moduleRecordsToJson and
moduleMethodsToJson flatten every TypeExpr to a single Qt TYPE-NAME STRING, so
optionality (along with nesting, map key types and descriptions) is gone before
generator_lib.cpp sees it. Widening that interface is a larger change and is
deliberately not attempted here. The only R3 test on a Qt surface covers
lidl_gen_client.cpp, which is on no live build path.
* chore: re-pin logos-lidl to master for the optionality accessors
lidl_compat.h re-exports typeIsOptional / optionalValueType / fieldIsOptional /
fieldValueType / paramIsOptional / paramValueType, which landed in
logos-lidl#7. The pinned lidl predated it, so CI failed to compile.
logos-lidl 8c95d4f -> 35f33d8. Tests: 199 pass, 0 fail.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* chore: re-pin logos-protocol to master for Codec<std::optional<T>>
The generated record codecs emit Codec<std::optional<T>> for an optional
field; that specialisation landed in logos-protocol#37 and the pinned
protocol predated it.
Note this repo's own tests would NOT have caught the omission -- the
generator tests string-assert emitted text rather than compiling it, so a
missing codec specialisation only surfaces when a real module compiles
generated optional code (logos-test-modules' ext provider).
logos-protocol 4359557 -> 72754ab. Tests: 199 pass, 0 fail.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* test(doctests): override logos-lidl alongside every logos-cpp-sdk override
The doc-tests build downstream repos (logoscore-cli, capability_module,
accounts_module) with --override-input logos-cpp-sdk. Nix does not carry the
overridden input's OWN lock, so those builds got this branch's cpp-sdk source
while still resolving logos-lidl from their own, older locks. The shipped
share/lidl-frontend/lidl_compat.h then calls accessors that lidl does not
have:
lidl_compat.h:46: error: 'paramValueType' has not been declared in 'lidl'
lidl_compat.h:92: error: 'fieldValueType' was not declared in this scope
Every --override-input logos-cpp-sdk now has a matching
--override-input <same-path>/logos-cpp-sdk/logos-lidl.
This is specific to the override path. A normal consumer running
'nix flake update logos-cpp-sdk' inherits cpp-sdk's own lock, which pins the
lidl carrying these accessors, and is unaffected.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* test(doctests): move logos-lidl at the qt-sdk nodes, not under logos-cpp-sdk
The doc-tests failed to build logos-qt-generator:
share/lidl-frontend/lidl_compat.h:46: error: 'paramValueType' has not been
declared in 'lidl'
MECHANISM. This SDK installs cpp-generator/experimental/lidl_compat.h into
$out/share/lidl-frontend/, and logos-qt-sdk's logos-qt-generator *compiles*
that installed header against qt-sdk's OWN logos-lidl input. Under
logos-qt-sdk, logos-lidl is a SIBLING of logos-cpp-sdk, not a descendant:
logos-qt-sdk
|-- logos-cpp-sdk <- --override-input moves this to the commit under test
`-- logos-lidl <- stays on qt-sdk's lock (8c95d4f), lacks the accessors
logos-logoscore-cli and logos-module-builder both declare
`logos-qt-sdk.inputs.logos-cpp-sdk.follows = "logos-cpp-sdk"` but no lidl
follows, so overriding the SDK hands qt-sdk a new lidl_compat.h next to its
old lidl. The failing derivation is logos-qt-generator — not anything in
logos-cpp-sdk, which is why the previous attempt aimed at the wrong node.
THE FIX is one `<path-to-logos-qt-sdk>/logos-lidl` override per qt-sdk node
that ends up on the SDK under test. A tree-walk over the resolved lock found
four in logoscore-cli's closure and two per module build; with the overrides
applied the walk reports zero remaining.
WHAT WAS REMOVED, and why it was doing nothing:
* The `.../logos-cpp-sdk/logos-lidl` overrides added in bef3ef5 were no-ops.
With only `--override-input logos-cpp-sdk <sha>`, that node's logos-lidl
already resolves to 35f33d87 out of cpp-sdk's own lock — nix >= 2.26
carries an overridden input's lock, and CI runs Determinate Nix. Verified
by resolving the lock with and without them: byte-identical.
* The `logos-module-client/...` overrides never matched anything. Nix says so
out loud ("does not match any input"): logoscore-cli has no such root
input; module-client only appears under logos-test-modules/, outside the
runtime closure. The prose claiming it pins the SDK is corrected too.
cpp-sdk-concurrent-dispatch is fixed here as well — it failed the same way and
carried no lidl overrides at all.
VERIFIED locally against bef3ef5, the exact commit CI failed on:
* accounts .lgx -> exit 0, logos-accounts_module-module-lib.lgx (5,939,898 B)
* logoscore CLI -> exit 0, ./logos/bin/logoscore reports
"logos-cpp-sdk bef3ef57d3f489073672e70a786c550df7edd003"
* negative control (same command minus the single qt-sdk lidl flag) fails
with CI's exact derivation,
/nix/store/pf96n2ldvhy6sq39ygkh5zdqx7dcn4df-logos-qt-generator-0.1.0.drv
* no "does not match any input" warnings remain on any command
The durable fix is a one-line bump of logos-qt-sdk's own flake.lock logos-lidl
to master (logos-lidl#7 is purely additive: six new inline helpers, nothing
removed or renamed). Once qt-sdk carries it, every override added here can go.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
486 lines
18 KiB
C++
486 lines
18 KiB
C++
#include <gtest/gtest.h>
|
|
#include "lidl_gen_client.h"
|
|
|
|
static ModuleDecl makeTestModule()
|
|
{
|
|
ModuleDecl m;
|
|
m.name = "wallet_module";
|
|
m.version = "1.0.0";
|
|
m.description = "Wallet";
|
|
m.category = "finance";
|
|
m.depends.push_back("crypto");
|
|
|
|
{
|
|
MethodDecl md;
|
|
md.name = "createAccount";
|
|
md.returnType = { TypeExpr::Primitive, "tstr", {} };
|
|
ParamDecl p; p.name = "passphrase"; p.type = { TypeExpr::Primitive, "tstr", {} };
|
|
md.params.push_back(p);
|
|
m.methods.push_back(md);
|
|
}
|
|
{
|
|
MethodDecl md;
|
|
md.name = "getBalance";
|
|
md.returnType = { TypeExpr::Primitive, "uint", {} };
|
|
ParamDecl p; p.name = "address"; p.type = { TypeExpr::Primitive, "tstr", {} };
|
|
md.params.push_back(p);
|
|
m.methods.push_back(md);
|
|
}
|
|
{
|
|
MethodDecl md;
|
|
md.name = "listAccounts";
|
|
TypeExpr elem = { TypeExpr::Primitive, "tstr", {} };
|
|
md.returnType = { TypeExpr::Array, "", { elem } };
|
|
m.methods.push_back(md);
|
|
}
|
|
|
|
EventDecl ed;
|
|
ed.name = "onTransfer";
|
|
ParamDecl ep; ep.name = "hash"; ep.type = { TypeExpr::Primitive, "tstr", {} };
|
|
ed.params.push_back(ep);
|
|
m.events.push_back(ed);
|
|
|
|
return m;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Header generation
|
|
// ---------------------------------------------------------------------------
|
|
|
|
TEST(LidlGenClient, HeaderHasClassName)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString h = lidlMakeHeader(m);
|
|
EXPECT_TRUE(h.contains("class WalletModule {"));
|
|
}
|
|
|
|
TEST(LidlGenClient, HeaderHasConstructor)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString h = lidlMakeHeader(m);
|
|
EXPECT_TRUE(h.contains("explicit WalletModule(LogosAPI* api)"));
|
|
}
|
|
|
|
TEST(LidlGenClient, HeaderHasSyncMethods)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString h = lidlMakeHeader(m);
|
|
EXPECT_TRUE(h.contains("QString createAccount("));
|
|
EXPECT_TRUE(h.contains("qulonglong getBalance("));
|
|
EXPECT_TRUE(h.contains("QStringList listAccounts("));
|
|
}
|
|
|
|
TEST(LidlGenClient, HeaderHasAsyncMethods)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString h = lidlMakeHeader(m);
|
|
EXPECT_TRUE(h.contains("void createAccountAsync("));
|
|
EXPECT_TRUE(h.contains("void getBalanceAsync("));
|
|
EXPECT_TRUE(h.contains("void listAccountsAsync("));
|
|
}
|
|
|
|
TEST(LidlGenClient, HeaderHasEventMethods)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString h = lidlMakeHeader(m);
|
|
EXPECT_TRUE(h.contains("bool on(const QString& eventName"));
|
|
}
|
|
|
|
// setEventSource / eventSource / trigger used to be emitted here: an
|
|
// author-facing way to SOURCE events through a CONSUMER wrapper. They had zero
|
|
// callers anywhere in the workspace, including the vendored SDK copies, and the
|
|
// generated code never used them either — m_eventSource was written only by its
|
|
// own setter and read only by trigger, so a trigger() call without a prior
|
|
// setEventSource() just warned and returned.
|
|
//
|
|
// Removing them also removes the reason a Qt wrapper had to keep a
|
|
// LogosAPIClient alongside the lp client: onEventResponse was the only lp-less
|
|
// call left. Pinned so the surface does not quietly reappear.
|
|
TEST(LidlGenClient, NoDeadEventSourceSurface)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString h = lidlMakeHeader(m);
|
|
EXPECT_FALSE(h.contains("setEventSource")) << h.toStdString();
|
|
EXPECT_FALSE(h.contains("m_eventSource")) << h.toStdString();
|
|
EXPECT_FALSE(h.contains("trigger(")) << h.toStdString();
|
|
}
|
|
|
|
TEST(LidlGenClient, HeaderHasIncludes)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString h = lidlMakeHeader(m);
|
|
EXPECT_TRUE(h.contains("#include \"logos_api.h\""));
|
|
EXPECT_TRUE(h.contains("#include \"logos_api_client.h\""));
|
|
EXPECT_TRUE(h.contains("#include \"logos_types.h\""));
|
|
}
|
|
|
|
TEST(LidlGenClient, HeaderHasPrivateMembers)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString h = lidlMakeHeader(m);
|
|
EXPECT_TRUE(h.contains("LogosAPI* m_api"));
|
|
EXPECT_TRUE(h.contains("LogosAPIClient* m_client"));
|
|
EXPECT_TRUE(h.contains("QString m_moduleName"));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Source generation
|
|
// ---------------------------------------------------------------------------
|
|
|
|
TEST(LidlGenClient, SourceHasConstructor)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString s = lidlMakeSource(m);
|
|
EXPECT_TRUE(s.contains("WalletModule::WalletModule(LogosAPI* api)"));
|
|
EXPECT_TRUE(s.contains("getClient(\"wallet_module\")"));
|
|
}
|
|
|
|
TEST(LidlGenClient, SourceHasSyncImplementations)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString s = lidlMakeSource(m);
|
|
EXPECT_TRUE(s.contains("WalletModule::createAccount("));
|
|
EXPECT_TRUE(s.contains("invokeRemoteMethod(\"wallet_module\", \"createAccount\""));
|
|
}
|
|
|
|
TEST(LidlGenClient, SourceHasAsyncImplementations)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString s = lidlMakeSource(m);
|
|
EXPECT_TRUE(s.contains("WalletModule::createAccountAsync("));
|
|
EXPECT_TRUE(s.contains("invokeRemoteMethodAsync(\"wallet_module\", \"createAccount\""));
|
|
}
|
|
|
|
TEST(LidlGenClient, SourceHasEventBoilerplate)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString s = lidlMakeSource(m);
|
|
EXPECT_TRUE(s.contains("WalletModule::on(const QString& eventName"));
|
|
EXPECT_TRUE(s.contains("ensureReplica()"));
|
|
}
|
|
|
|
TEST(LidlGenClient, SourceHasReturnConversion)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString s = lidlMakeSource(m);
|
|
// createAccount returns tstr → QString, should use .toString()
|
|
EXPECT_TRUE(s.contains("_result.toString()"));
|
|
// getBalance returns uint → qulonglong, so the accessor must be the 64-bit
|
|
// unsigned one; toInt() truncated and re-signed it.
|
|
EXPECT_TRUE(s.contains("_result.toULongLong()"));
|
|
// listAccounts returns [tstr] → QStringList, should use .toStringList()
|
|
EXPECT_TRUE(s.contains("_result.toStringList()"));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Metadata JSON generation
|
|
// ---------------------------------------------------------------------------
|
|
|
|
TEST(LidlGenClient, MetadataJson)
|
|
{
|
|
auto m = makeTestModule();
|
|
QString json = lidlGenerateMetadataJson(m);
|
|
EXPECT_TRUE(json.contains("\"name\": \"wallet_module\""));
|
|
EXPECT_TRUE(json.contains("\"version\": \"1.0.0\""));
|
|
EXPECT_TRUE(json.contains("\"category\": \"finance\""));
|
|
EXPECT_TRUE(json.contains("\"crypto\""));
|
|
}
|
|
|
|
TEST(LidlGenClient, MetadataJsonDefaults)
|
|
{
|
|
ModuleDecl m;
|
|
m.name = "bare";
|
|
QString json = lidlGenerateMetadataJson(m);
|
|
EXPECT_TRUE(json.contains("\"version\": \"0.0.0\""));
|
|
EXPECT_TRUE(json.contains("\"category\": \"general\""));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Edge cases
|
|
// ---------------------------------------------------------------------------
|
|
|
|
TEST(LidlGenClient, MethodWithManyParams)
|
|
{
|
|
ModuleDecl m;
|
|
m.name = "multi";
|
|
MethodDecl md;
|
|
md.name = "bigMethod";
|
|
md.returnType = { TypeExpr::Primitive, "tstr", {} };
|
|
for (int i = 0; i < 7; ++i) {
|
|
ParamDecl p;
|
|
p.name = QString("p%1").arg(i).toStdString();
|
|
p.type = { TypeExpr::Primitive, "tstr", {} };
|
|
md.params.push_back(p);
|
|
}
|
|
m.methods.push_back(md);
|
|
|
|
QString s = lidlMakeSource(m);
|
|
// Args are packed via packVariantList (one QVariant element per arg),
|
|
// regardless of arity — never a braced/`<<` list that would spread a
|
|
// QVariantList-typed arg.
|
|
EXPECT_TRUE(s.contains("packVariantList(p0, p1, p2, p3, p4, p5, p6)"));
|
|
}
|
|
|
|
// Regression: a QVariantList-typed ([any]/[int]/...) argument must be packed as
|
|
// ONE element, not concatenated into the args list. `QVariantList{v}` and
|
|
// `QVariantList() << v` both spread a QVariantList; packVariantList wraps each
|
|
// arg with QVariant::fromValue, so a single list arg stays a single arg.
|
|
TEST(LidlGenClient, ListArgIsPackedAsOneElement)
|
|
{
|
|
ModuleDecl m;
|
|
m.name = "arrs";
|
|
MethodDecl md;
|
|
md.name = "echoList";
|
|
TypeExpr elem = { TypeExpr::Primitive, "any", {} };
|
|
md.returnType = { TypeExpr::Array, "", { elem } };
|
|
ParamDecl p;
|
|
p.name = "v";
|
|
p.type = { TypeExpr::Array, "", { elem } };
|
|
md.params.push_back(p);
|
|
m.methods.push_back(md);
|
|
|
|
QString s = lidlMakeSource(m);
|
|
// Both sync and async pack the single list arg via packVariantList(v).
|
|
EXPECT_TRUE(s.contains("invokeRemoteMethod(\"arrs\", \"echoList\", packVariantList(v)"));
|
|
EXPECT_TRUE(s.contains("invokeRemoteMethodAsync(\"arrs\", \"echoList\", packVariantList(v)"));
|
|
// Guard against the spreading forms regressing back in.
|
|
EXPECT_FALSE(s.contains("QVariantList{v}"));
|
|
EXPECT_FALSE(s.contains("QVariantList() << v"));
|
|
}
|
|
|
|
TEST(LidlGenClient, VoidReturnMethod)
|
|
{
|
|
ModuleDecl m;
|
|
m.name = "test";
|
|
MethodDecl md;
|
|
md.name = "doStuff";
|
|
md.returnType = { TypeExpr::Primitive, "void", {} };
|
|
m.methods.push_back(md);
|
|
|
|
QString h = lidlMakeHeader(m);
|
|
// void return should have async callback with void()
|
|
EXPECT_TRUE(h.contains("std::function<void()>"));
|
|
|
|
QString s = lidlMakeSource(m);
|
|
// sync void method should not have "QVariant _result ="
|
|
// The source should just call the method without capturing return
|
|
EXPECT_FALSE(s.contains("QVariant _result = m_client->invokeRemoteMethod(\"test\", \"doStuff\""));
|
|
}
|
|
|
|
// Records: a `type` decl becomes a real C++ struct in the generated header, so a
|
|
// Qt consumer says `Status s = client.makeStatus();` rather than digging fields
|
|
// out of a QVariantMap. Additive — nothing generated records before this.
|
|
static ModuleDecl makeRecordModule()
|
|
{
|
|
ModuleDecl m;
|
|
m.name = "info_module";
|
|
m.version = "1.0.0";
|
|
|
|
TypeDecl rec;
|
|
rec.name = "Status";
|
|
FieldDecl a; a.name = "port"; a.type = { TypeExpr::Primitive, "uint", {} };
|
|
FieldDecl b; b.name = "blob"; b.type = { TypeExpr::Primitive, "bstr", {} };
|
|
rec.fields = {a, b};
|
|
m.types.push_back(rec);
|
|
|
|
{
|
|
MethodDecl md;
|
|
md.name = "describeStatus";
|
|
md.returnType = { TypeExpr::Primitive, "tstr", {} };
|
|
ParamDecl p; p.name = "s"; p.type = { TypeExpr::Named, "Status", {} };
|
|
md.params.push_back(p);
|
|
m.methods.push_back(md);
|
|
}
|
|
{
|
|
MethodDecl md;
|
|
md.name = "makeStatuses";
|
|
TypeExpr elem = { TypeExpr::Named, "Status", {} };
|
|
md.returnType = { TypeExpr::Array, "", { elem } };
|
|
m.methods.push_back(md);
|
|
}
|
|
return m;
|
|
}
|
|
|
|
// `isTaggedBytes()` is checked BEFORE `is_object()` in both the codec and the
|
|
// QVariant bridge, so a record whose only field is a tstr named `_bytes` is
|
|
// wire-identical to a tagged byte string and decodes as bytes — the struct
|
|
// silently disappears. The ambiguity is inherent to the tagged form; refusing
|
|
// to emit the one shape guaranteed to misdecode is what a generator can do
|
|
// about it.
|
|
TEST(LidlGenClient, RecordThatCollidesWithTheBytesTagIsRefused)
|
|
{
|
|
ModuleDecl m;
|
|
m.name = "c_module";
|
|
TypeDecl bad;
|
|
bad.name = "Sneaky";
|
|
FieldDecl f; f.name = "_bytes"; f.type = { TypeExpr::Primitive, "tstr", {} };
|
|
bad.fields = {f};
|
|
m.types.push_back(bad);
|
|
|
|
QString err;
|
|
EXPECT_FALSE(lidlCheckRecords(m, &err));
|
|
EXPECT_TRUE(err.contains("Sneaky")) << err.toStdString();
|
|
EXPECT_TRUE(err.contains("_bytes")) << err.toStdString();
|
|
|
|
// A SECOND field disambiguates it — isTaggedBytes requires exactly one key,
|
|
// so this shape round-trips and must still be allowed.
|
|
FieldDecl g; g.name = "other"; g.type = { TypeExpr::Primitive, "int", {} };
|
|
m.types[0].fields.push_back(g);
|
|
EXPECT_TRUE(lidlCheckRecords(m, nullptr));
|
|
|
|
// A `_bytes` field that is not the only one, and a differently-named sole
|
|
// field, are both fine.
|
|
ModuleDecl ok;
|
|
ok.name = "ok_module";
|
|
TypeDecl t; t.name = "Fine";
|
|
FieldDecl h; h.name = "payload"; h.type = { TypeExpr::Primitive, "tstr", {} };
|
|
t.fields = {h};
|
|
ok.types.push_back(t);
|
|
EXPECT_TRUE(lidlCheckRecords(ok, nullptr));
|
|
}
|
|
|
|
// The ASYNC overload must decode a record the same way the sync one does.
|
|
//
|
|
// `qvariant_cast<Status>(v)` does not fail on the wire's QVariantMap: no
|
|
// Q_DECLARE_METATYPE is emitted for the struct, so the cast silently yields a
|
|
// DEFAULT-CONSTRUCTED Status and the caller sees empty fields with no
|
|
// diagnostic. The sync path was already correct, which makes it worse — the
|
|
// same call would be right or wrong depending only on which overload the
|
|
// caller reached for.
|
|
TEST(LidlGenClient, AsyncRecordReturnsDecodeFieldByField)
|
|
{
|
|
const QString c = lidlMakeSource(makeRecordModule(), BindMode::Bound);
|
|
|
|
// A [Record] return, in the async callback.
|
|
EXPECT_TRUE(c.contains("StatusFromVariant")) << c.toStdString();
|
|
EXPECT_FALSE(c.contains("qvariant_cast<QList<Status>>")) << c.toStdString();
|
|
EXPECT_FALSE(c.contains("qvariant_cast<Status>")) << c.toStdString();
|
|
}
|
|
|
|
TEST(LidlGenClient, RecordsBecomeStructsWithConversions)
|
|
{
|
|
const QString h = lidlMakeHeader(makeRecordModule(), BindMode::Bound);
|
|
|
|
// The struct, at the 1-1 Qt spellings: 64-bit unsigned, QByteArray for bytes.
|
|
EXPECT_TRUE(h.contains("struct Status {")) << h.toStdString();
|
|
EXPECT_TRUE(h.contains("qulonglong port{};")) << h.toStdString();
|
|
EXPECT_TRUE(h.contains("QByteArray blob{};")) << h.toStdString();
|
|
|
|
// Conversions both ways.
|
|
EXPECT_TRUE(h.contains("inline QVariant StatusToVariant(const Status& v)")) << h.toStdString();
|
|
EXPECT_TRUE(h.contains("inline Status StatusFromVariant(const QVariant& value)")) << h.toStdString();
|
|
// A bstr field is a QByteArray: logos-protocol's QVariant<->JSON conversion
|
|
// already materialises the tagged {"_bytes":…} form as QByteArray, so binary
|
|
// survives without record-specific bytes handling.
|
|
EXPECT_TRUE(h.contains("__out.blob = __m.value(\"blob\").toByteArray();")) << h.toStdString();
|
|
|
|
// Methods speak the record: by const& in, typed list out. A QVariantList
|
|
// could not hold a Status without Q_DECLARE_METATYPE.
|
|
EXPECT_TRUE(h.contains("describeStatus(const Status& s")) << h.toStdString();
|
|
EXPECT_TRUE(h.contains("QList<Status> makeStatuses(")) << h.toStdString();
|
|
}
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Optionality
|
|
//
|
|
// This backend is not on any live build path (real Qt consumers come from the
|
|
// legacy interface-wrapper path), so the bar here is CONSISTENCY, not features:
|
|
// the two spellings of an optional field must not generate different structs
|
|
// from the same declaration.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
static ModuleDecl makeOptionalRecordModule(bool useFlagSpelling)
|
|
{
|
|
ModuleDecl m;
|
|
m.name = "opt_module";
|
|
m.version = "1.0.0";
|
|
|
|
TypeDecl t;
|
|
t.name = "Profile";
|
|
{
|
|
FieldDecl f; f.name = "required"; f.type = { TypeExpr::Primitive, "tstr", {} };
|
|
t.fields.push_back(f);
|
|
}
|
|
{
|
|
FieldDecl f;
|
|
f.name = "nickname";
|
|
if (useFlagSpelling) { // `? nickname: tstr`
|
|
f.type = { TypeExpr::Primitive, "tstr", {} };
|
|
f.optional = true;
|
|
} else { // `nickname: ?tstr`
|
|
f.type = { TypeExpr::Optional, "", { { TypeExpr::Primitive, "tstr", {} } } };
|
|
}
|
|
t.fields.push_back(f);
|
|
}
|
|
m.types.push_back(t);
|
|
|
|
MethodDecl md;
|
|
md.name = "echoProfile";
|
|
md.returnType = { TypeExpr::Named, "Profile", {} };
|
|
ParamDecl p; p.name = "v"; p.type = { TypeExpr::Named, "Profile", {} };
|
|
md.params.push_back(p);
|
|
m.methods.push_back(md);
|
|
return m;
|
|
}
|
|
|
|
TEST(LidlGenClient, BothOptionalSpellingsEmitIdenticalCode)
|
|
{
|
|
const QString flagged = lidlMakeHeader(makeOptionalRecordModule(true), BindMode::Bound);
|
|
const QString typed = lidlMakeHeader(makeOptionalRecordModule(false), BindMode::Bound);
|
|
// Reading `f.type` alone made the flag spelling emit a bare `QString` — a
|
|
// type with no empty inhabitant at all — from the same declaration that the
|
|
// type spelling turned into a QVariant.
|
|
EXPECT_EQ(flagged, typed) << flagged.toStdString() << "\n---\n" << typed.toStdString();
|
|
}
|
|
|
|
TEST(LidlGenClient, OptionalRecordFieldIsTwoStateQVariant)
|
|
{
|
|
const QString h = lidlMakeHeader(makeOptionalRecordModule(true), BindMode::Bound);
|
|
|
|
// QVariant, because Qt has no optional and an invalid QVariant is its one
|
|
// empty inhabitant.
|
|
EXPECT_TRUE(h.contains("QVariant nickname{};")) << h.toStdString();
|
|
EXPECT_TRUE(h.contains("QString required{};")) << h.toStdString();
|
|
// A record field is a NAMED slot: empty omits the key rather than writing an
|
|
// invalid QVariant into the map.
|
|
EXPECT_TRUE(h.contains("if (v.nickname.isValid())")) << h.toStdString();
|
|
// Absent and null both arrive as an invalid QVariant. Converting (the
|
|
// `.toString()` a required tstr field gets) would have turned "empty" into
|
|
// "", which is a VALUE.
|
|
EXPECT_TRUE(h.contains("__out.nickname = __m.value(\"nickname\");")) << h.toStdString();
|
|
EXPECT_FALSE(h.contains("__out.nickname = __m.value(\"nickname\").toString();"))
|
|
<< h.toStdString();
|
|
}
|
|
|
|
// The `_bytes` collision check must read through an optional too. A PRESENT
|
|
// `? _bytes: tstr` still encodes to {"_bytes": "..."} — the shape that decodes
|
|
// as a byte string and loses the record — so both spellings have to be refused.
|
|
// Reading `f.type` refused only the flag one.
|
|
TEST(LidlGenClient, BytesTagCollisionIsRefusedThroughAnOptional)
|
|
{
|
|
auto sneaky = [](bool useFlagSpelling) {
|
|
ModuleDecl m;
|
|
m.name = "sneaky_module";
|
|
TypeDecl t;
|
|
t.name = "Sneaky";
|
|
FieldDecl f;
|
|
f.name = "_bytes";
|
|
if (useFlagSpelling) {
|
|
f.type = { TypeExpr::Primitive, "tstr", {} };
|
|
f.optional = true;
|
|
} else {
|
|
f.type = { TypeExpr::Optional, "", { { TypeExpr::Primitive, "tstr", {} } } };
|
|
}
|
|
t.fields.push_back(f);
|
|
m.types.push_back(t);
|
|
return m;
|
|
};
|
|
|
|
for (bool flag : {true, false}) {
|
|
QString error;
|
|
EXPECT_FALSE(lidlCheckRecords(sneaky(flag), &error)) << "flagSpelling=" << flag;
|
|
EXPECT_TRUE(error.contains("Sneaky")) << error.toStdString();
|
|
}
|
|
}
|