|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.java.core.schema; |
| 7 | + |
| 8 | +import java.math.BigDecimal; |
| 9 | +import java.math.BigInteger; |
| 10 | +import java.math.RoundingMode; |
| 11 | +import java.nio.ByteBuffer; |
| 12 | +import java.time.Instant; |
| 13 | +import java.util.HexFormat; |
| 14 | +import java.util.concurrent.ThreadLocalRandom; |
| 15 | +import java.util.function.Predicate; |
| 16 | +import java.util.stream.IntStream; |
| 17 | +import software.amazon.smithy.java.core.serde.InterceptingSerializer; |
| 18 | +import software.amazon.smithy.java.core.serde.ShapeDeserializer; |
| 19 | +import software.amazon.smithy.java.core.serde.ShapeSerializer; |
| 20 | +import software.amazon.smithy.java.core.serde.document.Document; |
| 21 | +import software.amazon.smithy.java.io.ByteBufferUtils; |
| 22 | +import software.amazon.smithy.model.shapes.ShapeType; |
| 23 | + |
| 24 | +public class ShapeUtils { |
| 25 | + |
| 26 | + private ShapeUtils() { |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Create a serializable struct that only serializes members that pass the given predicate. |
| 32 | + * |
| 33 | + * @param schema Structure schema. |
| 34 | + * @param struct Struct to serialize. |
| 35 | + * @param memberPredicate Predicate that takes a member schema. |
| 36 | + * @return the filtered struct. |
| 37 | + */ |
| 38 | + public static SerializableStruct withFilteredMembers( |
| 39 | + Schema schema, |
| 40 | + SerializableStruct struct, |
| 41 | + Predicate<Schema> memberPredicate |
| 42 | + ) { |
| 43 | + return new SerializableStruct() { |
| 44 | + @Override |
| 45 | + public Schema schema() { |
| 46 | + return schema; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void serializeMembers(ShapeSerializer serializer) { |
| 51 | + struct.serializeMembers(new InterceptingSerializer() { |
| 52 | + @Override |
| 53 | + protected ShapeSerializer before(Schema schema) { |
| 54 | + return memberPredicate.test(schema) ? serializer : ShapeSerializer.nullSerializer(); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public <T> T getMemberValue(Schema member) { |
| 61 | + return memberPredicate.test(schema()) ? struct.getMemberValue(member) : null; |
| 62 | + } |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Attempts to copy the values from a struct into a shape builder. |
| 68 | + * |
| 69 | + * @param source The shape to copy from. |
| 70 | + * @param sink The builder to copy into. |
| 71 | + * @throws IllegalArgumentException if the two shapes are incompatible and don't use the same schemas. |
| 72 | + */ |
| 73 | + public static void copyShape(SerializableStruct source, ShapeBuilder<?> sink) { |
| 74 | + for (var member : source.schema().members()) { |
| 75 | + var value = source.getMemberValue(member); |
| 76 | + if (value != null) { |
| 77 | + sink.setMemberValue(member, value); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Generates a random instance of a shape using the provided builder. |
| 84 | + * |
| 85 | + * <p>This method populates the shape with randomly generated values for all its members. |
| 86 | + * Required members are always populated, while optional members have a 50% chance of being set. |
| 87 | + * For union types, exactly one member is randomly selected and populated. |
| 88 | + * |
| 89 | + * <p>This is useful for fuzz testing and generating test data. |
| 90 | + * |
| 91 | + * @param shapeBuilder The builder to use for constructing the shape. |
| 92 | + * @param <T> The type of shape to generate. |
| 93 | + * @return A randomly generated instance of the shape. |
| 94 | + */ |
| 95 | + public static <T extends SerializableShape> T generateRandom(ShapeBuilder<T> shapeBuilder) { |
| 96 | + return shapeBuilder.deserialize(new RandomShapeGenerator()).build(); |
| 97 | + } |
| 98 | + |
| 99 | + private static final class RandomShapeGenerator implements ShapeDeserializer { |
| 100 | + @Override |
| 101 | + public boolean readBoolean(Schema schema) { |
| 102 | + return random().nextBoolean(); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public ByteBuffer readBlob(Schema schema) { |
| 107 | + var bytes = new byte[randomInt(100)]; |
| 108 | + random().nextBytes(bytes); |
| 109 | + return ByteBuffer.wrap(bytes); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public byte readByte(Schema schema) { |
| 114 | + return (byte) random().nextInt(Byte.MIN_VALUE, Byte.MAX_VALUE + 1); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public short readShort(Schema schema) { |
| 119 | + return (short) random().nextInt(Short.MIN_VALUE, Short.MAX_VALUE + 1); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public int readInteger(Schema schema) { |
| 124 | + return random().nextInt(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public long readLong(Schema schema) { |
| 129 | + return random().nextLong(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public float readFloat(Schema schema) { |
| 134 | + return random().nextFloat(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public double readDouble(Schema schema) { |
| 139 | + return random().nextDouble(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public BigInteger readBigInteger(Schema schema) { |
| 144 | + int times = randomInt(3); |
| 145 | + var bigInt = BigInteger.valueOf(random().nextLong()); |
| 146 | + for (int i = 0; i < times; i++) { |
| 147 | + bigInt = bigInt.multiply(BigInteger.valueOf(random().nextLong(Long.MAX_VALUE - 100, Long.MAX_VALUE))); |
| 148 | + } |
| 149 | + return bigInt; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public BigDecimal readBigDecimal(Schema schema) { |
| 154 | + var bigDecimal = new BigDecimal(readBigInteger(schema)); |
| 155 | + int times = randomInt(); |
| 156 | + while (times-- > 0) { |
| 157 | + bigDecimal = bigDecimal.divide(new BigDecimal(readBigInteger(schema)), RoundingMode.CEILING); |
| 158 | + } |
| 159 | + return bigDecimal; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String readString(Schema schema) { |
| 164 | + return HexFormat.of().formatHex(ByteBufferUtils.getBytes(readBlob(schema))); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public Document readDocument() { |
| 169 | + //TODO add more shapes. |
| 170 | + return Document.of(readString(null)); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public Instant readTimestamp(Schema schema) { |
| 175 | + return Instant.now(); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public <T> void readStruct(Schema schema, T state, StructMemberConsumer<T> consumer) { |
| 180 | + if (schema.type().isShapeType(ShapeType.UNION)) { |
| 181 | + var member = schema.members().get(randomInt(schema.members().size())); |
| 182 | + consumer.accept(state, member, this); |
| 183 | + } else { |
| 184 | + for (var member : schema.members()) { |
| 185 | + if (member.hasTrait(TraitKey.REQUIRED_TRAIT) || random().nextBoolean()) { |
| 186 | + consumer.accept(state, member, this); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public <T> void readList(Schema schema, T state, ListMemberConsumer<T> consumer) { |
| 194 | + IntStream.range(0, randomInt()).forEach(i -> consumer.accept(state, this)); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public <T> void readStringMap(Schema schema, T state, MapMemberConsumer<String, T> consumer) { |
| 199 | + IntStream.range(0, randomInt()).forEach(i -> consumer.accept(state, readString(schema), this)); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public boolean isNull() { |
| 204 | + return random().nextBoolean(); |
| 205 | + } |
| 206 | + |
| 207 | + private static ThreadLocalRandom random() { |
| 208 | + return ThreadLocalRandom.current(); |
| 209 | + } |
| 210 | + |
| 211 | + private static int randomInt() { |
| 212 | + return random().nextInt(10); |
| 213 | + } |
| 214 | + |
| 215 | + private static int randomInt(int bound) { |
| 216 | + return random().nextInt(bound); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
0 commit comments