-
Notifications
You must be signed in to change notification settings - Fork 909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with Extended Model #2579
Comments
This is not intended behaviour, and I can't immediately see from manager.rb and node.rb how this would transpire, but also it seems plausible you're observing real problem and that we're somehow overwriting also the parent class, I just don't understand how. The most probable explanation is in model/model.rb we are manipulating the same @cmd object, and we would have needed to reassign with @cmd.dup, to avoid changing the parents @cmd object. Problem is most likely to exist here: Changing this to |
Thanks @ytti, after some trial and error it looks like beside |
Good work. I'm not against the solution you ended up with but not necessarily what I would have chosen as I'm not sure if we have same problem elsewhere there. In most modern OO languages copies are shallow, because there is no obvious cheap way to do deep copies. #copy makes shallow copy, which meant, while @cmd is new, its keys are still pointing to same objects are originals. The solution I would have likely gone for, would have been to indiscriminately create deep copies of each of the parents variables. For example: newvar = Marshal.load(Marshal.dump(oldvar)). I'm not 100% sure if our variables contain only marshable objects or not, but it will complain if not. This causes the oldvar to be serialised (for example storing into the disk) and then we deserialize it back into object, this will cause the copy to be deep. It is of course far more expensive than shallow copy, but in our case cost is immaterial and if it works, we can be confident we've fixed the same problem for all cases, without having to think about what the cases might be. |
I tried dump/load also but it's not possible to (un)marshal Proc code blocks, so it doesn't work on @cmd. I was also a bit worried about impacting other functionality. My gut feeling is that more stuff would need to be duped, but the @cmd[:cmd] was the minimum that made my test work so I left it at that. Since apparently in many years of oxidized nobody wanted to extend a class like I did, I'm also not mad if you don't apply the PR. It just looked like it should work in OO typically, but if it doesn't it's no big deal and I will just duplicate the whole ios.rb. Maybe we could just use touch up the doc then since it mentions https://github.com/ytti/oxidized/blob/master/docs/Creating-Models.md which is how I encountered the problem in the first place :) |
I think this is definitely legitimate problem that needs to be fixed. I think we should shallow copy them all, and in addition shallow copy the @cmd[:cmd], any other issues, we fix when we find them? |
I gave it a quick try with the above PR, still seems to work AFAICT :) |
Good enough to me, many thanks. |
I wanted to make an extended ios class that can add some more command output, e.g.:
And I would like to selectively apply this to only certain devices:
At first, it seemed to work great for the swvce1 device:
But unfortunately, it is now also included in my output of devices that should use the plain ios model:
And I can't figure out why. When logging OX_NODE_MODEL, it seems correct:
This is the full config:
I have started using oxidized this afternoon and I last touched Ruby when Rails was the hot new tech on the dotcom block, so most likely this is not a bug but me doing something wrong. But can you spot what? Thanks a bunch!
The text was updated successfully, but these errors were encountered: