Giter Site home page Giter Site logo

Comments (7)

tbkka avatar tbkka commented on September 7, 2024

Wrappers could easily be implemented as a generated base + extensions for the custom JSON behaviors. Someone recently asked me about "easy starter projects" -- I think that would be one.

I suspect Struct and even Any could also be done that way, though Any might require some adjustments to the rest of the library to expose the right customization points.

from swift-protobuf.

allevato avatar allevato commented on September 7, 2024

The NullValue enum in struct.proto is going to make this a little trickier. Since it's an enum and the json property is generated directly on the type, there's no way to have an extension replace it. (Aside: Even if we extract JSON support into separate extensions, we can't introduce our own hand-written extension on the same type without it causing an ambiguity.)

I haven't thought too deeply about how to solve this yet, but the options that come to mind feel like hacks:

  1. Add a method to ProtoEnum like remapJSONName(_ value: Self, _ name: String) and have the generated code call it, passing it the JSON string for the case. The default implementation in the protocol extension would just return the string verbatim. A hand-written extension on concrete enum types could then override it and return something else (in this case, "null").
  2. Just add a special case for Google_Protobuf_NullValue in the JSON serialization code.
  3. Other options...?

Any option will pick will likely have a small cost in terms of runtime library size, generated code size, and execution speed. Hopefully they're negligible; in terms of execution speed at least, surely the rest of the string processing will dominate whatever we add.

from swift-protobuf.

tbkka avatar tbkka commented on September 7, 2024

Since it's an enum and the json property is generated directly on the type, there's no way to have an extension replace it.

The technique I used to allow overriding generated methods on messages could work here. In the library, define:

extension ProtobufEnum {
   var json: String {return _protoc_generated_json}
}

The generator then emits a definition for _protoc_generated_json instead of defining json directly. Then we can override json in an extension (struct/enum extensions can override protocol extensions).

from swift-protobuf.

thomasvl avatar thomasvl commented on September 7, 2024

@allevato is working on this for Struct. Once that lands, I've got an idea for how we might be able to do this for Any.

from swift-protobuf.

thomasvl avatar thomasvl commented on September 7, 2024

#387 removed the Any version. This is now done.

from swift-protobuf.

allevato avatar allevato commented on September 7, 2024

🎉 🎉 🎉

from swift-protobuf.

tbkka avatar tbkka commented on September 7, 2024

Good work!

from swift-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.