Skip to content

Commit e4cf92f

Browse files
authored
Improve support for modifying set type metadata fields. (#194)
1 parent aa8f8ce commit e4cf92f

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

cloudinary-core/src/main/java/com/cloudinary/Util.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,19 @@ protected static String encodeAccessControl(Object accessControl) {
119119
}
120120

121121
protected static String encodeContext(Object context) {
122-
if (context != null && context instanceof Map) {
123-
Map<String, String> mapArg = (Map<String, String>) context;
122+
if (context instanceof Map) {
123+
Map<String, Object> mapArg = (Map<String, Object>) context;
124124
HashSet out = new HashSet();
125-
for (Map.Entry<String, String> entry : mapArg.entrySet()) {
126-
final String value = entry.getValue().replaceAll("([=\\|])","\\\\$1");
127-
out.add(entry.getKey() + "=" + value);
125+
for (Map.Entry<String, Object> entry : mapArg.entrySet()) {
126+
final String value;
127+
if (entry.getValue() instanceof List) {
128+
value = encodeList(((List) entry.getValue()).toArray());
129+
} else if (entry.getValue() instanceof String[]) {
130+
value = encodeList((String[]) entry.getValue());
131+
} else {
132+
value = entry.getValue().toString();
133+
}
134+
out.add(entry.getKey() + "=" + encodeSingleContextString(value));
128135
}
129136
return StringUtils.join(out.toArray(), "|");
130137
} else if (context == null) {
@@ -134,6 +141,26 @@ protected static String encodeContext(Object context) {
134141
}
135142
}
136143

144+
private static String encodeList(Object[] list) {
145+
StringBuilder builder = new StringBuilder("[");
146+
147+
boolean first = true;
148+
for (Object s : list) {
149+
if (!first) {
150+
builder.append(",");
151+
}
152+
153+
builder.append("\"").append(encodeSingleContextString(s.toString())).append("\"");
154+
first = false;
155+
}
156+
157+
return builder.append("]").toString();
158+
}
159+
160+
private static String encodeSingleContextString(String value) {
161+
return value.replaceAll("([=\\|])", "\\\\$1");
162+
}
163+
137164
@SuppressWarnings("unchecked")
138165
protected static final String buildCustomHeaders(Object headers) {
139166
if (headers == null) {
@@ -165,7 +192,7 @@ public static void clearEmpty(Map params) {
165192
@SuppressWarnings({"rawtypes", "unchecked"})
166193
public static final Map<String, Object> buildArchiveParams(Map options, String targetFormat) {
167194
Map<String, Object> params = new HashMap<String, Object>();
168-
if (options != null && options.size() > 0){
195+
if (options != null && options.size() > 0) {
169196
params.put("type", options.get("type"));
170197
params.put("mode", options.get("mode"));
171198
params.put("target_format", targetFormat);
@@ -198,7 +225,7 @@ private static void putEager(String name, Map from, Map<String, Object> to) {
198225

199226
private static void putBoolean(String name, Map from, Map<String, Object> to) {
200227
final Object value = from.get(name);
201-
if(value != null){
228+
if (value != null) {
202229
to.put(name, ObjectUtils.asBoolean(value));
203230
}
204231
}
@@ -209,16 +236,16 @@ private static void putObject(String name, Map from, Map<String, Object> to) {
209236

210237
private static void putObject(String name, Map from, Map<String, Object> to, Object defaultValue) {
211238
final Object value = from.get(name);
212-
if (value != null){
239+
if (value != null) {
213240
to.put(name, value);
214-
} else if(defaultValue != null){
241+
} else if (defaultValue != null) {
215242
to.put(name, defaultValue);
216243
}
217244
}
218245

219246
private static void putArray(String name, Map from, Map<String, Object> to) {
220247
final Object value = from.get(name);
221-
if (value != null){
248+
if (value != null) {
222249
to.put(name, ObjectUtils.asArray(value));
223250
}
224251
}

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractStructuredMetadataTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,21 @@ public void testUploaderUpdateMetadata() throws Exception {
216216
assertEquals("sample", ((List) result.get("public_ids")).get(0).toString());
217217
}
218218

219+
@Test
220+
public void testSetField() throws Exception {
221+
SetMetadataField field = createSetField("test123");
222+
ApiResponse fieldResult = addFieldToAccount(field);
223+
String fieldId = fieldResult.get("external_id").toString();
224+
Map result = cloudinary.uploader().updateMetadata(asMap(fieldId, new String[]{"id2", "id3"}), new String[]{"sample"}, null);
225+
assertNotNull(result);
226+
assertEquals("sample", ((List) result.get("public_ids")).get(0).toString());
227+
List<String> list = new ArrayList<String>(2);
228+
list.add("id1");
229+
list.add("id2");
230+
result = cloudinary.uploader().updateMetadata(asMap(fieldId, list), new String[]{"sample"}, null);
231+
assertNotNull(result);
232+
assertEquals("sample", ((List) result.get("public_ids")).get(0).toString());
233+
}
219234
// Metadata test helpers
220235
private SetMetadataField createSetField(String labelPrefix) {
221236
SetMetadataField setField = new SetMetadataField();

0 commit comments

Comments
 (0)