Create a custom Record

Record is the interface provided by Baker to represent an “object” of data

As you can read in the Record and LogLine page, Baker processes objects called records.

A Record, in Baker, is an interface that provides an abstraction over a record of flattened data, where columns of fields are indexed through integers.

If the Record implementations provided by Baker doesn’t fit your needs, you can create your own version, implementing the Record inteface.

How to use a custom version of the Record

Once your Record version is ready, you need to use it in your code.

In order to do so, you must create and fill a baker.Components struct.

type Components struct {
	Validate     ValidationFunc
	CreateRecord func() Record
	FieldByName  func(string) (FieldIndex, bool)
	FieldNames   []string

	// ... other fields
}

Validate

Validate is the function used to validate a record. It is called for each processed record unless not set or when the [general] dont_validate_fields = true configuration is set in the TOML file.

Regardless of the TOML configuration, the function is passed to all components that can use it at their will.

CreateRecord

CreateRecord is the function that creates a new record. If not set, a default function is used that creates a LogLine with , as field separator.

The function is used internally by Baker to create new records every time a new one comes from the input.

The function is also passed to components that can use it to create new records while processing.

FieldByName

FieldByName gets a field index by its name. The function is mainly used by the components (that receive it during setup) to retrieve the index of a field they need for filtering or processing, but it is also used internally by Baker when sending fields to the output (when at least one field is selected in the output TOML configuration).

FieldNames

FieldNames []string lists the names of the fields a Record can have. Since it’s a slice, its length also indicates the maximum number of fields.

Record conformance test

test_helper.go provides a test helper, RecordConformanceTest, one can and should use to verify their custom Record satisfies the invariants required for any Record implementation.

Just pass to RecordConformanceTest a factory function creating new instances of your Record.

Last modified October 29, 2020