Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate enum with no keys for large properties files #27

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/bazel-icu-local/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RULES_JVM_EXTERNAL_SHA = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea1
new_local_repository(
name = "l10nmessages_local",
path = "../../",
build_file = "l10nmessages_local_maven.BUILD.bzl",
build_file = "@//:l10nmessages_local_maven.BUILD.bzl",
)

http_archive(
Expand Down
2 changes: 1 addition & 1 deletion examples/bazel-local/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RULES_JVM_EXTERNAL_SHA = "cd1a77b7b02e8e008439ca76fd34f5b07aecb8c752961f9640dea1
new_local_repository(
name = "l10nmessages_local",
path = "../../",
build_file = "l10nmessages_local_maven.BUILD.bzl",
build_file = "@//:l10nmessages_local_maven.BUILD.bzl",
)

http_archive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,4 @@ public void setEnumType(EnumType enumType) {
public EnumType getEnumType() {
return enumType;
}

public void setGeneratorStyle(EnumType enumType) {
this.enumType = enumType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public static String toEnum(

String enumValues =
deduplicatedWithReporting.keySet().stream()
.map(key -> String.format(" %s(\"%s\")", toJavaIdentifier.convert(key), key))
.collect(Collectors.joining(",\n"));
.map(key -> String.format(" %s(\"%s\")", toJavaIdentifier.convert(key), key))
.collect(Collectors.joining(",\n"))
+ ";";

String packageString =
nameParts.getDotPackageName().isEmpty()
Expand All @@ -67,32 +68,42 @@ public static String toEnum(
+ "\n"
+ "@Generated(\"%4$s\")\n"
+ "public enum %5$s {\n"
+ "%6$s;\n"
+ "%6$s\n"
+ "\n"
+ " public static final String BASENAME = \"%7$s\";\n"
+ " public static final String MESSAGE_FORMAT_ADAPTER_PROVIDERS = \"%8$s\";\n"
+ "\n"
+ " private String key;\n"
+ "\n"
+ " %5$s(String key) {\n"
+ " this.key = key;\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public String toString() {\n"
+ " return key;\n"
+ " }\n"
+ "%9$s"
+ "%10$s"
+ "}\n",
packageString, // 1
enumType.WITH_ARGUMENT_BUILDERS.equals(enumType) ? IMPORT_STATEMENT_FOR_ARGUMENTS : "", // 2
IMPORT_GENERATED_ANNOTATION, // 3
generatedAnnotationValue, // 4
nameParts.getEnumName(), // 5
enumValues, // 6
enumType.NO_KEYS.equals(enumType) ? " ;" : enumValues, // 6
nameParts.getBaseName(), // 7
messageFormatAdapterProviders, // 8
enumType.WITH_ARGUMENT_BUILDERS.equals(enumType) ? formatContexts + "\n" : ""); // 9
enumType.NO_KEYS.equals(enumType) ? "" : generateConstructorBlock(nameParts.getEnumName()),
// 9
enumType.WITH_ARGUMENT_BUILDERS.equals(enumType) ? formatContexts + "\n" : "" // 10
);
}

private static String generateConstructorBlock(String enumName) {
return String.format(
""
+ " private String key;\n"
+ "\n"
+ " %1$s(String key) {\n"
+ " this.key = key;\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public String toString() {\n"
+ " return key;\n"
+ " }\n",
enumName);
}

private static String generateFormatContextForEntry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,41 @@ void toEnum() {
+ "}\n",
enumString);
}

@Test
void toEnumWithNoKeys() {
String baseName = "com.pinterest.l10nmessages.Messages";
NameParts nameParts = NameParts.fromBaseName(baseName);

LinkedHashMap<String, String> entries =
(LinkedHashMap<String, String>)
Maps.of(
"hello_username", "Hello {userName}, {count}",
"bye", "Bye");

String enumString =
L10nPropertiesEnumGenerator.toEnum(
entries,
nameParts,
ToJavaIdentifiers.ESCAPING_AND_UNDERSCORE,
MessageFormatAdapterProviders.JDK_NAMED_ARGS,
L10nPropertiesProcessor.class.getName(),
EnumType.NO_KEYS);

assertThat(enumString)
.isEqualTo(
"package com.pinterest.l10nmessages;\n"
+ "\n"
+ L10nPropertiesEnumGenerator.IMPORT_GENERATED_ANNOTATION
+ "\n"
+ "@Generated(\"com.pinterest.l10nmessages.L10nPropertiesProcessor\")\n"
+ "public enum Messages {\n"
+ " ;\n"
+ "\n"
+ " public static final String BASENAME = \"com.pinterest.l10nmessages.Messages\";\n"
+ " public static final String MESSAGE_FORMAT_ADAPTER_PROVIDERS = \"JDK_NAMED_ARGS\";\n"
+ "\n"
+ "}\n",
enumString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public void valid() {
checkGenerationSuccessful(compilation);
}

@Test
public void validArgumentBuilder() {
assumeTrue(shouldUseOldGeneratedAnnotation());
Compilation compilation = defaultCompile();
checkGenerationSuccessful(compilation);
}

@Test
public void validNoKey() {
assumeTrue(shouldUseOldGeneratedAnnotation());
Compilation compilation = defaultCompile();
checkGenerationSuccessful(compilation);
}

@Test
public void validJava9() {
assumeFalse(shouldUseOldGeneratedAnnotation());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.pinterest.l10nmessages.L10nPropertiesProcessorTest_IO.valid;

import com.pinterest.l10nmessages.EnumType;
import com.pinterest.l10nmessages.L10nProperties;

@L10nProperties(baseName = "com.pinterest.l10nmessages.L10nPropertiesProcessorTest_IO.validArgumentBuilder.TestMessages",
enumType = EnumType.WITH_ARGUMENT_BUILDERS)
public class TestApp {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.pinterest.l10nmessages.L10nPropertiesProcessorTest_IO.validArgumentBuilder;

import com.pinterest.l10nmessages.FormatContext;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.annotation.Generated;

@Generated("com.pinterest.l10nmessages.L10nPropertiesProcessor")
public enum TestMessages {
hello_user("hello_user");

public static final String BASENAME = "com.pinterest.l10nmessages.L10nPropertiesProcessorTest_IO.validArgumentBuilder.TestMessages";
public static final String MESSAGE_FORMAT_ADAPTER_PROVIDERS = "AUTO";

private String key;

TestMessages(String key) {
this.key = key;
}

@Override
public String toString() {
return key;
}

public static class FC_hello_user implements FormatContext<TestMessages> {
Map<String, Object> map = new LinkedHashMap<>();

@Override
public TestMessages getKey() {
return hello_user;
}

@Override
public Map<String, Object> getArguments() {
return map;
}

public FC_hello_user username(Object value) {
map.put("username", value);
return this;
}
}

public static FC_hello_user hello_user() {
return new FC_hello_user();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello_user=Hello {username}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.pinterest.l10nmessages.L10nPropertiesProcessorTest_IO.valid;

import com.pinterest.l10nmessages.EnumType;
import com.pinterest.l10nmessages.L10nProperties;

@L10nProperties(baseName = "com.pinterest.l10nmessages.L10nPropertiesProcessorTest_IO.validNoKey.TestMessages",
enumType = EnumType.NO_KEYS)
public class TestApp {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.pinterest.l10nmessages.L10nPropertiesProcessorTest_IO.validNoKey;

import javax.annotation.Generated;

@Generated("com.pinterest.l10nmessages.L10nPropertiesProcessor")
public enum TestMessages {
;

public static final String BASENAME = "com.pinterest.l10nmessages.L10nPropertiesProcessorTest_IO.validNoKey.TestMessages";
public static final String MESSAGE_FORMAT_ADAPTER_PROVIDERS = "AUTO";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello_user=Hello {username}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
*
* <p>{@link #WITH_ARGUMENT_BUILDERS} provide stronger typing but requires one class to be generated
* by message with arguments. If this not acceptable, use {@link #KEYS_ONLY} (default value).
*
* <p>For large files with messages that are not referenced explicitly by key in code it is possible
* to generate the enum with no keys {@link #NO_KEYS}. The enum can still be used to create the
* L10nMessages instance but obviously there no type checking anymore.
*/
public enum EnumType {
/**
Expand All @@ -28,5 +32,17 @@ public enum EnumType {
*
* <p>Typically used with {@link com.pinterest.l10nmessages.L10nMessages#format(FormatContext)}
*/
WITH_ARGUMENT_BUILDERS
WITH_ARGUMENT_BUILDERS,
/**
* Generates an enum with no keys.
*
* <p>This can be used to work with large properties files that generate the following compilation
* error: "error: code too large" when the enum becomes too large with {@link #KEYS_ONLY}.
*
* <p>Obviously strong typing does not apply anymore but other enum attribute can be used for
* example to create an instance of L10nMessages with {@link L10nMessages#builder(Class)}
*
* <p>Typically used with {@link com.pinterest.l10nmessages.L10nMessages#format(Object)}
*/
NO_KEYS,
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ public Locale getLocale() {
return locale;
}

/**
* Get the resource bundle used to load the messages.
*
* @return the resource bundle used to load the messages
*/
public ResourceBundle getResourceBundle() {
return resourceBundle;
}

/**
* Format a message for an "untyped" key and a given map of arguments.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ void getLocale() {
assertThat(m.getLocale()).isEqualTo(Locale.FRANCE);
}

@Test
void getResourceBundle() {
L10nMessages<com.pinterest.l10nmessages.Messages> m =
L10nMessages.builder(Messages.class).build();
assertThat(m.getResourceBundle().keySet().size()).isEqualTo(15);
}

@Test
public void enumTypedFormat() {
L10nMessages<com.pinterest.l10nmessages.Messages> m =
Expand Down