Skip to content

Commit

Permalink
Process aux field options in order, and let local setting takes over
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Jan 12, 2021
1 parent 63c1e1f commit 007572c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,11 @@ class DescriptorImplicits private[compiler] (
def fieldOptions: FieldOptions = {
val localOptions = fd.getOptions.getExtension[FieldOptions](Scalapb.field)

fd.getFile.scalaOptions.getAuxFieldOptionsList.asScala
.filter(_.getTarget == fd.getFullName())
.foldLeft(localOptions)((local, aux) =>
FieldOptions.newBuilder(aux.getOptions).mergeFrom(local).build
(fd.getFile.scalaOptions.getAuxFieldOptionsList.asScala
.collect {
case opt if opt.getTarget == fd.getFullName() => opt.getOptions
} :+ localOptions).reduce[FieldOptions](
(left, right) => left.toBuilder.mergeFrom(right).build()
)
}

Expand Down
41 changes: 41 additions & 0 deletions e2e/src/main/protobuf/scoped/order.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
syntax = "proto3";

package scalapb.e2e.scoped;

import "scalapb/scalapb.proto";

message OrderTest {
int32 x1 = 11; // renamed at package level and also locally here.
// File should take over.
int32 x2 = 12; // renamed in file twice. Second override should take over.
int32 x3 = 13 [
(scalapb.field).scala_name = "local_x3"
]; // renamed in file, and also locally. Local should override.
}

option (scalapb.options) = {
scope : FILE
// For x1
aux_field_options : [
{
target : "scalapb.e2e.scoped.OrderTest.x1"
options : {scala_name : "file_x1"}
},

// For x2
{
target : "scalapb.e2e.scoped.OrderTest.x2"
options : {scala_name : "file_x2_take1"}
},
{
target : "scalapb.e2e.scoped.OrderTest.x2"
options : {scala_name : "file_x2_take2"}
},

// For x3
{
target : "scalapb.e2e.scoped.OrderTest.x3"
options : {scala_name : "file_x3"}
}
]
};
11 changes: 11 additions & 0 deletions e2e/src/main/protobuf/scoped/package.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ option (scalapb.options) = {
}
}
]


// Testing order
aux_field_options: [
{
target: "scalapb.e2e.scoped.Foo.x1"
options: {
scala_name: "package_x1"
}
}
]
};
4 changes: 3 additions & 1 deletion e2e/src/main/protobuf/scoped/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax = "proto3";

package scalapb.e2e.scoped;

import "scalapb/scalapb.proto";

message Foo {
enum Vals {
DEFAULT = 0;
Expand All @@ -13,4 +15,4 @@ message Foo {
bytes b = 3;
}

message Untargeted { }
message Untargeted { }
17 changes: 17 additions & 0 deletions e2e/src/test/scala/scoped/OrderSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package scalapb.e2e.scoped

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers

import scalapb.changed.scoped.OrderTest

class OrderSpec extends AnyFlatSpec with Matchers {
// The goal of this test is to validate the order aux_field_options are applied.
// Having the code compile with the expected field name verifies that the expected
// transformation occurred.
val M = OrderTest(
file_x1 = 1,
file_x2_take2 = 2,
local_x3 = 3
)
}

0 comments on commit 007572c

Please sign in to comment.