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

Builder for Maps and Arrays in Maps #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions src/main/java/co/nstant/in/cbor/builder/MapEntryBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package co.nstant.in.cbor.builder;

import co.nstant.in.cbor.model.Array;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;

public class MapEntryBuilder<T extends MapBuilder<?>> extends AbstractBuilder<T> {
private final DataItem key;
Expand All @@ -26,6 +28,18 @@ public T value(String value) {
return put(key, convert(value));
}

public ArrayBuilder<T> valueArray() {
Array nestedArray = new Array();
this.put(key, nestedArray);
return new ArrayBuilder<>(getParent(), nestedArray);
}

public MapBuilder<T> valueMap() {
Map nestedMap = new Map();
this.put(key, nestedMap);
return new MapBuilder<>(getParent(), nestedMap);
}

private T put(DataItem key, DataItem value) {
getParent().put(key, value);
return getParent();
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/co/nstant/in/cbor/builder/MapBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public void testMapBuilder() {
.addKey("28").value(1.0f)
.addKey("29").value(1L)
.addKey("30").value("value")
.addKey(31)
.valueMap()
.addKey(3111).value(3112)
.addKey(3121).value(3122)
.end()
.addKey(34).valueArray().add(35).end()
.end()
.startMap()
.startArray(1).end()
Expand All @@ -61,7 +67,7 @@ public void testMapBuilder() {
assertEquals(2, dataItems.size());
assertTrue(dataItems.get(0) instanceof Map);
Map map = (Map) dataItems.get(0);
assertEquals(31, map.getKeys().size());
assertEquals(33, map.getKeys().size());
}

@Test
Expand Down