0

I'm trying to use the 'OO' features of jsonnet in combination with the parametrization capabilities available via the tla arguments mechanism

I want to create templates of different kinds of events. The base Event type just has 3 necessary fields - id, version, timestamp but passed in as parameters. So i used the function approach to create a template like so

{
  BaseEvent (id, version, timestamp) ::{
    id: id,
    version: version,
    timestamp: timestamp
  }
}

This works fine when those args are passed via --tla flags.

Now I want to create a specialized Event type, FirewallEvent whose output should look like this (and is also parametrized)

{
#fields from base event at the top level of the derived object
   id: '12345',
   timestamp: '2020/08/21 22:33:44.355',
   version: '1.0',

#specialized field for Firewall event type follow
    type: 'firewall'
    src_ip: ...

#etc...
}

I'm struggling to figure out how to set up the jsonnet template for the Firewall event type - specifically how to 'include' the fields from the base type in the root of the firewall object definition. This doesnt work...

local base_event = import 'base_event.libsonnet';
function(id, version, timestamp) {
  base_event.BaseEvent(id, version, timestamp) + { type: 'firewall' }
}

UPDATE: This worked (i'm a jsonnet n00b, so pardon my ignorance)

local base_event = import 'base_event.libsonnet'; 

function (timestamp, id, version) 
  base_event.BaseEvent(id, version, timestamp) + 
  { 
    type: 'firewall' 
  } 

However, I'm still wondering if there's a better/canonical way to declare that Firewall derives from a base type (and more generally, what's the right way to set up a series of parametrized templates for an object specialization hierarchy).

4

0 回答 0