From 6a36dda54e596262d9079bc303e094453e9b6558 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Tue, 22 Aug 2023 08:06:32 -0700 Subject: Generated types rely on interface --- bind/config.go | 4 ++++ dns/types.go | 13 ++++++++----- generate_dns_types.go | 4 +--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/bind/config.go b/bind/config.go index 1d4a81a..62c1324 100644 --- a/bind/config.go +++ b/bind/config.go @@ -119,6 +119,10 @@ type Zone struct { config *BINDConfig } +func (z *Zone) ZoneName() string { + return z.Name +} + func (z *Zone) AddKey(key string) { z.keys = append(z.keys, key) } diff --git a/dns/types.go b/dns/types.go index aa5112b..4a02d2c 100644 --- a/dns/types.go +++ b/dns/types.go @@ -4,22 +4,25 @@ import ( "fmt" _ "net" - "code.crute.me/mcrute/go_ddns_manager/bind" "github.com/miekg/dns" ) //go:generate go run ../generate_dns_types.go -func makeHeader(name string, zone *bind.Zone, t uint16, ttl int) dns.RR_Header { +type NamedZone interface { + ZoneName() string +} + +func makeHeader(name string, zone NamedZone, t uint16, ttl int) dns.RR_Header { return dns.RR_Header{ - Name: fmt.Sprintf("%s.%s", name, zone.Name), + Name: fmt.Sprintf("%s.%s", name, zone.ZoneName()), Rrtype: t, Class: dns.ClassINET, Ttl: uint32(ttl), } } -func toRRSet(z *bind.Zone, rr ...RR) []dns.RR { +func toRRSet(z NamedZone, rr ...RR) []dns.RR { o := []dns.RR{} for _, v := range rr { o = append(o, v.ToDNS(z)) @@ -28,7 +31,7 @@ func toRRSet(z *bind.Zone, rr ...RR) []dns.RR { } type RR interface { - ToDNS(*bind.Zone) dns.RR + ToDNS(NamedZone) dns.RR FromDNS(rr dns.RR) error MarshalJSON() ([]byte, error) UnmarshalJSON(data []byte) error diff --git a/generate_dns_types.go b/generate_dns_types.go index 4e2aade..4e90693 100644 --- a/generate_dns_types.go +++ b/generate_dns_types.go @@ -30,8 +30,6 @@ import ( "net" "github.com/miekg/dns" - - "code.crute.me/mcrute/go_ddns_manager/bind" ) {{ range $name, $fields := . -}} @@ -43,7 +41,7 @@ type {{ $name }} struct { {{ end -}} } -func (r *{{ $name }}) ToDNS(zone *bind.Zone) dns.RR { +func (r *{{ $name }}) ToDNS(zone NamedZone) dns.RR { return &dns.{{ $name }}{ Hdr: makeHeader(r.Name, zone, dns.Type{{ $name }}, r.Ttl), {{ range $fields -}} -- cgit v1.2.3