Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
fix(#1646): add workaround method for Faker composite expression errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ro4052 committed Jul 14, 2020
1 parent d4ed854 commit dbb27e2
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.scottlogic.datahelix.generator.common.RandomNumberGenerator;
import com.scottlogic.datahelix.generator.common.util.OrderedRandom;

import java.lang.reflect.InvocationTargetException;
import java.util.stream.Stream;

public class FakerGenerator implements StringGenerator {
Expand Down Expand Up @@ -71,6 +72,24 @@ public Stream<String> generateRandomValues(RandomNumberGenerator randomNumberGen
}

private String getFakerValue(Faker faker) {
return faker.expression("#{" + this.fakerSpec + "}");
try {
return faker.expression("#{" + this.fakerSpec + "}");
} catch (Exception e) {
return getFakerValueWorkaround(faker);
}
}

private String getFakerValueWorkaround(Faker faker) {
String elements[] = this.fakerSpec.split("\\.");
Object obj = faker;
for (String element : elements) {
try {
obj = obj.getClass().getMethod(element).invoke(obj);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
}

return (String) obj;
}
}

0 comments on commit dbb27e2

Please sign in to comment.