Keep options in a fixed order, and say which block a diff hunk is in

Options of a block (server options, a view, a zone) are listed and
written grouped by topic, alphabetical within a group, so the same
option is in the same place everywhere. Top-level statements keep the
order they were added in.

named.conf diffs add the enclosing blocks to each hunk header, like
git's function context: @@ -50,6 +49,8 @@ view "internal" {.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adamandClaude Opus 5.5 committed 2026-10-11 17:06:22 +01:00
1 parent 73d97ecc0a
commit b097e10bef
9 files changed
+326 -3

No files matched your search

+7 -1
View File
@@ -506,7 +506,9 @@ func (s *State) ServedRecordsByZone() map[int64][]*Record {
return out
}
// ScopedOptions returns the options of a scope, in position order.
// ScopedOptions returns the options of a scope: in a fixed order by name
// for a block (see optionLess), in the order they were added for
// top-level statements, which are written as entered.
func (s *State) ScopedOptions(scope string, scopeID int64) []*Option {
var out []*Option
for _, o := range s.Options {
@@ -514,6 +516,10 @@ func (s *State) ScopedOptions(scope string, scopeID int64) []*Option {
out = append(out, o)
}
}
if scope != ScopeTopLevel {
sort.Slice(out, func(i, j int) bool { return optionLess(out[i], out[j]) })
return out
}
sort.Slice(out, func(i, j int) bool {
if out[i].Position != out[j].Position {
return out[i].Position < out[j].Position
+55
View File
@@ -0,0 +1,55 @@
package model
// optionGroups orders the options of a block (options, a view, a zone) the
// same way everywhere: in named.conf, the web UI and the CLI. Related
// options are grouped; within a group they are alphabetical, and options
// not listed here come last, alphabetically. named doesn't care about the
// order of options in a block.
var optionGroups = [][]string{
// Files and identity.
{"bindkeys-file", "directory", "dump-file", "hostname", "managed-keys-directory", "memstatistics-file", "pid-file", "recursing-file", "secroots-file", "server-id", "session-keyfile", "statistics-file", "version"},
// Listening and source addresses.
{"listen-on", "listen-on-v6", "notify-source", "notify-source-v6", "query-source", "query-source-v6", "transfer-source", "transfer-source-v6"},
// Access control.
{"allow-query", "allow-query-cache", "allow-query-cache-on", "allow-query-on", "allow-recursion", "allow-recursion-on", "allow-update", "allow-update-forwarding", "blackhole", "deny-answer-addresses", "deny-answer-aliases", "update-policy"},
// Recursion, forwarding and the cache.
{"forward", "forwarders", "max-cache-size", "max-cache-ttl", "max-ncache-ttl", "minimal-responses", "prefetch", "qname-minimization", "rate-limit", "recursion", "response-policy", "serve-stale", "stale-answer-enable", "stale-answer-ttl", "stale-cache-enable"},
// DNSSEC.
{"dnssec-accept-expired", "dnssec-must-be-secure", "dnssec-policy", "dnssec-validation", "inline-signing", "key-directory", "trust-anchor-telemetry", "validate-except"},
// Transfers and notify.
{"allow-notify", "allow-transfer", "also-notify", "catalog-zones", "ixfr-from-differences", "masters", "max-transfer-idle-in", "max-transfer-idle-out", "max-transfer-time-in", "max-transfer-time-out", "notify", "notify-to-soa", "primaries", "provide-ixfr", "request-ixfr", "serial-query-rate", "transfer-format", "transfers-in", "transfers-out", "transfers-per-ns"},
}
var optionGroup = func() map[string]int {
m := map[string]int{}
for i, g := range optionGroups {
for _, n := range g {
m[n] = i
}
}
return m
}()
// optionLess orders two options of the same block: by group, then name,
// then (for an option given more than once, like listen-on) the order they
// were added in.
func optionLess(a, b *Option) bool {
ga, ok := optionGroup[a.Name]
if !ok {
ga = len(optionGroups)
}
gb, ok := optionGroup[b.Name]
if !ok {
gb = len(optionGroups)
}
if ga != gb {
return ga < gb
}
if a.Name != b.Name {
return a.Name < b.Name
}
if a.Position != b.Position {
return a.Position < b.Position
}
return a.ID < b.ID
}
+40
View File
@@ -0,0 +1,40 @@
package model
import (
"strings"
"testing"
)
func TestScopedOptionsOrder(t *testing.T) {
s := NewState()
add := func(scope string, names ...string) {
for i, n := range names {
id := int64(len(s.Options) + 1)
s.Options[id] = &Option{ID: id, Scope: scope, Name: n, Position: i}
}
}
// Added in an arbitrary order; listen-on twice.
add(ScopeView, "zzz-unknown", "forwarders", "listen-on", "recursion", "dnssec-validation", "aaa-unknown", "allow-query", "forward", "directory", "listen-on", "allow-transfer")
var got []string
for _, o := range s.ScopedOptions(ScopeView, 0) {
got = append(got, o.Name)
}
want := "directory listen-on listen-on allow-query forward forwarders recursion dnssec-validation allow-transfer aaa-unknown zzz-unknown"
if strings.Join(got, " ") != want {
t.Errorf("order:\n got %s\nwant %s", strings.Join(got, " "), want)
}
// An option given twice keeps the order it was added in.
l := s.ScopedOptions(ScopeView, 0)
if l[1].Position > l[2].Position {
t.Error("repeated listen-on swapped")
}
// Top-level statements stay as entered.
add(ScopeTopLevel, "logging", "acl", "key")
got = nil
for _, o := range s.ScopedOptions(ScopeTopLevel, 0) {
got = append(got, o.Name)
}
if strings.Join(got, " ") != "logging acl key" {
t.Errorf("top level: %v", got)
}
}