Skip to content

Commit

Permalink
Importing a fully qualified enum doesn't work SandroGrzicic#109
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlar committed Mar 2, 2015
1 parent 6d27835 commit c5d4934
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 2 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ object UpdateTestResources extends App {
// for now, this is hard-coded.
val importedSymbols = Map(
"PackageTest" -> ImportedSymbol("resources.generated.nested", isEnum = false, "resources.generated.nested"),
"PackageTestNoJavaPackage" -> ImportedSymbol("", 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)
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
@@ -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
@@ -0,0 +1,40 @@
// Generated by ScalaBuff, the Scala Protocol Buffers compiler. DO NOT EDIT!
// source: enum_to_import.proto

package resources.generated.other

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
@@ -0,0 +1 @@
List(PackageStatement(resources.generated.nested), EnumStatement(AnEnumToImport,List(EnumConstant(KEYBOARD,1), EnumConstant(MOUSE,2)),List()))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
List(PackageStatement(resources.generated), ImportStatement("enum_to_import.proto"), Message(AnImportEnumMessage,MessageBody(List(Field(required,resources.generated.nested.AnEnumToImport,to_import,1,List(),)),List(),List(),List(),List(),List(),List())))
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package resources.generated.nested;

enum AnEnumToImport {
KEYBOARD = 1;
MOUSE = 2;
}
7 changes: 7 additions & 0 deletions scalabuff-compiler/src/test/resources/proto/import_enum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package resources.generated;

import "enum_to_import.proto";

message AnImportEnumMessage {
required resources.generated.nested.AnEnumToImport to_import = 1;
}
2 changes: 2 additions & 0 deletions scalabuff-compiler/src/test/tests/ScalaBuffTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class ScalaBuffTest extends FunSuite with Matchers {
compile("import_use_fullname", None)
compile("package_name_no_java_package", Some("nested"))
compile("import_packages_no_java_package", None)
compile("enum_to_import", Some("nested"))
compile("import_enum", None)
}

test("run: unknown option") {
Expand Down

0 comments on commit c5d4934

Please sign in to comment.