<commit_before>package kupak

import (
	"errors"
	"gopkg.in/yaml.v2"
	"text/template"
)

func validateProperties(properties []Property) error {
	nameMap := make(map[string]bool)
	for i := range properties {
		if _, has := nameMap[properties[i].Name]; has {
			return errors.New("Duplicated property")
		}
		// validating types
		switch properties[i].Type {
		case "int":
		case "number":
		case "string":
			// TODO validate the default value and other type specification
			_ = "ok"
		default:
			return errors.New("Specified type is not valid")
		}
	}
	return nil
}

func (p *Pak) fetchAndMakeTemplates(baseUrl string) error {
	p.Templates = make([]*template.Template, len(p.ResourceUrls))
	for i := range p.ResourceUrls {
		url := joinUrl(baseUrl, p.ResourceUrls[i])
		data, err := fetchUrl(url)
		if err != nil {
			return err
		}
		t := template.New(p.ResourceUrls[i])
		resourceTemplate, err := t.Parse(string(data))
		if err != nil {
			return err
		}
		p.Templates[i] = resourceTemplate
	}<commit_msg>Template execution<commit_after>package kupak

import (
	"bytes"
	"errors"
	"text/template"

	"gopkg.in/yaml.v2"
)

func validateProperties(properties []Property) error {
	nameMap := make(map[string]bool)
	for i := range properties {
		if _, has := nameMap[properties[i].Name]; has {
			return errors.New("Duplicated property")
		}
		// validating types
		switch properties[i].Type {
		case "int":
		case "number":
		case "string":
			// TODO validate the default value and other type specification
			_ = "ok"
		default:
			return errors.New("Specified type is not valid")
		}
	}
	return nil
}

func (p *Pak) fetchAndMakeTemplates(baseUrl string) error {
	p.Templates = make([]*template.Template, len(p.ResourceUrls))
	for i := range p.ResourceUrls {
		url := joinUrl(baseUrl, p.ResourceUrls[i])
		data, err := fetchUrl(url)
		if err != nil {
			return err
		}
		t := template.New(p.ResourceUrls[i])
		t.Delims("$(", ")")
		resourceTemplate, err := t.Parse(string(data))
		if err != nil {
			return err
		}
		p.Templates[i] = resourceTemplate
	}
	return nil
}

func (p *Pak) ExecuteTemplates(values map[string]interface{}) ([][]byte, error) {
	// TODO validate values
	outputs := make([][]byte, len(p.Templates))
	for i := range p.Templates {
		buffer := &bytes.Buffer{}
		if err := p.valuesWithDefaults(values); err != nil {
			return nil, err
		}
		if err := p.Templates[i].Execute(buffer, values); err != nil {
			return nil, err
		}
		outputs[i] = buffer.Bytes()
	}
	return outputs, nil
}

func (p *Pak) valuesWithDefaults(values map[string]interface{}) error {
	for i := range p.Properties {
		if _, ok := values[p.Properties[i].Name]; !ok {
			values[p.Properties[i].Name] = p.Properties[i].Default
		}
	}
{'fraction_non_alphanumeric': 0.14884233737596472, 'fraction_numerical': 0.000735023888276369, 'mean_word_length': 3.8176991150442476, 'pattern_counts': {'":': 6, '<': 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': '7256589', 'n_tokens_mistral': 1027, 'n_tokens_neox': 941, 'n_words': 238}