scalascalapb

ScalaPB, proto3: set field to None, no optionalX prefix available


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
)

Solution

  • 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.