|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.shell.samples.e2e; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.context.annotation.Configuration; |
| 24 | +import org.springframework.core.convert.converter.Converter; |
| 25 | +import org.springframework.shell.command.CommandRegistration; |
| 26 | +import org.springframework.shell.command.annotation.Command; |
| 27 | +import org.springframework.shell.command.annotation.Option; |
| 28 | +import org.springframework.shell.standard.ShellComponent; |
| 29 | +import org.springframework.shell.standard.ShellMethod; |
| 30 | +import org.springframework.shell.standard.ShellOption; |
| 31 | +import org.springframework.stereotype.Component; |
| 32 | + |
| 33 | +public class OptionConversionCommands { |
| 34 | + |
| 35 | + @ShellComponent |
| 36 | + public static class LegacyAnnotation extends BaseE2ECommands { |
| 37 | + |
| 38 | + @ShellMethod(key = LEGACY_ANNO + "option-conversion-integer", group = GROUP) |
| 39 | + public String optionConversionIntegerAnnotation( |
| 40 | + @ShellOption Integer arg1 |
| 41 | + ) { |
| 42 | + return "Hello " + arg1; |
| 43 | + } |
| 44 | + |
| 45 | + @ShellMethod(key = LEGACY_ANNO + "option-conversion-custom", group = GROUP) |
| 46 | + public String optionConversionCustomAnnotation( |
| 47 | + @ShellOption MyPojo arg1 |
| 48 | + ) { |
| 49 | + return "Hello " + arg1; |
| 50 | + } |
| 51 | + |
| 52 | + @ShellMethod(key = LEGACY_ANNO + "option-conversion-customset", group = GROUP) |
| 53 | + public String optionConversionCustomSetAnnotation( |
| 54 | + @ShellOption Set<MyPojo> arg1 |
| 55 | + ) { |
| 56 | + return "Hello " + arg1; |
| 57 | + } |
| 58 | + |
| 59 | + @ShellMethod(key = LEGACY_ANNO + "option-conversion-customarray", group = GROUP) |
| 60 | + public String optionConversionCustomArrayAnnotation( |
| 61 | + @ShellOption MyPojo[] arg1 |
| 62 | + ) { |
| 63 | + return "Hello " + Arrays.asList(arg1); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP) |
| 68 | + public static class Annotation extends BaseE2ECommands { |
| 69 | + |
| 70 | + @Command(command = "option-conversion-integer") |
| 71 | + public String optionConversionIntegerAnnotation( |
| 72 | + @Option(longNames = "arg1") |
| 73 | + Integer arg1 |
| 74 | + ) { |
| 75 | + return "Hello " + arg1; |
| 76 | + } |
| 77 | + |
| 78 | + @Command(command = "option-conversion-custom") |
| 79 | + public String optionConversionCustomAnnotation( |
| 80 | + @Option(longNames = "arg1") |
| 81 | + MyPojo arg1 |
| 82 | + ) { |
| 83 | + return "Hello " + arg1; |
| 84 | + } |
| 85 | + |
| 86 | + @Command(command = "option-conversion-customset") |
| 87 | + public String optionConversionCustomSetAnnotation( |
| 88 | + @Option(longNames = "arg1") |
| 89 | + Set<MyPojo> arg1 |
| 90 | + ) { |
| 91 | + return "Hello " + arg1; |
| 92 | + } |
| 93 | + |
| 94 | + @Command(command = "option-conversion-customarray") |
| 95 | + public String optionConversionCustomArrayAnnotation( |
| 96 | + @Option(longNames = "arg1") |
| 97 | + MyPojo[] arg1 |
| 98 | + ) { |
| 99 | + return "Hello " + Arrays.asList(arg1); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Component |
| 104 | + public static class Registration extends BaseE2ECommands { |
| 105 | + |
| 106 | + @Bean |
| 107 | + public CommandRegistration optionConversionIntegerRegistration() { |
| 108 | + return getBuilder() |
| 109 | + .command(REG, "option-conversion-integer") |
| 110 | + .group(GROUP) |
| 111 | + .withOption() |
| 112 | + .longNames("arg1") |
| 113 | + .type(Integer.class) |
| 114 | + .and() |
| 115 | + .withTarget() |
| 116 | + .function(ctx -> { |
| 117 | + Integer arg1 = ctx.getOptionValue("arg1"); |
| 118 | + return "Hello " + arg1; |
| 119 | + }) |
| 120 | + .and() |
| 121 | + .build(); |
| 122 | + } |
| 123 | + |
| 124 | + @Bean |
| 125 | + public CommandRegistration optionConversionCustomRegistration() { |
| 126 | + return getBuilder() |
| 127 | + .command(REG, "option-conversion-custom") |
| 128 | + .group(GROUP) |
| 129 | + .withOption() |
| 130 | + .longNames("arg1") |
| 131 | + .type(MyPojo.class) |
| 132 | + .and() |
| 133 | + .withTarget() |
| 134 | + .function(ctx -> { |
| 135 | + MyPojo arg1 = ctx.getOptionValue("arg1"); |
| 136 | + return "Hello " + arg1; |
| 137 | + }) |
| 138 | + .and() |
| 139 | + .build(); |
| 140 | + } |
| 141 | + |
| 142 | + @Bean |
| 143 | + public CommandRegistration optionConversionCustomSetRegistration() { |
| 144 | + return getBuilder() |
| 145 | + .command(REG, "option-conversion-customset") |
| 146 | + .group(GROUP) |
| 147 | + .withOption() |
| 148 | + .longNames("arg1") |
| 149 | + .type(Set.class) |
| 150 | + .and() |
| 151 | + .withTarget() |
| 152 | + .function(ctx -> { |
| 153 | + Set<MyPojo> arg1 = ctx.getOptionValue("arg1"); |
| 154 | + return "Hello " + arg1; |
| 155 | + }) |
| 156 | + .and() |
| 157 | + .build(); |
| 158 | + } |
| 159 | + |
| 160 | + @Bean |
| 161 | + public CommandRegistration optionConversionCustomArrayRegistration() { |
| 162 | + return getBuilder() |
| 163 | + .command(REG, "option-conversion-customarray") |
| 164 | + .group(GROUP) |
| 165 | + .withOption() |
| 166 | + .longNames("arg1") |
| 167 | + .type(MyPojo[].class) |
| 168 | + .and() |
| 169 | + .withTarget() |
| 170 | + .function(ctx -> { |
| 171 | + MyPojo[] arg1 = ctx.getOptionValue("arg1"); |
| 172 | + return "Hello " + Arrays.asList(arg1); |
| 173 | + }) |
| 174 | + .and() |
| 175 | + .build(); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @Configuration(proxyBeanMethods = false) |
| 180 | + public static class CommonConfiguration { |
| 181 | + |
| 182 | + @Bean |
| 183 | + public Converter<String, MyPojo> stringToMyPojoConverter() { |
| 184 | + return new StringToMyPojoConverter(); |
| 185 | + } |
| 186 | + |
| 187 | + @Bean |
| 188 | + public Converter<String, Set<MyPojo>> stringToMyPojoSetConverter() { |
| 189 | + return new StringToMyPojoSetConverter(); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + public static class MyPojo { |
| 194 | + private String arg; |
| 195 | + |
| 196 | + public MyPojo(String arg) { |
| 197 | + this.arg = arg; |
| 198 | + } |
| 199 | + |
| 200 | + public String getArg() { |
| 201 | + return arg; |
| 202 | + } |
| 203 | + |
| 204 | + public void setArg(String arg) { |
| 205 | + this.arg = arg; |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public String toString() { |
| 210 | + return "MyPojo [arg=" + arg + "]"; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + static class StringToMyPojoConverter implements Converter<String, MyPojo> { |
| 215 | + |
| 216 | + @Override |
| 217 | + public MyPojo convert(String from) { |
| 218 | + return new MyPojo(from); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + static class StringToMyPojoSetConverter implements Converter<String, Set<MyPojo>> { |
| 223 | + |
| 224 | + @Override |
| 225 | + public Set<MyPojo> convert(String from) { |
| 226 | + Set<MyPojo> set = new HashSet<>(); |
| 227 | + set.add(new MyPojo(from)); |
| 228 | + return set; |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments