Coder Social home page Coder Social logo

Comments (13)

tswast avatar tswast commented on May 20, 2024 4

@tiancaiamao Go now supports oneof. We've written a reference document on how the Go protocol buffer compiler generates these, and how to use them: https://developers.google.com/protocol-buffers/docs/reference/go-generated#oneof

from protobuf.

dsymonds avatar dsymonds commented on May 20, 2024

We decided not to implement specific Go changes for oneof. It's a feature that simply doesn't fit well with Go.

from protobuf.

riannucci avatar riannucci commented on May 20, 2024

Should this be added to the README then? How do you recommend distinguishing between field values if they could all legitimately contain a zero value (say, where I would want to render the data in a web page)? e.g.

message Foo {
  oneof value {
    int64 int_val;
    string string_val;
  }
}

How would I distinguish:

Foo {int_val: 0}

from

Foo {string_val: ""}

AFAIK all the other supported languages would allow this distinction in some way or another?

from protobuf.

awalterschulze avatar awalterschulze commented on May 20, 2024

I would also like to know the answer to this.

from protobuf.

pdemarino avatar pdemarino commented on May 20, 2024

This looks like a problem - the value proposition of protocol buffers is that they are completely language-agnostic... This isn't a corner case, it's a component of the language specs that won't work on a language.

from protobuf.

kerinin avatar kerinin commented on May 20, 2024

Why not use empty interfaces and type switches here?

message Foo {
  oneof value {
    int64 int_val;
    string string_val;
  }
}
switch f := foo.(type) {
case int64:
  // do stuff
case string:
  // do stuff
}

This seems the the most idiomatic solution, and it prevents assigning multiple values concurrently.

from protobuf.

riannucci avatar riannucci commented on May 20, 2024

Whoa, that DOES sound like a pretty solid idea... So basically the oneof field would have a type interface{}?

from protobuf.

kerinin avatar kerinin commented on May 20, 2024

Yeah. You lose some type safety (ie, you could assign anything to it), and I guess there's an edge case around multiple variants of the same type with different names.

from protobuf.

riannucci avatar riannucci commented on May 20, 2024

Yeah, that's true. It does assume that all subfields in the oneof have distinct types (which is probably the majority use case, but proto3 doesn't assert that it must be the case).

from protobuf.

dsymonds avatar dsymonds commented on May 20, 2024

We have a design that we're working through now. It's unfortunately more complicated than your suggestion out of necessity.

from protobuf.

riannucci avatar riannucci commented on May 20, 2024

Awesome! This is fantastic, thank you :).

Great design choice (tagged-union interface) btw. type-select casting looks very natural.

from protobuf.

ernestoalejo avatar ernestoalejo commented on May 20, 2024

AppEngine SDK has the old version of the package bundled; if you try to import github.com/golang/protobuf it loads the old code without this commit even if you have the new code vendored in your project. I have manually downloaded this lib; copied it inside go_appengine/goroot/src; removed the old .a compiled file and goapp install github.com/golang/protobuf to generate it again. Now it works correctly.

There is a simpler way of updating/vendoring protobuf in the AppEngine devserver?

from protobuf.

tiancaiamao avatar tiancaiamao commented on May 20, 2024

How far did we go now? Will it be supported in the future or never be supported?
I have a .proto file contain oneof, is there any temporary solution.

from protobuf.

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.