//go:build ignore

// string_generate generates zstring.go which houses the StringToHandler map.

package main

import (
	"bytes"
	"html/template"
	"log"
	"strings"

	"codeberg.org/miekg/dns/cmd/atomdns/internal/generate"
)

const hdr = `
// Code generated by "go run string_generate.go"; DO NOT EDIT.

package handlers

`

var funcmap = template.FuncMap{
	"tolower": strings.ToLower,
}

var StringToHandler = template.Must(template.New("stringToHandler").Funcs(funcmap).Parse(`
import (
{{range .}} "codeberg.org/miekg/dns/cmd/atomdns/handlers/{{tolower .}}"
{{end}}
)

// StringToHandler is a map of strings to a handler creation function.
var StringToHandler = map[string]func() Handler {
{{range .}} "{{tolower .}}": func() Handler { return new({{tolower .}}.{{.}}) },
{{end}}
}

`))

const out = "zstring.go"

func main() {
	handlers, err := generate.Handlers()
	if err != nil {
		log.Fatal(err)
	}
	source := &bytes.Buffer{}
	source.WriteString(hdr)

	if err := StringToHandler.Execute(source, handlers); err != nil {
		log.Fatalf("Failed to generate %s: %v", out, err)
	}

	generate.Write(source, out)
}
