Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
pjforde1978
Visitor

roSGNode subclass type reflection

I've created a UrlNode, which is a ContentNode with a feed_url field added.

<?xml version="1.0" encoding="utf-8" ?>
<component name="UrlNode" extends="ContentNode">
  <interface>
    <field id="feed_url" type="string" />
  </interface>
</component>


When I iterate over my list of ContentNodes, I would like to be able to figure out if I'm looking at a ContentNode or a UrlNode. When I print out the object, it tells me that it is a <Component: roSGNode:UrlNode> but when I print out type(object) it tells me that it's an roSGNode.

Assuming that we can all agree that testing (object.feed_url <> invalid) would be cheating, how can I determine the subclass of an roSGNode?
0 Kudos
5 REPLIES 5
destruk
Streaming Star

Re: roSGNode subclass type reflection

What I would do here is check the last few characters of the url itself.  If it's an mp4 then it's progressive video, mp3 audio, hls, or html or php or asp.
That way you aren't extending the content node which has quite an impact on larger content sets.  When you do need to have a few extra fields, it's preferable (IMO) to use existing fields you are not utilizing like "album".
0 Kudos
pjforde1978
Visitor

Re: roSGNode subclass type reflection

That's an answer I was not expecting. I see that you've made over 2500 posts, so I don't quickly discard your opinion. That said, I would like to benefit from your experience by digging a little deeper.

What's your confidence level regarding the performance impacts of subclassing in BrightScript? Is there a number of child objects where it starts to become problematic?

Is the impact lessened, increased or similar if you use addField() to add a slot dynamically vs formally subclassing via an additional XML file?

Does BrightScript have profiling mechanisms to test these hypotheses on working hardware with incrementally larger iterations? Usually, these sorts of problems have a curve.

Ultimately, my reason for asking is deeper understanding. As I suggested at the end of my post, I could also just test object.feed_url <> invalid and move on, but that feels gross and leads to special cases in your codebase whereas most languages have a mechanism for reflecting on polymorphic class meta-information. A urlNode is a contentNode but the opposite is not necessarily true.
0 Kudos
destruk
Streaming Star

Re: roSGNode subclass type reflection

Profiler is here -
https://sdkdocs.roku.com/display/sdkdoc/Best+Practices+for+Data+Management

By inference, if you don't need to add a field to the existing contentnode item, and simply appropriate an existing field for your own purposes, that is quicker.
0 Kudos
pjforde1978
Visitor

Re: roSGNode subclass type reflection

Thanks for the amazing answers.

I'm still surprised that there's no capacity for reflection but tonight I also realized that there's no || assignment operator either. Every language has its idiosyncrasies.
0 Kudos
pjforde1978
Visitor

Re: roSGNode subclass type reflection

As it happens, roSGNode has an ifSGNodeDict interface that provides a subtype() function that returns the information I was looking for.

In my original example, calling node.subtype() on my UrlNode component would return "UrlNode".

https://sdkdocs.roku.com/display/sdkdoc/ifSGNodeDict

Case closed!
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.