11package act .cli .meta ;
22
33import act .Destroyable ;
4+ import act .app .CliContext ;
45import act .asm .Type ;
6+ import act .cli .ascii_table .impl .CollectionASCIITableAware ;
57import act .sys .meta .InvokeType ;
68import act .sys .meta .ReturnTypeInfo ;
7- import act .util .DataView ;
9+ import act .util .FastJsonPropertyPreFilter ;
10+ import act .util .PropertyFilter ;
811import act .util .DestroyableBase ;
12+ import com .alibaba .fastjson .serializer .SerializerFeature ;
913import org .osgl .$ ;
1014import org .osgl .util .C ;
1115import org .osgl .util .E ;
1216import org .osgl .util .S ;
1317
18+ import java .util .List ;
1419import java .util .Set ;
1520
1621/**
2429 */
2530public class CommandMethodMetaInfo extends DestroyableBase {
2631
32+ /**
33+ * Define how the command method return result should
34+ * be presented
35+ */
36+ public static enum View {
37+ /**
38+ * present the result using {@link act.cli.TableView}
39+ */
40+ TABLE () {
41+ @ Override
42+ @ SuppressWarnings ("unchecked" )
43+ public void print (Object result , PropertyFilter .MetaInfo filter , CliContext context ) {
44+ if (null == filter ) {
45+ // TODO: support Table View when filter annotation is not presented
46+ TO_STRING .print (result , null , context );
47+ return ;
48+ }
49+ List dataList ;
50+ if (result instanceof Iterable ) {
51+ dataList = C .list ((Iterable ) result );
52+ } else {
53+ dataList = C .listOf (result );
54+ }
55+ context .printTable (new CollectionASCIITableAware (dataList , filter .outputFields (), filter .labels ()));
56+ }
57+ },
58+
59+ /**
60+ * Present the result using {@link act.cli.JsonView}
61+ */
62+ JSON () {
63+ @ Override
64+ public void print (Object result , PropertyFilter .MetaInfo filter , CliContext context ) {
65+ String json ;
66+ if (null == filter ) {
67+ json = com .alibaba .fastjson .JSON .toJSONString (result , SerializerFeature .PrettyFormat );
68+ } else {
69+ FastJsonPropertyPreFilter f = new FastJsonPropertyPreFilter ();
70+ f .addIncludes (filter .outputFields ());
71+ f .addExcludes (filter .excludedFields ());
72+ json = com .alibaba .fastjson .JSON .toJSONString (result , f , SerializerFeature .PrettyFormat );
73+ }
74+ // TODO: handle labels in JSON serialization
75+ context .println (json );
76+ }
77+ },
78+
79+ /**
80+ * Present the result using {@link Object#toString()}
81+ */
82+ TO_STRING () {
83+ @ Override
84+ public void print (Object result , PropertyFilter .MetaInfo filter , CliContext context ) {
85+ if (null != filter ) {
86+ // if PropertyFilter annotation presented, then by default
87+ // use the JSON view to print the result
88+ JSON .print (result , filter , context );
89+ } else {
90+ context .println (result .toString ());
91+ }
92+ }
93+ };
94+
95+ public abstract void print (Object result , PropertyFilter .MetaInfo filter , CliContext context );
96+ }
97+
2798 private String methodName ;
2899 private String commandName ;
29100 private String helpMsg ;
30101 private InvokeType invokeType ;
31102 private CommanderClassMetaInfo clsInfo ;
32- private DataView .MetaInfo dataView ;
103+ private PropertyFilter .MetaInfo dataView ;
33104 private C .List <CommandParamMetaInfo > params = C .newList ();
34105 private ReturnTypeInfo returnType ;
35106 private Set <String > optionLeads = C .newSet ();
107+ private View view = View .TO_STRING ;
36108
37109 public CommandMethodMetaInfo (CommanderClassMetaInfo clsInfo ) {
38110 this .clsInfo = $ .NPE (clsInfo );
@@ -51,6 +123,15 @@ public String methodName() {
51123 return methodName ;
52124 }
53125
126+ public CommandMethodMetaInfo view (View view ) {
127+ this .view = $ .notNull (view );
128+ return this ;
129+ }
130+
131+ public View view () {
132+ return view ;
133+ }
134+
54135 public CommandMethodMetaInfo commandName (String name ) {
55136 commandName = $ .NPE (name );
56137 return this ;
@@ -87,12 +168,12 @@ public boolean isStatic() {
87168 return InvokeType .STATIC == invokeType ;
88169 }
89170
90- public CommandMethodMetaInfo dataView (DataView .MetaInfo dataView ) {
171+ public CommandMethodMetaInfo dataView (PropertyFilter .MetaInfo dataView ) {
91172 this .dataView = dataView ;
92173 return this ;
93174 }
94175
95- public DataView .MetaInfo dataViewInfo () {
176+ public PropertyFilter .MetaInfo dataViewInfo () {
96177 return dataView ;
97178 }
98179
0 commit comments