Skip to content

Commit

Permalink
Fake - Use ValueMetaInteger for methods that return long apache#3848
Browse files Browse the repository at this point in the history
For now ignore methods that doesn't return type (String, long or Date)
  • Loading branch information
nadment committed Apr 26, 2024
1 parent 3381c73 commit eac59e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.hop.core.Const;
import org.apache.hop.core.util.Utils;
Expand Down Expand Up @@ -200,8 +201,12 @@ public String[] getMethodNames(FakerType fakerType) {
Method[] methods = fakerType.getFakerClass().getDeclaredMethods();
ArrayList<String> tempNames = new ArrayList<>();
for (Method method : methods) {
// ignore Methods needing parameters for now
if (method.getParameterCount() == 0) {
// Ignore methods needing parameters for now
// or that doesn't return type (String, long or Date)
if (method.getParameterCount() == 0
&& (method.getReturnType().isAssignableFrom(String.class)
|| method.getReturnType().isAssignableFrom(long.class)
|| method.getReturnType().isAssignableFrom(Date.class))) {
tempNames.add(method.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

package org.apache.hop.pipeline.transforms.fake;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.hop.core.annotations.Transform;
import org.apache.hop.core.exception.HopTransformException;
import org.apache.hop.core.row.IRowMeta;
import org.apache.hop.core.row.IValueMeta;
import org.apache.hop.core.row.value.ValueMetaDate;
import org.apache.hop.core.row.value.ValueMetaInteger;
import org.apache.hop.core.row.value.ValueMetaString;
import org.apache.hop.core.variables.IVariables;
import org.apache.hop.metadata.api.HopMetadataProperty;
Expand Down Expand Up @@ -80,16 +83,36 @@ public void getFields(
IVariables variables,
IHopMetadataProvider metadataProvider)
throws HopTransformException {

for (FakeField field : fields) {
if (field.isValid()) {
IValueMeta v = null;
if (field.getType().equals("DateAndTime")) {
v = new ValueMetaDate(field.getName());
} else {
v = new ValueMetaString(field.getName());

try {
FakerType type = FakerType.valueOf(field.getType());
Method method = type.getFakerClass().getMethod(field.getTopic());
Class<?> returnType = method.getReturnType();

IValueMeta valueMeta = null;
if (returnType.isAssignableFrom(String.class)) {
valueMeta = new ValueMetaString(field.getName());
} else if (returnType.isAssignableFrom(int.class)
|| returnType.isAssignableFrom(long.class)) {
valueMeta = new ValueMetaInteger(field.getName());
} else if (returnType.isAssignableFrom(Date.class)) {
valueMeta = new ValueMetaDate(field.getName());
} else {
log.logError("Error unsupported faker return type");
}
valueMeta.setOrigin(name);
rowMeta.addValueMeta(valueMeta);
} catch (NoSuchMethodException e) {
log.logError(
"Error getting faker object or method for type "
+ field.getType()
+ " and topic "
+ field.getTopic(),
e);
}
v.setOrigin(name);
rowMeta.addValueMeta(v);
}
}
}
Expand Down

0 comments on commit eac59e0

Please sign in to comment.