Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand documentation on exporting Spicy types #252

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 109 additions & 1 deletion devel/spicy/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,118 @@ dependent types must be exported *first* in the EVT file for this to
work. As a result, you cannot export self-recursive unit types.

As you can see in the example, unit fields are always declared as
optional on the Zeek-side, as they may not have been set during
:zeek:see:`&optional` on the Zeek-side, as they may not have been set during
parsing. Unit variables are non-optional by default, unless declared
as ``&optional`` in Spicy.

.. _spicy_unit_export:

Controlling created Zeek types
""""""""""""""""""""""""""""""

If needed the automatic unit type exporting described above can be customized.
The general syntax for this is:

.. code-block:: spicy-evt

export SPICY_ID [with { [SIPCY_FIELD_NAME [&log], ]... }];
export SPICY_ID [as ZEEK_ID [ with { [SIPCY_FIELD_NAME [&log], ]...] }];
export SPICY_ID [without { [SIPCY_FIELD_NAME, ]... }];
export SPICY_ID [as ZEEK_ID [ without { [SIPCY_FIELD_NAME, ]...] }];
export SPICY_ID &log;
export SPICY_ID as ZEEK_ID &log;
Comment on lines +615 to +620
Copy link
Member

Choose a reason for hiding this comment

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

I'd still try to compress this into a single line but may be matter of taste.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what I initially tried, but it always overflowed the box in the rendered pages so it required horizontal scrolling, and also required a lot of nesting. I was unsure whether adding line breaks for readability would be confusing to readers. The full expression was

export SPICY_ID [&log | with { [SPICY_FIELD_NAME [&log], ]...} | without { [SPICY_FIELD_NAME, ]... } | as ZEEK_ID [&log | { [SPICY_FIELD_NAME [&log], ]...} | without { [SPICY_FIELD_NAME, ]...}]]

Copy link
Member

Choose a reason for hiding this comment

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

I'd still try to compress this into a single line but may be matter of taste.

How about this:

export SPICY_ID [as ZEEK_ID] [with/without { SPICY_FIELD_NAME [&log], ...}] [&log]

The field &log isn't 100% correct (doesn't apply to without), but seems good enough?


This allows

1. including fields to export by naming them in ``with``, e.g.,
``export foo::A with { x, y }`` creates a Zeek record with the Spicy unit
fields ``x`` and ``y`` added to the Zeek record type.

2. excluding fields from export by naming them in ``without``, e.g.,
``export foo::A without { x }`` creates a Zeek record with all fields but
``x`` of the Spicy unit exported.

3. renaming the Spicy type on export, e.g., ``export foo::A as bar::X`` exports
the Spicy unit type ``A`` in Spicy module ``foo`` to a Zeek record type ``X``
in Zeek module ``bar``.

4. controlling :zeek:see:`&log` attribute on generated Zeek record types. We can either
the mark all Zeek record fields ``&log`` by marking the whole type ``&log``
with e.g., ``export foo::A &log`` or ``export foo::A as bar::X &log``, or
mark individual fields ``&log`` in the ``with`` field list.

.. rubric:: Example: Controlling exported fields

Assume we are given a Spicy unit type ``foo::X``:

.. code-block:: spicy

module foo;

public type X = unit {
x: uint8;
y: uint8;
z: uint8;
};

To create a Zeek type without the field ``x`` we can use the following ``export``
statement:

.. code-block:: spicy-evt

export foo::X without { x };

.. rubric:: Example: Adding Zeek ``&log`` attributes to created Zeek record types

In Zeek records, data intended to be written to Zeek log streams needs to be
marked :zeek:see:`&log`. We can trigger creation of this attribute from Spicy
either per-field or for the whole ``record``.

The export statement

.. code-block:: spicy-evt

export foo::X &log;

creates a Zeek record type

.. code-block:: zeek

module foo;

export {

type X: record {
x: count &optional &log;
y: count &optional &log;
z: count &optional &log;
};

}

To mark individual fields :zeek:see:`&log` we can use the attribute on the
respective fields, e.g.,

.. code-block:: spicy-evt

export foo::X with { x &log, y, z };

creates a Zeek record type

.. code-block:: zeek

module foo;

export {

type X: record {
x: count &optional &log;
y: count &optional;
z: count &optional;
};

}

.. _spicy_struct:

Struct Types
Expand Down
Loading