question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing oneof dynamically

See original GitHub issue

Hi,

I can’t find a way nice way to access a oneof property type dynamically, maybe you can advise

Imagine I have the following one of type:

export interface Task_Payload {

    payload: {
        oneofKind: "renameTask";
        renameTask: RenameTask;
    } | {
        oneofKind: "noop";
        noop: NoopTask;
    } | {
        oneofKind: "trimming";
        trimming: TrimmingTask;
    } | {
        oneofKind: undefined;
    };
}

I want to be able to access the type of whatever object has been received


const kind = type.payload.oneofKind;

if (!kind) {
  throw new Error("No kind specified");
}

// it should be of the correct type or at least a 
// union of RenameTask|TrimmingTask|NoopTask
const oneOfKind = type.payload[kind]; 

// The above doesn't work.
}

with the current type definition I’ll have to hard code each possible variant where i’d just like to store the value as is.

Any workaround for this still being type safe?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
timostammcommented, Jul 20, 2021

Thank you for looking into this so thoroughly, @jcready.

We already have isOneofGroup(). I’ve added getOneofValue(), setOneofValue() and clearOneofValue().

They may be helpful in some obscure situations.

2reactions
timostammcommented, Jul 14, 2021

Yes, but doing this in the runtime is preferable because of less overall code size.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Dynamically access oneof value from a protobuf
Let's say I defined a protobuf message like this message Config { oneof config{ A a = 1 ...
Read more >
Javascript: Dynamically get a 'oneof' type · Issue #4666 - GitHub
Hello, I'm trying to dynamically get a oneof type. Let's assume we have AWS resources, and I have them wrapped in another protobuf:...
Read more >
Protobuf Any and Oneof fields for variant types - Microsoft Learn
Learn how to use the Any type and the Oneof keyword to represent variant ... Handling dynamic property types (that is, properties of...
Read more >
Google::ProtocolBuffers::Dynamic - MetaCPAN
Protocol Buffers objects are normal blessed hashes, with auto-generated accessors for message fields. Accessing the hash directly is possible but not ...
Read more >
Firebase Dynamic Links
Firebase Dynamic Links are links that work the way you want, on multiple platforms, and whether or not your app is already installed....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found