This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-nosql-couchbase-resourceprovider.git
commit c77bfa7d28324e3f92ceedfa1bd10f93532988ee Author: Stefan Seifert <[email protected]> AuthorDate: Mon Sep 14 21:19:29 2015 +0000 SLING-5024 Sling NoSQL Resource Provider for MongoDB (based on nosql.generic) git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1703061 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 11 +-- .../impl/CouchbaseNoSqlAdapter.java | 7 +- .../resourceprovider/impl/MapConverter.java | 87 ---------------------- .../resourceprovider/impl/MapConverterTest.java | 79 -------------------- 4 files changed, 6 insertions(+), 178 deletions(-) diff --git a/pom.xml b/pom.xml index e8e00f8..07c509d 100644 --- a/pom.xml +++ b/pom.xml @@ -67,18 +67,11 @@ <version>2.2.2</version> <scope>provided</scope> </dependency> - - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-lang3</artifactId> - <version>3.3.2</version> - <scope>provided</scope> - </dependency> - + <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.testing.sling-mock</artifactId> - <version>1.3.0</version> + <version>1.5.0</version> <scope>test</scope> </dependency> <dependency> diff --git a/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java b/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java index 9fe9931..5ebcc6f 100644 --- a/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java +++ b/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/CouchbaseNoSqlAdapter.java @@ -23,6 +23,7 @@ import java.util.Iterator; import org.apache.sling.nosql.couchbase.client.CouchbaseClient; import org.apache.sling.nosql.couchbase.client.CouchbaseKey; import org.apache.sling.nosql.generic.adapter.AbstractNoSqlAdapter; +import org.apache.sling.nosql.generic.adapter.MultiValueMode; import org.apache.sling.nosql.generic.adapter.NoSqlData; import com.couchbase.client.java.Bucket; @@ -79,7 +80,7 @@ public final class CouchbaseNoSqlAdapter extends AbstractNoSqlAdapter { return null; } else { - return new NoSqlData(path, MapConverter.mapListToArray(data.toMap())); + return new NoSqlData(path, data.toMap(), MultiValueMode.LISTS); } } } @@ -102,7 +103,7 @@ public final class CouchbaseNoSqlAdapter extends AbstractNoSqlAdapter { JsonObject envelope = doc.content(); String path = envelope.getString(PN_PATH); JsonObject data = envelope.getObject(PN_DATA); - return new NoSqlData(path, MapConverter.mapListToArray(data.toMap())); + return new NoSqlData(path, data.toMap(), MultiValueMode.LISTS); } @Override @@ -119,7 +120,7 @@ public final class CouchbaseNoSqlAdapter extends AbstractNoSqlAdapter { JsonObject envelope = JsonObject.create(); envelope.put(PN_PATH, data.getPath()); - envelope.put(PN_DATA, JsonObject.from(MapConverter.mapArrayToList(data.getProperties()))); + envelope.put(PN_DATA, JsonObject.from(data.getProperties(MultiValueMode.LISTS))); JsonDocument doc = JsonDocument.create(cacheKey, envelope); try { diff --git a/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/MapConverter.java b/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/MapConverter.java deleted file mode 100644 index a8278fd..0000000 --- a/src/main/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/MapConverter.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.sling.nosql.couchbase.resourceprovider.impl; - -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import org.apache.commons.lang3.ArrayUtils; - -/** - * Transforms NoSqlData maps to a valid form for couchbase JSON document. - * All arrays have to be transformed to lists. - */ -public final class MapConverter { - - private MapConverter() { - // static methods only - } - - /** - * @param map Map with multi-valued arrays - * @return Map with multi-valued lists - */ - public static Map<String, Object> mapArrayToList(Map<String, Object> map) { - for (Map.Entry<String, Object> entry : map.entrySet()) { - if (entry.getValue().getClass().isArray()) { - Class componentType = entry.getValue().getClass().getComponentType(); - if (componentType == int.class) { - entry.setValue(Arrays.asList(ArrayUtils.toObject((int[]) entry.getValue()))); - } - else if (componentType == long.class) { - entry.setValue(Arrays.asList(ArrayUtils.toObject((long[]) entry.getValue()))); - } - else if (componentType == double.class) { - entry.setValue(Arrays.asList(ArrayUtils.toObject((double[]) entry.getValue()))); - } - else if (componentType == boolean.class) { - entry.setValue(Arrays.asList(ArrayUtils.toObject((boolean[]) entry.getValue()))); - } - else { - entry.setValue(Arrays.asList((Object[]) entry.getValue())); - } - } - } - return map; - } - - /** - * @param map Map with multi-valued lists - * @return Map with multi-valued arrays - */ - @SuppressWarnings("unchecked") - public static Map<String, Object> mapListToArray(Map<String, Object> map) { - for (Map.Entry<String, Object> entry : map.entrySet()) { - if (entry.getValue() instanceof List) { - List list = (List) entry.getValue(); - if (list.size() == 0) { - entry.setValue(null); - } - else { - Class type = list.get(0).getClass(); - entry.setValue(list.toArray((Object[]) Array.newInstance(type, list.size()))); - } - } - } - return map; - } - -} diff --git a/src/test/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/MapConverterTest.java b/src/test/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/MapConverterTest.java deleted file mode 100644 index 1f8e1ee..0000000 --- a/src/test/java/org/apache/sling/nosql/couchbase/resourceprovider/impl/MapConverterTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.sling.nosql.couchbase.resourceprovider.impl; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; - -import java.util.Map; - -import org.apache.sling.nosql.couchbase.resourceprovider.impl.MapConverter; -import org.junit.Test; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Maps; - -public class MapConverterTest { - - @Test - public void testMapArrayToList() throws Exception { - Map<String, Object> result = MapConverter.mapArrayToList(Maps.newHashMap(ImmutableMap.<String, Object>builder() - .put("prop1", "value1") - .put("prop2", 2) - .put("stringArray", new String[] { "value1", "value2" }) - .put("integerArray", new Integer[] { 1, 2, 3 }) - .put("integerArray2", new int[] { 1, 2, 3 }) - .put("longArray", new long[] { 1L, 2L }) - .put("doubleArray", new double[] { 1.1d, 1.2d }) - .put("booleanArray", new boolean[] { true, false }) - .build())); - - assertEquals("prop1", "value1", result.get("prop1")); - assertEquals("prop2", 2, result.get("prop2")); - assertEquals("stringArray", ImmutableList.of("value1", "value2"), result.get("stringArray")); - assertEquals("integerArray", ImmutableList.of(1, 2, 3), result.get("integerArray")); - assertEquals("integerArray2", ImmutableList.of(1, 2, 3), result.get("integerArray2")); - assertEquals("longArray", ImmutableList.of(1L, 2L), result.get("longArray")); - assertEquals("doubleArray", ImmutableList.of(1.1d, 1.2d), result.get("doubleArray")); - assertEquals("booleanArray", ImmutableList.of(true, false), result.get("booleanArray")); - } - - @Test - public void testMapListToArray() throws Exception { - Map<String, Object> result = MapConverter.mapListToArray(Maps.newHashMap(ImmutableMap.<String, Object>builder() - .put("prop1", "value1") - .put("prop2", 2) - .put("stringArray", ImmutableList.of("value1", "value2")) - .put("integerArray", ImmutableList.of(1, 2, 3)) - .put("longArray", ImmutableList.of(1L, 2L)) - .put("doubleArray", ImmutableList.of(1.1d, 1.2d)) - .put("booleanArray", ImmutableList.of(true, false)) - .build())); - - assertEquals("prop1", "value1", result.get("prop1")); - assertEquals("prop2", 2, result.get("prop2")); - assertArrayEquals("stringArray", new String[] { "value1", "value2" }, (String[]) result.get("stringArray")); - assertArrayEquals("integerArray", new Integer[] { 1, 2, 3 }, (Integer[]) result.get("integerArray")); - assertArrayEquals("longArray", new Long[] { 1L, 2L }, (Long[]) result.get("longArray")); - assertArrayEquals("doubleArray", new Double[] { 1.1d, 1.2d }, (Double[]) result.get("doubleArray")); - assertArrayEquals("booleanArray", new Boolean[] { true, false }, (Boolean[]) result.get("booleanArray")); - } - -} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
