<commit_before>package main

import (
	"fmt"
	"net/mail"
	"os"
	"path/filepath"

	"github.com/adrianduke/configr"
)

const (
	EmailFromAddressKey = "email.fromAddress"
	EmailSubjectKey     = "email.subject"
	EmailRetryOnFailKey = "email.retryOnFail"
	EmailMaxRetriesKey  = "email.maxRetries"
)

type Email struct {
	FromAddress string
	Subject     string
	MaxRetries  int  // Note config.json contains string
	RetryOnFail bool // Note config.json contains string
}

func init() {
	configr.RequireKey(EmailFromAddressKey, "Email from address", func(v interface{}) error {
		_, err := mail.ParseAddress(v.(string))
		return err
	})
	configr.RequireKey(EmailSubjectKey, "Email subject")
	configr.RegisterKey(EmailRetryOnFailKey, "Retry sending email if it fails", false)
	configr.RegisterKey(EmailMaxRetriesKey, "How many times to retry email resending", 3)
}

func main() {
	// Wont work if GOPATH contains multiple DIRs
	path := filepath.Join(os.Getenv("GOPATH"), "src/github.com/adrianduke/configr/examples/simple-json/config.json")
	configr.AddSource(configr.NewFileSource(path))

	if err := configr.Parse(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fromAddress, err := configr.String(EmailFromAddressKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	emailSubject, err := configr.String(EmailSubjectKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	retryOnFail, err := configr.Bool(EmailRetryOnFailKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	maxRetries, err := configr.Int(EmailMaxRetriesKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	email := Email{
		FromAddress: fromAddress,
		Subject:     emailSubject,
		MaxRetries:  maxRetries,
		RetryOnFail: retryOnFail,
	}

	fmt.Printf("Gotten values:\n\t%#v\n\n", email)

	//
	// Generate blank config
	//
	f := configr.NewFileSource("")
	f.SetEncoding(configr.JSON)

	// f implements the encoder interface
	configBytes, err := configr.GenerateBlank(f)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Printf("Blank config:\n%s", string(configBytes))
}
<commit_msg>Updated simple json example<commit_after>package main

import (
	"fmt"
	"net/mail"
	"os"
	"path/filepath"

	"github.com/adrianduke/configr"
	_ "github.com/adrianduke/configr/sources/file/json"
)

const (
	EmailFromAddressKey = "email.fromAddress"
	EmailSubjectKey     = "email.subject"
	EmailRetryOnFailKey = "email.retryOnFail"
	EmailMaxRetriesKey  = "email.maxRetries"
)

type Email struct {
	FromAddress string
	Subject     string
	MaxRetries  int  // Note config.json contains string
	RetryOnFail bool // Note config.json contains string
}

func init() {
	configr.RequireKey(EmailFromAddressKey, "Email from address", func(v interface{}) error {
		_, err := mail.ParseAddress(v.(string))
		return err
	})
	configr.RequireKey(EmailSubjectKey, "Email subject")
	configr.RegisterKey(EmailRetryOnFailKey, "Retry sending email if it fails", false)
	configr.RegisterKey(EmailMaxRetriesKey, "How many times to retry email resending", 3)
}

func main() {
	// Wont work if GOPATH contains multiple DIRs
	path := filepath.Join(os.Getenv("GOPATH"), "src/github.com/adrianduke/configr/examples/simple-json/config.json")
	f := configr.NewFile(path)
	configr.AddSource(f)

	if err := configr.Parse(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fromAddress, err := configr.String(EmailFromAddressKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	emailSubject, err := configr.String(EmailSubjectKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	retryOnFail, err := configr.Bool(EmailRetryOnFailKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	maxRetries, err := configr.Int(EmailMaxRetriesKey)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	email := Email{
		FromAddress: fromAddress,
		Subject:     emailSubject,
		MaxRetries:  maxRetries,
		RetryOnFail: retryOnFail,
	}

	fmt.Printf("Gotten values:\n\t%#v\n\n", email)

	//
	// Generate blank config
	//

	// f implements the encoder interface
	configBytes, err := configr.GenerateBlank(f)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Printf("Blank config:\n%s", string(configBytes))
}

{'fraction_non_alphanumeric': 0.1333172613307618, 'fraction_numerical': 0.0033751205400192863, 'mean_word_length': 4.2123115577889445, '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': '6251311', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1536, 'n_words': 315}