Skip to content

Commit

Permalink
fix: codegen for outer class and multiple files variations (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvlugter authored Sep 16, 2021
1 parent 0afc814 commit 2f63014
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ case class FullyQualifiedName(protoName: String, name: String, parent: PackageNa
lazy val fullQualifiedName = s"${parent.javaPackage}.$name"
lazy val fullName = {
if (parent.javaMultipleFiles) name
else
parent.javaOuterClassnameOption
.map(outer => s"$outer.$name")
.getOrElse(name)
else s"${parent.javaOuterClassname}.$name"
}

lazy val typeImport = s"${parent.javaPackage}.$fullName"
Expand Down Expand Up @@ -86,13 +83,29 @@ case class PackageNaming(
pkg: String,
goPackage: Option[String],
javaPackageOption: Option[String],
javaOuterClassnameOption: Option[String],
javaOuterClassname: String,
javaMultipleFiles: Boolean) {
lazy val javaPackage: String = javaPackageOption.getOrElse(pkg)
lazy val javaOuterClassname: String = javaOuterClassnameOption.getOrElse(name)
}

object PackageNaming {
def apply(
protoFileName: String,
name: String,
pkg: String,
goPackage: Option[String],
javaPackageOption: Option[String],
javaOuterClassnameOption: Option[String],
javaMultipleFiles: Boolean): PackageNaming =
PackageNaming(
protoFileName,
name,
pkg,
goPackage,
javaPackageOption,
javaOuterClassnameOption.getOrElse(name),
javaMultipleFiles)

def from(descriptor: Descriptors.FileDescriptor): PackageNaming = {
val name =
descriptor.getName
Expand All @@ -114,11 +127,20 @@ object PackageNaming {
val javaPackage =
generalOptions.get("google.protobuf.FileOptions.java_package").map(_.toString())

val javaOuterClassname =
val javaOuterClassnameOption =
generalOptions
.get("google.protobuf.FileOptions.java_outer_classname")
.map(_.toString())

val javaOuterClassname =
javaOuterClassnameOption.getOrElse {
val existingNames =
descriptor.getMessageTypes.asScala.map(_.getName) ++
descriptor.getEnumTypes.asScala.map(_.getName) ++
descriptor.getServices.asScala.map(_.getName)
if (existingNames.contains(name)) name + "OuterClass" else name
}

val javaMultipleFiles =
generalOptions.get("google.protobuf.FileOptions.java_multiple_files").contains(true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ syntax = "proto3";

package com.example;

// FIXME: action generation requires a java_outer_classname
// this should not be mandatory, it isn't for the other components
option java_outer_classname = "EchoActionModel";

import "google/protobuf/empty.proto";
Expand All @@ -43,4 +41,4 @@ service EchoAction {
rpc Create(CreateUser) returns (google.protobuf.Empty){

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 Lightbend Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package com.example.action1;

// no java_multiple_files option, classes will be wrapped in outer class
// no java_outer_classname option, will default to "SimpleActionOuterClass"

import "akkaserverless/annotations.proto";

message Request {}
message Response {}

service SimpleAction {
option (akkaserverless.service) = {
type: SERVICE_TYPE_ACTION
};

rpc Method(Request) returns (Response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 Lightbend Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package com.example.action2;

// no java_multiple_files option, classes will be wrapped in outer class
// no java_outer_classname option, will default to "SimpleActionApi"

import "akkaserverless/annotations.proto";

message Request {}
message Response {}

service AnotherSimpleAction {
option (akkaserverless.service) = {
type: SERVICE_TYPE_ACTION
};

rpc Method(Request) returns (Response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 Lightbend Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package com.example.action3;

option java_multiple_files = true;
// no java_outer_classname option, will default to "SomeActionOuterClass"

import "akkaserverless/annotations.proto";

message Request {}
message Response {}

service SomeAction {
option (akkaserverless.service) = {
type: SERVICE_TYPE_ACTION
};

rpc Method(Request) returns (Response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 Lightbend Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package com.example.action4;

option java_multiple_files = true;
// no java_outer_classname option, will default to "SomeOtherActionApi"

import "akkaserverless/annotations.proto";

message Request {}
message Response {}

service SomeOtherAction {
option (akkaserverless.service) = {
type: SERVICE_TYPE_ACTION
};

rpc Method(Request) returns (Response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 Lightbend Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package com.example.action5;

option java_multiple_files = true;
option java_outer_classname = "YetAnotherActionApi";

import "akkaserverless/annotations.proto";

message Request {}
message Response {}

service YetAnotherAction {
option (akkaserverless.service) = {
type: SERVICE_TYPE_ACTION
};

rpc Method(Request) returns (Response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2021 Lightbend Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package com.example.eventsourcedentity;

import "akkaserverless/annotations.proto";

option java_multiple_files = true;
// no java_outer_classname option, will default to "SomeEventSourcedEntityApi"

message Request {}
message Response {}

service SomeEventSourcedEntityService {
option (akkaserverless.service) = {
type: SERVICE_TYPE_ENTITY
component: "com.example.eventsourcedentity.domain.SomeEventSourcedEntity"
};

rpc Method(Request) returns (Response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021 Lightbend Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package com.example.eventsourcedentity.domain;

import "akkaserverless/annotations.proto";

option java_multiple_files = true;
// no java_outer_classname option, will default to "SomeEventSourcedEntityDomain"

option (akkaserverless.file).event_sourced_entity = {
name: "SomeEventSourcedEntity"
entity_type: "some-event-sourced-entity"
state: "SomeEventSourcedEntityState"
events: ["SomeEvent"]
};

message SomeEventSourcedEntityState {
string some_field = 1;
}

message SomeEvent {
string some_field = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2021 Lightbend Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package com.example.valueentity;

import "akkaserverless/annotations.proto";

option java_multiple_files = true;
// no java_outer_classname option, will default to "SomeValueEntityApi"

message Request {}
message Response {}

service SomeValueEntityService {
option (akkaserverless.service) = {
type: SERVICE_TYPE_ENTITY
component: "com.example.valueentity.domain.SomeValueEntity"
};

rpc Method(Request) returns (Response);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2021 Lightbend Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package com.example.valueentity.domain;

import "akkaserverless/annotations.proto";

option java_multiple_files = true;
// no java_outer_classname option, will default to "SomeValueEntityDomain"

option (akkaserverless.file).value_entity = {
name: "SomeValueEntity"
entity_type: "some-value-entity"
state: "SomeValueEntityState"
};

message SomeValueEntityState {
string some_field = 1;
}
Loading

0 comments on commit 2f63014

Please sign in to comment.