Code Monkey home page Code Monkey logo

Comments (4)

jroper avatar jroper commented on May 24, 2024 1

I may, I'll see about time. I mainly created the issue to save others the time from working out whether it exists or not. As an inefficient work around, this works:

import com.google.protobuf.DynamicMessage
import com.google.protobuf.field_mask.FieldMask
import com.google.protobuf.util.FieldMaskUtil
import scalapb.{ GeneratedMessage, GeneratedMessageCompanion }

object UpdateMaskSupport {

  def merge[T <: GeneratedMessage](source: T, destination: T, fieldMask: FieldMask)(implicit
      companion: GeneratedMessageCompanion[T]
  ): T = {
    val sourceBytes = source.toByteString
    val destBytes   = destination.toByteString

    val sourceJava      = DynamicMessage.parseFrom(companion.javaDescriptor, sourceBytes)
    val destJavaBuilder = DynamicMessage.parseFrom(companion.javaDescriptor, destBytes).toBuilder
    FieldMaskUtil.merge(FieldMask.toJavaProto(fieldMask), sourceJava, destJavaBuilder)
    companion.parseFrom(destJavaBuilder.build().toByteString.newCodedInput())
  }
}

A more efficient solution exists if you're happy also generating Java protobuf classes, but the above works without Java protobuf classes generated.

from scalapb.

thesamet avatar thesamet commented on May 24, 2024

Hey @jroper , thanks for making this feature request. Will you be able to work on a PR?

from scalapb.

jroper avatar jroper commented on May 24, 2024

More efficient one if Java classes are generated:

import com.google.protobuf.field_mask.FieldMask
import com.google.protobuf.util.FieldMaskUtil
import scalapb.{GeneratedMessage, GeneratedMessageCompanion, JavaProtoSupport}

object UpdateMaskSupport {

  def merge[T <: GeneratedMessage, J <: com.google.protobuf.GeneratedMessage](fieldMask: FieldMask, source: T, destination: T)(implicit
      companion: GeneratedMessageCompanion[T] with JavaProtoSupport[T, J]
  ): T = {
    val sourceJava      = companion.toJavaProto(source)
    val destJavaBuilder = companion.toJavaProto(destination).toBuilder
    FieldMaskUtil.merge(FieldMask.toJavaProto(fieldMask), sourceJava, destJavaBuilder)
    companion.fromJavaProto(destJavaBuilder.build().asInstanceOf[J])
  }
}

from scalapb.

thesamet avatar thesamet commented on May 24, 2024

I wonder if this is still needed by anyone. Closing due to inactivity, feel free to re-open or send a PR.

from scalapb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.