Skip to content

Commit

Permalink
Use human-readable dataset/location types for CLI parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
paytoncain committed May 3, 2024
1 parent dffca92 commit d4625f7
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private static boolean fieldsContainExplicitField(List<FieldNameWithType> fields
private static List<String> getDeclaredFields(DatasetType datasetType, LocationType locationType, List<Type> explicitClasses)
throws ClassNotFoundException {
List<FieldNameWithType> fields = getBaseFields(datasetType, locationType);

while (fieldsContainExplicitField(fields, explicitClasses)) {
fields = processExplicitFields(fields, explicitClasses);
}
Expand Down Expand Up @@ -293,10 +293,10 @@ private static List<FieldNameWithType> fieldsToFieldNames(Class<?> clazz, Class<
@Command(name = "generate-translator", description = "Generate default CSV or Excel translator", mixinStandardHelpOptions = true, versionProvider = VersionProvider.class)
static class GenerateTranslator extends GenerateTranslatorCommand<PackingJob> {

@Option(names = {"--dataset-type", "-dt"}, description = "Dataset type")
@Option(names = {"--dataset-type", "-dt"}, description = "Dataset type", converter = DatasetTypeConverter.class)
private DatasetType datasetType;

@Option(names = {"--location-type", "-lt"}, description = "Deployment location type", required = true)
@Option(names = {"--location-type", "-lt"}, description = "Deployment location type", required = true, converter = LocationTypeConverter.class)
private LocationType locationType;

@Parameters(description = "Translator type")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package edu.colorado.cires.pace.cli.command.dataset;

import edu.colorado.cires.pace.translator.DatasetType;
import picocli.CommandLine.ITypeConverter;

public class DatasetTypeConverter implements ITypeConverter<DatasetType> {

@Override
public DatasetType convert(String s) {
return DatasetType.fromName(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package edu.colorado.cires.pace.cli.command.dataset;

import edu.colorado.cires.pace.translator.LocationType;
import picocli.CommandLine.ITypeConverter;

public class LocationTypeConverter implements ITypeConverter<LocationType> {

@Override
public LocationType convert(String s) {
return LocationType.fromName(s);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package edu.colorado.cires.pace.translator;

import java.util.Arrays;
import java.util.stream.Collectors;

public enum DatasetType {
SOUND_CLIPS("sound clips"),
AUDIO("audio"),
Expand All @@ -17,4 +20,22 @@ public enum DatasetType {
public String getName() {
return name;
}

public static DatasetType fromName(String name) {
return switch (name) {
case "sound clips" -> SOUND_CLIPS;
case "audio" -> AUDIO;
case "CPOD" -> CPOD;
case "detections" -> DETECTIONS;
case "sound level metrics" -> SOUND_LEVEL_METRICS;
case "sound propagation models" -> SOUND_PROPAGATION_MODELS;
default -> throw new IllegalArgumentException(String.format(
"Invalid dataset type: %s. Was not one of: %s",
name,
Arrays.stream(DatasetType.values())
.map(DatasetType::getName)
.collect(Collectors.joining(", "))
));
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package edu.colorado.cires.pace.translator;

import java.util.Arrays;
import java.util.stream.Collectors;

public enum LocationType {
STATIONARY_MARINE("stationary marine"),
MULTIPOINT_STATIONARY_MARINE("multipoint stationary marine"),
Expand All @@ -15,4 +18,20 @@ public enum LocationType {
public String getName() {
return name;
}

public static LocationType fromName(String name) {
return switch (name) {
case "stationary marine" -> STATIONARY_MARINE;
case "multipoint stationary marine" -> MULTIPOINT_STATIONARY_MARINE;
case "mobile marine" -> MOBILE_MARINE;
case "stationary terrestrial" -> STATIONARY_TERRESTRIAL;
default -> throw new IllegalArgumentException(String.format(
"Invalid location type: %s. Was not one of: %s",
name,
Arrays.stream(LocationType.values())
.map(LocationType::getName)
.collect(Collectors.joining(", "))
));
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package edu.colorado.cires.pace.translator;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class DatasetTypeTest {

@ParameterizedTest
@CsvSource(value = {
"sound clips,SOUND_CLIPS",
"audio,AUDIO",
"CPOD,CPOD",
"detections,DETECTIONS",
"sound level metrics,SOUND_LEVEL_METRICS",
"sound propagation models,SOUND_PROPAGATION_MODELS",
})
void fromName(String name, DatasetType expectedDatasetType) {
assertEquals(expectedDatasetType, DatasetType.fromName(name));
}

@Test
void fromNameInvalid() {
String name = "invalid";
Exception exception = assertThrows(IllegalArgumentException.class, () -> DatasetType.fromName(name));
assertEquals(String.format(
"Invalid dataset type: %s. Was not one of: %s",
name,
Arrays.stream(DatasetType.values())
.map(DatasetType::getName)
.collect(Collectors.joining(", "))
), exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package edu.colorado.cires.pace.translator;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class LocationTypeTest {

@ParameterizedTest
@CsvSource(value = {
"stationary marine,STATIONARY_MARINE",
"multipoint stationary marine,MULTIPOINT_STATIONARY_MARINE",
"mobile marine,MOBILE_MARINE",
"stationary terrestrial,STATIONARY_TERRESTRIAL"
})
void fromName(String inputString, LocationType expectedType) {
assertEquals(expectedType, LocationType.fromName(inputString));
}

@Test
void fromNameInvalidName() {
String name = "invalid";
Exception exception = assertThrows(IllegalArgumentException.class, () -> LocationType.fromName(name));
assertEquals(String.format(
"Invalid location type: %s. Was not one of: %s",
name,
Arrays.stream(LocationType.values())
.map(LocationType::getName)
.collect(Collectors.joining(", "))
), exception.getMessage());
}
}

0 comments on commit d4625f7

Please sign in to comment.