//go:build ignore

package main

import (
	"bytes"
	"flag"
	"fmt"
	"go/ast"
	"log"
	"slices"

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

var skip = []string{"NSEC", "NSEC3", "OPT", "CSYNC"}

var hdr = `
// Code generated by "go run len_generate.go"; DO NOT EDIT.

package dns

`

const out = "zlen.go"

func main() {
	flag.Parse()
	specs, err := generate.StructTypeSpecs("rdata/rdata.go")
	if err != nil {
		log.Fatalf("Failed to generate %s: %v", out, err)
	}
	b := &bytes.Buffer{}
	b.WriteString(hdr)

	for _, spec := range specs {
		rrname := spec.Name.Name
		if slices.Contains(skip, rrname) {
			continue
		}

		fmt.Fprintf(b, "func (rr *%s) Len() int { ", rrname)
		strct := spec.Type.(*ast.StructType)
		if generate.IsEmbedded(strct) {
			fmt.Fprintf(b, "return rr.%s.Len() }", strct.Fields.List[0].Type)
			continue
		}

		fmt.Fprintf(b, "return rr.Hdr.Len() + rr.%s.Len() }\n", rrname)
	}

	generate.Write(b, out)
}
