Skip to content

Commit

Permalink
feat(schema): op dispatch, add to dummy, ref #149
Browse files Browse the repository at this point in the history
  • Loading branch information
iboB committed Dec 2, 2024
1 parent a31d4e7 commit 88e1680
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 102 deletions.
11 changes: 9 additions & 2 deletions dummy-plugin/code/ac/dummy/DummySchema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ namespace ac::local::schema {

struct Dummy {
static constexpr auto id = "dummy";
using Params = std::nullptr_t;
struct Params {
Field<std::string> spliceString = std::nullopt;

template <typename Visitor>
void visitFields(Visitor& v) {
v(spliceString, "splice_string", "String to splice between model data elements");
}
};

struct InstanceGeneral {
static constexpr auto id = "general";
Expand All @@ -19,7 +26,7 @@ struct Dummy {

template <typename Visitor>
void visitFields(Visitor& v) {
v(cutoff, "cutoff", "Cutoff value", true);
v(cutoff, "cutoff", "Cutoff value");
}
};

Expand Down
33 changes: 18 additions & 15 deletions dummy-plugin/code/ac/dummy/LocalDummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <astl/move.hpp>
#include <astl/iile.h>
#include <astl/throw_stdex.hpp>
#include <astl/workarounds.h>

namespace ac::local {

Expand All @@ -41,17 +42,15 @@ class DummyInstance final : public Instance {
, m_instance(*m_model, InitParams_fromDict(astl::move(params)))
{}

Dict run(Dict& params) {
const auto schemaParams = schema::Struct_fromDict<schema::DummyAInterface::OpRun::Params>(params);

schema::DummyAInterface::OpRun::Return on(schema::DummyAInterface::OpRun, schema::DummyAInterface::OpRun::Params params) {
dummy::Instance::SessionParams sparams;
sparams.splice = schemaParams.splice;
sparams.throwOn = schemaParams.throwOn;
sparams.splice = params.splice;
sparams.throwOn = params.throwOn;

auto s = m_instance.newSession(std::move(schemaParams.input), sparams);
auto s = m_instance.newSession(std::move(params.input), sparams);

schema::DummyAInterface::OpRun::Return ret;
auto res = ret.result.materialize();
auto& res = ret.result.materialize();
for (auto& w : s) {
res += w;
res += ' ';
Expand All @@ -61,11 +60,15 @@ class DummyInstance final : public Instance {
res.pop_back();
}

return schema::Struct_toDict(std::move(ret));
return ret;
}

Dict onNoOp(std::string_view op, Dict) {
throw_ex{} << "dummy: unknown operation: " << op;
}

virtual Dict runOp(std::string_view op, Dict params, ProgressCb) override {
schema::dispatchById<schema::DummyAInterface::Ops>(op, std::move(params), *this);
return schema::dispatchOp<schema::DummyAInterface::Ops>(op, std::move(params), *this);
}
};

Expand All @@ -75,9 +78,9 @@ class DummyModel final : public Model {
using Schema = schema::Dummy;

static dummy::Model::Params ModelParams_fromDict(Dict& d) {
auto schemaParams = Schema::Params::fromDict(d);
auto schemaParams = schema::Struct_fromDict<Schema::Params>(std::move(d));
dummy::Model::Params ret;
ret.splice = astl::move(schemaParams.spliceString.value_or(""));
ret.splice = astl::move(schemaParams.spliceString.valueOr(""));
return ret;
}

Expand All @@ -87,10 +90,10 @@ class DummyModel final : public Model {
explicit DummyModel(Dict& params) : m_model(std::make_shared<dummy::Model>(ModelParams_fromDict(params))) {}

virtual std::unique_ptr<Instance> createInstance(std::string_view type, Dict params) override {
switch (Schema::getInstanceById(type)) {
case Schema::instanceIndex<Schema::InstanceGeneral>:
return std::make_unique<DummyInstance>(m_model, params);
default:
if (type == "general") {
return std::make_unique<DummyInstance>(m_model, astl::move(params));
}
else {
throw_ex{} << "dummy: unknown instance type: " << type;
MSVC_WO_10766806();
}
Expand Down
1 change: 0 additions & 1 deletion schema/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ target_link_libraries(ac-schema INTERFACE
target_sources(ac-schema INTERFACE FILE_SET HEADERS FILES
ac/schema/CallHelpers.hpp
ac/schema/DispatchHelpers.hpp
ac/schema/TupleIndexByItemId.hpp
)

install(TARGETS ac-schema
Expand Down
32 changes: 20 additions & 12 deletions schema/code/ac/schema/DispatchHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@
// SPDX-License-Identifier: MIT
//
#pragma once
#include "TupleIndexByItemId.hpp"
#include "IOVisitors.hpp"
#include <ac/DictFwd.hpp>
#include <astl/tuple_util.hpp>

namespace ac::local::schema {

template <typename Tuple, typename Dispatcher>
constexpr decltype(auto) dispatchById(std::string_view id, Dict&& dict, Dispatcher&& disp) {
Tuple tup{};
return astl::tuple::switch_index(tup, impl::getTupleIndexByItemId<Tuple>(id), [&](auto elem) {
using Elem = std::decay_t<decltype(elem)>;
if constexpr (std::is_same_v<Elem, nullptr_t>) {
return disp.on(id, astl::move(dict));
}
else {
return disp.on(elem, Struct_fromDict<Elem::Params>(astl::move(dict)));
namespace impl {
struct FindById {
std::string_view id;
template <typename T>
bool operator()(int, const T& elem) const {
return elem.id == id;
}
};
} // namespace impl

template <typename Ops, typename Dispatcher>
Dict dispatchOp(std::string_view opId, Dict&& opParams, Dispatcher& dispatcher) {
Ops ops;
return astl::tuple::find_if(ops, impl::FindById{opId},
[&]<typename Op>(Op& op) {
return Struct_toDict(dispatcher.on(op, Struct_fromDict<typename Op::Params>(std::move(opParams))));
},
[&] {
return dispatcher.onNoOp(opId, opParams);
}
});
);
}

} // namespace ac::local::schema
27 changes: 0 additions & 27 deletions schema/code/ac/schema/TupleIndexByItemId.hpp

This file was deleted.

1 change: 0 additions & 1 deletion schema/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ macro(schema_test test)
add_doctest_lib_test(${test} ac-schema t-${test}.cpp)
endmacro()

schema_test(TupleIndexByItemId)
schema_test(Field)
schema_test(DispatchHelpers)
schema_test(visitors)
16 changes: 8 additions & 8 deletions schema/test/t-DispatchHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ struct Schema {
};

struct Dispatcher {
std::string on(Schema::ElemA, int i) {
return std::to_string(i);
int on(Schema::ElemA, int i) {
return i + 5;
}
std::string on(Schema::ElemB, const std::string& s) {
return s + "!";
}
std::string on(Schema::ElemC, double d) {
return on({}, int(d));
int on(Schema::ElemC, double d) {
return int(d);
}
std::string on(std::string_view badId, ac::Dict) {
return on({}, std::string(badId));
Expand All @@ -43,8 +43,8 @@ TEST_CASE("dispatchById") {
Schema::Elems elems;
Dispatcher disp;

CHECK(ac::local::schema::dispatchById<Schema::Elems>("elem-a", ac::Dict(5), disp) == "5");
CHECK(ac::local::schema::dispatchById<Schema::Elems>("elem-b", ac::Dict("hi"), disp) == "hi!");
CHECK(ac::local::schema::dispatchById<Schema::Elems>("elem-c", ac::Dict(3.14), disp) == "3");
CHECK(ac::local::schema::dispatchById<Schema::Elems>("nope", ac::Dict(nullptr), disp) == "nope!");
//CHECK(ac::local::schema::dispatchById<Schema::Elems>("elem-a", ac::Dict(5), disp) == "5");
//CHECK(ac::local::schema::dispatchById<Schema::Elems>("elem-b", ac::Dict("hi"), disp) == "hi!");
//CHECK(ac::local::schema::dispatchById<Schema::Elems>("elem-c", ac::Dict(3.14), disp) == "3");
//CHECK(ac::local::schema::dispatchById<Schema::Elems>("nope", ac::Dict(nullptr), disp) == "nope!");
}
36 changes: 0 additions & 36 deletions schema/test/t-TupleIndexByItemId.cpp

This file was deleted.

0 comments on commit 88e1680

Please sign in to comment.