How to deserialize a protobuf in scala - scala

I have a protobuf serialized Object in a file generated by a Python process. I now need to deserialize this in Scala.
I dont see scala docs in official google protobuf documentation. Any references? How should i go about it.
I started looking into ScalaPB for this but getting below error. Any suggestions -
plugins.sbt file : `addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.34")
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.10.7"`
Error - [error] (update) sbt.librarymanagement.ResolveException: Error downloading com.thesamet:sbt-protoc;sbtVersion=1.0;scalaVersion=2.12:0.99.34 [error] Not found [error] Not found [error] not found: C:\Users\USER\.ivy2\local\com.thesamet\sbt-protoc\scala_2.12\sbt_1.0\0.99.34\ivys\ivy.xml [error] not found: https://repo1.maven.org/maven2/com/thesamet/sbt-protoc_2.12_1.0/0.99.34/sbt-protoc-0.99.34.pom [error] (ssExtractDependencies) sbt.librarymanagement.ResolveException: Error downloading com.thesamet:sbt-protoc;sbtVersion=1.0;scalaVersion=2.12:0.99.34 [error] Not found [error] Not found [error] not found: C:\Users\USER\.ivy2\local\com.thesamet\sbt-protoc\scala_2.12\sbt_1.0\0.99.34\ivys\ivy.xml [error] not found: https://repo1.maven.org/maven2/com/thesamet/sbt-protoc_2.12_1.0/0.99.34/sbt-protoc-0.99.34.pom [error] Total time: 8 s, completed Jul 23, 2020, 6:41:59 PM [info] shutting down sbt server

You need to have access to proto files that describe message schemas of objects that are serialised in the python process. The files must be shared between projects and it is mandatory information defining the contract.
You can then use ScalaPB to generate corresponding case classes. These classes are autogenerated by the sbt plugin and they provide helper methods that can be used for deserialisation. Check methods parseFrom provided in companion objects of generated Message classes

Related

Scalatra - not found: object scalate

