ProtoMapper extends AbstractProtoMapper { @Override public Value toProto(Object val) { Value.Builder builder = Value.newBuilder(); if (val == null) { builder.setNullValue(NullValue.NULL_VALUE); } else if (val instanceof Boolean) { builder.setBoolValue((Boolean) val); } else if (val instanceof Double) { builder.setNumberValue((Double) val); } else if (val instanceof String) { builder.setStringValue((String) val); } else if (val instanceof Map) { Map<String, Object> map = (Map<String, Object>) val; Struct.Builder struct = Struct.newBuilder(); for (Map.Entry<String, Object> pair : map.entrySet()) { struct.putFields(pair.getKey(), toProto(pair.getValue())); } builder.setStructValue(struct.build()); } else if (val instanceof List) { ListValue.Builder list = ListValue.newBuilder(); for (Object obj : (List<Object>)val) { list.addValues(toProto(obj)); } builder.setListValue(list.build()); } else { throw new ClassCastException("cannot map to Value type: "+val); } return builder.build(); } private  ProtoMapper(); @Override Value toProto(Object val); @Override Object fromProto(Value any); @Override List<WorkflowTask> fromProto(WorkflowTaskPb.WorkflowTask.WorkflowTaskList list); @Override WorkflowTaskPb.WorkflowTask toProto(final WorkflowTask from); @Override WorkflowTask fromProto(final WorkflowTaskPb.WorkflowTask from); @Override WorkflowTaskPb.WorkflowTask.WorkflowTaskList toProto(List<WorkflowTask> list); @Override Any toProto(Any in); @Override Any fromProto(Any in); static final ProtoMapper INSTANCE; }
@Test public void workflowTaskToProto() { final WorkflowTask taskWithDefaultRetryCount = new WorkflowTask(); final WorkflowTask taskWith1RetryCount = new WorkflowTask(); taskWith1RetryCount.setRetryCount(1); final WorkflowTask taskWithNoRetryCount = new WorkflowTask(); taskWithNoRetryCount.setRetryCount(0); assertEquals(-1, mapper.toProto(taskWithDefaultRetryCount).getRetryCount()); assertEquals(1, mapper.toProto(taskWith1RetryCount).getRetryCount()); assertEquals(0, mapper.toProto(taskWithNoRetryCount).getRetryCount()); }