Javascript (Connector) way to access just the key fields?

4 posts / 0 new
Last post
Offline
Last seen: 2 months 1 week ago
Joined: 05/17/2021
Posts: 2
Javascript (Connector) way to access just the key fields?

I am using RTI Connector for javascript and trying to find a way to access just the key fields when reading in a message. I know this can be done in C++ and Java, but in the Javascript API Documentation, this is only mentioned for when the sample has been disposed https://community.rti.com/static/documentation/connector/current/api/javascript/data.html?highlight=dictionary#accessing-key-values-of-disposed-samples

Otherwise, the getJson() function gets ALL fields, not just the instance key fields. Is there an equivalent function for Javascript that performs the same operation as the key_value() function in the C++ API?

Offline
Last seen: 2 weeks 5 days ago
Joined: 10/22/2018
Posts: 90

What is the function you are referring to in C++ and Java?

I don't think that the example in the documentation that you supplied for JavaScript (here) does what you think it does:

// The output and input are using the following type:
// struct ShapeType {
//     @key string<128> color;
//     long x;
//     long y;
//     long shapesize;
// }

output.instance.set('x', 4)
output.instance.set('color', 'Green')
// Assume that some data associated with this instance has already been sent
output.write({ action: 'dispose' })
await input.wait()
input.take()
let sample = input.samples.get(0)

if (sample.info.get('instance_state') === 'NOT_ALIVE_DISPOSED') {
    // sample.info.get('valid_data') will be false in this situation
    // Only the key-fields should be accessed
    let color = sample.get('color') // 'Green'
    // The fields 'x','y' and 'shapesize' cannot be retrieved because they're
    // not part of the key
    // You can also call getJson() to get all of the key fields in a JSON object.
    // Again, only the key fields returned within the JSON object should
    // be used.
    let keyValues = sample.getJson() // { color: 'Green', x: 0, y: 0, shapesize: 0 }
}

The call to getJson() there will still contain a JSON object with all of the key/value pairs (that is, JSON key/value pairs) contained in sample, but when the instance state is disposed, it comes with the disclaimer that only the key values (that is, IDL key values) should be accessed.

 

Offline
Last seen: 2 months 1 week ago
Joined: 05/17/2021
Posts: 2

Thanks for the reply. The function I'm referring to is key_value(). https://community.rti.com/static/documentation/connext-dds/5.2.0/doc/api/connext_dds/api_cpp2/classdds_1_1sub_1_1DataReader.html#a44fca51d1564498fd383611e7f16a43f

Ok, I see what you mean about the getJson() function. You're correct that I misunderstood what the documentation was saying about that.

Howard's picture
Offline
Last seen: 6 hours 3 min ago
Joined: 11/29/2012
Posts: 447

The key_value() method that you refer to takes an "instance_handle".  This method is only useful when you don't have the actual value of data, but a handle to the instance...for which you want to know the key value.

(in many Listener callbacks, you may get a handle to the instance that's relevant to the callback)

In any case, key_value() will return a full data sample object with just the key fields filled out.  NOTE: your code will still need to know which members of the data structure comprise the key of the data type.  It's not that you only get the key values back.  You get an entire data structure in which only the key members have any real value.

So, what you're asking for is not possible in C/C++ either...