Repository navigation
Expand file tree
/
Copy pathdeep_diff.cpp
More file actions
40 lines (36 loc) · 1.46 KB
/
Copy pathdeep_diff.cpp
File metadata and controls
40 lines (36 loc) · 1.46 KB
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
// Copyright 2026 Dmitry
// SPDX-License-Identifier: Apache-2.0
#include <vardiff/vardiff.hpp>
#include <iostream>
struct Item { int id; int count; };
template <> struct vardiff::schema<Item> {
static auto fields() {
return std::make_tuple(vardiff::field("id", &Item::id),
vardiff::field("count", &Item::count));
}
};
struct Inventory {
std::vector<Item> inventory;
std::map<std::string, std::optional<Item>> equipment;
};
template <> struct vardiff::schema<Inventory> {
static auto fields() {
return std::make_tuple(vardiff::field("inventory", &Inventory::inventory),
vardiff::field("equipment", &Inventory::equipment));
}
};
int main() {
const Inventory before{{{1, 10}, {2, 20}, {3, 30}}, {{"hand", Item{4, 1}}}};
const Inventory after{{{2, 20}, {3, 30}, {1, 12}}, {{"hand", Item{4, 2}}}};
const auto changes = vardiff::deep_diff(before, after, vardiff::key(&Item::id));
for (const auto& change : changes) {
std::cout << vardiff::to_string(change.kind) << ": ";
if (change.kind == vardiff::change_kind::moved) {
std::cout << change.from << " -> " << change.name << '\n';
} else {
std::cout << change.name << ": " << change.before << " -> " << change.after << '\n';
}
}
// One move plus two leaf edits; inputs remain unchanged.
return changes.size() == 3 && before.inventory[0].count == 10 ? 0 : 1;
}