/** * Registers a delegate. Returns true if registration is successful. */ public <T> boolean registerDelegate(Delegate<T> delegate) { return registerDelegate(delegate.typeClass().getName(), delegate); }
@Override protected EnumIO<?> resolveEnumFrom(Input input) throws IOException { return getEnumIO(input.readString(), true); }
@Override protected <T> HasSchema<T> tryWritePojoIdTo(Output output, int fieldNumber, Class<T> clazz, boolean registered) throws IOException { HasSchema<T> hs = getSchemaWrapper(clazz, false); if (hs == null || (registered && hs instanceof Lazy<?>)) return null; output.writeString(fieldNumber, clazz.getName(), false); return hs; }
@Override protected Class<?> resolveClassFrom(Input input, boolean mapped, boolean array) throws IOException { return resolveClass(input.readString()); }
/** * Returns true if this there is no existing one or the same schema has already been registered (this must be done * on application startup). * <p> * NOTE: This is only supported when {@link RuntimeEnv#ID_STRATEGY} is {@link DefaultIdStrategy}. */ public static <T> boolean register(Class<T> typeClass, Schema<T> schema) { if (ID_STRATEGY instanceof DefaultIdStrategy) return ((DefaultIdStrategy) ID_STRATEGY).registerPojo(typeClass, schema); throw new RuntimeException( "RuntimeSchema.register is only supported on DefaultIdStrategy"); }
/** * Maps the {@code baseClass} to a specific non-interface/non-abstract {@code typeClass} and registers it (this must * be done on application startup). * <p> * With this approach, there is no overhead of writing the type metadata if a {@code baseClass} field is serialized. * <p> * Returns true if the baseClass does not exist. * <p> * NOTE: This is only supported when {@link RuntimeEnv#ID_STRATEGY} is {@link DefaultIdStrategy}. * * @throws IllegalArgumentException * if the {@code typeClass} is an interface or an abstract class. */ public static <T> boolean map(Class<? super T> baseClass, Class<T> typeClass) { if (ID_STRATEGY instanceof DefaultIdStrategy) return ((DefaultIdStrategy) ID_STRATEGY).map(baseClass, typeClass); throw new RuntimeException( "RuntimeSchema.map is only supported on DefaultIdStrategy"); }
public void testIt() throws Exception { // contains all fields DefaultIdStrategy primary = new DefaultIdStrategy(); DefaultIdStrategy g1 = new DefaultIdStrategy(primary, GROUP1); DefaultIdStrategy g2 = new DefaultIdStrategy(primary, GROUP2); DefaultIdStrategy g3 = new DefaultIdStrategy(primary, GROUP3); verifyPrimary(primary); verifyGroup1(g1); verifyGroup2(g2); verifyGroup3(g3); }
@Override protected Class<?> resolveArrayComponentTypeFrom(Input input, boolean mapped) throws IOException { return resolveClass(input.readString()); }
/** * Returns true if this there is no existing one or the same schema has already been registered (this must be done * on application startup). * <p> * NOTE: This is only supported when {@link RuntimeEnv#ID_STRATEGY} is {@link DefaultIdStrategy}. */ public static <T> boolean register(Class<T> typeClass) { if (ID_STRATEGY instanceof DefaultIdStrategy) return ((DefaultIdStrategy) ID_STRATEGY).registerPojo(typeClass); throw new RuntimeException( "RuntimeSchema.register is only supported on DefaultIdStrategy"); }
/** * Maps the {@code baseClass} to a specific non-interface/non-abstract {@code typeClass} and registers it (this must * be done on application startup). * <p> * With this approach, there is no overhead of writing the type metadata if a {@code baseClass} field is serialized. * <p> * Returns true if the baseClass does not exist. * <p> * NOTE: This is only supported when {@link RuntimeEnv#ID_STRATEGY} is {@link DefaultIdStrategy}. * * @throws IllegalArgumentException * if the {@code typeClass} is an interface or an abstract class. */ public static <T> boolean map(Class<? super T> baseClass, Class<T> typeClass) { if (ID_STRATEGY instanceof DefaultIdStrategy) return ((DefaultIdStrategy) ID_STRATEGY).map(baseClass, typeClass); throw new RuntimeException( "RuntimeSchema.map is only supported on DefaultIdStrategy"); }
/** * Registers a delegate. Returns true if registration is successful. */ public <T> boolean registerDelegate(Delegate<T> delegate) { return registerDelegate(delegate.typeClass().getName(), delegate); }
@Override protected <T> HasSchema<T> tryWritePojoIdTo(Output output, int fieldNumber, Class<T> clazz, boolean registered) throws IOException { HasSchema<T> hs = getSchemaWrapper(clazz, false); if (hs == null || (registered && hs instanceof Lazy<?>)) return null; output.writeString(fieldNumber, clazz.getName(), false); return hs; }
@Override protected Class<?> resolveArrayComponentTypeFrom(Input input, boolean mapped) throws IOException { return resolveClass(input.readString()); }
/** * Returns true if this there is no existing one or the same schema has already been registered (this must be done * on application startup). * <p> * NOTE: This is only supported when {@link RuntimeEnv#ID_STRATEGY} is {@link DefaultIdStrategy}. */ public static <T> boolean register(Class<T> typeClass) { if (ID_STRATEGY instanceof DefaultIdStrategy) return ((DefaultIdStrategy) ID_STRATEGY).registerPojo(typeClass); throw new RuntimeException( "RuntimeSchema.register is only supported on DefaultIdStrategy"); }
@Override protected EnumIO<?> resolveEnumFrom(Input input) throws IOException { return getEnumIO(input.readString(), true); }
protected static void initProtobufObjectCodec() { ((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY).registerDelegate(new Delegate<Object>() { @Override public FieldType getFieldType() { return FieldType.BYTES; } @Override public Object readFrom(Input input) throws IOException { return JsonUtils.readValue(input.readByteArray(), Object.class); } @Override public void writeTo(Output output, int number, Object value, boolean repeated) throws IOException { output.writeByteArray(number, JsonUtils.writeValueAsBytes(value), false); } @Override public void transfer(Pipe pipe, Input input, Output output, int number, boolean repeated) { throw new IllegalStateException("not support."); } @Override public Class<?> typeClass() { return Object.class; } }); }
@Override protected <T> HasSchema<T> writePojoIdTo(Output output, int fieldNumber, Class<T> clazz) throws IOException { output.writeString(fieldNumber, clazz.getName(), false); // it is important to return the schema initialized (if it hasn't been). return getSchemaWrapper(clazz, true); }
@Override protected Class<?> resolveClassFrom(Input input, boolean mapped, boolean array) throws IOException { return resolveClass(input.readString()); }
/** * Returns true if this there is no existing one or the same schema has already been registered (this must be done * on application startup). * <p> * NOTE: This is only supported when {@link RuntimeEnv#ID_STRATEGY} is {@link DefaultIdStrategy}. */ public static <T> boolean register(Class<T> typeClass, Schema<T> schema) { if (ID_STRATEGY instanceof DefaultIdStrategy) return ((DefaultIdStrategy) ID_STRATEGY).registerPojo(typeClass, schema); throw new RuntimeException( "RuntimeSchema.register is only supported on DefaultIdStrategy"); }
public void testPojoWithImmutableListAsDelegate() throws Exception { ImmutableListAsDelegate delegate = null; if (RuntimeEnv.ID_STRATEGY instanceof DefaultIdStrategy) { if (!((DefaultIdStrategy) RuntimeEnv.ID_STRATEGY) .registerDelegate(ImmutableList.class.getName(), delegate = new ImmutableListAsDelegate(new CollectionSchemaForBaz()))) { // couldn't register delegate = null; } } Schema<PojoWithImmutableListAsDelegate> schema = RuntimeSchema .getSchema(PojoWithImmutableListAsDelegate.class); Pipe.Schema<PojoWithImmutableListAsDelegate> pipeSchema = ((RuntimeSchema<PojoWithImmutableListAsDelegate>) schema) .getPipeSchema(); PojoWithImmutableListAsDelegate p = new PojoWithImmutableListAsDelegate().fill(); byte[] data = toByteArray(p, schema); PojoWithImmutableListAsDelegate pFromByteArray = new PojoWithImmutableListAsDelegate(); mergeFrom(data, 0, data.length, pFromByteArray, schema); assertEquals(p, pFromByteArray); PojoWithImmutableListAsDelegate pFromStream = new PojoWithImmutableListAsDelegate(); ByteArrayInputStream in = new ByteArrayInputStream(data); mergeFrom(in, pFromStream, schema); assertEquals(p, pFromStream); roundTrip(p, schema, pipeSchema); }