<commit_before>package terraform

import (
	"bytes"
	"encoding/gob"
	"fmt"
	"io"
	"sort"
	"strings"
	"sync"
)

// Diff tracks the differences between resources to apply.
type Diff struct {
	Resources map[string]*ResourceDiff
	once      sync.Once
}

// ReadDiff reads a diff structure out of a reader in the format that
// was written by WriteDiff.
func ReadDiff(src io.Reader) (*Diff, error) {
	var result *Diff

	dec := gob.NewDecoder(src)
	if err := dec.Decode(&result); err != nil {
		return nil, err
	}

	return result, nil
}

// WriteDiff writes a diff somewhere in a binary format.
func WriteDiff(d *Diff, dst io.Writer) error {
	return gob.NewEncoder(dst).Encode(d)
}

func (d *Diff) init() {
	d.once.Do(func() {
		if d.Resources == nil {
			d.Resources = make(map[string]*ResourceDiff)
		}
	})
}

// String outputs the diff in a long but command-line friendly output<commit_msg>terraform: add a format byte to the diff file so we can iterate maybe<commit_after>package terraform

import (
	"bytes"
	"encoding/gob"
	"errors"
	"fmt"
	"io"
	"sort"
	"strings"
	"sync"
)

const diffFormatByte byte = 1

// Diff tracks the differences between resources to apply.
type Diff struct {
	Resources map[string]*ResourceDiff
	once      sync.Once
}

// ReadDiff reads a diff structure out of a reader in the format that
// was written by WriteDiff.
func ReadDiff(src io.Reader) (*Diff, error) {
	var result *Diff

	var formatByte [1]byte
	n, err := src.Read(formatByte[:])
	if err != nil {
		return nil, err
	}
	if n != len(formatByte) {
		return nil, errors.New("failed to read diff version byte")
	}

	if formatByte[0] != diffFormatByte {
		return nil, fmt.Errorf("unknown diff file version: %d", formatByte[0])
	}

	dec := gob.NewDecoder(src)
	if err := dec.Decode(&result); err != nil {
		return nil, err
	}

	return result, nil
}

// WriteDiff writes a diff somewhere in a binary format.
func WriteDiff(d *Diff, dst io.Writer) error {
	n, err := dst.Write([]byte{diffFormatByte})
	if err != nil {
		return err
	}
	if n != 1 {
		return errors.New("failed to write diff version byte")
	}

	return gob.NewEncoder(dst).Encode(d)
}

func (d *Diff) init() {
	d.once.Do(func() {
		if d.Resources == nil {
			d.Resources = make(map[string]*ResourceDiff)
		}
	})
}

// String outputs the diff in a long but command-line friendly output
{'fraction_non_alphanumeric': 0.12387096774193548, 'fraction_numerical': 0.002150537634408602, 'mean_word_length': 3.615079365079365, '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': '4580316', 'n_tokens_mistral': 808, 'n_tokens_neox': 785, 'n_words': 271}