1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

Make it easier to add new Rtypes. (#169)

* NEW: docs/adding-new-rtypes.md
* Mark all "if" and "switch" statements with `#rtype_variations`
* Make consistent use of `default: panic()`
This commit is contained in:
Tom Limoncelli
2017-08-04 12:26:29 -07:00
committed by GitHub
parent f214fb4515
commit 2cbabd859b
9 changed files with 213 additions and 27 deletions

View File

@@ -85,7 +85,9 @@ func (r *RecordConfig) String() (content string) {
}
content = fmt.Sprintf("%s %s %s %d", r.Type, r.NameFQDN, r.Target, r.TTL)
switch r.Type {
switch r.Type { // #rtype_variations
case "A", "AAAA", "CNAME", "PTR", "TXT":
// Nothing special.
case "MX":
content += fmt.Sprintf(" priority=%d", r.MxPreference)
case "SOA":
@@ -93,7 +95,10 @@ func (r *RecordConfig) String() (content string) {
case "CAA":
content += fmt.Sprintf(" caatag=%s caaflag=%d", r.CaaTag, r.CaaFlag)
default:
// assume nothing special for A,CNAME,AAAA, and other simple types.
msg := fmt.Sprintf("rc.String rtype %v unimplemented", r.Type)
panic(msg)
// We panic so that we quickly find any switch statements
// that have not been updated for a new RR type.
}
for k, v := range r.Metadata {
content += fmt.Sprintf(" %s=%s", k, v)
@@ -168,7 +173,7 @@ func (rc *RecordConfig) ToRR() dns.RR {
}
// Fill in the data.
switch rdtype {
switch rdtype { // #rtype_variations
case dns.TypeA:
rr.(*dns.A).A = net.ParseIP(rc.Target)
case dns.TypeAAAA:
@@ -205,6 +210,8 @@ func (rc *RecordConfig) ToRR() dns.RR {
rr.(*dns.TXT).Txt = []string{rc.Target}
default:
panic(fmt.Sprintf("ToRR: Unimplemented rtype %v", rc.Type))
// We panic so that we quickly find any switch statements
// that have not been updated for a new RR type.
}
return rr
@@ -269,11 +276,19 @@ func (dc *DomainConfig) Punycode() error {
if err != nil {
return err
}
if rec.Type == "MX" || rec.Type == "CNAME" {
switch rec.Type { // #rtype_variations
case "ALIAS", "MX", "NS", "CNAME", "SRV":
rec.Target, err = idna.ToASCII(rec.Target)
if err != nil {
return err
}
case "A", "CAA":
// Nothing to do.
default:
msg := fmt.Sprintf("Punycode rtype %v unimplemented", rec.Type)
panic(msg)
// We panic so that we quickly find any switch statements
// that have not been updated for a new RR type.
}
}
return nil