Skip to content

Commit

Permalink
Fake - Use ValueMetaInteger for methods that return long #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 6925d9f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 41 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,38 @@ 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(long.class)) {
valueMeta = new ValueMetaInteger(field.getName());
} else if (returnType.isAssignableFrom(Date.class)) {
valueMeta = new ValueMetaDate(field.getName());
}

if (valueMeta != null) {
valueMeta.setOrigin(name);
rowMeta.addValueMeta(valueMeta);
} else {
log.logError("Error unsupported faker return type");
}
} 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
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->
<pipeline>
<info>
Expand All @@ -34,30 +30,28 @@ limitations under the License.
<created_date>2021/04/28 15:29:02.042</created_date>
<modified_user>-</modified_user>
<modified_date>2021/04/28 15:29:02.042</modified_date>
<key_for_session_key>H4sIAAAAAAAAAAMAAAAAAAAAAAA=</key_for_session_key>
<is_key_private>N</is_key_private>
</info>
<notepads>
<notepad>
<note>Fake Data transform generates data based on categories
</note>
<xloc>80</xloc>
<yloc>48</yloc>
<width>299</width>
<heigth>41</heigth>
<fontname>Segoe UI</fontname>
<fontsize>11</fontsize>
<fontbold>N</fontbold>
<fontitalic>N</fontitalic>
<fontcolorred>14</fontcolorred>
<fontcolorgreen>58</fontcolorgreen>
<fontcolorblue>90</fontcolorblue>
<backgroundcolorred>201</backgroundcolorred>
<backgroundcolorgreen>232</backgroundcolorgreen>
<backgroundcolorblue>251</backgroundcolorblue>
<bordercolorred>14</bordercolorred>
<bordercolorgreen>58</bordercolorgreen>
<backgroundcolorgreen>232</backgroundcolorgreen>
<backgroundcolorred>201</backgroundcolorred>
<bordercolorblue>90</bordercolorblue>
<bordercolorgreen>58</bordercolorgreen>
<bordercolorred>14</bordercolorred>
<fontbold>N</fontbold>
<fontcolorblue>90</fontcolorblue>
<fontcolorgreen>58</fontcolorgreen>
<fontcolorred>14</fontcolorred>
<fontitalic>N</fontitalic>
<fontname>Segoe UI</fontname>
<fontsize>11</fontsize>
<height>41</height>
<xloc>80</xloc>
<yloc>48</yloc>
<note>Fake Data transform generates data based on categories
</note>
<width>305</width>
</notepad>
</notepads>
<order>
Expand Down Expand Up @@ -87,8 +81,8 @@ limitations under the License.
</fields>
<interval_in_ms>5000</interval_in_ms>
<last_time_field>FiveSecondsAgo</last_time_field>
<never_ending>N</never_ending>
<limit>1000</limit>
<never_ending>N</never_ending>
<row_time_field>now</row_time_field>
<attributes/>
<GUI>
Expand All @@ -107,44 +101,59 @@ limitations under the License.
<method>none</method>
<schema_name/>
</partitioning>
<locale>en</locale>
<fields>
<field>
<name>fullName</name>
<type>Name</type>
<topic>fullName</topic>
<type>Name</type>
</field>
<field>
<name>Birthday</name>
<topic>birthday</topic>
<type>DateAndTime</type>
</field>
<field>
<name>bloodGroup</name>
<type>Name</type>
<topic>bloodGroup</topic>
<type>Name</type>
</field>
<field>
<name>creditCardNumber</name>
<type>Business</type>
<topic>creditCardNumber</topic>
<type>Business</type>
</field>
<field>
<name>cellNumber</name>
<type>PhoneNumber</type>
<topic>cellPhone</topic>
<type>PhoneNumber</type>
</field>
<field>
<name>favouriteBook</name>
<type>Book</type>
<topic>title</topic>
<type>Book</type>
</field>
<field>
<name>workplace</name>
<type>Company</type>
<topic>name</topic>
<type>Company</type>
</field>
<field>
<name>programmingLanguage</name>
<type>ProgrammingLanguage</type>
<topic>name</topic>
<type>ProgrammingLanguage</type>
</field>
<field>
<name>Digit</name>
<topic>digit</topic>
<type>Number</type>
</field>
<field>
<name>Number</name>
<topic>randomNumber</topic>
<type>Number</type>
</field>
</fields>
<locale>en</locale>
<attributes/>
<GUI>
<xloc>288</xloc>
Expand Down

0 comments on commit 6925d9f

Please sign in to comment.