Skip to content

Commit

Permalink
Merge branch 'release/1.85'
Browse files Browse the repository at this point in the history
  • Loading branch information
circleci committed Mar 17, 2023
2 parents 79ed3cf + 8ab5e6d commit d6ae49b
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>no.entur</groupId>
<artifactId>schema2proto-parent</artifactId>
<version>1.84</version>
<version>1.85</version>
<packaging>pom</packaging>
<name>Schema 2 proto parent pom</name>
<description>Schema2proto - XSD schema to protobuf.</description>
Expand All @@ -24,7 +24,7 @@
<owasp-dependency-check.version>8.1.2</owasp-dependency-check.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spotless.version>2.34.0</spotless.version>
<spotless.version>2.35.0</spotless.version>
</properties>
<build>
<pluginManagement>
Expand Down
5 changes: 5 additions & 0 deletions schema2proto-lib/example_config/modifyproto.yml
Expand Up @@ -47,6 +47,11 @@ newFields:
- { targetMessageType: existingPackageName.ExistingMessageType, name: field_name, type: packageName.FieldType, importProto: packageName/file.proto, label: repeated, fieldNumber: 12 }
- { targetMessageType: existingPackageName.ExistingMessageType, name: field_name_2, type: FieldTypeCommon, importProto: packageName/file2.proto, fieldNumber: 13 }

# Modify an existing field on an existing type. Note: All on same line (snake yaml)
modifyFields:
- { targetMessageType: existingPackageName.ExistingMessageType, name: existing_field_name, documentation: "This documentation completely replaces existing documentation on the field." }
- { targetMessageType: existingPackageName.ExistingMessageType, name: existing_field_name2, documentationPattern: "(.*)", documentation: "Replaces matching regex pattern of the existing documentation with this text - matching groups supported $1" }

# Add new enum value to existing enum. Note: All on same line (snake yaml)
newEnumConstants:
- { targetEnumType: existingPackageName.ExistingEnumType, name: ENUM_CONSTANT_TYPE_NEW_VALUE, fieldNumber: 12, documentation: "Enum value documentation" }
Expand Down
2 changes: 1 addition & 1 deletion schema2proto-lib/pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>no.entur</groupId>
<artifactId>schema2proto-parent</artifactId>
<version>1.84</version>
<version>1.85</version>
</parent>
<artifactId>schema2proto-lib</artifactId>
<packaging>jar</packaging>
Expand Down
Expand Up @@ -70,6 +70,7 @@
import no.entur.schema2proto.compatibility.ProtolockBackwardsCompatibilityChecker;
import no.entur.schema2proto.modifyproto.config.FieldOption;
import no.entur.schema2proto.modifyproto.config.MergeFrom;
import no.entur.schema2proto.modifyproto.config.ModifyField;
import no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration;
import no.entur.schema2proto.modifyproto.config.NewEnumConstant;
import no.entur.schema2proto.modifyproto.config.NewField;
Expand All @@ -87,6 +88,7 @@ public static ModifyProtoConfiguration parseConfigurationFile(File configFile, F
Constructor constructor = new Constructor(ModifyProtoConfigFile.class);
TypeDescription customTypeDescription = new TypeDescription(ModifyProtoConfigFile.class);
customTypeDescription.addPropertyParameters("newFields", NewField.class);
customTypeDescription.addPropertyParameters("modifyFields", ModifyField.class);
customTypeDescription.addPropertyParameters("mergeFrom", MergeFrom.class);
customTypeDescription.addPropertyParameters("valdiationRules", FieldOption.class);
constructor.addTypeDescription(customTypeDescription);
Expand Down Expand Up @@ -125,6 +127,10 @@ public static ModifyProtoConfiguration parseConfigurationFile(File configFile, F
configuration.newFields = new ArrayList<>(config.newFields);
}

if (config.modifyFields != null) {
configuration.modifyFields = new ArrayList<>(config.modifyFields);
}

if (config.newEnumConstants != null) {
configuration.newEnumConstants = new ArrayList<>(config.newEnumConstants);
}
Expand Down Expand Up @@ -204,6 +210,10 @@ public void modifyProto(ModifyProtoConfiguration configuration) throws IOExcepti
addField(newField, prunedSchema);
}

