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

使 ArgumentsRunner 支持参数绑定 #1

Open
LamGC opened this issue May 7, 2020 · 2 comments
Open

使 ArgumentsRunner 支持参数绑定 #1

LamGC opened this issue May 7, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request question Further information is requested
Milestone

Comments

@LamGC
Copy link
Owner

LamGC commented May 7, 2020

尝试给 ArgumentsRunner 增加对未标注 @Argument 注解的参数,或标注了特定注解的参数注入对象的功能,类似于 Spring 的参数绑定一样。
详细计划的细节稍后补充。

@LamGC LamGC added enhancement New feature or request question Further information is requested labels May 7, 2020
@LamGC LamGC self-assigned this May 7, 2020
@LamGC
Copy link
Owner Author

LamGC commented May 7, 2020

给这个功能举个例子:

public class MainCommand {

    @Command()
    public int getRandomNumber(@Argument(name = "max") int max, Random random) {
        if(random == null) {
            random = new Random();
        }

        return random.nextInt(max);
    }
}

random是一个没有类型对应StringParameterParser,也没有@Argument注解的参数,但ArgumentsRunner的调用方在调用run方法的时候可以传递一组对象进去,例如:

Set<Object> objectSet = new HashSet<>();
objectSet.add(new Random());
new ArgumentsRunner(MainCommand.class).run(new MainCommand(), objectSet);

这样 MainCommand.getRandomNumber 方法的random参数就不会为null,而是传入了objectSet里的Random对象。
这个设计有一些问题或前提要求:

  • 一种类型只能有一个对象,可能需要自定义一个限定每个类型只能有一个对象的Set;
  • 可能需要一个新的注解以标注需要绑定的参数(或者引用Spring里的@Autowired?但是这样会要导入Spring,不是很好);

@LamGC
Copy link
Owner Author

LamGC commented Jul 15, 2020

或许可以仅限制generateParamListByFlag返回的List长度满足带@Argument注解参数的数量即可,然后再进行补充?
如果可以的话,打算先让ArgumentsRunner内的参数解析部分模块化后再这么做(如果确定要这么做的话)。

@LamGC LamGC added this to the v1.4.0 milestone Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant