Giter VIP home page Giter VIP logo

Comments (2)

timostamm avatar timostamm commented on May 26, 2024

There is a hoisting problem with extending generated classes. Let's say you have this .proto:

message Foo {
   Bar bar = 1;
}

message Bar {
}

Now you extend Bar:

class MyBar extends Bar {
   greet() {
     console.log("hello world")
   }
}

But what happens if you deserialize this?

let foo = Foo.deserialize(data);
foo.bar.greet() 
        ^^^^^

bar will be an instance of Bar, not MyBar. So extending generated classes only works in a very limited way.

from protobuf-ts.

timostamm avatar timostamm commented on May 26, 2024

Could you give me some an example of what you want to achieve?

Do you want to add a method to specific types, or do you want to overwrite functionality?

Some well-known-types already have special methods:

['google.type.Color'](source: TypescriptFile, descriptor: DescriptorProto) {
const Color = this.imports.type(source, descriptor);
return [
`
/**
* Returns hexadecimal notation of the color: #RRGGBB[AA]
*
* R (red), G (green), B (blue), and A (alpha) are hexadecimal characters
* (0–9, A–F). A is optional. For example, #ff0000 is equivalent to
* #ff0000ff.
*
* See https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#RGB_colors
*/
function toHex(message: ${Color}): string {
let hex = [
message.red.toString(16),
message.green.toString(16),
message.blue.toString(16),
];
if (message.alpha) {
let alpha = Math.max(Math.min(message.alpha.value, 1), 0);
hex.push(Math.round(alpha * 255).toString(16));
}
return '#' + hex.map(i => i.length < 2 ? '0' + i : i).join('');
}

I thought about exposing this functionality. So that users can add custom functions to be generated...

from protobuf-ts.

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.