for (ModifyField modifyField : configuration.modifyFields) {
modifyField(modifyField, prunedSchema);
}

for (NewEnumConstant newEnumValue : configuration.newEnumConstants) {
addEnumConstant(newEnumValue, prunedSchema);
}
Expand Down Expand Up @@ -436,6 +446,24 @@ private void addField(NewField newField, Schema prunedSchema) throws InvalidProt

}

private void modifyField(ModifyField modifyField, Schema prunedSchema) throws InvalidProtobufException {
MessageType type = (MessageType) prunedSchema.getType(modifyField.targetMessageType);
if (type == null) {
throw new InvalidProtobufException("Did not find existing type " + modifyField.targetMessageType);
}
Field field = type.field(modifyField.field);
if (field == null) {
throw new InvalidProtobufException("Did not find existing field " + modifyField.field);
}
if (modifyField.documentation != null) {
if (modifyField.documentationPattern != null) {
field.updateDocumentation(field.documentation().replaceAll(modifyField.documentationPattern, modifyField.documentation));
} else {
field.updateDocumentation(modifyField.documentation);
}
}
}

public void addFieldOption(FieldOption fieldOption, Schema prunedSchema) throws InvalidProtobufException {
MessageType type = (MessageType) prunedSchema.getType(fieldOption.targetMessageType);
if (type == null) {
Expand Down
Expand Up @@ -26,6 +26,7 @@

import no.entur.schema2proto.modifyproto.config.FieldOption;
import no.entur.schema2proto.modifyproto.config.MergeFrom;
import no.entur.schema2proto.modifyproto.config.ModifyField;
import no.entur.schema2proto.modifyproto.config.NewEnumConstant;
import no.entur.schema2proto.modifyproto.config.NewField;

Expand All @@ -35,6 +36,7 @@ public class ModifyProtoConfigFile {
public List<String> includes;
public List<String> excludes;
public List<NewField> newFields;
public List<ModifyField> modifyFields;
public List<MergeFrom> mergeFrom;
public List<String> customImportLocations;
public List<NewEnumConstant> newEnumConstants;
Expand Down
@@ -0,0 +1,46 @@
/*-
* #%L
* schema2proto-lib
* %%
* Copyright (C) 2019 - 2020 Entur
* %%
* Licensed under the EUPL, Version 1.1 or – as soon they will be
* approved by the European Commission - subsequent versions of the
* EUPL (the "Licence");
*
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl5
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
* #L%
*/
package no.entur.schema2proto.modifyproto.config;

public class ModifyField {
/**
* Name of the existing message type.
*/
public String targetMessageType;

/**
* Name of the field on the existing message type to modify.
*/
public String field;

/**
* Regex pattern to match in the existing documentation. If not provided, the existing documentation is completely replaced by
* {@link ModifyField#documentation}.
*/
public String documentationPattern;

/**
* Replacement string for the documentation of the existing field. Regex capturing groups from the {@link ModifyField#documentationPattern} are supported.
*/
public String documentation;
}
Expand Up @@ -33,6 +33,7 @@ public class ModifyProtoConfiguration {
public List<String> includes = new ArrayList<>();
public List<String> excludes = new ArrayList<>();
public List<NewField> newFields = new ArrayList<>();
public List<ModifyField> modifyFields = new ArrayList<>();
public List<MergeFrom> mergeFrom = new ArrayList<>();
public List<String> customImportLocations = new ArrayList<>();
public List<NewEnumConstant> newEnumConstants = new ArrayList<>();
Expand Down
26 changes: 26 additions & 0 deletions schema2proto-lib/src/test/java/no/entur/schema2proto/modifyproto/ModifyProtoTest.java 100755 → 100644
Expand Up @@ -35,6 +35,7 @@
import no.entur.schema2proto.InvalidConfigurationException;
import no.entur.schema2proto.modifyproto.config.FieldOption;
import no.entur.schema2proto.modifyproto.config.MergeFrom;
import no.entur.schema2proto.modifyproto.config.ModifyField;
import no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration;
import no.entur.schema2proto.modifyproto.config.NewField;

Expand Down Expand Up @@ -183,6 +184,31 @@ public void testAddField() throws IOException, InvalidProtobufException, Invalid

}

@Test
public void testModifyField() 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();

ModifyField modifyField = new ModifyField();
modifyField.targetMessageType = "A";
modifyField.field = "response_timestamp";
modifyField.documentationPattern = "(^.*$)";
modifyField.documentation = "[Additional documentation] $1";

ModifyField modifyField2 = new ModifyField();
modifyField2.targetMessageType = "B";
modifyField2.field = "value";
modifyField2.documentationPattern = null;
modifyField2.documentation = "Whole documentation replaced";

ModifyProtoConfiguration configuration = new ModifyProtoConfiguration();
configuration.inputDirectory = source;
configuration.modifyFields = Arrays.asList(modifyField, modifyField2);
modifyProto(configuration);

compareExpectedAndGenerated(expected, "modifyfield.proto", generatedRootFolder, "simple.proto");
}

@Test
public void testAddFieldOption() throws IOException, InvalidProtobufException, InvalidConfigurationException {

Expand Down
@@ -0,0 +1,22 @@
// default.proto at 0:0
syntax = "proto3";

// Type for Status of termination response.
message A {
// [Additional documentation] Time individual response element was created.
uint64 response_timestamp = 1 [(validate.rules).uint64.gte = 20];
}

message B {
LangType lang = 1;
// Whole documentation replaced
string value = 2;

}

enum LangType {
// Default
LANG_TYPE_UNSPECIFIED = 0;
LANG_TYPE_A = 1;
LANG_TYPE_B = 2;
}
2 changes: 1 addition & 1 deletion schema2proto-maven-plugin/pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>no.entur</groupId>
<artifactId>schema2proto-parent</artifactId>
<version>1.84</version>
<version>1.85</version>
</parent>
<artifactId>schema2proto-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
Expand Down
2 changes: 1 addition & 1 deletion schema2proto-wire/pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>no.entur</groupId>
<artifactId>schema2proto-parent</artifactId>
<version>1.84</version>
<version>1.85</version>
</parent>
<artifactId>schema2proto-wire</artifactId>
<packaging>jar</packaging>
Expand Down
Expand Up @@ -54,7 +54,7 @@ public final class Field {
private final Location location;
private Label label;
private String name;
private final String documentation;
private String documentation;
private int tag;
private final String defaultValue;
private String elementType;
Expand Down Expand Up @@ -301,6 +301,10 @@ public void updateName(String newFieldName) {
name = newFieldName;
}

public void updateDocumentation(String newDocumentation) {
documentation = newDocumentation;
}

public String getElementType() {
return elementType;
}
Expand Down
2 changes: 1 addition & 1 deletion schema2proto-xsdproto/pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>no.entur</groupId>
<artifactId>schema2proto-parent</artifactId>
<version>1.84</version>
<version>1.85</version>
</parent>
<artifactId>schema2proto-xsdproto</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion schema2proto-xsom/pom.xml
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>no.entur</groupId>
<artifactId>schema2proto-parent</artifactId>
<version>1.84</version>
<version>1.85</version>
</parent>
<artifactId>schema2proto-xsom</artifactId>
<name>schema2proto-XSOM</name>
Expand Down

0 comments on commit d6ae49b

Please sign in to comment.