Search in sources :

Example 1 with ModifyProtoConfiguration

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");
}
Also used : ModifyProtoConfiguration(no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration) ArrayList(java.util.ArrayList) FieldOption(no.entur.schema2proto.modifyproto.config.FieldOption) File(java.io.File) AbstractMappingTest(no.entur.schema2proto.AbstractMappingTest) Test(org.junit.jupiter.api.Test)

Example 2 with ModifyProtoConfiguration

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");
}
Also used : ModifyProtoConfiguration(no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration) MergeFrom(no.entur.schema2proto.modifyproto.config.MergeFrom) File(java.io.File) AbstractMappingTest(no.entur.schema2proto.AbstractMappingTest) Test(org.junit.jupiter.api.Test)

Example 3 with ModifyProtoConfiguration

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);
    }
}
Also used : ModifyProtoConfiguration(no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ModifyProto(no.entur.schema2proto.modifyproto.ModifyProto) InvalidProtobufException(no.entur.schema2proto.modifyproto.InvalidProtobufException) IOException(java.io.IOException) InvalidConfigurationException(no.entur.schema2proto.InvalidConfigurationException)

Example 4 with ModifyProtoConfiguration

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;
}
Also used : Schema(com.squareup.wire.schema.Schema) LoggerFactory(org.slf4j.LoggerFactory) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) MergeFrom(no.entur.schema2proto.modifyproto.config.MergeFrom) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) HashSet(java.util.HashSet) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Type(com.squareup.wire.schema.Type) GoPackageNameHelper.packageNameToGoPackageName(no.entur.schema2proto.generateproto.GoPackageNameHelper.packageNameToGoPackageName) Path(java.nio.file.Path) InvalidConfigurationException(no.entur.schema2proto.InvalidConfigurationException) MessageType(com.squareup.wire.schema.MessageType) FieldOption(no.entur.schema2proto.modifyproto.config.FieldOption) OptionReader(com.squareup.wire.schema.internal.parser.OptionReader) NewField(no.entur.schema2proto.modifyproto.config.NewField) Logger(org.slf4j.Logger) Files(java.nio.file.Files) BackwardsCompatibilityCheckException(no.entur.schema2proto.compatibility.BackwardsCompatibilityCheckException) Constructor(org.yaml.snakeyaml.constructor.Constructor) ProtoFile(com.squareup.wire.schema.ProtoFile) Collection(java.util.Collection) FileWriter(java.io.FileWriter) SyntaxReader(com.squareup.wire.schema.internal.parser.SyntaxReader) Set(java.util.Set) IdentifierSet(com.squareup.wire.schema.IdentifierSet) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) ProtolockBackwardsCompatibilityChecker(no.entur.schema2proto.compatibility.ProtolockBackwardsCompatibilityChecker) ModifyProtoConfiguration(no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) EnumConstant(com.squareup.wire.schema.EnumConstant) NewEnumConstant(no.entur.schema2proto.modifyproto.config.NewEnumConstant) Objects(java.util.Objects) List(java.util.List) SchemaLoader(com.squareup.wire.schema.SchemaLoader) Writer(java.io.Writer) Optional(java.util.Optional) Field(com.squareup.wire.schema.Field) TypeDescription(org.yaml.snakeyaml.TypeDescription) Options(com.squareup.wire.schema.Options) EnumType(com.squareup.wire.schema.EnumType) InputStream(java.io.InputStream) ModifyProtoConfiguration(no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration) InputStream(java.io.InputStream) Constructor(org.yaml.snakeyaml.constructor.Constructor) TypeDescription(org.yaml.snakeyaml.TypeDescription) ProtoFile(com.squareup.wire.schema.ProtoFile) File(java.io.File) Yaml(org.yaml.snakeyaml.Yaml) InvalidConfigurationException(no.entur.schema2proto.InvalidConfigurationException)

Example 5 with ModifyProtoConfiguration

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");
}
Also used : ModifyProtoConfiguration(no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration) ArrayList(java.util.ArrayList) File(java.io.File) AbstractMappingTest(no.entur.schema2proto.AbstractMappingTest) Test(org.junit.jupiter.api.Test)

Aggregations

ModifyProtoConfiguration (no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration)16 File (java.io.File)15 AbstractMappingTest (no.entur.schema2proto.AbstractMappingTest)13 Test (org.junit.jupiter.api.Test)13 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)3 InvalidConfigurationException (no.entur.schema2proto.InvalidConfigurationException)3 FieldOption (no.entur.schema2proto.modifyproto.config.FieldOption)3 MergeFrom (no.entur.schema2proto.modifyproto.config.MergeFrom)3 NewField (no.entur.schema2proto.modifyproto.config.NewField)3 ImmutableList (com.google.common.collect.ImmutableList)2 EnumConstant (com.squareup.wire.schema.EnumConstant)2 EnumType (com.squareup.wire.schema.EnumType)2 Field (com.squareup.wire.schema.Field)2 IdentifierSet (com.squareup.wire.schema.IdentifierSet)2 Location (com.squareup.wire.schema.Location)2 MessageType (com.squareup.wire.schema.MessageType)2 Options (com.squareup.wire.schema.Options)2 ProtoFile (com.squareup.wire.schema.ProtoFile)2 Schema (com.squareup.wire.schema.Schema)2