Support empty payload type, as long as the event is not empty
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 25 Mar 2016 17:53:25 +0000 (13:53 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 25 Mar 2016 17:57:56 +0000 (13:57 -0400)
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
barectf/config.py
barectf/metadata.py
barectf/tsdl182gen.py
tests/config/fail/event/fail.bats
tests/config/fail/event/pt-empty.yaml [deleted file]
tests/config/fail/event/pt-no.yaml [deleted file]

index 74de0ca50464c4fe2907ed0b19d859b00cf62d98..401810e7b43eba55fc9f3f02a84f81a0d3f729da 100644 (file)
@@ -309,16 +309,13 @@ class _BarectfMetadataValidator:
 
                     self._cur_entity = _Entity.EVENT_PAYLOAD
 
-                    if ev.payload_type is None:
-                        raise ConfigError('missing payload type in event "{}"'.format(ev_name), e)
-
                     try:
                         self._validate_entity(ev.payload_type)
                     except Exception as e:
                         raise ConfigError('invalid payload type in event "{}"'.format(ev_name), e)
 
-                    if not ev.payload_type.fields:
-                        raise ConfigError('empty payload type in event "{}"'.format(ev_name), e)
+                    if stream.is_event_empty(ev):
+                        raise ConfigError('event "{}" is empty'.format(ev_name))
             except Exception as e:
                 raise ConfigError('invalid stream "{}"'.format(stream_name), e)
 
@@ -1110,14 +1107,6 @@ class _MetadataTypesHistologyValidator:
             raise ConfigError('invalid event context type for event "{}"'.format(ev_name), e)
 
         # validate event payload type
-        if ev.payload_type is None:
-            raise ConfigError('event payload type must exist in event "{}"'.format(ev_name))
-
-        # TODO: also check arrays, sequences, and variants
-        if type(ev.payload_type) is metadata.Struct:
-            if not ev.payload_type.fields:
-                raise ConfigError('event payload type must have at least one field for event "{}"'.format(ev_name))
-
         try:
             self._validate_entity_type_histology(ev.payload_type)
         except Exception as e:
@@ -2193,10 +2182,7 @@ class _YamlConfigParser:
 
             event.context_type = t
 
-        if 'payload-type' not in event_node:
-            raise ConfigError('missing "payload-type" property in event object')
-
-        if event_node['payload-type'] is not None:
+        if 'payload-type' in event_node and event_node['payload-type'] is not None:
             try:
                 t = self._create_type(event_node['payload-type'])
             except Exception as e:
index 92cd2936b291259e3fd9541ac0a557ace49944fb..fa938d80a70913d829dfdee80699607a7bde6520 100644 (file)
@@ -406,6 +406,9 @@ class Struct(Type):
     def __getitem__(self, key):
         return self.fields[key]
 
+    def __len__(self):
+        return len(self._fields)
+
 
 class Variant(Type):
     def __init__(self):
@@ -736,6 +739,23 @@ class Stream:
     def events(self):
         return self._events
 
+    def is_event_empty(self, event):
+        total_fields = 0
+
+        if self.event_header_type:
+            total_fields += len(self.event_header_type)
+
+        if self.event_context_type:
+            total_fields += len(self.event_context_type)
+
+        if event.context_type:
+            total_fields += len(event.context_type)
+
+        if event.payload_type:
+            total_fields += len(event.payload_type)
+
+        return total_fields == 0
+
 
 class Metadata:
     def __init__(self):
index 35a515ab6c1d68c4962c592eb880cd61ad3cc622..586d9abe5e761ab52bdc32b66ac6f709d0598b5d 100644 (file)
@@ -312,6 +312,10 @@ def _gen_event_block(stream, ev, cg):
 
     if ev.payload_type is not None:
         _gen_entity('fields', ev.payload_type, cg)
+    else:
+        fake_payload = metadata.Struct()
+        fake_payload.min_align = 8
+        _gen_entity('fields', fake_payload, cg)
 
     _gen_end_block(cg)
 
index 0722f01ad72463da57f5089e09fb94211f4f542e..e911649994bbd12d85592df0efc5c604728f5530 100644 (file)
@@ -33,17 +33,7 @@ load ../../common
   barectf_config_check_fail
 }
 
-@test 'no "payload-type" property in event object makes barectf fail' {
-  barectf_assert_file_exists pt-no.yaml
-  barectf_config_check_fail
-}
-
 @test 'invalid "payload-type" property field type (not a structure) in event object makes barectf fail' {
   barectf_assert_file_exists pt-not-struct.yaml
   barectf_config_check_fail
 }
-
-@test 'empty struct type as "payload-type" property in event object makes barectf fail' {
-  barectf_assert_file_exists pt-empty.yaml
-  barectf_config_check_fail
-}
diff --git a/tests/config/fail/event/pt-empty.yaml b/tests/config/fail/event/pt-empty.yaml
deleted file mode 100644 (file)
index 84f1113..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-version: '2.1'
-metadata:
-  type-aliases:
-    uint16:
-      class: int
-      size: 16
-  trace:
-    byte-order: le
-  streams:
-    my_stream:
-      packet-context-type:
-        class: struct
-        fields:
-          packet_size: uint16
-          content_size: uint16
-      events:
-        my_event:
-          payload-type:
-            class: struct
diff --git a/tests/config/fail/event/pt-no.yaml b/tests/config/fail/event/pt-no.yaml
deleted file mode 100644 (file)
index 3cf2693..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-version: '2.1'
-metadata:
-  type-aliases:
-    uint16:
-      class: int
-      size: 16
-  trace:
-    byte-order: le
-  streams:
-    my_stream:
-      packet-context-type:
-        class: struct
-        fields:
-          packet_size: uint16
-          content_size: uint16
-      events:
-        my_event:
-          context-type:
-            class: struct
-            fields:
-              allo: uint16
This page took 0.028745 seconds and 4 git commands to generate.