Stefgen

Stefgen is a command-line code generator that takes STEF schema files as input and generates type-safe serialization and deserialization code for multiple programming languages. It parses STEF schema definitions and produces optimized code that can efficiently read and write STEF-encoded data.

Installation

Build stefgen from source:

cd stefgen
make build

This will create the stefgen binary in the bin/ directory.

Usage

stefgen [flags] <path-to-schema-file>

Command Line Arguments

Flag Type Required Description
-lang string Yes Target language for code generation. Supported values: go, java
-outdir string Yes Output directory where generated source files will be written
-testoutdir string No Output directory for test files. If unspecified, defaults to outdir. Only used with -lang=java
<schema-file> path Yes Path to the STEF schema file (.stef) to process

Examples

Generate Go Code

# Generate Go code from a STEF schema
stefgen -lang=go -outdir=./generated schema.stef

Generate Java Code

# Generate Java code with separate test directory
stefgen -lang=java -outdir=./src/main/java -testoutdir=./src/test/java schema.stef

Generate Java Code (Simple)

# Generate Java code with tests in same directory
stefgen -lang=java -outdir=./java-gen schema.stef

Supported Languages

Generated Code Structure

stefgen creates:

Example Workflow

1. Define Your Schema

Create a STEF schema file (e.g., user.stef):

package userdata

struct User root {
    Id uint64
    Name string
}

2. Generate Code

stefgen -lang=go -outdir=./generated user.stef

3. Use Generated Code

The generated Go code can then be imported and used in your application:

import "github.com/splunk/stef/go/pkg"
import "your-module/generated/userdata"

// Prepare a memory buffer to write the STEF stream to.
buf := &pkg.MemChunkWriter{}

// Create a Writer for the JSON-like schema
w := userdata.NewWriter(buf, pkg.WriterOptions{})

// Build a record in memory.
writer.Record.Value.SetId(1234)
writer.Record.Value.SetName("Foo Bar")

// Write the record to the stream.
writer.Write()

// Flush the stream to the buffer.
writer.Flush()