Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/part2 rendering of entities #1288

Merged
merged 4 commits into from
Sep 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/apiTypesV2/Attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::string Attribute::render(ConnectionInfo* ciP, RequestType requestType, bool
std::string out;

out = "{";
out += pcontextAttribute->toJson(true, false); // param 1 'true' as it is the last and only element
out += pcontextAttribute->toJson(true, false, "normalized"); // param 1 'true' as it is the last and only element
out += "}";

if (comma)
Expand Down
28 changes: 21 additions & 7 deletions src/lib/apiTypesV2/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,39 @@ Entity::~Entity()
/* ****************************************************************************
*
* Entity::render -
*
* The rendering of JSON in APIv2 depends on the URI param 'options'
* Rendering methods:
* o 'normalized' (default)
* o 'keyValues' (less verbose, only name and values shown for attributes - no type, no metadatas)
* o 'values' (only the values of the attributes are printed, in a vector)
*/
std::string Entity::render(ConnectionInfo* ciP, RequestType requestType, bool comma)
{
std::string renderMode = "normalized";

if (ciP->uriParamOptions["keyValues"] == true)
{
renderMode = "keyValues";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunatelly, "keyValues" seems that will not be used at the end. However, the issue is still under discussion, so probably the best strategy is to progress this PR without changes, then fix it in a next PR once it gets clear. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I really believe that would be easier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the end seems that "keyValues" will be used. Keep tunned :)

NTC

}
else if (ciP->uriParamOptions["values"]== true)
{
renderMode = "values";
}

if ((errorCode.description == "") && ((errorCode.error == "OK") || (errorCode.error == "")))
{
std::string out = "{";

out += JSON_VALUE("id", id);

if (type != "")
{
out += ",";
out += JSON_VALUE("type", type);
}
out += ",";
out += JSON_STR("type") + ":" + ((type != "")? JSON_STR(type) : "null");

if (attributeVector.size() != 0)
{
out += ",";
out += attributeVector.toJson(true, false);

out += attributeVector.toJson(true, false, renderMode);
}

out += "}";
Expand Down
41 changes: 24 additions & 17 deletions src/lib/ngsi/ContextAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,52 +519,58 @@ std::string ContextAttribute::render
* the code paths of the rendering process
*
*/
std::string ContextAttribute::toJson(bool isLastElement, bool types)
std::string ContextAttribute::toJson(bool isLastElement, bool types, const std::string& renderMode)
{
std::string out;

if (types == true)
{
out = JSON_STR(name) + ":{" + JSON_STR("type") + ":" + JSON_STR(type) + "}";
}
else if ((type == "") && (metadataVector.size() == 0))
else if ((renderMode == "values") || (renderMode == "keyValues"))
{
out = (renderMode == "keyValues")? JSON_STR(name) + ":" : "";

if (compoundValueP != NULL)
{
if (compoundValueP->isObject())
{
out = JSON_STR(name) + ":{" + compoundValueP->toJson(true) + "}";
out += "{" + compoundValueP->toJson(true) + "}";
}
else if (compoundValueP->isVector())
{
out = JSON_STR(name) + ":[" + compoundValueP->toJson(true) + "]";
out += "[" + compoundValueP->toJson(true) + "]";
}
}
else if (valueType == orion::ValueTypeNumber)
{
char num[32];

snprintf(num, sizeof(num), "%f", numberValue);
out = JSON_VALUE_NUMBER(name, num);
out += num;
}
else if (valueType == orion::ValueTypeString)
{
out = JSON_VALUE(name, stringValue);
out += JSON_STR(stringValue);
}
else if (valueType == orion::ValueTypeBoolean)
{
out = JSON_VALUE_BOOL(name, boolValue);
out += (boolValue == true)? "true" : "false";
}
}
else
else // Render mode: normalized
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code comnets should be aligned with spec. In this particular case, I'm not sure if "normalized" will be a term used in the spec.

However, again, until things get clear I'm afraid cannot fix this.

NTC

{
out = JSON_STR(name) + ":{";

if (type != "")
{
out += JSON_VALUE("type", type) + ",";
}
//
// type
//
out += (type != "")? JSON_VALUE("type", type) : JSON_STR("type") + ":" + "null";
out += ",";


//
// value
//
if (compoundValueP != NULL)
{
if (compoundValueP->isObject())
Expand Down Expand Up @@ -596,11 +602,12 @@ std::string ContextAttribute::toJson(bool isLastElement, bool types)
{
out += JSON_VALUE("value", stringValue);
}
out += ",";

if (metadataVector.size() > 0)
{
out += "," + metadataVector.toJson(true);
}
//
// metadata
//
out += JSON_STR("metadata") + ":" + "{" + metadataVector.toJson(true) + "}";

out += "}";
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ngsi/ContextAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ typedef struct ContextAttribute
std::string render(ConnectionInfo* ciP, RequestType request, const std::string& indent, bool comma = false, bool omitValue = false);
std::string renderAsJsonObject(ConnectionInfo* ciP, RequestType request, const std::string& indent, bool comma, bool omitValue = false);
std::string renderAsNameString(ConnectionInfo* ciP, RequestType request, const std::string& indent, bool comma = false);
std::string toJson(bool isLastElement, bool types);
std::string toJson(bool isLastElement, bool types, const std::string& renderMode);
void present(const std::string& indent, int ix);
void release(void);
std::string toString(void);
Expand Down
15 changes: 13 additions & 2 deletions src/lib/ngsi/ContextAttributeVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static std::string addedLookup(const std::vector<std::string>& added, std::strin
* If anybody needs an attribute named 'id' or 'type', then API v1
* will have to be used to retrieve that information.
*/
std::string ContextAttributeVector::toJson(bool isLastElement, bool types)
std::string ContextAttributeVector::toJson(bool isLastElement, bool types, const std::string& renderMode)
{
if (vec.size() == 0)
{
Expand Down Expand Up @@ -117,6 +117,12 @@ std::string ContextAttributeVector::toJson(bool isLastElement, bool types)
//
std::string out;
int renderedAttributes = 0;

if (renderMode == "values")
{
out = JSON_STR("attributeValues") + ":" + "[";
}

for (unsigned int ix = 0; ix < vec.size(); ++ix)
{
if ((vec[ix]->name == "id") || (vec[ix]->name == "type"))
Expand All @@ -125,7 +131,12 @@ std::string ContextAttributeVector::toJson(bool isLastElement, bool types)
}

++renderedAttributes;
out += vec[ix]->toJson(renderedAttributes == validAttributes, types);
out += vec[ix]->toJson(renderedAttributes == validAttributes, types, renderMode);
}

if (renderMode == "values")
{
out += "]";
}

return out;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ngsi/ContextAttributeVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ typedef struct ContextAttributeVector
bool comma = false,
bool omitValue = false,
bool attrsAsName = false);
std::string toJson(bool isLastElement, bool types);
std::string toJson(bool isLastElement, bool types, const std::string& renderMode);
} ContextAttributeVector;

#endif // SRC_LIB_NGSI_CONTEXTATTRIBUTEVECTOR_H_
68 changes: 22 additions & 46 deletions src/lib/ngsi/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,57 +273,33 @@ std::string Metadata::toJson(bool isLastElement)
{
std::string out;

if (type == "")
out = JSON_STR(name) + ":{";

out += (type != "")? JSON_VALUE("type", type) : JSON_STR("type") + ":" + "null";
out += ",";

if (valueType == orion::ValueTypeString)
{
if (valueType == orion::ValueTypeNumber)
{
char num[32];

snprintf(num, sizeof(num), "%f", numberValue);
out = JSON_VALUE_NUMBER(name, num);
}
else if (valueType == orion::ValueTypeBoolean)
{
out = JSON_VALUE_BOOL(name, boolValue);
}
else if (valueType == orion::ValueTypeString)
{
out = JSON_VALUE(name, stringValue);
}
else
{
LM_E(("Runtime Error (invalid type for metadata %s)", name.c_str()));
out = JSON_VALUE(name, stringValue);
}
out += JSON_VALUE("value", stringValue);
}
else
else if (valueType == orion::ValueTypeNumber)
{
out = JSON_STR(name) + ":{";
out += JSON_VALUE("type", type) + ",";

if (valueType == orion::ValueTypeString)
{
out += JSON_VALUE("value", stringValue);
}
else if (valueType == orion::ValueTypeNumber)
{
char num[32];

snprintf(num, sizeof(num), "%f", numberValue);
out += JSON_VALUE_NUMBER("value", num);
}
else if (valueType == orion::ValueTypeBoolean)
{
out += JSON_VALUE_BOOL("value", boolValue);
}
else
{
LM_E(("Runtime Error (invalid value type for metadata %s)", name.c_str()));
out += JSON_VALUE("value", stringValue);
}
char num[32];

out += "}";
snprintf(num, sizeof(num), "%f", numberValue);
out += JSON_VALUE_NUMBER("value", num);
}
else if (valueType == orion::ValueTypeBoolean)
{
out += JSON_VALUE_BOOL("value", boolValue);
}
else
{
LM_E(("Runtime Error (invalid value type for metadata %s)", name.c_str()));
out += JSON_VALUE("value", stringValue);
}

out += "}";

if (!isLastElement)
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionTypes/EntityType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ std::string EntityType::toJson(ConnectionInfo* ciP)
out += JSON_STR("attrs") + ":";

out += "{";
out += contextAttributeVector.toJson(false, true);
out += contextAttributeVector.toJson(false, true, "normalized");
out += "}";

out += "," + JSON_STR("count") + ":" + countV;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionTypes/EntityTypeResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ std::string EntityTypeResponse::toJson(ConnectionInfo* ciP)
out += JSON_STR("attrs") + ":";

out += "{";
out += entityType.contextAttributeVector.toJson(false, true);
out += entityType.contextAttributeVector.toJson(false, true, "normalized");
out += "}";

out += "," + JSON_STR("count") + ":" + countV;
Expand Down
1 change: 1 addition & 0 deletions src/lib/rest/ConnectionInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static const char* validOptions[] =
"count",
"normalized",
"values",
"keyValues",
"text"
};

Expand Down
1 change: 1 addition & 0 deletions src/lib/rest/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ static int connectionTreat
size_t dataLen = *upload_data_size;
static int reqNo = 1;


// 1. First call - setup ConnectionInfo and get/check HTTP headers
if (ciP == NULL)
{
Expand Down
Loading