From fe6cc755a1a13271408142c969b3d1bfa39e8577 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Fri, 11 Mar 2016 13:37:09 -0500 Subject: [PATCH] config: prefer using $inherit prop in 2.1 Signed-off-by: Philippe Proulx --- README.md | 10 ++-- barectf/config.py | 60 +++++++++++++++--------- doc/examples/linux-fs-simple/config.yaml | 28 +++++------ doc/examples/parallella/config.yaml | 22 ++++----- 4 files changed, 69 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 8cf3db9..4fe944a 100644 --- a/README.md +++ b/README.md @@ -323,7 +323,7 @@ type-aliases: class: integer size: 64 clock-int: - inherit: uint64 + $inherit: uint64 property-mappings: - type: clock name: my_clock @@ -388,7 +388,7 @@ streams: class: struct fields: value: - inherit: uint16 + $inherit: uint16 signed: true ``` @@ -627,8 +627,8 @@ Type objects represent CTF types. | Property | Type | Description | Required? | Default value | |---|---|---|---|---| -| `class` | String | Type class | Required if `inherit` property is absent | N/A | -| `inherit` | String | Name of type alias from which to inherit properties | Required if `class` property is absent | N/A | +| `class` | String | Type class | Required if `$inherit` property is absent | N/A | +| `$inherit` | String | Name of type alias from which to inherit properties | Required if `class` property is absent | N/A | The accepted values for the `class` property are: @@ -642,7 +642,7 @@ The accepted values for the `class` property are: | `array` | Array/sequence types | | `var`
`variant` | Variant type | -The `inherit` property accepts the name of any previously defined +The `$inherit` property accepts the name of any previously defined type alias. Any propery in a type object that inherits from another type object overrides the parent properties as follows: diff --git a/barectf/config.py b/barectf/config.py index 84f0a1f..eb816fb 100644 --- a/barectf/config.py +++ b/barectf/config.py @@ -187,12 +187,6 @@ def _get_first_unknown_prop(node, known_props): return prop_name -def _get_first_unknown_type_prop(type_node, known_props): - kp = known_props + ['inherit', 'class'] - - return _get_first_unknown_prop(type_node, kp) - - # This validator validates the configured metadata for barectf specific # needs. # @@ -1207,12 +1201,20 @@ class _YamlConfigParser: mapped_clock = self._clocks[clock_name] int_obj.property_mappings.append(metadata.PropertyMapping(mapped_clock, prop)) + def _get_first_unknown_type_prop(self, type_node, known_props): + kp = known_props + ['inherit', 'class'] + + if self._version >= 201: + kp.append('$inherit') + + return _get_first_unknown_prop(type_node, kp) + def _create_integer(self, obj, node): if obj is None: # create integer object obj = metadata.Integer() - unk_prop = _get_first_unknown_type_prop(node, [ + unk_prop = self._get_first_unknown_type_prop(node, [ 'size', 'align', 'signed', @@ -1342,7 +1344,7 @@ class _YamlConfigParser: # create floating point number object obj = metadata.FloatingPoint() - unk_prop = _get_first_unknown_type_prop(node, [ + unk_prop = self._get_first_unknown_type_prop(node, [ 'size', 'align', 'byte-order', @@ -1417,7 +1419,7 @@ class _YamlConfigParser: # create enumeration object obj = metadata.Enum() - unk_prop = _get_first_unknown_type_prop(node, [ + unk_prop = self._get_first_unknown_type_prop(node, [ 'value-type', 'members', ]) @@ -1491,7 +1493,7 @@ class _YamlConfigParser: # create string object obj = metadata.String() - unk_prop = _get_first_unknown_type_prop(node, [ + unk_prop = self._get_first_unknown_type_prop(node, [ 'encoding', ]) @@ -1519,7 +1521,7 @@ class _YamlConfigParser: # create structure object obj = metadata.Struct() - unk_prop = _get_first_unknown_type_prop(node, [ + unk_prop = self._get_first_unknown_type_prop(node, [ 'min-align', 'fields', ]) @@ -1562,7 +1564,7 @@ class _YamlConfigParser: # create array object obj = metadata.Array() - unk_prop = _get_first_unknown_type_prop(node, [ + unk_prop = self._get_first_unknown_type_prop(node, [ 'length', 'element-type', ]) @@ -1596,7 +1598,7 @@ class _YamlConfigParser: # create variant object obj = metadata.Variant() - unk_prop = _get_first_unknown_type_prop(node, [ + unk_prop = self._get_first_unknown_type_prop(node, [ 'tag', 'types', ]) @@ -1647,19 +1649,35 @@ class _YamlConfigParser: if not _is_assoc_array_prop(type_node): raise ConfigError('type objects must be associative arrays') - if 'inherit' in type_node and 'class' in type_node: - raise ConfigError('cannot specify both "inherit" and "class" properties in type object') + # inherit: + # v2.0: "inherit" + # v2.1+: "$inherit" + inherit_node = None + + if self._version >= 200: + if 'inherit' in type_node: + inherit_prop = 'inherit' + inherit_node = type_node[inherit_prop] + + if self._version >= 201: + if '$inherit' in type_node: + if inherit_node is not None: + raise ConfigError('cannot specify both "inherit" and "$inherit" properties of type object: prefer "$inherit"') + + inherit_prop = '$inherit' + inherit_node = type_node[inherit_prop] - if 'inherit' in type_node: - inherit = type_node['inherit'] + if inherit_node is not None and 'class' in type_node: + raise ConfigError('cannot specify both "{}" and "class" properties in type object'.format(inherit_prop)) - if not _is_str_prop(inherit): - raise ConfigError('"inherit" property of type object must be a string') + if inherit_node is not None: + if not _is_str_prop(inherit_node): + raise ConfigError('"{}" property of type object must be a string'.format(inherit_prop)) - base = self._lookup_type_alias(inherit) + base = self._lookup_type_alias(inherit_node) if base is None: - raise ConfigError('cannot inherit from type alias "{}": type alias does not exist'.format(inherit)) + raise ConfigError('cannot inherit from type alias "{}": type alias does not exist at this point'.format(inherit_node)) func = self._type_to_create_type_func[type(base)] else: diff --git a/doc/examples/linux-fs-simple/config.yaml b/doc/examples/linux-fs-simple/config.yaml index 6c0b59c..1d8a87c 100644 --- a/doc/examples/linux-fs-simple/config.yaml +++ b/doc/examples/linux-fs-simple/config.yaml @@ -14,16 +14,16 @@ metadata: class: integer size: 64 int8: - inherit: uint8 + $inherit: uint8 signed: true int16: - inherit: int8 + $inherit: int8 size: 16 int32: - inherit: int8 + $inherit: int8 size: 32 int64: - inherit: int8 + $inherit: int8 size: 64 float: class: floating-point @@ -43,7 +43,7 @@ metadata: length: 16 element-type: byte clock-int: - inherit: uint64 + $inherit: uint64 property-mappings: - type: clock name: default @@ -147,37 +147,37 @@ metadata: min-align: 8 fields: uint1: - inherit: uint8 + $inherit: uint8 size: 1 align: 1 int1: - inherit: int8 + $inherit: int8 size: 1 align: 1 uint2: - inherit: uint8 + $inherit: uint8 size: 2 align: 1 int3: - inherit: int8 + $inherit: int8 size: 3 align: 1 uint4: - inherit: uint8 + $inherit: uint8 size: 4 align: 1 int5: - inherit: int8 + $inherit: int8 size: 5 align: 1 uint6: - inherit: uint8 + $inherit: uint8 size: 6 align: 1 int7: - inherit: int8 + $inherit: int8 size: 7 align: 1 uint8: - inherit: uint8 + $inherit: uint8 align: 1 diff --git a/doc/examples/parallella/config.yaml b/doc/examples/parallella/config.yaml index 2ff3bbb..13585a2 100644 --- a/doc/examples/parallella/config.yaml +++ b/doc/examples/parallella/config.yaml @@ -17,16 +17,16 @@ metadata: class: integer size: 64 int8: - inherit: uint8 + $inherit: uint8 signed: true int16: - inherit: int8 + $inherit: int8 size: 16 int32: - inherit: int8 + $inherit: int8 size: 32 int64: - inherit: int8 + $inherit: int8 size: 64 float: class: floating-point @@ -46,7 +46,7 @@ metadata: length: 16 element-type: byte clock_int: - inherit: uint64 + $inherit: uint64 property-mappings: - type: clock name: default @@ -118,27 +118,27 @@ metadata: min-align: 8 fields: uint1: - inherit: uint8 + $inherit: uint8 size: 1 align: 1 int1: - inherit: int8 + $inherit: int8 size: 1 align: 1 uint2: - inherit: uint8 + $inherit: uint8 size: 2 align: 1 int3: - inherit: int8 + $inherit: int8 size: 3 align: 1 uint4: - inherit: uint8 + $inherit: uint8 size: 4 align: 1 int5: - inherit: int8 + $inherit: int8 size: 5 align: 1 string_and_float: -- 2.34.1