Skip to content

Commit

Permalink
Array reduction - part of multi-issue #1542
Browse files Browse the repository at this point in the history
  • Loading branch information
kzangeli committed Jan 26, 2024
1 parent 71c9582 commit 93f9f14
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 39 deletions.
7 changes: 4 additions & 3 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Fixed Issue:
* #1535 No matching with 'q' when matching subscriptions for deletion of an entity
## Fixed Issues:
* #1535: No matching with 'q' when matching subscriptions for deletion of an entity
* #1542: Array Reduction (arrays of one single element are "flattened" to that very element)

New Features:
## New Features:
* Support for VocabularyProperty
* Support for the new URL parameter "format" for output formats (normalized, concise, simplified)
1 change: 1 addition & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
Flask==2.0.3
pyOpenSSL==22.0.0
paho-mqtt==1.5.1
Werkzeug==2.3.6
1 change: 1 addition & 0 deletions src/lib/logMsg/traceLevels.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ typedef enum TraceLevels
LmtRegex, // Regular expressions - all of them
LmtDateTime, // DateTime (ISO8601) conversion
LmtMimeType, // MimeType
LmtArrayReduction, // Arrays of only one item are reduced to the item

//
// Legacy
Expand Down
52 changes: 51 additions & 1 deletion src/lib/orionld/payloadCheck/pCheckAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

extern "C"
{
#include "kbase/kMacros.h" // K_FT
#include "kalloc/kaStrdup.h" // kaStrdup
#include "kjson/KjNode.h" // KjNode
#include "kjson/kjLookup.h" // kjLookup
Expand Down Expand Up @@ -105,6 +106,33 @@ static const char* attrTypeChangeTitle(OrionldAttributeType oldType, OrionldAttr



// -----------------------------------------------------------------------------
//
// arrayReduce -
//
static void arrayReduce(KjNode* valueP)
{
if (valueP->type != KjArray)
return;

if (valueP->value.firstChildP == NULL)
return;

if (valueP->value.firstChildP->next != NULL)
return;

// It's an array with one single item
KjNode* itemP = valueP->value.firstChildP;

valueP->type = itemP->type;
valueP->value = itemP->value;
valueP->lastChild = itemP->lastChild;

LM_T(LmtArrayReduction, ("Reduced an array of one single item to a JSON %s", kjValueType(valueP->type)));
}



// -----------------------------------------------------------------------------
//
// pCheckTypeFromContext -
Expand Down Expand Up @@ -166,24 +194,37 @@ static bool pCheckTypeFromContext(KjNode* attrP, OrionldContextItem* attrContext
}
}
else if (strcmp(attrContextInfoP->type, "@set") == 0)
{
LM_T(LmtArrayReduction, ("the type for '%s' in the @context is '@set' - arrayReduction is set to FALSE", attrP->name));
arrayReduction = false;
}
else if (strcmp(attrContextInfoP->type, "@list") == 0)
{
LM_T(LmtArrayReduction, ("the type for '%s' in the @context is '@list' - arrayReduction is set to FALSE", attrP->name));
arrayReduction = false;
}
}

//
// Array Reduction
//
LM_T(LmtArrayReduction, ("Attribute: '%s' - arrayReduction: %s, type '%s'", attrP->name, K_FT(arrayReduction), kjValueType(attrP->type)));

if ((attrP->type == KjArray) && (arrayReduction == true))
{
LM_T(LmtArrayReduction, ("The value of '%s' is an array and arrayReduction is ON", attrP->name));
if ((attrP->value.firstChildP != NULL) && (attrP->value.firstChildP->next == NULL))
{
LM_T(LmtArrayReduction, ("'%s' is an array of one single element => arrayReduction is PERFORMED", attrP->name));

KjNode* arrayItemP = attrP->value.firstChildP;

attrP->type = arrayItemP->type;
attrP->value = arrayItemP->value;
attrP->lastChild = arrayItemP->lastChild; // Might be an array or object inside the array ...
}
else
LM_T(LmtArrayReduction, ("'%s' is an array of zero od +1 elements => arrayReduction is NOT performed", attrP->name));
}

return true;
Expand Down Expand Up @@ -336,7 +377,7 @@ inline bool pCheckAttributeArray
valueP->lastChild = attrP->lastChild;

pCheckAttributeTransform(attrP, "Property", valueP);

arrayReduce(valueP);
return true;
}

Expand Down Expand Up @@ -1022,14 +1063,23 @@ static bool pCheckAttributeObject
if (isGeoJsonValue(valueP) == true)
attributeType = GeoProperty;
else
{
attributeType = Property;
arrayReduce(valueP);
}
}
else if (objectP != NULL)
{
attributeType = Relationship;
arrayReduce(objectP);
}
else if (languageMapP != NULL)
attributeType = LanguageProperty;
else if (vocabP != NULL)
{
attributeType = VocabularyProperty;
arrayReduce(vocabP);
}
else
{
// If new attribute and no value field at all - error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,15 @@ Location: /ngsi-ld/v1/entities/urn:E1
02. GET 'urn:E1'
================
HTTP/1.1 200 OK
Content-Length: 97
Content-Length: 95
Content-Type: application/json
Date: REGEX(.*)
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)

{
"id": "urn:E1",
"occupier": {
"object": [
"urn:ngsi-ld:Person:You"
],
"object": "urn:ngsi-ld:Person:You",
"type": "Relationship"
},
"type": "T"
Expand Down
6 changes: 2 additions & 4 deletions test/functionalTest/cases/0000_ngsild/ngsild_issue_0310.test
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Location: /ngsi-ld/v1/entities/urn:ngsi-ld:Entity02
04. Get the Entity from step 3 and make sure the array is intact
================================================================
HTTP/1.1 200 OK
Content-Length: 97
Content-Length: 95
Content-Type: application/json
Date: REGEX(.*)
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
Expand All @@ -133,9 +133,7 @@ Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
"type": "T1",
"urn:ngsi-ld:attrs:P1": {
"type": "Property",
"value": [
12
]
"value": 12
}
}

Expand Down
Loading

0 comments on commit 93f9f14

Please sign in to comment.