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 bb1c27a..debe536 100644 --- a/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/compiler/Generator.scala +++ b/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/compiler/Generator.scala @@ -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" 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 970dc6c..5af9ebf 100644 --- a/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/test/UpdateTestResources.scala +++ b/scalabuff-compiler/src/main/net/sandrogrzicic/scalabuff/test/UpdateTestResources.scala @@ -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) diff --git a/scalabuff-compiler/src/test/resources/generated/ImportEnum.scala b/scalabuff-compiler/src/test/resources/generated/ImportEnum.scala new file mode 100644 index 0000000..4fe18f2 --- /dev/null +++ b/scalabuff-compiler/src/test/resources/generated/ImportEnum.scala @@ -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}]") + } + } +} diff --git a/scalabuff-compiler/src/test/resources/generated/nested/EnumToImport.scala b/scalabuff-compiler/src/test/resources/generated/nested/EnumToImport.scala new file mode 100644 index 0000000..237f6dd --- /dev/null +++ b/scalabuff-compiler/src/test/resources/generated/nested/EnumToImport.scala @@ -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}]") + } + } +} diff --git a/scalabuff-compiler/src/test/resources/generated/other/EnumToImport.scala b/scalabuff-compiler/src/test/resources/generated/other/EnumToImport.scala new file mode 100644 index 0000000..0c87601 --- /dev/null +++ b/scalabuff-compiler/src/test/resources/generated/other/EnumToImport.scala @@ -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}]") + } + } +} diff --git a/scalabuff-compiler/src/test/resources/parsed/EnumToImport.txt b/scalabuff-compiler/src/test/resources/parsed/EnumToImport.txt new file mode 100644 index 0000000..816f774 --- /dev/null +++ b/scalabuff-compiler/src/test/resources/parsed/EnumToImport.txt @@ -0,0 +1 @@ +List(PackageStatement(resources.generated.nested), EnumStatement(AnEnumToImport,List(EnumConstant(KEYBOARD,1), EnumConstant(MOUSE,2)),List())) diff --git a/scalabuff-compiler/src/test/resources/parsed/ImportEnum.txt b/scalabuff-compiler/src/test/resources/parsed/ImportEnum.txt new file mode 100644 index 0000000..0731da1 --- /dev/null +++ b/scalabuff-compiler/src/test/resources/parsed/ImportEnum.txt @@ -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()))) diff --git a/scalabuff-compiler/src/test/resources/proto/enum_to_import.proto b/scalabuff-compiler/src/test/resources/proto/enum_to_import.proto new file mode 100644 index 0000000..3fdecdb --- /dev/null +++ b/scalabuff-compiler/src/test/resources/proto/enum_to_import.proto @@ -0,0 +1,6 @@ +package resources.generated.nested; + +enum AnEnumToImport { + KEYBOARD = 1; + MOUSE = 2; +} \ No newline at end of file diff --git a/scalabuff-compiler/src/test/resources/proto/import_enum.proto b/scalabuff-compiler/src/test/resources/proto/import_enum.proto new file mode 100644 index 0000000..d9e27c2 --- /dev/null +++ b/scalabuff-compiler/src/test/resources/proto/import_enum.proto @@ -0,0 +1,7 @@ +package resources.generated; + +import "enum_to_import.proto"; + +message AnImportEnumMessage { + required resources.generated.nested.AnEnumToImport to_import = 1; +} \ No newline at end of file diff --git a/scalabuff-compiler/src/test/tests/ScalaBuffTest.scala b/scalabuff-compiler/src/test/tests/ScalaBuffTest.scala index 1f9eabc..10792d2 100644 --- a/scalabuff-compiler/src/test/tests/ScalaBuffTest.scala +++ b/scalabuff-compiler/src/test/tests/ScalaBuffTest.scala @@ -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") {