From 863185ed42fa68ff54a496995dc42e1a8abe4d0b Mon Sep 17 00:00:00 2001 From: jruaux Date: Tue, 4 May 2021 11:25:31 -0700 Subject: [PATCH] improved faker help --- .../redislabs/riot/gen/FakerHelpCommand.java | 100 ++++++++++++------ 1 file changed, 65 insertions(+), 35 deletions(-) diff --git a/riot-gen/src/main/java/com/redislabs/riot/gen/FakerHelpCommand.java b/riot-gen/src/main/java/com/redislabs/riot/gen/FakerHelpCommand.java index f683b11b5..a8959a780 100644 --- a/riot-gen/src/main/java/com/redislabs/riot/gen/FakerHelpCommand.java +++ b/riot-gen/src/main/java/com/redislabs/riot/gen/FakerHelpCommand.java @@ -1,48 +1,78 @@ package com.redislabs.riot.gen; -import java.io.PrintWriter; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - import com.github.javafaker.Faker; - +import org.springframework.util.ClassUtils; import picocli.CommandLine; import picocli.CommandLine.Command; import picocli.CommandLine.Help; import picocli.CommandLine.IHelpCommandInitializable2; +import java.io.PrintWriter; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.*; +import java.util.stream.Stream; + @Command(name = "faker-help", header = "Displays help information about Faker", synopsisHeading = "%nUsage: ", helpCommand = true) public class FakerHelpCommand implements IHelpCommandInitializable2, Runnable { - private final static List EXCLUDES = Arrays.asList("instance", "options"); - - private PrintWriter outWriter; - - public void run() { - for (Method method : Faker.class.getDeclaredMethods()) { - if (EXCLUDES.contains(method.getName())) { - continue; - } - if (!method.getReturnType().getPackage().equals(Faker.class.getPackage())) { - continue; - } - List names = Arrays.stream(method.getReturnType().getDeclaredMethods()) - .filter(m -> m.getParameters().length == 0).map(Method::getName).collect(Collectors.toList()); - outWriter.println(String.format("%-30.30s: %s", method.getName(), String.join(" ", names))); - } - } - - public void init(CommandLine helpCommandLine, Help.ColorScheme colorScheme, PrintWriter out, PrintWriter err) { - this.outWriter = notNull(out, "outWriter"); - } - - static T notNull(T object, String description) { - if (object == null) { - throw new NullPointerException(description); - } - return object; - } + @CommandLine.Parameters(description = "Name of the Faker provider to show help for", paramLabel = "") + private String provider; + + private final static List EXCLUDES = Arrays.asList("instance", "options"); + + private PrintWriter outWriter; + + @Override + public void run() { + Stream methods = Arrays.stream(Faker.class.getDeclaredMethods()).filter(m -> !EXCLUDES.contains(m.getName())).filter(m -> m.getReturnType().getPackage().equals(Faker.class.getPackage())); + if (provider == null) { + outWriter.println("Run 'riot-gen faker-help ' for documentation on a specific Faker provider:"); + methods.sorted(Comparator.comparing(Method::getName)).forEach(p -> outWriter.println(" " + p.getName() + " " + url(ClassUtils.getShortName(p.getReturnType())))); + } else { + Optional method = methods.filter(m -> m.getName().equals(provider)).findFirst(); + if (method.isPresent()) { + Class type = method.get().getReturnType(); + String shortName = ClassUtils.getShortName(type); + outWriter.println("Available fakes: " + url(shortName)); + for (Method subMethod : type.getDeclaredMethods()) { + outWriter.println(" " + provider + "." + methodToString(subMethod)); + } + } else { + outWriter.println("No such field: " + provider); + } + } + } + + private String url(String name) { + return "http://dius.github.io/java-faker/apidocs/com/github/javafaker/" + name + ".html"; + } + + private String methodToString(Method method) { + String result = method.getName(); + Parameter[] parameters = method.getParameters(); + if (parameters.length == 0) { + return result; + } + result += "("; + List paramNames = new ArrayList<>(); + for (Parameter parameter : parameters) { + paramNames.add(ClassUtils.getShortName(parameter.getType())); + } + result += String.join(",", paramNames); + result += ")"; + return result; + } + + public void init(CommandLine helpCommandLine, Help.ColorScheme colorScheme, PrintWriter out, PrintWriter err) { + this.outWriter = notNull(out, "outWriter"); + } + + static T notNull(T object, String description) { + if (object == null) { + throw new NullPointerException(description); + } + return object; + } }