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

di-support #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/main/java/io/airlift/airline/Accessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import io.airlift.airline.factory.DefaultConstructorFactory;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
Expand Down Expand Up @@ -77,7 +78,7 @@ public Object getValue(Object instance)
try {
Object nextInstance = intermediateField.get(instance);
if (nextInstance == null) {
nextInstance = ParserUtil.createInstance(intermediateField.getType());
nextInstance = new DefaultConstructorFactory().createInstance(intermediateField.getType());
intermediateField.set(instance, nextInstance);
}
instance = nextInstance;
Expand Down
56 changes: 33 additions & 23 deletions src/main/java/io/airlift/airline/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,22 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import io.airlift.airline.model.ArgumentsMetadata;
import io.airlift.airline.model.CommandGroupMetadata;
import io.airlift.airline.model.CommandMetadata;
import io.airlift.airline.model.GlobalMetadata;
import io.airlift.airline.model.MetadataLoader;
import io.airlift.airline.model.OptionMetadata;
import io.airlift.airline.factory.CommandFactory;
import io.airlift.airline.factory.DefaultConstructorFactory;
import io.airlift.airline.model.*;

import java.util.List;
import java.util.Map;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;
import static io.airlift.airline.ParserUtil.createInstance;
import static io.airlift.airline.ParserUtil.configureInstance;

public class Cli<C>
{
public static <T> CliBuilder<T> builder(String name)
private final CommandFactory commandFactory;

public static <T> CliBuilder<T> builder(String name)
{
Preconditions.checkNotNull(name, "name is null");
return new CliBuilder<T>(name);
Expand All @@ -64,9 +63,11 @@ private Cli(String name,
TypeConverter typeConverter,
Class<? extends C> defaultCommand,
Iterable<Class<? extends C>> defaultGroupCommands,
Iterable<GroupBuilder<C>> groups)
Iterable<GroupBuilder<C>> groups,
CommandFactory commandFactory)
{
Preconditions.checkNotNull(name, "name is null");
this.commandFactory = commandFactory;
Preconditions.checkNotNull(name, "name is null");
Preconditions.checkNotNull(typeConverter, "typeConverter is null");

CommandMetadata defaultCommandMetadata = null;
Expand Down Expand Up @@ -117,13 +118,14 @@ public C parse(Iterable<String> args)

CommandMetadata command = state.getCommand();

return createInstance(command.getType(),
command.getAllOptions(),
state.getParsedOptions(),
command.getArguments(),
state.getParsedArguments(),
command.getMetadataInjections(),
ImmutableMap.<Class<?>, Object>of(GlobalMetadata.class, metadata));
C instance = (C) commandFactory.createInstance(command.getType());
return configureInstance(instance,
command.getAllOptions(),
state.getParsedOptions(),
command.getArguments(),
state.getParsedArguments(),
command.getMetadataInjections(),
ImmutableMap.<Class<?>, Object>of(GlobalMetadata.class, metadata));
}

private void validate(ParseState state)
Expand Down Expand Up @@ -168,6 +170,7 @@ public static class CliBuilder<C>
protected final String name;
protected String description;
protected TypeConverter typeConverter = new TypeConverter();
protected CommandFactory commandFactory = new DefaultConstructorFactory();
protected String optionSeparators;
private Class<? extends C> defaultCommand;
private final List<Class<? extends C>> defaultCommandGroupCommands = newArrayList();
Expand Down Expand Up @@ -202,11 +205,18 @@ public CliBuilder<C> withDescription(String description)
// return this;
// }

public CliBuilder<C> withDefaultCommand(Class<? extends C> defaultCommand)
{
this.defaultCommand = defaultCommand;
return this;
}

public CliBuilder<C> withCommandFactory(CommandFactory commandFactory)
{
this.commandFactory = commandFactory;
return this;
}

public CliBuilder<C> withDefaultCommand(Class<? extends C> defaultCommand)
{
this.defaultCommand = defaultCommand;
return this;
}

public CliBuilder<C> withCommand(Class<? extends C> command)
{
Expand Down Expand Up @@ -243,7 +253,7 @@ public GroupBuilder<C> withGroup(String name)

public Cli<C> build()
{
return new Cli<C>(name, description, typeConverter, defaultCommand, defaultCommandGroupCommands, groups.values());
return new Cli<C>(name, description, typeConverter, defaultCommand, defaultCommandGroupCommands, groups.values(), commandFactory);
}
}

Expand Down
30 changes: 7 additions & 23 deletions src/main/java/io/airlift/airline/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,14 @@

public class ParserUtil
{
public static <T> T createInstance(Class<T> type)
public static <T> T configureInstance(T commandInstance,
Iterable<OptionMetadata> options,
ListMultimap<OptionMetadata, Object> parsedOptions,
ArgumentsMetadata arguments,
Iterable<Object> parsedArguments,
Iterable<Accessor> metadataInjection,
Map<Class<?>, Object> bindings)
{
if (type != null) {
try {
return type.getConstructor().newInstance();
}
catch (Exception e) {
throw new ParseException(e, "Unable to create instance %s", type.getName());
}
}
return null;
}

public static <T> T createInstance(Class<?> type,
Iterable<OptionMetadata> options,
ListMultimap<OptionMetadata, Object> parsedOptions,
ArgumentsMetadata arguments,
Iterable<Object> parsedArguments,
Iterable<Accessor> metadataInjection,
Map<Class<?>, Object> bindings)
{
// create the command instance
T commandInstance = (T) ParserUtil.createInstance(type);

// inject options
for (OptionMetadata option : options) {
List<?> values = parsedOptions.get(option);
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/io/airlift/airline/SingleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.airlift.airline.factory.CommandFactory;
import io.airlift.airline.factory.DefaultConstructorFactory;
import io.airlift.airline.model.ArgumentsMetadata;
import io.airlift.airline.model.CommandMetadata;
import io.airlift.airline.model.MetadataLoader;
Expand All @@ -28,7 +30,7 @@
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.airlift.airline.ParserUtil.createInstance;
import static io.airlift.airline.ParserUtil.configureInstance;

public class SingleCommand<C>
{
Expand Down Expand Up @@ -66,13 +68,15 @@ public C parse(Iterable<String> args)

CommandMetadata command = state.getCommand();

return createInstance(command.getType(),
command.getAllOptions(),
state.getParsedOptions(),
command.getArguments(),
state.getParsedArguments(),
command.getMetadataInjections(),
ImmutableMap.<Class<?>, Object>of(CommandMetadata.class, commandMetadata));
CommandFactory commandFactory = new DefaultConstructorFactory();
final C instance = (C) commandFactory.createInstance(command.getType());
return configureInstance(instance,
command.getAllOptions(),
state.getParsedOptions(),
command.getArguments(),
state.getParsedArguments(),
command.getMetadataInjections(),
ImmutableMap.<Class<?>, Object>of(CommandMetadata.class, commandMetadata));
}

private void validate(ParseState state)
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/io/airlift/airline/SuggestCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.airlift.airline.factory.CommandFactory;
import io.airlift.airline.factory.DefaultConstructorFactory;
import io.airlift.airline.model.CommandGroupMetadata;
import io.airlift.airline.model.CommandMetadata;
import io.airlift.airline.model.GlobalMetadata;
Expand All @@ -17,7 +19,7 @@
import java.util.concurrent.Callable;

import static com.google.common.collect.Lists.newArrayList;
import static io.airlift.airline.ParserUtil.createInstance;
import static io.airlift.airline.ParserUtil.configureInstance;

@Command(name = "suggest")
public class SuggestCommand
Expand Down Expand Up @@ -57,13 +59,15 @@ public Iterable<String> generateSuggestions()
bindings.put(CommandMetadata.class, state.getCommand());
}

Suggester suggester = createInstance(suggesterMetadata.getSuggesterClass(),
ImmutableList.<OptionMetadata>of(),
null,
null,
null,
suggesterMetadata.getMetadataInjections(),
bindings.build());
CommandFactory commandFactory = new DefaultConstructorFactory();
final Suggester instance = commandFactory.createInstance(suggesterMetadata.getSuggesterClass());
Suggester suggester = configureInstance(instance,
ImmutableList.<OptionMetadata>of(),
null,
null,
null,
suggesterMetadata.getMetadataInjections(),
bindings.build());

return suggester.suggest();
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/airlift/airline/factory/CommandFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.airlift.airline.factory;

public interface CommandFactory {
<T> T createInstance(Class<T> instanceClass);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.airlift.airline.factory;

import io.airlift.airline.ParseException;

public class DefaultConstructorFactory implements CommandFactory {
@Override
public <T> T createInstance(Class<T> type) {
if (type != null) {
try {
return type.getConstructor().newInstance();
}
catch (Exception e) {
throw new ParseException(e, "Unable to create instance %s", type.getName());
}
}
return null;
}
}