Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import proto (without java_package) from another package #107

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ object Generator {
/** Update fields which have custom types. */
protected def fixCustomTypes(tree: List[Node], enumNames: mutable.Set[String], customFieldTypes: mutable.Buffer[EnumVal], importedSymbols: Map[String, ImportedSymbol]) {
for (fType <- customFieldTypes if !fType.isMessage && !fType.isEnum) {
if (enumNames.contains(fType.name.dropUntilLast('.')) || importedSymbols.get(fType.scalaType).exists(_.isEnum)) {
if (enumNames.contains(fType.name.dropUntilLast('.')) || importedSymbols.get(fType.scalaType.dropUntilLast('.')).exists(_.isEnum)) {
fType.isEnum = true
fType.name = "Enum"
fType.defaultValue = fType.scalaType + "._UNINITIALIZED"
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ object UpdateTestResources extends App {
val protoFiles = protoDirFile.listFiles(protoFileFilter)
for (file <- protoFiles) {
val fileName = file.getName.dropRight(protoExtension.length).camelCase
val generatedParsedFile = new File(parsedDir + fileName + parsedExtension)
val generatedParsedFile = new File(parsedDir + fileName.camelCase + parsedExtension)
generatedParsedFile.delete()

val generatedParsed = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(generatedParsedFile), "utf-8"))
Expand Down Expand Up @@ -70,7 +70,11 @@ 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("nested", isEnum = false))
val importedSymbols = Map(
"PackageTest" -> ImportedSymbol("resources.generated.nested", isEnum = false, "resources.generated.nested"),
"PackageTestNoJavaPackage" -> ImportedSymbol("", isEnum = false, "resources.generated.nested"),
"AnEnumToImport" -> ImportedSymbol("", isEnum = true, "resources.generated.nested")
)

val generated = Generator(parsed, file.getName, importedSymbols, generateJsonMethod = true, None)
val generatedPath = testDir + generated.path + generated.file + ".scala"
Expand Down
103 changes: 103 additions & 0 deletions scalabuff-compiler/src/test/resources/generated/ImportEnum.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Generated by ScalaBuff, the Scala Protocol Buffers compiler. DO NOT EDIT!
// source: import_enum.proto

package resources.generated

//import "enum_to_import.proto"

final case class AnImportEnumMessage (
`toImport`: resources.generated.nested.AnEnumToImport.EnumVal = resources.generated.nested.AnEnumToImport._UNINITIALIZED
) extends com.google.protobuf.GeneratedMessageLite
with com.google.protobuf.MessageLite.Builder
with net.sandrogrzicic.scalabuff.Message[AnImportEnumMessage]
with net.sandrogrzicic.scalabuff.Parser[AnImportEnumMessage] {



def writeTo(output: com.google.protobuf.CodedOutputStream) {
output.writeEnum(1, `toImport`)
}

def getSerializedSize = {
import com.google.protobuf.CodedOutputStream._
var __size = 0
__size += computeEnumSize(1, `toImport`)

__size
}

def mergeFrom(in: com.google.protobuf.CodedInputStream, extensionRegistry: com.google.protobuf.ExtensionRegistryLite): AnImportEnumMessage = {
import com.google.protobuf.ExtensionRegistryLite.{getEmptyRegistry => _emptyRegistry}
var __toImport: resources.generated.nested.AnEnumToImport.EnumVal = resources.generated.nested.AnEnumToImport._UNINITIALIZED

def __newMerged = AnImportEnumMessage(
__toImport
)
while (true) in.readTag match {
case 0 => return __newMerged
case 8 => __toImport = resources.generated.nested.AnEnumToImport.valueOf(in.readEnum())
case default => if (!in.skipField(default)) return __newMerged
}
null
}

def mergeFrom(m: AnImportEnumMessage) = {
AnImportEnumMessage(
m.`toImport`
)
}

def getDefaultInstanceForType = AnImportEnumMessage.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("\"toImport\": ").append("\"").append(`toImport`).append("\"").append(',')
if (sb.last.equals(',')) sb.length -= 1
sb.append(indent0).append("}")
sb.toString()
}

}

object AnImportEnumMessage {
@scala.beans.BeanProperty val defaultInstance = new AnImportEnumMessage()

def parseFrom(data: Array[Byte]): AnImportEnumMessage = defaultInstance.mergeFrom(data)
def parseFrom(data: Array[Byte], offset: Int, length: Int): AnImportEnumMessage = defaultInstance.mergeFrom(data, offset, length)
def parseFrom(byteString: com.google.protobuf.ByteString): AnImportEnumMessage = defaultInstance.mergeFrom(byteString)
def parseFrom(stream: java.io.InputStream): AnImportEnumMessage = defaultInstance.mergeFrom(stream)
def parseDelimitedFrom(stream: java.io.InputStream): Option[AnImportEnumMessage] = defaultInstance.mergeDelimitedFromStream(stream)

val TO_IMPORT_FIELD_NUMBER = 1

def newBuilder = defaultInstance.newBuilderForType
def newBuilder(prototype: AnImportEnumMessage) = defaultInstance.mergeFrom(prototype)

}

object ImportEnum {
def registerAllExtensions(registry: com.google.protobuf.ExtensionRegistryLite) {
}

private val fromBinaryHintMap = collection.immutable.HashMap[String, Array[Byte] ⇒ com.google.protobuf.GeneratedMessageLite](
"AnImportEnumMessage" -> (bytes ⇒ AnImportEnumMessage.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}]")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,18 @@ final case class UsesImportPackage (
override def getParserForType = this
def newBuilderForType = getDefaultInstanceForType
def toBuilder = this
def toJson(indent: Int = 0): String = "ScalaBuff JSON generation not enabled. Use --generate_json_method to enable."
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 UsesImportPackage {
Expand Down
Original file line number Diff line number Diff line change
@@ -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}]")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,18 @@ final case class UseFullImportedName (
override def getParserForType = this
def newBuilderForType = getDefaultInstanceForType
def toBuilder = this
def toJson(indent: Int = 0): String = "ScalaBuff JSON generation not enabled. Use --generate_json_method to enable."
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("\"fullnameTest\": ").append(`fullnameTest`.toJson(indent + 1)).append(',')
if (sb.last.equals(',')) sb.length -= 1
sb.append(indent0).append("}")
sb.toString()
}

}

object UseFullImportedName {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Generated by ScalaBuff, the Scala Protocol Buffers compiler. DO NOT EDIT!
// source: enum_to_import.proto

package resources.generated.nested

object AnEnumToImport extends net.sandrogrzicic.scalabuff.Enum {
sealed trait EnumVal extends Value
val _UNINITIALIZED = new EnumVal { val name = "UNINITIALIZED ENUM VALUE"; val id = -1 }

val KEYBOARD = new EnumVal { val name = "KEYBOARD"; val id = 1 }
val MOUSE = new EnumVal { val name = "MOUSE"; val id = 2 }

val KEYBOARD_VALUE = 1
val MOUSE_VALUE = 2

def valueOf(id: Int) = id match {
case 1 => KEYBOARD
case 2 => MOUSE
case _default => throw new net.sandrogrzicic.scalabuff.UnknownEnumException(_default)
}
val internalGetValueMap = new com.google.protobuf.Internal.EnumLiteMap[EnumVal] {
def findValueByNumber(id: Int): EnumVal = valueOf(id)
}
}

object EnumToImport {
def registerAllExtensions(registry: com.google.protobuf.ExtensionRegistryLite) {
}

private val fromBinaryHintMap = collection.immutable.HashMap[String, Array[Byte] ⇒ com.google.protobuf.GeneratedMessageLite](

)

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}]")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ final case class PackageTest (
override def getParserForType = this
def newBuilderForType = getDefaultInstanceForType
def toBuilder = this
def toJson(indent: Int = 0): String = "ScalaBuff JSON generation not enabled. Use --generate_json_method to enable."
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 PackageTest {
Expand Down
Loading