Github user kishorvpatil commented on a diff in the pull request:

    https://github.com/apache/storm/pull/2882#discussion_r227061218
  
    --- Diff: storm-core/src/jvm/org/apache/storm/command/AdminCommands.java ---
    @@ -104,6 +115,164 @@ public void printCliHelp(String command, PrintStream 
out) {
             }
         }
     
    +    /**
    +     * Print value in a human readable format.
    +     * @param value what to print.
    +     * @return a human readable string
    +     */
    +    public static String prettyPrint(TBase value) {
    +        StringBuilder builder = new StringBuilder();
    +        prettyPrint(value, 0, builder);
    +        return builder.toString();
    +    }
    +
    +    private static void println(StringBuilder out, int depth, Object 
value) {
    +        for (int i = 0; i < depth; i++) {
    +            out.append("\t");
    +        }
    +        out.append(value);
    +        out.append("\n");
    +    }
    +
    +    private static void prettyPrint(TBase value, int depth, StringBuilder 
out) {
    +        if (value == null) {
    +            println(out, depth,"null");
    +            return;
    +        }
    +        println(out, depth, "{");
    +        prettyPrintFields(value, depth + 1, out);
    +        println(out, depth, "}");
    +    }
    +
    +    private static void prettyPrintFields(TBase value, int depth, 
StringBuilder out) {
    +        for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> entry : 
FieldMetaData.getStructMetaDataMap(value.getClass()).entrySet()) {
    +            TFieldIdEnum key = entry.getKey();
    +            if (!value.isSet(key)) {
    +                println(out, depth, key.getFieldName() + ": not set");
    +            } else {
    +                Object o = value.getFieldValue(key);
    +                prettyPrintKeyValue(key.getFieldName(), o, depth, out);
    +            }
    +        }
    +    }
    +
    +    private static String keyStr(String key) {
    +        return key == null ? "" : (key + ": ");
    +    }
    +
    +    private static void prettyPrintKeyValue(String key, Object o, int 
depth, StringBuilder out) {
    +        //Special cases for storm...
    +        if ("json_conf".equals(key) && o instanceof String) {
    +            try {
    +                o = Utils.parseJson((String)o);
    +            } catch (Exception e) {
    +                LOG.error("Could not parse json_conf as JSON", e);
    +            }
    +        }
    +        if (o instanceof TBase) {
    +            println(out, depth, keyStr(key) + "{");
    +            prettyPrintFields((TBase) o, depth + 1, out);
    +            println(out, depth, "}");
    +        } else if (o instanceof Map) {
    +            println(out, depth, keyStr(key) + "{");
    +            for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) 
o).entrySet()) {
    +                prettyPrintKeyValue(entry.getKey().toString(), 
entry.getValue(), depth + 1, out);
    +            }
    +            println(out, depth, "}");
    +        } else if (o instanceof Collection) {
    +            println(out, depth, keyStr(key) + "[");
    +            for (Object sub: (Collection)o) {
    +                prettyPrintKeyValue(null, sub, depth + 1, out);
    +            }
    +            println(out, depth, "]");
    +        } else if (o instanceof String) {
    +            println(out, depth, keyStr(key) + "\"" + o + "\"");
    +        } else {
    +            println(out, depth, keyStr(key) + o);
    +        }
    +    }
    +
    +    private static class PrintTopo implements AdminCommand {
    +
    +        @Override
    +        public void run(String[] args, Map<String, Object> conf, String 
command) throws Exception {
    +            for (String arg: args) {
    +                System.out.println(arg + ":");
    --- End diff --
    
    We should probably print quotes around `arg` to make it more compatible 
json like output


---

Reply via email to