|
| 1 | +import com.azure.autorest.customization.ClassCustomization; |
| 2 | +import com.azure.autorest.customization.ConstructorCustomization; |
| 3 | +import com.azure.autorest.customization.Customization; |
| 4 | +import com.azure.autorest.customization.LibraryCustomization; |
| 5 | +import com.azure.autorest.customization.PackageCustomization; |
| 6 | +import com.azure.autorest.customization.JavadocCustomization; |
| 7 | +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; |
| 8 | +import com.github.javaparser.ast.body.FieldDeclaration; |
| 9 | +import com.github.javaparser.StaticJavaParser; |
| 10 | +import com.github.javaparser.ast.Modifier; |
| 11 | +import com.github.javaparser.ast.NodeList; |
| 12 | +import com.github.javaparser.ast.body.Parameter; |
| 13 | +import com.github.javaparser.ast.stmt.BlockStmt; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import java.util.Arrays; |
| 16 | + |
| 17 | +/** |
| 18 | + * This class contains the customization code to customize the AutoRest generated code for OpenAI. |
| 19 | + */ |
| 20 | +public class AppConfigCustomizations extends Customization { |
| 21 | + |
| 22 | + @Override |
| 23 | + public void customize(LibraryCustomization customization, Logger logger) { |
| 24 | + PackageCustomization models = customization.getPackage("com.azure.v2.data.appconfiguration.models"); |
| 25 | + PackageCustomization appConfigPackages = customization.getPackage("com.azure.v2.data.appconfiguration"); |
| 26 | + hideClient(appConfigPackages); |
| 27 | + hideModels(models); |
| 28 | + renameServiceVersionClassName(appConfigPackages); |
| 29 | + |
| 30 | +// customizeKeyValueFilter(models.getClass("ConfigurationSettingsFilter")); |
| 31 | +// customizeKeyValueFields(models.getClass("SettingFields")); |
| 32 | +// customizeSnapshot(models.getClass("ConfigurationSnapshot")); |
| 33 | + } |
| 34 | + |
| 35 | + private void hideModels(PackageCustomization models) { |
| 36 | + // TODO: move both 'Key' and 'KeyValue' class to implementation package |
| 37 | + models |
| 38 | + .getClass("Key") |
| 39 | + .setModifier(0); |
| 40 | + models |
| 41 | + .getClass("KeyValue") |
| 42 | + .setModifier(0); |
| 43 | + } |
| 44 | + |
| 45 | + private void hideClient(PackageCustomization appConfigPackages) { |
| 46 | + // TODO: move both 'AzureAppConfigurationClient' and 'AzureAppConfigurationClientBuilder' class to implementation package |
| 47 | + appConfigPackages |
| 48 | + .getClass("AzureAppConfigurationClient") |
| 49 | + .setModifier(0); |
| 50 | + appConfigPackages |
| 51 | + .getClass("AzureAppConfigurationClientBuilder") |
| 52 | + .setModifier(0); |
| 53 | + } |
| 54 | + |
| 55 | + private void renameServiceVersionClassName(PackageCustomization appConfigPackages) { |
| 56 | + appConfigPackages |
| 57 | + .getClass("AzureAppConfigurationServiceVersion") |
| 58 | + .rename("ConfigurationServiceVersion"); |
| 59 | + } |
| 60 | + |
| 61 | + // TODO: LRO is not support yet in codegen-v2, wait for codegen-v2 to support LRO |
| 62 | + // KeyValueFilter is used in LRO and no other place use it, so it is not generate setters. |
| 63 | + private void customizeKeyValueFilter(ClassCustomization classCustomization) { |
| 64 | + // Edit javadoc of `setLabel` method |
| 65 | + classCustomization.getMethod("setLabel") |
| 66 | + .getJavadoc() |
| 67 | + .setDescription("Set the label property: Filters {@link ConfigurationSetting} by their label field."); |
| 68 | + // Edit javadoc of `getKey` method |
| 69 | + classCustomization.getMethod("getKey") |
| 70 | + .getJavadoc() |
| 71 | + .setDescription("Get the key property: Filters {@link ConfigurationSetting} by their key field."); |
| 72 | + // Edit javadoc of `getLabel` method |
| 73 | + classCustomization.getMethod("getLabel") |
| 74 | + .getJavadoc() |
| 75 | + .setDescription("Get the label property: Filters {@link ConfigurationSetting} by their label field."); |
| 76 | + } |
| 77 | + |
| 78 | + private void customizeSnapshot(ClassCustomization classCustomization) { |
| 79 | + classCustomization.customizeAst(ast -> { |
| 80 | + ast.addImport("java.time.Duration"); |
| 81 | + |
| 82 | + ast.getClassByName(classCustomization.getClassName()).ifPresent(clazz -> { |
| 83 | + // Transfer Long to Duration internally |
| 84 | + clazz.getMethodsByName("getRetentionPeriod").get(0) |
| 85 | + .setType("Duration") |
| 86 | + .setBody(StaticJavaParser.parseBlock("{ return this.retentionPeriod == null ? null : Duration.ofSeconds(this.retentionPeriod); }")); |
| 87 | + |
| 88 | + clazz.getMethodsByName("setRetentionPeriod").get(0) |
| 89 | + .setParameter(0, new Parameter().setType("Duration").setName("retentionPeriod")) |
| 90 | + .setBody(StaticJavaParser.parseBlock("{ this.retentionPeriod = retentionPeriod == null ? null : retentionPeriod.getSeconds(); return this; }")); |
| 91 | + }); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + private void customizeKeyValueFields(ClassCustomization classCustomization) { |
| 96 | + classCustomization.customizeAst(ast -> { |
| 97 | + // Add imports required by class changes. |
| 98 | + ast.addImport("java.util.Locale"); |
| 99 | + |
| 100 | + ClassOrInterfaceDeclaration clazz = ast.getClassByName(classCustomization.getClassName()).get(); |
| 101 | + |
| 102 | + // Modify fromString() method |
| 103 | + |
| 104 | + clazz.getMethodsByName("fromString").get(0) |
| 105 | + .setJavadocComment(StaticJavaParser.parseJavadoc(joinWithNewline( |
| 106 | + "Creates or finds a {@link SettingFields} from its string representation.", |
| 107 | + "@param name a name to look for.", |
| 108 | + "@return the corresponding {@link SettingFields}" |
| 109 | + ))); |
| 110 | + |
| 111 | + // Add class-level javadoc |
| 112 | + clazz.setJavadocComment(StaticJavaParser.parseJavadoc(joinWithNewline( |
| 113 | + "Fields in {@link ConfigurationSetting} that can be returned from GET queries.", |
| 114 | + "@see SettingSelector" |
| 115 | + ))); |
| 116 | + |
| 117 | + // Add toStringMapper static new method to SettingFields |
| 118 | + clazz.addMethod("toStringMapper", Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC).setType("String") |
| 119 | + .addParameter("SettingFields", "field") |
| 120 | + .setBody(new BlockStmt(new NodeList<>(StaticJavaParser.parseStatement("return field.toString().toLowerCase(Locale.US);")))) |
| 121 | + .addAnnotation(Deprecated.class) |
| 122 | + .setJavadocComment(StaticJavaParser.parseJavadoc(joinWithNewline( |
| 123 | + " * Converts the SettingFields to a string that is usable for HTTP requests and logging.", |
| 124 | + " * @param field SettingFields to map.", |
| 125 | + " * @return SettingFields as a lowercase string in the US locale.", |
| 126 | + " * @deprecated This method is no longer needed. SettingFields is using lower case enum value for the HTTP requests." |
| 127 | + ))); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + private static String joinWithNewline(String... lines) { |
| 132 | + return String.join("\n", lines); |
| 133 | + } |
| 134 | +} |
0 commit comments