0

Using the Person and Address message definition in https://scalapb.github.io/generated-code.html

syntax = "proto3";
package trexo.scalapb;

message Person {
  string name = 1;
  int32 age = 2;

  repeated Address addresses = 3;
}

message Address {
  string street = 1;
  string city = 2;
}

The build settings are:

// project/plugins.sbt
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.28")
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.10.1"

// build.sbt
name         := "ProtobufPlayAround"
version      := "0.1"
scalaVersion := "2.12.10"

PB.targets in Compile := Seq(
  scalapb.gen() -> (sourceManaged in Compile).value
)

val scalapbVersion = scalapb.compiler.Version.scalapbVersion

libraryDependencies ++= Seq(
  "org.scalatest" %% "scalatest" % "3.1.1" % "test",
  "com.thesamet.scalapb" %% "scalapb-runtime" % scalapbVersion  % "protobuf"
)

Then I created a test:

import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
import trexo.scalapb.myprotos.{Address, Person}

class SerializeSpec extends AnyFreeSpec with Matchers {
  "TEST #1 .update() with concrete value (no Option)" in {
    val street1 = "Mount Everest"
    val city1   = "Himālaya"

    val expectedAddr = Address(street1, city1)

    val updAddr = Address().update(
      _.street := street1,
      _.city   := city1
    )
    updAddr shouldEqual expectedAddr
  }

  "TEST #2 .update() with Option value" in {
    val bogusAddr = Address(street = "Huh?", city = "Ah?")

    val emptyAddr = bogusAddr.update(
      _.optionalStreet := None,
      _.optionalCity   := None
    )

    emptyAddr shouldEqual Address()
  }
}

TEST #1 passed. TEST #2 failed to compile

Error:(98, 40) value optionalStreet is not a member of scalapb.lenses.Lens[trexo.scalapb.myprotos.Address,trexo.scalapb.myprotos.Address] val emptyAddr = bogusAddr.update(_.optionalStreet := None)

QUESTION: In ScalaPB documentation, Optional Fields it is said that there is a way to update optional fields (reproduced below for convenience). Has the _.optionalX way of updating changed to a different syntax?

val p = Person().update(
  // Pass the value directly:
  _.name := "John",

  // Use the optionalX prefix and pass an Option:
  _.optionalAge := Some(37)  // ...or None
)
Polymerase
  • 6,311
  • 11
  • 47
  • 65

1 Answers1

1

In your implementation, you do not have optional fields. If you want to have an optional field you need to write code like this:

syntax = "proto3";
package trexo.scalapb;
import "google/protobuf/wrappers.proto";

message Person {
  string name = 1;
  google.protobuf.Int32Value age = 2;

  repeated Address addresses = 3;
}

message Address {
  string street = 1;
  string city = 2;
}

Please see in example field age, now it will be optional.

Vladislav Kievski
  • 1,637
  • 11
  • 12
  • Thanks. Saw this in ScalaPB doc [Primitive wrappers](https://scalapb.github.io/customizations.html#primitive-wrappers) but didn't fully understand. I got confused by the Protobuf documentation saying in `proto3` all fields are optional in the message definition. – Polymerase Mar 27 '20 at 15:15
  • All your types will be optional. If you remove repeated before Address it will be optional. – Vladislav Kievski Mar 28 '20 at 07:36
  • `message Address` doesn't have any `repeated`. As it was defined, the generated classes doesn't have street, city as Option[String]. Only when I use your solution, changing `string` to `google.protobuf.StringValue` that the generated `Address` class has street, city as Option[String] instead of String. And then `Address.update()` now has `_optionalStreet` and `_.optionalCity` methods. – Polymerase Mar 28 '20 at 16:26
  • You have address as ```repeated Address addresses = 3;```, if you remove repeated, like ```Address addresses = 3;```, address will be optional. – Vladislav Kievski Mar 29 '20 at 08:10