From 958a799c450e35515603332a6728235f379acf53 Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 23:45:05 +0000 Subject: [PATCH] feat(dns): add explicit DNS service constructor aliases Co-Authored-By: Virgil --- action.go | 18 ++++++++++++++---- service.go | 13 +++++++++++++ service_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/action.go b/action.go index ceeb5da..942a784 100644 --- a/action.go +++ b/action.go @@ -25,10 +25,10 @@ var ( ) const ( - actionArgBind = "bind" - actionArgIP = "ip" - actionArgName = "name" - actionArgPort = "port" + actionArgBind = "bind" + actionArgIP = "ip" + actionArgName = "name" + actionArgPort = "port" actionArgHealthPort = "health_port" ) @@ -192,6 +192,8 @@ func (service *Service) RegisterActions(registrar ActionRegistrar) { // NewServiceWithRegistrar builds a DNS service and registers its actions in one step. // +// Deprecated: use NewDNSServiceWithRegistrar for a more explicit constructor name. +// // service := dns.NewServiceWithRegistrar(dns.ServiceConfiguration{}, registrar) // // registrar now exposes dns.resolve, dns.resolve.txt, dns.resolve.all, dns.reverse, dns.serve, dns.health, dns.discover func NewServiceWithRegistrar(options ServiceOptions, registrar ActionRegistrar) *Service { @@ -201,6 +203,14 @@ func NewServiceWithRegistrar(options ServiceOptions, registrar ActionRegistrar) return NewService(options) } +// NewDNSServiceWithRegistrar builds a DNS service and registers its actions in one step. +// +// service := dns.NewDNSServiceWithRegistrar(dns.ServiceConfiguration{}, registrar) +// // registrar now exposes dns.resolve, dns.resolve.txt, dns.resolve.all, dns.reverse, dns.serve, dns.health, dns.discover +func NewDNSServiceWithRegistrar(options ServiceOptions, registrar ActionRegistrar) *Service { + return NewServiceWithRegistrar(options, registrar) +} + // HandleAction executes a DNS action by name. // // payload, ok, err := service.HandleAction(ActionResolve, map[string]any{ diff --git a/service.go b/service.go index aef2f5a..915b062 100644 --- a/service.go +++ b/service.go @@ -176,6 +176,8 @@ type Options = ServiceOptions // NewService builds a DNS service from cached records and optional discovery hooks. // +// Deprecated: use NewDNSService for a more explicit constructor name. +// // service := dns.NewService(dns.ServiceConfig{ // Records: map[string]dns.NameRecords{ // "gateway.charon.lthn": {A: []string{"10.10.10.10"}}, @@ -265,6 +267,17 @@ func NewService(options ServiceOptions) *Service { return service } +// NewDNSService builds a DNS service from cached records and optional discovery hooks. +// +// service := dns.NewDNSService(dns.ServiceConfiguration{ +// Records: map[string]dns.NameRecords{ +// "gateway.charon.lthn": {A: []string{"10.10.10.10"}}, +// }, +// }) +func NewDNSService(options ServiceOptions) *Service { + return NewService(options) +} + func (service *Service) resolveHSDClient(client *HSDClient) (*HSDClient, error) { if client != nil { return client, nil diff --git a/service_test.go b/service_test.go index dfcb152..d25a334 100644 --- a/service_test.go +++ b/service_test.go @@ -130,6 +130,46 @@ func TestServiceOptionsAliasBuildsService(t *testing.T) { } } +func TestNewDNSServiceAliasToExistingConstructor(t *testing.T) { + service := NewDNSService(ServiceOptions{ + Records: map[string]NameRecords{ + "gateway.charon.lthn": { + A: []string{"10.10.10.10"}, + }, + }, + }) + + result, ok := service.ResolveAddress("gateway.charon.lthn") + if !ok { + t.Fatal("expected service constructed from NewDNSService alias to resolve") + } + if len(result.Addresses) != 1 || result.Addresses[0] != "10.10.10.10" { + t.Fatalf("unexpected resolve result from NewDNSService: %#v", result.Addresses) + } +} + +func TestNewDNSServiceWithRegistrarAliasRegistersActions(t *testing.T) { + recorder := &actionRecorder{} + service := NewDNSServiceWithRegistrar(ServiceOptions{}, recorder) + if service == nil { + t.Fatal("expected service instance from NewDNSServiceWithRegistrar") + } + + if len(recorder.names) != len(service.ActionNames()) { + t.Fatalf("expected %d registered action names, got %d", len(service.ActionNames()), len(recorder.names)) + } + + expected := map[string]struct{}{} + for _, name := range service.ActionNames() { + expected[name] = struct{}{} + } + for _, name := range recorder.names { + if _, ok := expected[name]; !ok { + t.Fatalf("unexpected action name registered by NewDNSServiceWithRegistrar: %q", name) + } + } +} + func TestServiceResolveUsesMostSpecificWildcard(t *testing.T) { service := NewService(ServiceOptions{ Records: map[string]NameRecords{