Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Reserved Indices #4

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion AdvancedFeatures.md
Original file line number Diff line number Diff line change
@@ -45,4 +45,19 @@ AbstractPibifyHandlerCache handlerCache = AbstractPibifyHandlerCache.getConcrete
"<<FQDN.Of.PibifyHandlerCache>>");
```

This ensure that the IDE is able to resolve the class and the code compiles without any issues.
This ensure that the IDE is able to resolve the class and the code compiles without any issues.

### Reserved Indices

If the client is deprecating a field and removing it from the codebase, it is recommended to mark the index as reserved.
This can be done by using the `@PibifyClassMetadata` annotation on the pojo and mark the indices which should not be
used.
Once this is in place, a validation step will ensure any Pojo that attempts to use a reserved index will fail during
compile time
and the client can take necessary action.

```java

@PibifyClassMetadata(reservedIndices = {2, 3})
public class ClassWithSimpleFields {
```
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ This is the class that the clients use to get an instance of Handler for the sup
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-core</artifactId>
<scope>compile</scope>
<version>1.5</version>
<version>1.6</version>
</dependency>

```
@@ -71,7 +71,7 @@ This is the class that the clients use to get an instance of Handler for the sup
<plugin>
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-maven-plugin</artifactId>
<version>1.5</version>
<version>1.6</version>
<configuration>
<excludes>
<exclude>com/flipkart/pibify/toskip/**</exclude>
2 changes: 1 addition & 1 deletion pibify-core/pom.xml
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-core</artifactId>
<packaging>jar</packaging>
<version>1.5</version>
<version>1.6</version>
<name>pibify-core</name>
<properties>
<protobuf-java.version>4.28.2</protobuf-java.version>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
*
* *Copyright [2025] [Original Author]
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package com.flipkart.pibify.core;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* This annotation can be used to provide additional metadata to Pibify at class level
* Author bageshwar.pn
* Date 15/01/25
*/
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface PibifyClassMetadata {
int[] reservedIndices() default {};
}
4 changes: 2 additions & 2 deletions pibify-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>maven-plugin</packaging>
<version>1.5</version>
<version>1.6</version>
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-maven-plugin</artifactId>

@@ -172,7 +172,7 @@
<dependency>
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-core</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>

<dependency>
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
import com.flipkart.pibify.codegen.log.SpecGenLog;
import com.flipkart.pibify.codegen.log.SpecGenLogLevel;
import com.flipkart.pibify.core.Pibify;
import com.flipkart.pibify.core.PibifyClassMetadata;
import com.pibify.shaded.com.google.common.collect.ArrayListMultimap;
import com.pibify.shaded.com.google.common.collect.Multimap;
import com.squareup.javapoet.ArrayTypeName;
@@ -375,6 +376,8 @@ private CodeGenSpec createImpl(Class<?> type) throws CodeGenException {
validateAllArgsConstructor(spec);
}

checkForUsageOfReservedIndices(type, spec);

return spec;
} catch (IntrospectionException e) {
throw new CodeGenException(e.getMessage(), e);
@@ -421,6 +424,17 @@ private void validate(Class<?> type, CodeGenSpec spec) {
checkValidConstructor(type, spec);
}

private void checkForUsageOfReservedIndices(Class<?> type, CodeGenSpec spec) {
PibifyClassMetadata meta = type.getAnnotation(PibifyClassMetadata.class);
if (meta != null) {
for (int reservedIndex : meta.reservedIndices()) {
if (spec.getFields().stream().anyMatch(f -> f.getIndex() == reservedIndex)) {
log(new CodeSpecGenLog(SpecGenLogLevel.ERROR, "Reserved index " + reservedIndex + " is already in use"));
}
}
}
}

private void checkValidConstructor(Class<?> type, CodeGenSpec spec) {

// No need to validate constructor for abstract classes
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
import com.flipkart.pibify.test.data.ClassWithNativeCollectionsOfCollections2;
import com.flipkart.pibify.test.data.ClassWithNativeFields;
import com.flipkart.pibify.test.data.ClassWithReferences;
import com.flipkart.pibify.test.data.ClassWithSimpleFields;
import com.flipkart.pibify.test.data.jsoncreator.DuplicatePropertyNames;
import com.flipkart.pibify.test.data.jsoncreator.MismatchedPropertyNames;
import com.flipkart.pibify.test.data.jsoncreator.PartialConstructor;
@@ -680,4 +681,15 @@ public void testRenamedBooleanInConstructor() throws CodeGenException {
assertEquals(SpecGenLogLevel.INFO, creator.status(forTest));
assertEquals(0, creator.getLogsForCurrentEntity().size());
}

@Test
public void testReservedIndices() throws CodeGenException {
Class<?> forTest = ClassWithSimpleFields.class;
BeanIntrospectorBasedCodeGenSpecCreator creator = new BeanIntrospectorBasedCodeGenSpecCreator();
CodeGenSpec codeGenSpec = creator.create(forTest);
assertNotNull(codeGenSpec);
creator.getLogsForCurrentEntity().forEach(l -> System.out.println(l.getLogMessage()));
assertEquals(SpecGenLogLevel.ERROR, creator.status(forTest));
assertEquals(2, creator.getLogsForCurrentEntity().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
*
* *Copyright [2025] [Original Author]
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package com.flipkart.pibify.test.data;

import com.flipkart.pibify.core.Pibify;
import com.flipkart.pibify.core.PibifyClassMetadata;

/**
* This class is used for testing reserved indicices
* Author bageshwar.pn
* Date 25/07/24
*/

@PibifyClassMetadata(reservedIndices = {2, 3})
public class ClassWithSimpleFields {

@Pibify(1)
public String aString = "1";

@Pibify(2)
public int anInt = 2;

@Pibify(3)
public long aLong = 3L;

}
2 changes: 1 addition & 1 deletion test-environments/dropwizard/test-dropwizard/pom.xml
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@
<dependency>
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-core</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>
</dependencies>

2 changes: 1 addition & 1 deletion test-environments/lombok/pom.xml
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@
<dependency>
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-core</artifactId>
<version>1.5</version>
<version>1.6</version>
<scope>compile</scope>
</dependency>
</dependencies>
6 changes: 3 additions & 3 deletions test-environments/pibify-test-vanilla/pom.xml
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@
<plugin>
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-maven-plugin</artifactId>
<version>1.5</version>
<version>1.6</version>
<configuration>
<excludes>
<exclude>com/flipkart/pibify/toskip/**</exclude>
@@ -68,7 +68,7 @@
<plugin>
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-maven-plugin</artifactId>
<version>1.5</version>
<version>1.6</version>
<configuration>
<incremental>true</incremental>
<incrementalBuildProvider>com.flipkart.pibify.mvn.GitIncrementalBuildProvider</incrementalBuildProvider>
@@ -137,7 +137,7 @@
<dependency>
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-core</artifactId>
<version>1.5</version>
<version>1.6</version>
<scope>compile</scope>
</dependency>
<dependency>
2 changes: 1 addition & 1 deletion test-environments/vertx/pom.xml
Original file line number Diff line number Diff line change
@@ -122,7 +122,7 @@
<dependency>
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-core</artifactId>
<version>1.5</version>
<version>1.6</version>
<scope>compile</scope>
</dependency>
</dependencies>
Loading