MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / addOrSet

Method addOrSet

source/json_tool/json_tool.cpp:79–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

77}
78
79FormattedJson Star::addOrSet(bool add,
80 JsonPath::PathPtr path,
81 FormattedJson const& input,
82 InsertLocation insertLocation,
83 FormattedJson const& value) {
84 JsonPath::EmptyPathOp<FormattedJson> emptyPathOp = [&value, add](FormattedJson const& document) {
85 if (!add || document.type() == Json::Type::Null)
86 return value;
87 throw JsonException("Cannot add a value to the entire document, it is not empty.");
88 };
89 JsonPath::ObjectOp<FormattedJson> objectOp = [&value, &insertLocation](
90 FormattedJson const& object, String const& key) {
91 if (insertLocation.is<AtBeginning>())
92 return object.prepend(key, value);
93 if (insertLocation.is<AtEnd>())
94 return object.append(key, value);
95 if (insertLocation.is<BeforeKey>())
96 return object.insertBefore(key, value, insertLocation.get<BeforeKey>().key);
97 if (insertLocation.is<AfterKey>())
98 return object.insertAfter(key, value, insertLocation.get<AfterKey>().key);
99 return object.set(key, value);
100 };
101 JsonPath::ArrayOp<FormattedJson> arrayOp = [&value, add](FormattedJson const& array, Maybe<size_t> i) {
102 if (i.isValid()) {
103 if (add)
104 return array.insert(*i, value);
105 return array.set(*i, value);
106 }
107 return array.append(value);
108 };
109 return path->apply(input, emptyPathOp, objectOp, arrayOp);
110}
111
112void forEachFileRecursive(String const& directory, function<void(String)> func) {
113 for (pair<String, bool> entry : File::dirList(directory)) {

Callers

nothing calls this directly

Calls 9

insertBeforeMethod · 0.80
isValidMethod · 0.80
typeMethod · 0.45
prependMethod · 0.45
appendMethod · 0.45
insertAfterMethod · 0.45
setMethod · 0.45
insertMethod · 0.45
applyMethod · 0.45

Tested by

no test coverage detected