<commit_before>package schema

import (
	"bytes"
	"go/format"
	"io"
	"log"
)

type Schema interface {
	isSchema()

	Generate(dst io.Writer, pkg string) error
}

type Versioned struct {
	Name    string
	Version uint32
}

type SchemaV1 struct {
	Envelopes map[string]Envelope
	Messages  map[Versioned]Message
}

func (*SchemaV1) isSchema() {}

func (s *SchemaV1) Generate(dst io.Writer, pkg string) error {
	const debug = false

	var buf bytes.Buffer

	data := &genData{
		Package: pkg,
		Schema:  s,
	}
	if err := codegen.Execute(&buf, data); err != nil {
		return err
	}
	pretty, err := format.Source(buf.Bytes())
	if err != nil {
		if debug {
			log.Printf("gofmt: %v", err)
			pretty = buf.Bytes()
		} else {
			return err
		}
	}
	if _, err := dst.Write(pretty); err != nil {
		return err
	}
	return nil
}

type Envelope map[uint]Versioned

type Message interface {
	isMessage()

	GetSlots() []Slot
	GetFields() []Field
}<commit_msg>Clearer error reporting for template errors<commit_after>package schema

import (
	"bytes"
	"go/format"
	"io"
	"log"
)

type Schema interface {
	isSchema()

	Generate(dst io.Writer, pkg string) error
}

type Versioned struct {
	Name    string
	Version uint32
}

type SchemaV1 struct {
	Envelopes map[string]Envelope
	Messages  map[Versioned]Message
}

func (*SchemaV1) isSchema() {}

func (s *SchemaV1) Generate(dst io.Writer, pkg string) error {
	const debug = false

	var buf bytes.Buffer

	data := &genData{
		Package: pkg,
		Schema:  s,
	}
	if err := codegen.Execute(&buf, data); err != nil {
		return fmt.Errorf("template error: %v", err)
	}
	pretty, err := format.Source(buf.Bytes())
	if err != nil {
		if debug {
			log.Printf("gofmt: %v", err)
			pretty = buf.Bytes()
		} else {
			return err
		}
	}
	if _, err := dst.Write(pretty); err != nil {
		return err
	}
	return nil
}

type Envelope map[uint]Versioned

type Message interface {
	isMessage()

	GetSlots() []Slot
	GetFields() []Field
}
{'fraction_non_alphanumeric': 0.1313340227507756, 'fraction_numerical': 0.005170630816959669, 'mean_word_length': 3.607142857142857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3257283', 'n_tokens_mistral': 750, 'n_tokens_neox': 702, 'n_words': 202}