def get_first_enum_field_display_name(annotation_value, parser=None):
    """ Returns the displayName of annotations of a specific structure.

    All annotations used for FPGA interchange capnp file annotations have
    an enum as the first field.  This functions returns the displayName of the
    supplied annotation_value, if and only if the first field of the
    annotation_value is an enum.

    The parser argument is optional, as the default parser is the global
    parser defined by pycapnp.  In the event that the annotation in question
    was not parsed by this parser, that parser should be supplied.
    This case should be rare.

    """

    field0_proto = annotation_value.schema.fields_list[0].proto
    if field0_proto.which() != 'slot':
        return None

    field0_type = field0_proto.slot.type
    if field0_type.which() != 'enum':
        return None

    e = get_module_from_id(field0_type.enum.typeId, parser)
    return e.schema.node.displayName
def get_annotation_value(annotation, parser=None):
    """ Get annotation value from an annotation.  Schema that the annotation belongs too is required. """

    annotation_module = get_module_from_id(annotation.id, parser)
    name = annotation_module.__name__

    which = annotation.value.which()
    if which == 'struct':
        annotation_type = annotation_module._nodeSchema.node.annotation.type

        assert annotation_type.which() == 'struct'
        struct_type_id = annotation_type.struct.typeId
        annotation_struct_type = get_module_from_id(struct_type_id, parser)
        return name, annotation.value.struct.as_struct(annotation_struct_type)
    elif which == 'void':
        return name, None
    elif which in [
            'bool', 'float32', 'float64', 'int16', 'int32', 'int64', 'int8',
            'text', 'uint16', 'uint32', 'uint64', 'uint8'
    ]:
        return name, getattr(annotation.value, which)
    else:
        raise NotImplementedError(
            'Annotation of type {} not supported yet.'.format(which))
    def round_trip_rapidyaml(self, prefix, in_message):
        strings, value = to_rapidyaml(in_message)
        yaml_string = ryml.emit(value)

        if OUTPUT_FILES:
            with open(prefix + '_test_rapidyaml.yaml', 'w') as f:
                f.write(yaml_string)

        value_out = ryml.parse(yaml_string)
        message = get_module_from_id(in_message.schema.node.id).new_message()
        from_rapidyaml(message, value_out)
        compare_capnp(self, in_message, message)

        strings, value2 = to_rapidyaml(message)
        yaml_string2 = ryml.emit(value2)

        if OUTPUT_FILES:
            with open(prefix + '_test_rapidyaml2.yaml', 'w') as f:
                f.write(yaml_string2)

        self.assertTrue(yaml_string == yaml_string2)
    def round_trip_yaml(self, prefix, in_message):
        value = to_yaml(in_message)
        yaml_string = yaml.dump(value, sort_keys=False, Dumper=Dumper)

        if OUTPUT_FILES:
            with open(prefix + '_test_yaml.yaml', 'w') as f:
                f.write(yaml_string)

        value_out = yaml.load(yaml_string, Loader=SafeLoader)
        message = get_module_from_id(in_message.schema.node.id).new_message()
        from_yaml(message, value_out)
        compare_capnp(self, in_message, message)

        value2 = to_yaml(message)
        yaml_string2 = yaml.dump(value2, sort_keys=False, Dumper=Dumper)

        if OUTPUT_FILES:
            with open(prefix + '_test_yaml2.yaml', 'w') as f:
                f.write(yaml_string2)

        self.assertTrue(yaml_string == yaml_string2)
    def round_trip_json(self, prefix, in_message):
        value = to_json(in_message)
        json_string = json.dumps(value, indent=2)

        if OUTPUT_FILES:
            with open(prefix + '_test_json.json', 'w') as f:
                f.write(json_string)

        value_out = json.loads(json_string)
        message = get_module_from_id(in_message.schema.node.id).new_message()
        from_json(message, value_out)
        compare_capnp(self, in_message, message)

        value2 = to_json(message)
        json_string2 = json.dumps(value2, indent=2)

        if OUTPUT_FILES:
            with open(prefix + '_test_json2.json', 'w') as f:
                f.write(json_string2)

        self.assertTrue(json_string == json_string2)