|
| 1 | +package io.reflectoring.dsl; |
| 2 | + |
| 3 | +import java.lang.reflect.Field; |
| 4 | +import java.math.BigDecimal; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collection; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import au.com.dius.pact.consumer.dsl.PactDslJsonBody; |
| 11 | +import au.com.dius.pact.consumer.dsl.PactDslJsonRootValue; |
| 12 | + |
| 13 | +public class PactDslJsonBodyLikeMapper { |
| 14 | + |
| 15 | + private static final Set<Class<?>> SIMPLE_TYPES = new HashSet<>(Arrays.asList( |
| 16 | + Boolean.class, |
| 17 | + boolean.class, |
| 18 | + Integer.class, |
| 19 | + int.class, |
| 20 | + Double.class, |
| 21 | + double.class, |
| 22 | + Float.class, |
| 23 | + float.class, |
| 24 | + BigDecimal.class, |
| 25 | + Number.class, |
| 26 | + String.class, |
| 27 | + Long.class, |
| 28 | + long.class |
| 29 | + )); |
| 30 | + |
| 31 | + public static PactDslJsonBody like(Object object) { |
| 32 | + return like(object, new PactDslJsonBody()); |
| 33 | + } |
| 34 | + |
| 35 | + public static PactDslJsonBody like(Object object, PactDslJsonBody body) { |
| 36 | + try { |
| 37 | + return recursiveLike(object, body); |
| 38 | + } catch (IllegalAccessException e) { |
| 39 | + throw new IllegalStateException("could not create PactDslJsonBody due to exception!", e); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private static PactDslJsonBody recursiveLike(Object object, PactDslJsonBody body) throws IllegalAccessException { |
| 44 | + Field[] fields = object.getClass().getDeclaredFields(); |
| 45 | + for (Field field : fields) { |
| 46 | + field.setAccessible(true); |
| 47 | + |
| 48 | + Object fieldValue = field.get(object); |
| 49 | + |
| 50 | + if (fieldValue == null) { |
| 51 | + // fields with null values will not be mapped |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + if (isSimpleType(field.getType())) { |
| 56 | + mapSimpleFieldWithName(field.getName(), fieldValue, body); |
| 57 | + } else if (isCollectionType(field.getType())) { |
| 58 | + mapCollectionField(field.getName(), (Collection) fieldValue, body); |
| 59 | + } else { |
| 60 | + mapComplexField(field.getName(), fieldValue, body); |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + return body; |
| 66 | + } |
| 67 | + |
| 68 | + private static void mapSimpleFieldWithName(String fieldName, Object fieldValue, PactDslJsonBody body) throws IllegalAccessException { |
| 69 | + Class<?> type = fieldValue.getClass(); |
| 70 | + if (String.class == type) { |
| 71 | + body.stringType(fieldName, (String) fieldValue); |
| 72 | + } else if (Boolean.class == type || boolean.class == type) { |
| 73 | + body.booleanType(fieldName, (Boolean) fieldValue); |
| 74 | + } else if (Integer.class == type || int.class == type || Long.class == type || long.class == type) { |
| 75 | + body.integerType(fieldName, (Integer) fieldValue); |
| 76 | + } else if (Double.class == type || double.class == type) { |
| 77 | + body.decimalType(fieldName, (Double) fieldValue); |
| 78 | + } else if (Float.class == type || float.class == type) { |
| 79 | + body.decimalType(fieldName, ((Float) fieldValue).doubleValue()); |
| 80 | + } else if (BigDecimal.class == type) { |
| 81 | + body.decimalType(fieldName, (BigDecimal) fieldValue); |
| 82 | + } else if (Number.class.isAssignableFrom(type)) { |
| 83 | + body.numberType(fieldName, (Number) fieldValue); |
| 84 | + } else { |
| 85 | + throw new IllegalStateException(String.format("field '%s' of type '%s' is not a simple field", fieldName, type)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static PactDslJsonRootValue getRootValueForType(Class<?> type) { |
| 90 | + if (String.class == type) { |
| 91 | + return PactDslJsonRootValue.stringType(); |
| 92 | + } else if (Boolean.class == type || boolean.class == type) { |
| 93 | + return PactDslJsonRootValue.booleanType(); |
| 94 | + } else if (Integer.class == type || int.class == type || Long.class == type || long.class == type) { |
| 95 | + return PactDslJsonRootValue.integerType(); |
| 96 | + } else if (Double.class == type || double.class == type) { |
| 97 | + return PactDslJsonRootValue.decimalType(); |
| 98 | + } else if (Float.class == type || float.class == type) { |
| 99 | + return PactDslJsonRootValue.decimalType(); |
| 100 | + } else if (BigDecimal.class == type) { |
| 101 | + return PactDslJsonRootValue.decimalType(); |
| 102 | + } else if (Number.class.isAssignableFrom(type)) { |
| 103 | + return PactDslJsonRootValue.numberType(); |
| 104 | + } else { |
| 105 | + throw new IllegalStateException(String.format("unsupported type '%s'", type)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static void mapCollectionField(String fieldName, Collection<?> collection, PactDslJsonBody body) throws IllegalAccessException { |
| 110 | + if (collection.isEmpty()) { |
| 111 | + throw new IllegalArgumentException("matching empty lists is not supported!"); |
| 112 | + } |
| 113 | + |
| 114 | + Class<?> listType = collection.iterator().next().getClass(); |
| 115 | + |
| 116 | + if (isSimpleType(listType)) { |
| 117 | + PactDslJsonRootValue rootValue = getRootValueForType(listType); |
| 118 | + body.eachLike(fieldName, rootValue); |
| 119 | + } else if (isCollectionType(listType)) { |
| 120 | + throw new IllegalArgumentException("collections of collections are not supported"); |
| 121 | + } else { |
| 122 | + PactDslJsonBody nestedBody = body.eachLike(fieldName); |
| 123 | + for (Object complexObject : collection) { |
| 124 | + mapComplexFieldWithoutOpeningObject(complexObject, nestedBody); |
| 125 | + } |
| 126 | + nestedBody.closeObject().closeArray(); |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + private static void mapComplexField(String fieldName, Object fieldValue, PactDslJsonBody body) throws IllegalAccessException { |
| 132 | + PactDslJsonBody nestedBody = body.object(fieldName); |
| 133 | + mapComplexFieldWithoutOpeningObject(fieldValue, nestedBody); |
| 134 | + } |
| 135 | + |
| 136 | + private static void mapComplexFieldWithoutOpeningObject(Object fieldValue, PactDslJsonBody nestedBody) throws IllegalAccessException { |
| 137 | + recursiveLike(fieldValue, nestedBody); |
| 138 | + nestedBody.closeObject(); |
| 139 | + } |
| 140 | + |
| 141 | + private static boolean isSimpleType(Class<?> type) { |
| 142 | + return SIMPLE_TYPES.contains(type); |
| 143 | + } |
| 144 | + |
| 145 | + private static boolean isCollectionType(Class<?> type) { |
| 146 | + return Collection.class.isAssignableFrom(type); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments