summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/property_replacer.html6
-rw-r--r--template.c25
-rw-r--r--template.h2
3 files changed, 19 insertions, 14 deletions
diff --git a/doc/property_replacer.html b/doc/property_replacer.html
index 217b6dc0..dc09d33c 100644
--- a/doc/property_replacer.html
+++ b/doc/property_replacer.html
@@ -715,8 +715,10 @@ Useful for secure pathname generation (with dynafiles).
</td>
</tr>
<tr>
-<td><b>optional-field</b></td>
-<td>In templates that are used for building field lists (in particular, ommongodb), completely remove this field if the corresponding property is not present. Currently implemented only for the <b>$!&lt;name&gt;</b> properties.</td>
+<td><b>mandatory-field</b></td>
+<td>In templates that are used for building field lists (in particular, ommongodb), include
+this field, even if it is empty (or NULL). If not set, the field will be removed from
+the output field set if empty. The latter is the default case.
</tr>
</tbody>
</table>
diff --git a/template.c b/template.c
index 475eae75..27b508c6 100644
--- a/template.c
+++ b/template.c
@@ -85,7 +85,7 @@ static struct cnfparamdescr cnfparamdescrProperty[] = {
{ "regex.match", eCmdHdlrInt, 0 },
{ "regex.submatch", eCmdHdlrInt, 0 },
{ "droplastlf", eCmdHdlrBinary, 0 },
- { "optional", eCmdHdlrBinary, 0 },
+ { "mandatory", eCmdHdlrBinary, 0 },
{ "spifno1stsp", eCmdHdlrBinary, 0 }
};
static struct cnfparamblk pblkProperty =
@@ -296,15 +296,18 @@ rsRetVal tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjso
if(pTpe->fieldName == NULL)
continue;
jsonf = json_object_new_string((char*) pTpe->data.constant.pConstant);
+ json_object_object_add(json, (char*)pTpe->fieldName, jsonf);
} else if(pTpe->eEntryType == FIELD) {
pVal = (uchar*) MsgGetProp(pMsg, pTpe, pTpe->data.field.propid,
pTpe->data.field.propName, &propLen, &bMustBeFreed);
- jsonf = json_object_new_string_len((char*)pVal, propLen);
+ if(pTpe->data.field.options.bMandatory || propLen > 0) {
+ jsonf = json_object_new_string_len((char*)pVal, propLen);
+ json_object_object_add(json, (char*)pTpe->fieldName, jsonf);
+ }
if(bMustBeFreed) { /* json-c makes its own private copy! */
free(pVal);
}
}
- json_object_object_add(json, (char*)pTpe->fieldName, jsonf);
}
*pjson = (iRet == RS_RET_OK) ? json : NULL;
@@ -664,8 +667,8 @@ static void doOptions(unsigned char **pp, struct templateEntry *pTpe)
} else {
pTpe->data.field.options.bJSONf = 1;
}
- } else if(!strcmp((char*)Buf, "optional-field")) {
- pTpe->data.field.options.bOptionalField = 1;
+ } else if(!strcmp((char*)Buf, "mandatory-field")) {
+ pTpe->data.field.options.bMandatory = 1;
} else {
dbgprintf("Invalid field option '%s' specified - ignored.\n", Buf);
}
@@ -1283,7 +1286,7 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o)
int i;
int droplastlf = 0;
int spifno1stsp = 0;
- int optional = 0;
+ int mandatory = 0;
int frompos = -1;
int topos = -1;
int fieldnum = -1;
@@ -1314,8 +1317,8 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o)
cstrFinalize(name);
} else if(!strcmp(pblkProperty.descr[i].name, "droplastlf")) {
droplastlf = pvals[i].val.d.n;
- } else if(!strcmp(pblkProperty.descr[i].name, "optional")) {
- optional = pvals[i].val.d.n;
+ } else if(!strcmp(pblkProperty.descr[i].name, "mandatory")) {
+ mandatory = pvals[i].val.d.n;
} else if(!strcmp(pblkProperty.descr[i].name, "spifno1stsp")) {
spifno1stsp = pvals[i].val.d.n;
} else if(!strcmp(pblkProperty.descr[i].name, "outname")) {
@@ -1477,7 +1480,7 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o)
}
pTpe->data.field.options.bDropLastLF = droplastlf;
pTpe->data.field.options.bSPIffNo1stSP = spifno1stsp;
- pTpe->data.field.options.bOptionalField = optional;
+ pTpe->data.field.options.bMandatory = mandatory;
pTpe->data.field.eCaseConv = caseconv;
switch(formatType) {
case F_NONE:
@@ -1995,8 +1998,8 @@ void tplPrintList(rsconf_t *conf)
if(pTpe->data.field.options.bJSONf) {
dbgprintf("[format as JSON field] ");
}
- if(pTpe->data.field.options.bOptionalField) {
- dbgprintf("[optional field - skip in field template if not present] ");
+ if(pTpe->data.field.options.bMandatory) {
+ dbgprintf("[mandatory field] ");
}
if(pTpe->data.field.options.bDropLastLF) {
dbgprintf("[drop last LF in msg] ");
diff --git a/template.h b/template.h
index 0d083216..42e262f8 100644
--- a/template.h
+++ b/template.h
@@ -115,7 +115,7 @@ struct templateEntry {
unsigned bCSV: 1; /* format field in CSV (RFC 4180) format */
unsigned bJSON: 1; /* format field JSON escaped */
unsigned bJSONf: 1; /* format field JSON *field* (n/v pair) */
- unsigned bOptionalField: 1; /* optional field - skip in field template if not present */
+ unsigned bMandatory: 1; /* mandatory field - emit even if empty */
} options; /* options as bit fields */
} field;
} data;