Treat null values in JSON as being absent rather than 'None'

The current method of serialization for keys containing null
values in JSON input files is that they become a part of the
metadata tree, with a value expressed as the string 'None' in
output (when invoked with --key) or when inserted into templates.

The use of 'None' creates awkward ambiguity (no way for downstream
code logic to tell if the request JSON key originally contained
the string "None" or if it was actually null), and also throws
conversion errors when requested in any type other than 'raw'.

This patch proposes that keys with null values be handled as
though they were absent from the metadata completely. (If
such a key is requested in command-line invokations using "--key"
and "--key-default", the provided default will be printed.)

Change-Id: I9288dffb13d781e0c4dd9e5be0b483c090ec2d7c
This commit is contained in:
Jonathan Brownell 2014-10-08 15:17:16 -07:00
parent ef971cd603
commit e78b8d6964
2 changed files with 11 additions and 0 deletions

View File

@ -158,6 +158,8 @@ def print_key(
for key in keys:
try:
config = config[key]
if config is None:
raise TypeError()
except (KeyError, TypeError):
if default is not None:
print(str(default))

View File

@ -39,6 +39,7 @@ TEMPLATE_PATHS = [
CONFIG = {
"x": "foo",
"y": False,
"z": None,
"database": {
"url": "sqlite:///blah"
}
@ -114,6 +115,14 @@ class TestRunOSConfigApplier(testtools.TestCase):
self.stdout.read().strip())
self.assertEqual('', self.logger.output)
def test_print_null_key(self):
self.assertEqual(0, apply_config.main(
['os-apply-config.py', '--metadata', self.path, '--key',
'z', '--type', 'raw', '--key-default', '']))
self.stdout.seek(0)
self.assertEqual('', self.stdout.read().strip())
self.assertEqual('', self.logger.output)
def test_print_key_missing(self):
self.assertEqual(1, apply_config.main(
['os-apply-config.py', '--metadata', self.path, '--key',