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
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 25 additions & 1 deletion AdvancedFeatures.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,28 @@ 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 ensures 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 must choose a different index for their field.
This helps prevent accidental reuse of indices from deprecated fields, which could cause backward compatibility issues
when deserializing older data.

For example, if you remove a field that used index 2, you should mark index 2 as reserved to ensure
it's not accidentally reused by a new field in the future.

```java
// Indices 2 and 3 were previously used for deprecated fields 'oldStatus' and 'legacyId'
@PibifyClassMetadata(reservedIndices = {2, 3})
public class ClassWithSimpleFields {
@Pibify(1)
private String name;
@Pibify(4) // Note: Skipping indices 2 and 3 as they are reserved
private int count;
}
Comment on lines +52 to +72
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Documentation needs correction regarding validation timing

The implementation validates reserved indices during build-time (maven plugin execution) rather than compile-time as stated in the documentation. While the feature works as intended to prevent accidental reuse of indices, the timing of the validation should be accurately documented.

  • Update the documentation to state that validation occurs during the build process rather than at compile-time
  • Consider mentioning that violations are reported as build errors through the maven plugin
🔗 Analysis chain

Verify documentation matches implementation.

Let's verify that the documentation accurately reflects the implementation details:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify implementation details match documentation

# Check annotation definition
echo "Checking @PibifyClassMetadata annotation definition..."
rg -l "PibifyClassMetadata"

# Verify compile-time validation
echo "Checking validation implementation..."
rg -l "checkForUsageOfReservedIndices"

# Look for test coverage
echo "Checking test coverage..."
rg -l "testReservedIndices"

Length of output: 871


Script:

#!/bin/bash
# Check annotation definition
echo "=== PibifyClassMetadata Annotation ==="
cat pibify-core/src/main/java/com/flipkart/pibify/core/PibifyClassMetadata.java

# Check validation implementation
echo -e "\n=== Validation Implementation ==="
rg "checkForUsageOfReservedIndices" -A 10 pibify-maven-plugin/src/main/java/com/flipkart/pibify/codegen/BeanIntrospectorBasedCodeGenSpecCreator.java

# Check test implementation
echo -e "\n=== Test Implementation ==="
rg "testReservedIndices" -A 10 pibify-maven-plugin/src/test/java/com/flipkart/pibify/codegen/BeanIntrospectorBasedCodeGenSpecCreatorTest.java

Length of output: 3236

🧰 Tools
🪛 LanguageTool

[uncategorized] ~53-~53: This verb may not be in the correct form. Consider using a different form for this context.
Context: ...assMetadata` annotation on the pojo and mark the indices which should not be used. O...

(AI_EN_LECTOR_REPLACEMENT_VERB_FORM)

```
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>

```
Expand All @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion pibify-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
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
Expand Up @@ -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>

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

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -680,4 +681,18 @@ 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);
List<SpecGenLog> logs = new ArrayList<>(creator.getLogsForCurrentEntity());
logs.forEach(l -> System.out.println(l.getLogMessage()));
assertEquals(SpecGenLogLevel.ERROR, creator.status(forTest));
assertEquals(2, logs.size());
assertEquals("com.flipkart.pibify.test.data.ClassWithSimpleFields Reserved index 2 is already in use", logs.get(0).getLogMessage());
assertEquals("com.flipkart.pibify.test.data.ClassWithSimpleFields Reserved index 3 is already in use", logs.get(1).getLogMessage());
}
}
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;

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

Expand Down Expand Up @@ -112,7 +112,7 @@
<plugin>
<groupId>com.flipkart.pibify</groupId>
<artifactId>pibify-maven-plugin</artifactId>
<version>1.3</version>
<version>1.6</version>
<configuration>
<excludes>
<!-- <exclude>com/flipkart/pibify/toskip/**</exclude>-->
Expand Down
2 changes: 1 addition & 1 deletion test-environments/lombok/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 3 additions & 3 deletions test-environments/pibify-test-vanilla/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion test-environments/vertx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Loading