Context: I'm looking to pick up Scalatra for the first time. I'm following instructions from Scalatra In Action as well as the sample code repo provided. I'm able to get a template project created using g8. When I update the code in the PageController file and start the jetty server I get some really strange error messaging, see below.
c:\Users\eric.barnes\Desktop\scalatra-cms>sbt
[info] welcome to sbt
1.4.5 (Oracle Corporation Java 16.0.2)
[info] loading global plugins from C:\Users\eric.barnes\.sbt\1.0\plugins
[info] loading settings for project scalatra-cms-build from plugins.sbt ...
[info] loading project definition from C:\Users\eric.barnes\Desktop\scalatra-cms\project
[info] loading settings for project hello from build.sbt ...
[info] set current project to Scalatra CMS (in build
file:/C:/Users/eric.barnes/Desktop/scalatra-cms/)
[info] sbt server started at local:sbt-server-dc962f233e7070ae620d
[info] started sbt server sbt:Scalatra CMS> ~jetty:start [info] compiling 1 Scala source to
C:\Users\eric.barnes\Desktop\scalatra-cms\target\scala-2.13\classes ...
[error] C:\Users\eric.barnes\Desktop\scalatra-
cms\src\main\scala\com\example\cms\PageController.scala:4:8: not found: object scalate
[error] import scalate.ScalateSupport [error] ^
[error] C:\Users\eric.barnes\Desktop\scalatra-
cms\src\main\scala\com\example\cms\PageController.scala:6:31: not found: type
ScalatraCmsStack [error] class PagesController extends ScalatraCmsStack {
[error] ^
[error] C:\Users\eric.barnes\Desktop\scalatra-
cms\src\main\scala\com\example\cms\PageController.scala:8:3: not found: value get
[error] get("/pages/:slug") {
[error] ^
[error] C:\Users\eric.barnes\Desktop\scalatra-
cms\src\main\scala\com\example\cms\PageController.scala:9:5: not found: value contentType
[error] contentType="text/html"
[error] ^
[error] C:\Users\eric.barnes\Desktop\scalatra-
cms\src\main\scala\com\example\cms\PageController.scala:10:35: not found: value params
[error] PageDao.pages find (_.slug == params("slug")) match {
[error] ^
[error] C:\Users\eric.barnes\Desktop\scalatra-
cms\src\main\scala\com\example\cms\PageController.scala:11:26: not found: value ssp
[error] case Some(page) => ssp("/pages/show", "page" -> page)
[error] ^
[error] 6 errors found [error] (Compile / compileIncremental) Compilation failed
[error] Total time: 2 s, completed Oct 4, 2021, 10:51:45 AM [info] 1. Monitoring source files
for hello/jetty:start... [info] Press <enter> to interrupt or '?' for more options.
Questions: The error messaging here is a little confusing for a first time Scalatra user. I'm using the same code in the PageController repo so why am I seeing these errors and how can I resolve them? It looks like my shell doesn't recognize the scalate library as an existing library but it's my understanding scalate comes packages with scalatra on install so I'm quite confused.
You have to add the following dependency to your build file to use Scalate (which is a template engine in Scala) in your Scalatra project:
"org.scalatra" %% "scalatra-scalate" % ScalatraVersion
You can see the build configuration of the Sample project at: https://github.com/scalatra/scalatra-in-action/blob/master/chapter02/project/build.scala
By the way, Scalatra in Action is a little old book based on Scalatra 2.4 while the latest major version of Scalatra 2.8. Though most of topics in the book are still effective for Scalatra 2.8, Scalate support has been deprecated in Scalatra 2.8. We recommend using Twirl instead of Scalate in Scalatra 2.8.

Iteratee is not a member of package play.api.libs

While upgrading from play 2.4 -> 2.5 I hit an issue where
Symbol 'type <none>.iteratee.Iteratee' is missing from the classpath
I checked the migration guide and included the dependency "com.typesafe.play" % "play-iteratees_2.11" % 2.5.18 into sbt. From the new play-iteratees repo, I was under the impression that package play.api.libs.iteratee is where I'd find what to import. But it doesnt seem to work?
Error log:
[error] /Users/arnogau/Documents/alice/common/src/main/scala/common/util/Autodesk.scala:23: object iteratee is not a member of package play.api.libs
[error] import play.api.libs.iteratee.Iteratee
[error] ^
[error] /Users/arnogau/Documents/alice/common/src/main/scala/common/util/Autodesk.scala:158: Symbol 'type <none>.iteratee.Iteratee' is missing from the classpath.
[error] This symbol is required by 'method play.api.libs.ws.WSRequest.get'.
[error] Make sure that type Iteratee is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'WSRequest.class' was compiled against an incompatible version of <none>.iteratee.
[error] val req = client.url(MANIFEST_URL(urn)).withToken(token).get()
[error] ^
[error] two errors found

Linker error in ScalaJS: "Referring to non-existent class"

I am getting a bunch of linker errors when trying to link to FastParse in a Scala project which I've just tried to build a ScalaJS web app in.
I added the ScalaJS dependency for FastParse into my build.sbt: here's approximately the relevant line (full file here):
libraryDependencies ++= Seq(
"com.lihaoyi" %%% "fastparse" % "0.4.1"
)
My errors are voluminous, but here's a representative one:
[error] Referring to non-existent class fastparse.Implicits$Repeater$
[error] called from parsers.MainParser$$anonfun$impls$1.apply()fastparse.core.Parser
[error] called from parsers.MainParser$$anonfun$impls$1.apply()java.lang.Object
[error] called from scala.Option.getOrElse(scala.Function0)java.lang.Object
[error] called from fastparse.StringReprOps$.errorMessage(fastparse.utils.ParserInput,java.lang.String,scala.Int)java.lang.String
[error] called from fastparse.core.ParseError.<init>(fastparse.core.Parsed$Failure)
[error] called from fastparse.Api.<init>(scala.reflect.ClassTag,fastparse.utils.ElemSetHelper,fastparse.utils.ReprOps,scala.math.Ordering)
[error] called from fastparse.StringApi.<init>()
[error] called from fastparse.all$.<init>()
[error] called from parsers.MainParser$.<init>()
[error] called from webapp.WebApp$.makeChoices(java.lang.String,java.lang.String)scala.collection.immutable.List
[error] called from webapp.WebApp$.$$js$exported$meth$makeChoices(java.lang.String,java.lang.String)java.lang.Object
[error] called from webapp.WebApp$.makeChoices
[error] exported to JavaScript with #JSExport
[error] involving instantiated classes:
[error] parsers.MainParser$$anonfun$impls$1
[error] scala.None$
[error] scala.Some
[error] fastparse.StringReprOps$
[error] webapp.WebApp$
What am I doing wrong?
The problem was that I was requiring the JVM version of FastParse two lines up in my build.sbt. This broke my build apparently. I removed that line and cleaned my build and now both the ScalaJS and ScalaJVM versions of the project are building nicely.
(Thanks heaps to Li Haoyi for spotting the problem for me)

Bad Symbolic reference to reactivemongo.api.collections.GenericHandlers encountered in class file 'JSONGenericHandlers.class'

I'm having my apis in play 2.3 with reactive mongo. Recently, i tried to cleaned the project and during the process, some things got updated. Later, when i tried to run or compile that, i'm getting these errors. Apart from clean, i didn't do anything. Kindly help me.
[info] Compiling 48 Scala sources and 1 Java source to /home/Ruthvick/zcapi/zceapi /target /scala-2.11/classes...
[error] bad symbolic reference to reactivemongo.api.collections.GenericHandlers encountered in class file 'JSONGenericHandlers.class'.
[error] Cannot access type GenericHandlers in package reactivemongo.api.collections. The current classpath may be
[error] missing a definition for reactivemongo.api.collections.GenericHandlers, or JSONGenericHandlers.class may have been compiled against a version that's
[error] incompatible with the one found on the current classpath.
[error] /home/Ruthvick/zcapi/zceapi/app/controllers/Application.scala:28: type arguments [play.modules.reactivemongo.json.collection.JSONCollection] do not conform to method collection's type parameter bounds [C <: reactivemongo.api.Collection]
[error] def collection: JSONCollection = db.collection[JSONCollection]("shoppage")
[error] ^
[error] /home/Ruthvick/zcapi/zceapi/app/controllers/Application.scala:47: could not find implicit value for parameter writer: GenericCollection.this.pack.Writer[play.api.libs.json.JsObject]
[error] collection.insert(result).map { lastError =>
[error] ^
[error] 60 errors found
[error] (compile:compile) Compilation failed
[error] application -
Thanks,
Your version of Play is not compatible with the newest Snapshot of Play Reactive Mongo. You could just use Version 0.10.5.
Add this to your Library Dependencies
"org.reactivemongo" %% "play2-reactivemongo" % "0.10.5.0.akka23"

Error using Mockito and Specs2

I have the following spec:
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
class LinkUserServiceSpec extends Specification with Mockito {
val linkUserService = mock[LinkUserService]
"The 'LinkUserService' isUserLinked method" should {
"return false when a previously unlinked userId is passed in for a given service" in {
linkUserService.isUserLinked("nobody", "YT") returns false
linkUserService.isUserLinked("nobody", "YT") must beFalse
}
}
}
And the following dependency in my build.sbt:
"org.specs2" %% "specs2" % "2.2" % "test"
However I get this error when I type test into the sbt console:
[error] bad symbolic reference. A signature in MocksCreation.class refers to type MockSettings
[error] in package org.mockito which is not available.
[error] It may be completely missing from the current classpath, or the version on
[error] the classpath might be incompatible with the version used when compiling MocksCreation.class.
[error] bad symbolic reference. A signature in MockitoStubs.class refers to term stubbing
[error] in package org.mockito which is not available.
[error] It may be completely missing from the current classpath, or the version on
[error] the classpath might be incompatible with the version used when compiling MockitoStubs.class.
[error] two errors found
[error] (test:compile) Compilation failed
[error] Total time: 3 s, completed Sep 12, 2013 3:23:41 PM
Anyone know what this could be?
Apparently if you want to use Mockito with Specs2 you have to provide the dependency yourself, I added the following to my build.sbt and things started working:
"org.mockito" % "mockito-all" % "1.9.5"