Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Patch.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Frameworks/StroikaPreComp.h"
5
6#include "Patch.h"
7
8using namespace std;
9
10using namespace Stroika::Foundation;
14
16using namespace Stroika::Foundation::DataExchange::JSON::Patch;
17
18/*
19 ********************************************************************************
20 ********************** JSON::Patch::OperationItemType **************************
21 ********************************************************************************
22 */
23String JSON::Patch::OperationItemType::ToString () const
24{
26 sb << "{"sv;
27 sb << "op: "sv << op;
28 sb << ", path: "sv << path;
29 if (value) {
30 sb << ", value: "sv << value;
31 }
32 sb << "}"sv;
33 return sb;
34}
35
36VariantValue JSON::Patch::OperationItemType::Apply (const VariantValue& v) const
37{
38 Debug::TraceContextBumper ctx{"JSON::Patch::OperationItemType::Apply", "*this={},v={}"_f, *this, v};
39 using Context = JSON::PointerType::Context;
40 using MapElt = Context::MapElt;
41 using SeqElt = Context::SeqElt;
42 VariantValue result = v;
43 // @todo - very rough draft
44 switch (op) {
45 case OperationType::eAdd: {
46 // WORKS FOR ONE SIMPLE TEST CASE - MUST CAREFULLY RECONSIDER ALGORIUTHM - MAYBE RIGHT - BUT AT LEAST NOT VERY FAR OFF...
47 if (optional<tuple<Context, VariantValue>> oMatch = this->path.ApplyWithContext (v)) {
48 //DbgTrace ("oMa={}"_f, oMatch);
49 Context c = get<Context> (*oMatch);
50 if (c.fStack.empty ()) {
52 }
53 auto stackTop = c.fStack.Pop ();
54 VariantValue vv = get<VariantValue> (*oMatch); // not sure what this means? IGNORE?
55 //DbgTrace ("c={}"_f, c);
56 //DbgTrace ("vv={}"_f, vv);
57 //DbgTrace ("value2add={}"_f, this->value);
58 if (auto mo = get_if<MapElt> (&stackTop)) {
59 mo->fOrigValue.Add (mo->fEltName, this->value);
60 result = c.ConstructNewFrom (VariantValue{mo->fOrigValue});
61 }
62 else if (auto so = get_if<SeqElt> (&stackTop)) {
63 so->fOrigValue.Insert (so->fIndex, this->value);
64 result = c.ConstructNewFrom (VariantValue{so->fOrigValue});
65 }
66 else {
67 AssertNotReached (); // cuz must be one of those two types in variant
68 }
69 //DbgTrace ("result={}"_f, result);
70 }
71 else {
72 Execution::Throw (Execution::RuntimeErrorException{"operator add target not found"sv});
73 }
74 } break;
75 case OperationType::eRemove: {
77 } break;
78 default: {
80 } break;
81 }
82 //DbgTrace ("returning {}"_f, result);
83 return result;
84}
85
86const DataExchange::ObjectVariantMapper JSON::Patch::OperationItemType::kMapper = [] () {
88 mapper += JSON::PointerType::kMapper;
89 mapper.AddCommonType<OperationType> ();
90 mapper.AddCommonType<optional<VariantValue>> ();
91 mapper.AddClass<OperationItemType> ({
92 {"op"sv, &OperationItemType::op},
93 {"path"sv, &OperationItemType::path},
94 {"value"sv, &OperationItemType::value},
95 });
96 return mapper;
97}();
98
99/*
100 ********************************************************************************
101 ********************** JSON::Patch::OperationItemsType *************************
102 ********************************************************************************
103 */
104const DataExchange::ObjectVariantMapper JSON::Patch::OperationItemsType::kMapper = [] () {
105 ObjectVariantMapper mapper;
106 mapper += OperationItemType::kMapper;
107 mapper.AddCommonType<OperationItemsType> ();
108 return mapper;
109}();
110
111VariantValue JSON::Patch::OperationItemsType::Apply (const VariantValue& v) const
112{
113 VariantValue result = v;
114 this->Sequence<OperationItemType>::Apply ([&] (const OperationItemType& op) { result = op.Apply (result); });
115 return result;
116}
#define AssertNotImplemented()
Definition Assertions.h:401
#define AssertNotReached()
Definition Assertions.h:355
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
ObjectVariantMapper can be used to map C++ types to and from variant-union types, which can be transp...
nonvirtual void AddClass(const Traversal::Iterable< StructFieldInfo > &fieldDescriptions, const ClassMapperOptions< CLASS > &mapperOptions={})
nonvirtual void AddCommonType(ARGS &&... args)
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
nonvirtual void Apply(const function< void(ArgByValueType< T > item)> &doToElement, Execution::SequencePolicy seq=Execution::SequencePolicy::eDEFAULT) const
Run the argument function (or lambda) on each element of the container.
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43
STL namespace.