Skip to content

使 ArgumentsRunner 支持参数绑定 #1

Open

Description

@LamGC

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

Activity

  1. added
    enhancement
    question
    on May 7, 2020
  2. self-assigned this
    on May 7, 2020
  3. LamGC commented on May 7, 2020

    @LamGC
    OwnerAuthor

    给这个功能举个例子:

    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,不是很好);
  4. LamGC commented on Jul 15, 2020

    @LamGC
    OwnerAuthor

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

  5. added this to the v1.4.0 milestone on Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementquestion

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions