diff --git a/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/compiler/Generator.scala b/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/compiler/Generator.scala index d14e1c0..bb1c27a 100644 --- a/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/compiler/Generator.scala +++ b/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/compiler/Generator.scala @@ -766,8 +766,9 @@ object Generator { }.foreach { case (name, symbol) => // namespaces might be empty for imported message types - val namespacePrefix = if (symbol.packageName.isEmpty) "" else symbol.packageName + "." val protoPkgPrefix = if (symbol.protoPackage.isEmpty) "" else symbol.protoPackage + "." + // Java package defaults to proto package according to spec + val namespacePrefix = if (symbol.packageName.isEmpty) protoPkgPrefix else symbol.packageName + "." field.fType.scalaType = namespacePrefix + field.fType.scalaType.stripPrefix(protoPkgPrefix) field.fType.defaultValue = namespacePrefix + field.fType.defaultValue.stripPrefix(protoPkgPrefix) } diff --git a/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/test/UpdateTestResources.scala b/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/test/UpdateTestResources.scala index c8a618e..970dc6c 100644 --- a/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/test/UpdateTestResources.scala +++ b/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/test/UpdateTestResources.scala @@ -70,7 +70,10 @@ object UpdateTestResources extends App { // if we have a valid parsing tree, generate a Scala proto class. // for now, this is hard-coded. - val importedSymbols = Map("PackageTest" -> ImportedSymbol("resources.generated.nested", isEnum = false, "resources.generated.nested")) + val importedSymbols = Map( + "PackageTest" -> ImportedSymbol("resources.generated.nested", isEnum = false, "resources.generated.nested"), + "PackageTestNoJavaPackage" -> ImportedSymbol("", isEnum = false, "resources.generated.nested") + ) val generated = Generator(parsed, file.getName, importedSymbols, generateJsonMethod = true, None) val generatedPath = testDir + generated.path + generated.file + ".scala" diff --git a/scalabuff-compiler/src/test/resources/generated/ImportPackagesNoJavaPackage.scala b/scalabuff-compiler/src/test/resources/generated/ImportPackagesNoJavaPackage.scala new file mode 100644 index 0000000..cb23338 --- /dev/null +++ b/scalabuff-compiler/src/test/resources/generated/ImportPackagesNoJavaPackage.scala @@ -0,0 +1,103 @@ +// Generated by ScalaBuff, the Scala Protocol Buffers compiler. DO NOT EDIT! +// source: import_packages_no_java_package.proto + +package resources.generated + +//import "package_name_no_java_package.proto" + +final case class UsesPackageTestNoJavaPackage ( + `packageTest`: resources.generated.nested.PackageTestNoJavaPackage = resources.generated.nested.PackageTestNoJavaPackage.defaultInstance +) extends com.google.protobuf.GeneratedMessageLite + with com.google.protobuf.MessageLite.Builder + with net.sandrogrzicic.scalabuff.Message[UsesPackageTestNoJavaPackage] + with net.sandrogrzicic.scalabuff.Parser[UsesPackageTestNoJavaPackage] { + + + + def writeTo(output: com.google.protobuf.CodedOutputStream) { + output.writeMessage(1, `packageTest`) + } + + def getSerializedSize = { + import com.google.protobuf.CodedOutputStream._ + var __size = 0 + __size += computeMessageSize(1, `packageTest`) + + __size + } + + def mergeFrom(in: com.google.protobuf.CodedInputStream, extensionRegistry: com.google.protobuf.ExtensionRegistryLite): UsesPackageTestNoJavaPackage = { + import com.google.protobuf.ExtensionRegistryLite.{getEmptyRegistry => _emptyRegistry} + var __packageTest: resources.generated.nested.PackageTestNoJavaPackage = resources.generated.nested.PackageTestNoJavaPackage.defaultInstance + + def __newMerged = UsesPackageTestNoJavaPackage( + __packageTest + ) + while (true) in.readTag match { + case 0 => return __newMerged + case 10 => __packageTest = readMessage[resources.generated.nested.PackageTestNoJavaPackage](in, __packageTest, _emptyRegistry) + case default => if (!in.skipField(default)) return __newMerged + } + null + } + + def mergeFrom(m: UsesPackageTestNoJavaPackage) = { + UsesPackageTestNoJavaPackage( + m.`packageTest` + ) + } + + def getDefaultInstanceForType = UsesPackageTestNoJavaPackage.defaultInstance + def clear = getDefaultInstanceForType + def isInitialized = true + def build = this + def buildPartial = this + def parsePartialFrom(cis: com.google.protobuf.CodedInputStream, er: com.google.protobuf.ExtensionRegistryLite) = mergeFrom(cis, er) + override def getParserForType = this + def newBuilderForType = getDefaultInstanceForType + def toBuilder = this + def toJson(indent: Int = 0): String = { + val indent0 = "\n" + ("\t" * indent) + val (indent1, indent2) = (indent0 + "\t", indent0 + "\t\t") + val sb = StringBuilder.newBuilder + sb + .append("{") + sb.append(indent1).append("\"packageTest\": ").append(`packageTest`.toJson(indent + 1)).append(',') + if (sb.last.equals(',')) sb.length -= 1 + sb.append(indent0).append("}") + sb.toString() + } + +} + +object UsesPackageTestNoJavaPackage { + @scala.beans.BeanProperty val defaultInstance = new UsesPackageTestNoJavaPackage() + + def parseFrom(data: Array[Byte]): UsesPackageTestNoJavaPackage = defaultInstance.mergeFrom(data) + def parseFrom(data: Array[Byte], offset: Int, length: Int): UsesPackageTestNoJavaPackage = defaultInstance.mergeFrom(data, offset, length) + def parseFrom(byteString: com.google.protobuf.ByteString): UsesPackageTestNoJavaPackage = defaultInstance.mergeFrom(byteString) + def parseFrom(stream: java.io.InputStream): UsesPackageTestNoJavaPackage = defaultInstance.mergeFrom(stream) + def parseDelimitedFrom(stream: java.io.InputStream): Option[UsesPackageTestNoJavaPackage] = defaultInstance.mergeDelimitedFromStream(stream) + + val PACKAGE_TEST_FIELD_NUMBER = 1 + + def newBuilder = defaultInstance.newBuilderForType + def newBuilder(prototype: UsesPackageTestNoJavaPackage) = defaultInstance.mergeFrom(prototype) + +} + +object ImportPackagesNoJavaPackage { + def registerAllExtensions(registry: com.google.protobuf.ExtensionRegistryLite) { + } + + private val fromBinaryHintMap = collection.immutable.HashMap[String, Array[Byte] ⇒ com.google.protobuf.GeneratedMessageLite]( + "UsesPackageTestNoJavaPackage" -> (bytes ⇒ UsesPackageTestNoJavaPackage.parseFrom(bytes)) + ) + + def deserializePayload(payload: Array[Byte], payloadType: String): com.google.protobuf.GeneratedMessageLite = { + fromBinaryHintMap.get(payloadType) match { + case Some(f) ⇒ f(payload) + case None ⇒ throw new IllegalArgumentException(s"unimplemented deserialization of message payload of type [${payloadType}]") + } + } +} diff --git a/scalabuff-compiler/src/test/resources/generated/nested/PackageNameNoJavaPackage.scala b/scalabuff-compiler/src/test/resources/generated/nested/PackageNameNoJavaPackage.scala new file mode 100644 index 0000000..96196a5 --- /dev/null +++ b/scalabuff-compiler/src/test/resources/generated/nested/PackageNameNoJavaPackage.scala @@ -0,0 +1,101 @@ +// Generated by ScalaBuff, the Scala Protocol Buffers compiler. DO NOT EDIT! +// source: package_name_no_java_package.proto + +package resources.generated.nested + +final case class PackageTestNoJavaPackage ( + `requiredField`: Int = 0 +) extends com.google.protobuf.GeneratedMessageLite + with com.google.protobuf.MessageLite.Builder + with net.sandrogrzicic.scalabuff.Message[PackageTestNoJavaPackage] + with net.sandrogrzicic.scalabuff.Parser[PackageTestNoJavaPackage] { + + + + def writeTo(output: com.google.protobuf.CodedOutputStream) { + output.writeInt32(1, `requiredField`) + } + + def getSerializedSize = { + import com.google.protobuf.CodedOutputStream._ + var __size = 0 + __size += computeInt32Size(1, `requiredField`) + + __size + } + + def mergeFrom(in: com.google.protobuf.CodedInputStream, extensionRegistry: com.google.protobuf.ExtensionRegistryLite): PackageTestNoJavaPackage = { + import com.google.protobuf.ExtensionRegistryLite.{getEmptyRegistry => _emptyRegistry} + var __requiredField: Int = 0 + + def __newMerged = PackageTestNoJavaPackage( + __requiredField + ) + while (true) in.readTag match { + case 0 => return __newMerged + case 8 => __requiredField = in.readInt32() + case default => if (!in.skipField(default)) return __newMerged + } + null + } + + def mergeFrom(m: PackageTestNoJavaPackage) = { + PackageTestNoJavaPackage( + m.`requiredField` + ) + } + + def getDefaultInstanceForType = PackageTestNoJavaPackage.defaultInstance + def clear = getDefaultInstanceForType + def isInitialized = true + def build = this + def buildPartial = this + def parsePartialFrom(cis: com.google.protobuf.CodedInputStream, er: com.google.protobuf.ExtensionRegistryLite) = mergeFrom(cis, er) + override def getParserForType = this + def newBuilderForType = getDefaultInstanceForType + def toBuilder = this + def toJson(indent: Int = 0): String = { + val indent0 = "\n" + ("\t" * indent) + val (indent1, indent2) = (indent0 + "\t", indent0 + "\t\t") + val sb = StringBuilder.newBuilder + sb + .append("{") + sb.append(indent1).append("\"requiredField\": ").append("\"").append(`requiredField`).append("\"").append(',') + if (sb.last.equals(',')) sb.length -= 1 + sb.append(indent0).append("}") + sb.toString() + } + +} + +object PackageTestNoJavaPackage { + @scala.beans.BeanProperty val defaultInstance = new PackageTestNoJavaPackage() + + def parseFrom(data: Array[Byte]): PackageTestNoJavaPackage = defaultInstance.mergeFrom(data) + def parseFrom(data: Array[Byte], offset: Int, length: Int): PackageTestNoJavaPackage = defaultInstance.mergeFrom(data, offset, length) + def parseFrom(byteString: com.google.protobuf.ByteString): PackageTestNoJavaPackage = defaultInstance.mergeFrom(byteString) + def parseFrom(stream: java.io.InputStream): PackageTestNoJavaPackage = defaultInstance.mergeFrom(stream) + def parseDelimitedFrom(stream: java.io.InputStream): Option[PackageTestNoJavaPackage] = defaultInstance.mergeDelimitedFromStream(stream) + + val REQUIRED_FIELD_FIELD_NUMBER = 1 + + def newBuilder = defaultInstance.newBuilderForType + def newBuilder(prototype: PackageTestNoJavaPackage) = defaultInstance.mergeFrom(prototype) + +} + +object PackageNameNoJavaPackage { + def registerAllExtensions(registry: com.google.protobuf.ExtensionRegistryLite) { + } + + private val fromBinaryHintMap = collection.immutable.HashMap[String, Array[Byte] ⇒ com.google.protobuf.GeneratedMessageLite]( + "PackageTestNoJavaPackage" -> (bytes ⇒ PackageTestNoJavaPackage.parseFrom(bytes)) + ) + + def deserializePayload(payload: Array[Byte], payloadType: String): com.google.protobuf.GeneratedMessageLite = { + fromBinaryHintMap.get(payloadType) match { + case Some(f) ⇒ f(payload) + case None ⇒ throw new IllegalArgumentException(s"unimplemented deserialization of message payload of type [${payloadType}]") + } + } +} diff --git a/scalabuff-compiler/src/test/resources/parsed/ImportPackagesNoJavaPackage.txt b/scalabuff-compiler/src/test/resources/parsed/ImportPackagesNoJavaPackage.txt new file mode 100644 index 0000000..ce127bc --- /dev/null +++ b/scalabuff-compiler/src/test/resources/parsed/ImportPackagesNoJavaPackage.txt @@ -0,0 +1 @@ +List(PackageStatement(resources.generated), ImportStatement("package_name_no_java_package.proto"), Message(UsesPackageTestNoJavaPackage,MessageBody(List(Field(required,PackageTestNoJavaPackage,package_test,1,List(),)),List(),List(),List(),List(),List(),List()))) diff --git a/scalabuff-compiler/src/test/resources/parsed/PackageNameNoJavaPackage.txt b/scalabuff-compiler/src/test/resources/parsed/PackageNameNoJavaPackage.txt new file mode 100644 index 0000000..7723ca3 --- /dev/null +++ b/scalabuff-compiler/src/test/resources/parsed/PackageNameNoJavaPackage.txt @@ -0,0 +1 @@ +List(PackageStatement(resources.generated.nested), Message(PackageTestNoJavaPackage,MessageBody(List(Field(required,Int32,required_field,1,List(),)),List(),List(),List(),List(),List(),List()))) diff --git a/scalabuff-compiler/src/test/resources/proto/import_packages_no_java_package.proto b/scalabuff-compiler/src/test/resources/proto/import_packages_no_java_package.proto new file mode 100644 index 0000000..ef79f33 --- /dev/null +++ b/scalabuff-compiler/src/test/resources/proto/import_packages_no_java_package.proto @@ -0,0 +1,7 @@ +package resources.generated; + +import "package_name_no_java_package.proto"; + +message UsesPackageTestNoJavaPackage { + required PackageTestNoJavaPackage package_test = 1; +} diff --git a/scalabuff-compiler/src/test/resources/proto/package_name_no_java_package.proto b/scalabuff-compiler/src/test/resources/proto/package_name_no_java_package.proto new file mode 100644 index 0000000..a95626c --- /dev/null +++ b/scalabuff-compiler/src/test/resources/proto/package_name_no_java_package.proto @@ -0,0 +1,5 @@ +package resources.generated.nested; + +message PackageTestNoJavaPackage { + required int32 required_field = 1; +} diff --git a/scalabuff-compiler/src/test/tests/ScalaBuffTest.scala b/scalabuff-compiler/src/test/tests/ScalaBuffTest.scala index b7360d5..1f9eabc 100644 --- a/scalabuff-compiler/src/test/tests/ScalaBuffTest.scala +++ b/scalabuff-compiler/src/test/tests/ScalaBuffTest.scala @@ -159,6 +159,8 @@ class ScalaBuffTest extends FunSuite with Matchers { compile("package_name", Some("nested")) compile("import_packages", None) compile("import_use_fullname", None) + compile("package_name_no_java_package", Some("nested")) + compile("import_packages_no_java_package", None) } test("run: unknown option") {