Skip to content

Commit

Permalink
Merge pull request #119 from heinezen/fix/apply-value
Browse files Browse the repository at this point in the history
Skip reassignment of base value in `Object::calculate_value`
  • Loading branch information
TheJJ authored Sep 16, 2024
2 parents e09e965 + cd18bb3 commit 3ab28d8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion nyan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ if(UNIX)
endif()
endif()

if(WIN32)
if(WIN32 AND (NOT WINDOWS_STORE))
set_target_properties(nyan PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
target_link_libraries(nyan PRIVATE DbgHelp)
endif()
Expand Down
26 changes: 19 additions & 7 deletions nyan/nyan_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ int test_parser(const std::string &base_path, const std::string &filename) {

std::shared_ptr<View> root = db->new_view();

Object test = root->get_object("test.Test");
Object second = root->get_object("test.Second");
Object first = root->get_object("test.First");

std::cout << "before change: First.member == "
<< *root->get_object("test.First").get<Int>("member")
<< std::endl;

std::optional<std::shared_ptr<Object>> optional_wat3 = first.get_optional<Object>("wat3", 0);

if (optional_wat3.has_value()) {
if ((*optional_wat3)->get_name() != "test.Second") {
std::optional<std::shared_ptr<Object>> wat3_first = first.get_optional<Object>("wat3", 0);
if (wat3_first.has_value()) {
if ((*wat3_first)->get_name() != "test.Second") {
std::cout << "First.wat3 has wrong value at t=0" << std::endl;
return 1;
}
Expand All @@ -47,6 +47,19 @@ int test_parser(const std::string &base_path, const std::string &filename) {
return 1;
}

std::optional<std::shared_ptr<Object>> wat3_second = second.get_optional<Object, true>("wat3", 0);
if (wat3_second.has_value()) {
std::cout << "Second.wat3 should be None" << std::endl;
return 1;
}

std::optional<std::shared_ptr<Object>> wat3_test = test.get_optional<Object, true>("wat3", 0);
if (wat3_test.has_value()) {
std::cout << "Test.wat3 should be None" << std::endl;
return 1;
}


Object patch = root->get_object("test.FirstPatch");
for (int i = 0; i < 3; i++) {
Transaction tx = root->new_transaction();
Expand All @@ -56,8 +69,8 @@ int test_parser(const std::string &base_path, const std::string &filename) {
}
}

optional_wat3 = first.get_optional<Object>("wat3");
if (optional_wat3.has_value()) {
wat3_first = first.get_optional<Object>("wat3");
if (wat3_first.has_value()) {
std::cout << "First.wat3 should be None by patch" << std::endl;
return 1;
}
Expand Down Expand Up @@ -250,7 +263,6 @@ std::pair<flags_t, params_t> argparse(int argc, char** argv) {


int main(int argc, char **argv) {

auto args = nyan::argparse(argc, argv);
nyan::flags_t flags = args.first;
nyan::params_t params = args.second;
Expand Down
15 changes: 8 additions & 7 deletions nyan/object.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2023 the nyan authors, LGPLv3+. See copying.md for legal info.
// Copyright 2016-2024 the nyan authors, LGPLv3+. See copying.md for legal info.

#include "object.h"

Expand Down Expand Up @@ -170,15 +170,16 @@ ValueHolder Object::calculate_value(const memberid_t &member, order_t t) const {
ValueHolder result = base_value->copy();

// walk back and apply the value changes
while (true) {
const Member *change = parents[defined_by]->get(member);

// skip the parent that assigns the value
// this prevents reassignment errors e.g. from assigning None
int parent_idx = defined_by - 1;
while (parent_idx >= 0) {
const Member *change = parents[parent_idx]->get(member);
if (change != nullptr) {
result->apply(*change);
}
if (defined_by == 0) {
break;
}
defined_by -= 1;
parent_idx -= 1;
}

return result;
Expand Down
1 change: 1 addition & 0 deletions test/test.nyan
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Second(First):
member *= 5.5
First.nice_member = False
# nap : int
wat3 = None

NestingBase(First):

Expand Down

0 comments on commit 3ab28d8

Please sign in to comment.