Skip to content
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

Question: RestrictedClassType and Null value #33

Closed
mpainenz opened this issue Feb 15, 2016 · 2 comments
Closed

Question: RestrictedClassType and Null value #33

mpainenz opened this issue Feb 15, 2016 · 2 comments

Comments

@mpainenz
Copy link
Contributor

I am working with a REST/JSON implementation, and serializing from JSON into Class Bindings

In the Yang model, there is a leaf with a range specified.

leaf max-call-duration {
  description
    "The limit in seconds on how long calls or call attempts can be in the system for the endpoint; Default=0.";
  type uint16 {
    range "1..max";
  }
  units "seconds";
}

In the JSON being received, I am receiving a null value

maxCallDuration": null,

(Please ignore the different key name format in camel case, that's a quirk of the API)

In python, it generates a ValueError:

ValueError: max_call_duration must be of a type compatible with base=RestrictedClassType(base_type=np.uint16, restriction_dict={'range': [u'1..max']}), is_leaf=True, yang_name="max-call-duration", parent=self, path_helper=self._path_helper, extmethods=self._extmethods

I'm curious if this is a problem with the YANG definition, or potentially the Yang Blinding object should accept Null?

Any thoughts?

Thanks,
Mark

@robshakir
Copy link
Owner

I can't see any case where this can be valid. In general, pyangbind will try to cast the input to the type that is expected, so for example, if we have the following module:

module "test" {
    prefix "t";
    namespace "http://rob.sh/yang/t";

    container test {
        leaf number {
            type uint32;
        }

        leaf number-range {
            type uint32 {
                range "1..max";
            }
        }
    }
}

Then, we can do:

>>> t.test.number = False
>>> t.test.number = 0

Since int(False) == 0, however:

>>> t.test.number = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tmp.py", line 67, in _set_number
    raise ValueError("""number must be of a type compatible with base=np.uint32, is_leaf=True, yang_name="number", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True""")
ValueError: number must be of a type compatible with base=np.uint32, is_leaf=True, yang_name="number", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True
>>> int(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'NoneType'

You cannot set an int to None because NoneType cannot be cast to int. So, even without the range, I don't think that this is valid. The reason that this is relevant is because the following JSON:

{
    "value": null
}

is loaded by Python's json library to use None rather than null:

>>> import json
>>> j = json.load(open("test.json", 'r'))
>>> print j
{u'value': None}

In your specific case, this isn't ever valid though - because if None/null were an integer value then it would be zero - which is outside of the 1..max range. With a normal leaf of this nature, one still get the error when one tries to set the value to 0:

>>> t.test.number_range = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tmp.py", line 93, in _set_number_range
    raise ValueError("""number_range must be of a type compatible with base=RestrictedClassType(base_type=np.uint32, restriction_dict={'range': [u'1..max']}), is_leaf=True, yang_name="number-range", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True""")
ValueError: number_range must be of a type compatible with base=RestrictedClassType(base_type=np.uint32, restriction_dict={'range': [u'1..max']}), is_leaf=True, yang_name="number-range", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True
>>> t.test.number_range = 1

So, I'd say that your input JSON is not valid.

r.

@mpainenz
Copy link
Contributor Author

OK Rob, thanks for clarifying, that all seems fine to me.

Much appreciated,
Mark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants