|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 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 | + * http://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 com.datastax.oss.common.sink.config; |
| 17 | + |
| 18 | +import java.io.FileWriter; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.PrintWriter; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.*; |
| 23 | +import org.apache.kafka.common.config.ConfigDef; |
| 24 | + |
| 25 | +/** Generates Asiidoc documentation for a ConfigDef */ |
| 26 | +public class AsciiDocGenerator { |
| 27 | + |
| 28 | + public static String toAscidoc(ConfigDef configDef) { |
| 29 | + StringBuilder b = new StringBuilder(); |
| 30 | + for (ConfigDef.ConfigKey key : sortedConfigs(configDef)) { |
| 31 | + if (key.internalConfig) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + getConfigKeyAsciiDoc(configDef, key, b); |
| 35 | + b.append("\n"); |
| 36 | + } |
| 37 | + return b.toString(); |
| 38 | + } |
| 39 | + |
| 40 | + protected static void getConfigKeyAsciiDoc( |
| 41 | + ConfigDef configDef, ConfigDef.ConfigKey key, StringBuilder b) { |
| 42 | + b.append("[#").append(key.name).append("]").append("\n"); |
| 43 | + for (String docLine : key.documentation.split("\n")) { |
| 44 | + if (docLine.length() == 0) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + b.append(docLine).append("\n+\n"); |
| 48 | + } |
| 49 | + b.append("Type: ").append(getConfigValue(key, "Type")).append("\n"); |
| 50 | + if (key.hasDefault()) { |
| 51 | + b.append("Default: ").append(getConfigValue(key, "Default")).append("\n"); |
| 52 | + } |
| 53 | + if (key.validator != null) { |
| 54 | + b.append("Valid Values: ").append(getConfigValue(key, "Valid Values")).append("\n"); |
| 55 | + } |
| 56 | + b.append("Importance: ").append(getConfigValue(key, "Importance")).append("\n"); |
| 57 | + } |
| 58 | + |
| 59 | + protected static String getConfigValue(ConfigDef.ConfigKey key, String headerName) { |
| 60 | + switch (headerName) { |
| 61 | + case "Name": |
| 62 | + return key.name; |
| 63 | + case "Description": |
| 64 | + return key.documentation; |
| 65 | + case "Type": |
| 66 | + return key.type.toString().toLowerCase(Locale.ROOT); |
| 67 | + case "Default": |
| 68 | + if (key.hasDefault()) { |
| 69 | + if (key.defaultValue == null) return "null"; |
| 70 | + String defaultValueStr = ConfigDef.convertToString(key.defaultValue, key.type); |
| 71 | + if (defaultValueStr.isEmpty()) return "\"\""; |
| 72 | + else return defaultValueStr; |
| 73 | + } else return ""; |
| 74 | + case "Valid Values": |
| 75 | + return key.validator != null ? key.validator.toString() : ""; |
| 76 | + case "Importance": |
| 77 | + return key.importance.toString().toLowerCase(Locale.ROOT); |
| 78 | + default: |
| 79 | + throw new RuntimeException( |
| 80 | + "Can't find value for header '" + headerName + "' in " + key.name); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + protected static List<ConfigDef.ConfigKey> sortedConfigs(ConfigDef configDef) { |
| 85 | + final Map<String, Integer> groupOrd = new HashMap<>(configDef.groups().size()); |
| 86 | + int ord = 0; |
| 87 | + for (String group : configDef.groups()) { |
| 88 | + groupOrd.put(group, ord++); |
| 89 | + } |
| 90 | + |
| 91 | + List<ConfigDef.ConfigKey> configs = new ArrayList<>(configDef.configKeys().values()); |
| 92 | + Collections.sort(configs, (k1, k2) -> compare(k1, k2, groupOrd)); |
| 93 | + return configs; |
| 94 | + } |
| 95 | + |
| 96 | + protected static int compare( |
| 97 | + ConfigDef.ConfigKey k1, ConfigDef.ConfigKey k2, Map<String, Integer> groupOrd) { |
| 98 | + int cmp = |
| 99 | + k1.group == null |
| 100 | + ? (k2.group == null ? 0 : -1) |
| 101 | + : (k2.group == null |
| 102 | + ? 1 |
| 103 | + : Integer.compare(groupOrd.get(k1.group), groupOrd.get(k2.group))); |
| 104 | + if (cmp == 0) { |
| 105 | + cmp = Integer.compare(k1.orderInGroup, k2.orderInGroup); |
| 106 | + if (cmp == 0) { |
| 107 | + // first take anything with no default value |
| 108 | + if (!k1.hasDefault() && k2.hasDefault()) cmp = -1; |
| 109 | + else if (!k2.hasDefault() && k1.hasDefault()) cmp = 1; |
| 110 | + else { |
| 111 | + cmp = k1.importance.compareTo(k2.importance); |
| 112 | + if (cmp == 0) return k1.name.compareTo(k2.name); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return cmp; |
| 117 | + } |
| 118 | + |
| 119 | + public static void generateConfigDefDoc(Path path, String name, String title, ConfigDef configDef) |
| 120 | + throws IOException { |
| 121 | + try (FileWriter fileWriter = new FileWriter(path.resolve(name).toFile())) { |
| 122 | + PrintWriter pw = new PrintWriter(fileWriter); |
| 123 | + pw.append("= ").append(title).append("\n\n"); |
| 124 | + pw.append("== Parameters").append("\n\n"); |
| 125 | + pw.append(toAscidoc(configDef)); |
| 126 | + pw.flush(); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments