use of no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration in project schema2proto by entur.
the class ModifyProtoTest method testAddFieldOption.
@Test
public void testAddFieldOption() throws IOException, InvalidProtobufException, InvalidConfigurationException {
File expected = new File("src/test/resources/modify/expected/nopackagename").getCanonicalFile();
File source = new File("src/test/resources/modify/input/nopackagename").getCanonicalFile();
List<FieldOption> fieldOptions = new ArrayList<>();
FieldOption fieldOption = new FieldOption();
fieldOption.targetMessageType = "A";
fieldOption.field = "response_timestamp";
fieldOption.option = "[(validate.rules).uint64.gte = 20]";
fieldOptions.add(fieldOption);
ModifyProtoConfiguration configuration = new ModifyProtoConfiguration();
configuration.inputDirectory = source;
configuration.fieldOptions = Collections.singletonList(fieldOption);
modifyProto(configuration);
compareExpectedAndGenerated(expected, "addedFieldOption.proto", generatedRootFolder, "simple.proto");
}
use of no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration in project schema2proto by entur.
the class ModifyProtoTest method testMergeProto.
@Test
public void testMergeProto() throws IOException, InvalidProtobufException, InvalidConfigurationException {
File expected = new File("src/test/resources/modify/expected/nopackagename").getCanonicalFile();
File source = new File("src/test/resources/modify/input/nopackagename").getCanonicalFile();
File mergefrom = new File("src/test/resources/modify/mergefrom/nopackagename").getCanonicalFile();
MergeFrom m = new MergeFrom();
m.sourceFolder = mergefrom;
m.protoFile = "mergefrom.proto";
ModifyProtoConfiguration configuration = new ModifyProtoConfiguration();
configuration.inputDirectory = source;
configuration.mergeFrom = Collections.singletonList(m);
modifyProto(configuration);
compareExpectedAndGenerated(expected, "mergefrom.proto", generatedRootFolder, "simple.proto");
}
use of no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration in project schema2proto by entur.
the class ModifyProtoMojo method execute.
public void execute() throws MojoExecutionException {
if (configFile == null || !configFile.exists()) {
throw new MojoExecutionException("Config file not found");
}
getLog().info(String.format("Modifying proto files from using config file %s. Output is defined in config file", configFile));
try {
ModifyProtoConfiguration configuration = ModifyProto.parseConfigurationFile(configFile, project.getBasedir());
// Override based on maven parameter -DfailIfRemovedFields
if (failIfRemovedFields != null) {
configuration.failIfRemovedFields = failIfRemovedFields;
}
new ModifyProto().modifyProto(configuration);
} catch (IOException e) {
throw new MojoExecutionException("Error modifying proto files", e);
} catch (InvalidConfigurationException e) {
throw new MojoExecutionException("Invalid modify configuration file", e);
} catch (InvalidProtobufException e) {
throw new MojoExecutionException("Could not fully modify proto files", e);
}
}
use of no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration in project schema2proto by entur.
the class ModifyProto method parseConfigurationFile.
public static ModifyProtoConfiguration parseConfigurationFile(File configFile, File basedir) throws IOException, InvalidConfigurationException {
ModifyProtoConfiguration configuration = new ModifyProtoConfiguration();
try (InputStream in = Files.newInputStream(configFile.toPath())) {
// Parse config file
Constructor constructor = new Constructor(ModifyProtoConfigFile.class);
TypeDescription customTypeDescription = new TypeDescription(ModifyProtoConfigFile.class);
customTypeDescription.addPropertyParameters("newFields", NewField.class);
customTypeDescription.addPropertyParameters("mergeFrom", MergeFrom.class);
customTypeDescription.addPropertyParameters("valdiationRules", FieldOption.class);
constructor.addTypeDescription(customTypeDescription);
Yaml yaml = new Yaml(constructor);
LOGGER.info("Using configFile {}", configFile);
ModifyProtoConfigFile config = yaml.load(in);
// Check config values
if (config.outputDirectory == null) {
throw new InvalidConfigurationException("No output directory");
} else {
configuration.outputDirectory = new File(basedir, config.outputDirectory);
configuration.outputDirectory.mkdirs();
}
if (config.inputDirectory == null) {
throw new InvalidConfigurationException("no input directory");
} else {
configuration.inputDirectory = new File(basedir, config.inputDirectory);
}
if (config.includes != null) {
configuration.includes.addAll(config.includes.stream().filter(e -> StringUtils.trimToNull(e) != null).collect(Collectors.toList()));
}
if (config.excludes != null) {
configuration.excludes.addAll(config.excludes.stream().filter(e -> StringUtils.trimToNull(e) != null).collect(Collectors.toList()));
}
if (config.mergeFrom != null) {
configuration.mergeFrom = new ArrayList<>(config.mergeFrom);
}
if (config.newFields != null) {
configuration.newFields = new ArrayList<>(config.newFields);
}
if (config.newEnumConstants != null) {
configuration.newEnumConstants = new ArrayList<>(config.newEnumConstants);
}
if (config.fieldOptions != null) {
configuration.fieldOptions = new ArrayList<>(config.fieldOptions);
}
configuration.includeBaseTypes = config.includeBaseTypes;
if (config.protoLockFile != null) {
configuration.protoLockFile = new File(basedir, config.protoLockFile);
}
configuration.failIfRemovedFields = config.failIfRemovedFields;
if (config.customImportLocations != null) {
configuration.customImportLocations = new ArrayList<>(config.customImportLocations.stream().filter(e -> StringUtils.trimToNull(e) != null).collect(Collectors.toList()));
}
configuration.basedir = basedir;
configuration.includeGoPackageOptions = config.includeGoPackageOptions;
configuration.goPackageSourcePrefix = config.goPackageSourcePrefix;
}
return configuration;
}
use of no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration in project schema2proto by entur.
the class ModifyProtoTest method testRemoveIndependentMessageType.
@Test
public void testRemoveIndependentMessageType() throws IOException, InvalidProtobufException, InvalidConfigurationException {
File expected = new File("src/test/resources/modify/expected/nopackagename").getCanonicalFile();
File source = new File("src/test/resources/modify/input/nopackagename").getCanonicalFile();
List<String> excludes = new ArrayList<>();
excludes.add("A");
ModifyProtoConfiguration configuration = new ModifyProtoConfiguration();
configuration.inputDirectory = source;
configuration.excludes = excludes;
modifyProto(configuration);
compareExpectedAndGenerated(expected, "missing_a.proto", generatedRootFolder, "simple.proto");
}
Aggregations