Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Use enum for package type #1609

Merged
merged 5 commits into from
Dec 10, 2016
Merged

Conversation

objmagic
Copy link
Contributor

@objmagic objmagic commented Dec 9, 2016

This PR refactors so that enum is used for topology package type. It addresses discussions at #1599 (comment)

Follow-up of #1599

cc @billonahill @mycFelix

@objmagic objmagic added this to the 0.15.0 milestone Dec 9, 2016
@objmagic objmagic self-assigned this Dec 9, 2016
@objmagic objmagic requested a review from billonahill December 9, 2016 20:13
@objmagic objmagic changed the title Use enum for package type. Use enum for package type Dec 9, 2016
TAR;

@Override
public String toString() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a custom toString necessary? IIRC the enum toString prints nicely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it will return a lowercase one or an uppercase one.


@Override
public String toString() {
switch (this) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.name() does this

public static String getBaseName(String file) {
return new File(file).getName();
}

public static String getPkgType(String topologyBinaryFile) {
String pkgType;
public static PackageType getPackageType(String topologyBinaryFile) {
String basename = FileUtils.getBaseName(topologyBinaryFile);
if (FileUtils.isOriginalPackagePex(basename)) {
Copy link
Contributor

@billonahill billonahill Dec 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement can be replaced with a static factory method on the enum. Something like this:

public enum PackageType {
  PEX,
  JAR,
  TAR
  
  public static PackageType getType(String filename) {
    // parse extension
    return PackageType.valueOf(extension);
  }
}

Actually, at that point you no longer even need FileUtils.getPackageType anymore, since callers can just get the type from the enum

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that will be easier. I realized that I am just cargo-culting.

@@ -186,7 +186,7 @@ protected String formatJavaOpts(String javaOpts) {
auroraProperties.put("COMPONENT_RAMMAP", Runtime.componentRamMap(runtime));
auroraProperties.put("COMPONENT_JVM_OPTS_IN_BASE64",
formatJavaOpts(TopologyUtils.getComponentJvmOptions(topology)));
auroraProperties.put("TOPOLOGY_PACKAGE_TYPE", Context.topologyPackageType(config));
auroraProperties.put("TOPOLOGY_PACKAGE_TYPE", Context.topologyPackageType(config).toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to rely on toString() here since that should only ever really be used for logging. If we need a string better to call .name() on the enum, or create a value field in the enum that is defined as part of the enum (i.e., TAR("tar")).

}

public static PackageType getPackageType(String topologyBinaryFile) {
String basename = FileUtils.getBaseName(topologyBinaryFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove ~20 lines of code and just use guava Files class here:

String extension = Files.getFileExtension(topologyBinaryFile)
return PackageType.valueOf(extension.toUpperCase();

@@ -212,7 +212,7 @@ private SchedulerUtils() {
commands.add(Context.systemConfigSandboxFile(config));
commands.add(Runtime.componentRamMap(runtime));
commands.add(SchedulerUtils.encodeJavaOpts(TopologyUtils.getComponentJvmOptions(topology)));
commands.add(Context.topologyPackageType(config));
commands.add(Context.topologyPackageType(config).toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use .name() than toString. Also check on the cases, think both return caps.

Assert.assertEquals(PackageType.getPackageType(pexFile), PackageType.PEX);
}

@Test(expected = RuntimeException.class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this RuntimeException or a subclass? If a subclass we should expect that instead.

JAR,
TAR;

public static PackageType getPackageType(String topologyBinaryFile) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rename topologyBinaryFile to something more generic like filename.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about packageFile, since this class is PackageType? filename is too general

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

@billonahill
Copy link
Contributor

👍 once CI passes

@mycFelix
Copy link
Contributor

👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants