Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Crocker <dcrocker@eschertech.com>2020-01-23 15:43:23 +0300
committerDavid Crocker <dcrocker@eschertech.com>2020-01-23 15:43:23 +0300
commit6a2a98ff7d8013eba9bf5bdc504c55ff2cec6e18 (patch)
tree17698fa23711bbc89551e0eb24dba059c5383124 /src
parente8a73d3af0f79d58576ae87eb470d15345d791f6 (diff)
Added maximum depth option in JSON object model reporting
Diffstat (limited to 'src')
-rw-r--r--src/GCodes/GCodeBuffer/StringParser.cpp14
-rw-r--r--src/ObjectModel/ObjectModel.cpp69
-rw-r--r--src/ObjectModel/ObjectModel.h8
-rw-r--r--src/Version.h2
4 files changed, 59 insertions, 34 deletions
diff --git a/src/GCodes/GCodeBuffer/StringParser.cpp b/src/GCodes/GCodeBuffer/StringParser.cpp
index 4036b4be..a85e96de 100644
--- a/src/GCodes/GCodeBuffer/StringParser.cpp
+++ b/src/GCodes/GCodeBuffer/StringParser.cpp
@@ -2077,8 +2077,8 @@ void StringParser::BalanceNumericTypes(ExpressionValue& val1, ExpressionValue& v
{
throw ConstructParseException("expected numeric operands");
}
- val1.Set(0);
- val2.Set(0);
+ val1.Set((int32_t)0);
+ val2.Set((int32_t)0);
}
}
@@ -2098,8 +2098,8 @@ void StringParser::BalanceTypes(ExpressionValue& val1, ExpressionValue& val2, bo
{
throw ConstructParseException("cannot convert operands to same type");
}
- val1.Set(0);
- val2.Set(0);
+ val1.Set((int32_t)0);
+ val2.Set((int32_t)0);
}
}
@@ -2121,7 +2121,7 @@ void StringParser::EnsureNumeric(ExpressionValue& val, bool evaluate)
{
throw ConstructParseException("expected numeric operand");
}
- val.Set(0);
+ val.Set((int32_t)0);
}
}
@@ -2303,7 +2303,7 @@ ExpressionValue StringParser::ParseIdentifierExpression(StringBuffer& stringBuff
}
String<MaxVariableNameLength> id;
- ObjectExplorationContext context("v", applyLengthOperator);
+ ObjectExplorationContext context("v", 99, applyLengthOperator);
// Loop parsing identifiers and index expressions
// When we come across an index expression, evaluate it, add it to the context, and place a marker in the identifier string.
@@ -2410,7 +2410,7 @@ ExpressionValue StringParser::ParseIdentifierExpression(StringBuffer& stringBuff
{
throw ConstructParseException("expected numeric operand");
}
- rslt.Set(0);
+ rslt.Set((int32_t)0);
}
}
else if (id.Equals("sin"))
diff --git a/src/ObjectModel/ObjectModel.cpp b/src/ObjectModel/ObjectModel.cpp
index fc9a096a..7628ed0a 100644
--- a/src/ObjectModel/ObjectModel.cpp
+++ b/src/ObjectModel/ObjectModel.cpp
@@ -59,12 +59,24 @@ ObjectModel::ObjectModel() noexcept
// ObjectExplorationContext members
-ObjectExplorationContext::ObjectExplorationContext(const char *reportFlags, bool wal) noexcept
- : numIndicesProvided(0), numIndicesCounted(0), shortForm(false), onlyLive(false), includeVerbose(false), wantArrayLength(wal), includeNulls(false)
+ObjectExplorationContext::ObjectExplorationContext(const char *reportFlags, unsigned int initialMaxDepth, bool wal) noexcept
+ : maxDepth(initialMaxDepth), currentDepth(0), numIndicesProvided(0), numIndicesCounted(0),
+ shortForm(false), onlyLive(false), includeVerbose(false), wantArrayLength(wal), includeNulls(false)
{
while (true)
{
- switch(*reportFlags)
+ if (isdigit(*reportFlags))
+ {
+ maxDepth = *reportFlags - '0';
+ ++reportFlags;
+ while (isdigit(*reportFlags))
+ {
+ maxDepth = (10 * maxDepth) + (*reportFlags - '0');
+ ++reportFlags;
+ }
+ }
+
+ switch (*reportFlags)
{
case '\0':
return;
@@ -114,45 +126,54 @@ bool ObjectExplorationContext::ShouldReport(const ObjectModelEntryFlags f) const
// Report this object
void ObjectModel::ReportAsJson(OutputBuffer* buf, ObjectExplorationContext& context, uint8_t tableNumber, const char* filter) const
{
- bool added = false;
- const uint8_t *descriptor;
- const ObjectModelTableEntry *tbl = GetObjectModelTable(descriptor);
- if (tableNumber < descriptor[0])
+ if (context.IncreaseDepth())
{
- size_t numEntries = descriptor[tableNumber + 1];
- while (tableNumber != 0)
+ bool added = false;
+ const uint8_t *descriptor;
+ const ObjectModelTableEntry *tbl = GetObjectModelTable(descriptor);
+ if (tableNumber < descriptor[0])
{
- --tableNumber;
- tbl += descriptor[tableNumber + 1];
- }
+ size_t numEntries = descriptor[tableNumber + 1];
+ while (tableNumber != 0)
+ {
+ --tableNumber;
+ tbl += descriptor[tableNumber + 1];
+ }
- while (numEntries != 0)
- {
- if (tbl->Matches(filter, context))
+ while (numEntries != 0)
{
- if (tbl->ReportAsJson(buf, context, this, filter, !added))
+ if (tbl->Matches(filter, context))
{
- added = true;
+ if (tbl->ReportAsJson(buf, context, this, filter, !added))
+ {
+ added = true;
+ }
}
+ --numEntries;
+ ++tbl;
+ }
+ if (added && *filter == 0)
+ {
+ buf->cat('}');
}
- --numEntries;
- ++tbl;
}
- if (added && *filter == 0)
+ if (!added)
{
- buf->cat('}');
+ buf->cat("null");
}
+ context.DecreaseDepth();
}
- if (!added)
+ else
{
- buf->cat("null");
+ buf->cat("{}");
}
}
// Construct a JSON representation of those parts of the object model requested by the user. This version is called on the root of the tree.
void ObjectModel::ReportAsJson(OutputBuffer *buf, const char *filter, const char *reportFlags, bool wantArrayLength) const
{
- ObjectExplorationContext context(reportFlags, wantArrayLength);
+ const unsigned int defaultMaxDepth = (wantArrayLength) ? 99 : (filter[0] == 0) ? 1 : 99;
+ ObjectExplorationContext context(reportFlags, defaultMaxDepth, wantArrayLength);
ReportAsJson(buf, context, 0, filter);
}
diff --git a/src/ObjectModel/ObjectModel.h b/src/ObjectModel/ObjectModel.h
index ad404be4..9a18ffe6 100644
--- a/src/ObjectModel/ObjectModel.h
+++ b/src/ObjectModel/ObjectModel.h
@@ -101,7 +101,6 @@ struct ExpressionValue
void Set(bool b) noexcept { type = TYPE_OF(bool); bVal = b; }
void Set(char c) noexcept { type = TYPE_OF(char); cVal = c; }
void Set(int32_t i) noexcept { type = TYPE_OF(int32_t); iVal = i; }
- void Set(int i) noexcept { type = TYPE_OF(int32_t); iVal = i; }
void Set(float f) noexcept { type = TYPE_OF(float); fVal = f; param = 1; }
void Set(const char *s) noexcept { type = TYPE_OF(const char*); sVal = s; }
@@ -129,8 +128,11 @@ enum class ObjectModelEntryFlags : uint8_t
class ObjectExplorationContext
{
public:
- ObjectExplorationContext(const char *reportFlags, bool wal) noexcept;
+ ObjectExplorationContext(const char *reportFlags, unsigned int initialMaxDepth, bool wal) noexcept;
+ void SetMaxDepth(unsigned int d) noexcept { maxDepth = d; }
+ bool IncreaseDepth() noexcept { if (currentDepth < maxDepth) { ++currentDepth; return true; } return false; }
+ void DecreaseDepth() noexcept { --currentDepth; }
void AddIndex(int32_t index) THROWS_GCODE_EXCEPTION;
void AddIndex() THROWS_GCODE_EXCEPTION;
void RemoveIndex() THROWS_GCODE_EXCEPTION;
@@ -146,6 +148,8 @@ public:
private:
static constexpr size_t MaxIndices = 4; // max depth of array nesting
+ unsigned int maxDepth;
+ unsigned int currentDepth;
size_t numIndicesProvided; // the number of indices provided, when we are doing a value lookup
size_t numIndicesCounted; // the number of indices passed in the search string
int32_t indices[MaxIndices];
diff --git a/src/Version.h b/src/Version.h
index 3ac41602..451cf6a3 100644
--- a/src/Version.h
+++ b/src/Version.h
@@ -20,7 +20,7 @@
#endif
#ifndef DATE
-# define DATE "2020-01-22b2"
+# define DATE "2020-01-23b1"
#endif
#define AUTHORS "reprappro, dc42, chrishamm, t3p3, dnewman, printm3d"