Skip to content
This repository has been archived by the owner on May 28, 2022. It is now read-only.

Adds schema methods to Option and Config classes. #646

Merged
merged 1 commit into from
Nov 20, 2014

Conversation

FranciscoCanas
Copy link
Member

Adds a schema() method to Config and Options classes that returns their json schemas. Adds unit tests to test_config.py to test schema methods for Config and all Option types. Also adds a test_float file with FloatOption unit tests.

Related to: #632

"""Returns the full json-schema for this Config instance."""
schema = {'type': 'object'}
for name, instance in self.options.items():
schema[name] = instance.schema()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you plan on handling default values and required options?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess defaults might not be necessary as Config can already handle that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this in terms of the plugins, which already have Config classes that have defaults set for options.

One way to add 'required' options to the schema definitions might be to change the base Config class' schema to list all of its options under 'properties', and then have the required ones in a 'required' list:
{
'type': 'object',
'properties': {
'option1': { 'type': 'string' },
...
},
'required': ['option1']
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually while looking into 'required' properties I realize I need to nest all of the option schemas inside a 'properties' attribute, like in the comment on 'required' above. I will change this later today.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add to that, the tests should probably also try and load the schema with jsonschema.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also about the required: Should we have anything in required at all for a Config? Since we are allowing partial PATCH changes to a config.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like PATCHing is the special case. We'd want to check for required fields when creating a new Config.

@dideler dideler added this to the 2014 Fall - UCOSP milestone Nov 9, 2014
for name, instance in self.options.items():
schema['properties'][name] = instance.schema()
if instance.is_required():
required.append(name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add these to the schema directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally tried having 'required': [] as part of the schema, and filling it as needed when options are required. But json-schema validate() throws exceptions if a schema it receives as input has an empty list of 'required'.

@@ -30,6 +30,7 @@

class StringOption(Option):
"""Represents a string value."""
schema_type = 'string'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are class constants, I would make them ALL_CAPS.

@FranciscoCanas FranciscoCanas changed the title Adds schema methods to options and Config files. Adds schema methods to Option and Config classes. Nov 20, 2014
Config schema methods generate a json schemas based on
the config instance's options. Option schema methods
generate schemas based on the type of Option, its
default values, and whether its required or not.

Also adds schema unit tests for Config and Options,
and adds test_float unit test file.

Needed for Freeseer#632
@@ -59,6 +61,7 @@ def decode(self, value):

class FloatOption(Option):
"""Represents a floating point number value."""
SCHEMA_TYPE = 'number'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't been following this project closely so perhaps I'm missing something, but would this be better as 'float'?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON schema spec doesn't actually include floats. Instead, it has 'number' for any numbers, both whole and fractional, and 'integer' for just integers. It's described here:
http://json-schema.org/latest/json-schema-core.html#anchor8

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's a handy page. Carry on.

@mtomwing mtomwing merged commit 2a128fc into Freeseer:master Nov 20, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants