[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [dferry] serialization: Add Arguments::Reader::isDictKey() and fix pretty-printing.
From:       Andreas Hartmetz <ahartmetz () gmail ! com>
Date:       2016-12-04 18:52:11
Message-ID: E1cDbtT-0001fw-8J () code ! kde ! org
[Download RAW message or body]

Git commit eb1418add5a83a80adbe3c44925907d765828d80 by Andreas Hartmetz.
Committed on 04/12/2016 at 18:40.
Pushed by ahartmetz into branch 'master'.

Add Arguments::Reader::isDictKey() and fix pretty-printing.

The fixes are:
- use isDictKey() - there was still a TODO after removing
  NextDictEntry
- make it work when WITH_DICT_ENTRY is defined

Also make the pretty-printer output a bit easier to understand.
The one char indentation for dict keys and values was ugly, and
the "v " for variant was hard to distinguish from "V " for dict
entry value, so switch to "* " for variant.

M  +28   -11   serialization/arguments.cpp
M  +2    -0    serialization/arguments.h

https://commits.kde.org/dferry/eb1418add5a83a80adbe3c44925907d765828d80

diff --git a/serialization/arguments.cpp b/serialization/arguments.cpp
index 5ad3148..88ab65e 100644
--- a/serialization/arguments.cpp
+++ b/serialization/arguments.cpp
@@ -649,7 +649,13 @@ std::string Arguments::prettyPrint() const
     while (!isDone) {
         // HACK use nestingPrefix to determine when we're switching from key to \
                value - this can be done
         //      more cleanly with an aggregate stack if translation or similar makes \
                this approach too ugly
-        if (strEndsWith(nestingPrefix, "{")) {
+        if (reader.isDictKey()) {
+            if (strEndsWith(nestingPrefix, "V ")) {
+                nestingPrefix.resize(nestingPrefix.size() - strlen("V "));
+                assert(strEndsWith(nestingPrefix, "{ "));
+            }
+        }
+        if (strEndsWith(nestingPrefix, "{ ")) {
             nestingPrefix += "K ";
         } else if (strEndsWith(nestingPrefix, "K ")) {
             nestingPrefix.replace(nestingPrefix.size() - strlen("K "), strlen("V "), \
"V "); @@ -672,7 +678,7 @@ std::string Arguments::prettyPrint() const
         case Arguments::BeginVariant:
             reader.beginVariant();
             ret << nestingPrefix << "begin variant\n";
-            nestingPrefix += "v ";
+            nestingPrefix += "* ";
             break;
         case Arguments::EndVariant:
             reader.endVariant();
@@ -706,21 +712,22 @@ std::string Arguments::prettyPrint() const
         case Arguments::BeginDict: {
             inEmptyArray = \
!reader.beginDict(Arguments::Reader::ReadTypesOnlyIfEmpty);  ret << nestingPrefix << \
                "begin dict\n";
-            nestingPrefix += "{K ";
+            nestingPrefix += "{ ";
             break; }
-#if 0 // TODO
-        case Arguments::NextDictEntry:
-            reader.nextDictEntry();
-            if (strEndsWith(nestingPrefix, "V ")) {
-                nestingPrefix.resize(nestingPrefix.size() - strlen("V "));
-                assert(strEndsWith(nestingPrefix, "{"));
-            }
+#ifdef WITH_DICT_ENTRY
+        // We *could* use those states to be a bit more efficient than with calling \
isDictKey() all +        // the time, but let's keep it simple, and WITH_DICT_ENTRY \
as a non-default configuration. +        case Arguments::BeginDictEntry:
+            reader.beginDictEntry();
+            break;
+        case Arguments::EndDictEntry:
+            reader.endDictEntry();
             break;
 #endif
         case Arguments::EndDict:
             reader.endDict();
             inEmptyArray = reader.isInsideEmptyArray();
-            nestingPrefix.resize(nestingPrefix.size() - strlen("{V "));
+            nestingPrefix.resize(nestingPrefix.size() - strlen("{ V "));
             ret << nestingPrefix << "end dict\n";
             break;
         case Arguments::Boolean: {
@@ -1726,6 +1733,16 @@ void Arguments::Reader::skipDict()
     }
 }
 
+bool Arguments::Reader::isDictKey() const
+{
+    if (!d->m_aggregateStack.empty()) {
+        const Private::AggregateInfo &aggregateInfo = d->m_aggregateStack.back();
+        return aggregateInfo.aggregateType == BeginDict &&
+               d->m_signaturePosition == aggregateInfo.arr.containedTypeBegin;
+    }
+    return false;
+}
+
 void Arguments::Reader::endDict()
 {
     VALID_IF(m_state == EndDict, Error::ReadWrongType);
diff --git a/serialization/arguments.h b/serialization/arguments.h
index faa8c1e..0f39252 100644
--- a/serialization/arguments.h
+++ b/serialization/arguments.h
@@ -217,6 +217,8 @@ public:
 
         bool beginDict(EmptyArrayOption option = SkipIfEmpty);
         void skipDict(); // like skipArray()
+        bool isDictKey() const; // this can be used to track whether the current \
value is a dict key or value, e.g. +                                // for \
pretty-printing purposes (it is usually clear in marshalling code).  void endDict(); \
// like endArray()  
         void beginStruct();


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic