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

Add support for metadata array value #433

Merged
merged 2 commits into from
Aug 3, 2020
Merged
Changes from 1 commit
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
Next Next commit
Add support for metadata array value
  • Loading branch information
patrick-tolosa committed Aug 2, 2020
commit eb13ba3f54f534ceb83c5673d65e0682bacf3a49
47 changes: 44 additions & 3 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -320,9 +320,50 @@ function encode_key_value(arg) {
return entries(arg).map(([k, v]) => `${k}=${v}`).join('|');
}

function encode_context(arg) {
if (!isObject(arg)) { return arg; }
return entries(arg).map(([k, v]) => `${k}=${v.replace(/([=|])/g, '\\$&')}`).join('|');

/**
* @description Escape = and | with two backslashes \\
* @param {string|number} value
* @return {string}
*/
function escapeMetadataValue(value) {
return value.toString().replace(/([=|])/g, '\\$&');
}


/**
*
* @description Encode metadata fields based on incoming value.
* If array, escape as color_id=[\"green\",\"red\"]
* If string/number, escape as in_stock_id=50
*
* Joins resulting values with a pipe:
* in_stock_id=50|color_id=[\"green\",\"red\"]
*
* = and | and escaped by default (this can't be turned off)
*
* @param metadataObj
* @return {string}
*/
function encode_context(metadataObj) {
if (!isObject(metadataObj)) { return metadataObj; }

return entries(metadataObj).map(([key, value]) => {
// if string, simply parse the value and move on
if (isString(value)) {
return `${key}=${escapeMetadataValue(value)}`;

// If array, parse each item individually
} else if (isArray(value)) {
let values = value.map((innerVal) => {
return `\"${escapeMetadataValue(innerVal)}\"`
}).join(',');
return `${key}=[${values}]`
// if unknown, return the value as string
} else {
return value.toString();
}
}).join('|');
}

function build_eager(transformations) {
26 changes: 26 additions & 0 deletions test/integration/api/admin/structured_metadata_spec.js
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ const EXTERNAL_ID_ENUM_2 = 'metadata_external_id_enum_2_' + TEST_ID;
const EXTERNAL_ID_SET = 'metadata_external_id_set_' + TEST_ID;
const EXTERNAL_ID_SET_2 = 'metadata_external_id_set_2_' + TEST_ID;
const EXTERNAL_ID_SET_3 = 'metadata_external_id_set_3_' + TEST_ID;
const EXTERNAL_ID_SET_4 = 'metadata_external_id_set_4_' + TEST_ID;
const EXTERNAL_ID_DELETE = 'metadata_deletion_' + TEST_ID;
const EXTERNAL_ID_DELETE_2 = 'metadata_deletion_2_' + TEST_ID;
const EXTERNAL_ID_DATE_VALIDATION = 'metadata_date_validation_' + TEST_ID;
@@ -414,4 +415,29 @@ describe("structured metadata api", function () {
});
});
});

it('Should update an metadata field that is an array', (done) => {
let metadata = {
"external_id": EXTERNAL_ID_SET_4,
"label": EXTERNAL_ID_SET_4,
"type": "set",
"datasource": {
"values": [
{
"external_id": "1",
"value": "Email",
"state": "active"
}
]
}
};

api.add_metadata_field(metadata, (res, res2) => {
cloudinary.v2.uploader.update_metadata({[EXTERNAL_ID_SET_4]: [1]}, ['sample'], (err, res) => {
expect(typeof err).to.be('undefined');
expect(res.public_ids[0]).to.equal('sample');
done();
})
});
});
});