Skip to content

Commit

Permalink
JNG-4227 support string substitution (#85)
Browse files Browse the repository at this point in the history
* add string substitution to map proxy

* add string substitution

* remove unused code
  • Loading branch information
Madmat8 authored Nov 22, 2023
1 parent 89a4d94 commit c1fe065
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/main/java/hu/blackbelt/structured/map/proxy/MapProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,15 @@ private void invokeSet(Method m, Object[] args) throws ExecutionException {
throw new IllegalStateException("Could not call set on immutable object");
}
String attrName = Character.toLowerCase(m.getName().charAt(3)) + m.getName().substring(4);
final Object value = args[0];
Object value = args[0];

if (value instanceof String
&& args.length > 1
&& args[1] instanceof Object[]
&& ((Object[]) args[1]).length > 0) {
value = String.format((String) value,(Object[]) args[1]);
}

AttributeInfo attributeInfo = typeInfoCache.get(clazz).get(attrName);
if (attributeInfo == null || !attributeInfo.isComposite()) {
internal.put(getKeyName(clazz, attrName), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,29 @@ public void testBuilderAdd() {
assertEquals(1, user.getUserDetails().stream().filter(userDetail -> userDetail.getId().equals("id4")).count());
}

@Test
void testWithWithStringSubstitution() {
UserDetail userDetail = MapBuilderProxy.builder(UserDetailBuilder.class, UserDetail.class).newInstance().id("1").note("Note %d").build();

assertEquals("1", userDetail.getId());
assertEquals("Note %d", userDetail.getNote());

userDetail = MapBuilderProxy.builder(UserDetailBuilder.class, UserDetail.class).newInstance().id("2").note("Note %d", 2).build();

assertEquals("2", userDetail.getId());
assertEquals("Note 2", userDetail.getNote());

userDetail = MapBuilderProxy.builder(UserDetailBuilder.class, UserDetail.class).newInstance().id("%d", 3).note("%s %d", "Note", 3).build();

assertEquals("3", userDetail.getId());
assertEquals("Note 3", userDetail.getNote());

userDetail = MapBuilderProxy.builder(UserDetailBuilder.class, UserDetail.class).newInstance().id("%d", 4).note("%s %.2f %d", "Note", 2.34, 4).build();

assertEquals("4", userDetail.getId());
assertEquals("Note 2.34 4", userDetail.getNote());
}

<T> T getMapHolderValue(Object input, Object key, Class<T> target) {
return (T) ((MapHolder) input).toMap().get(key);
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/hu/blackbelt/structured/map/proxy/MapProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,26 @@ public void testBuildFromBean() {
assertMapStructure(map);
}

@Test
public void testSetWithStringSubstitution() {
UserDetail userDetail = MapProxy.builder(UserDetail.class).newInstance();
userDetail.setId("1");
userDetail.setNote("Note%d");

assertEquals("Note%d", userDetail.getNote());

userDetail.setId("2");
userDetail.setNote("Note%d", 1);

assertEquals("Note1", userDetail.getNote());

userDetail.setId("%d", 3);
userDetail.setNote("%s %d", "Note", 1);

assertEquals("Note 1", userDetail.getNote());
assertEquals("3", userDetail.getId());
}

<T> T getMapHolderValue(Object input, Object key, Class<T> target) {
return (T) ((MapHolder) input).toMap().get(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public interface UserDetail {
@Key(name = "__id")
String getId();

void setId(String id);
void setId(String id, Object... args);

String getNote();

void setNote(String note);
void setNote(String note, Object... args);

static boolean equals(UserDetail o1, Object o2) {
if (o2 == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

public interface UserDetailBuilder {
UserDetailBuilder id(String id);
UserDetailBuilder note(String note);
UserDetailBuilder id(String id, Object... args);
UserDetailBuilder note(String note, Object... args);
UserDetail build();
}

0 comments on commit c1fe065

Please sign in to comment.