Skip to content

Commit

Permalink
Reduce the use of PopulateFromRaw*()
Browse files Browse the repository at this point in the history
  • Loading branch information
tlimoncelli committed Jan 14, 2025
1 parent 78353a6 commit 5bd4854
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
10 changes: 5 additions & 5 deletions models/t_mx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package models

import (
"fmt"
"strings"
)

Expand All @@ -13,14 +12,15 @@ func (rc *RecordConfig) SetTargetMX(preference uint16, mx string) error {

// SetTargetMXStrings is like SetTargetMX but accepts strings.
func (rc *RecordConfig) SetTargetMXStrings(pref, target string) error {
return PopulateFromRawMX(rc, []string{rc.Name, pref, target}, nil, "")
rdata, err := ParseMX([]string{rc.Name, pref, target}, "")
if err != nil {
return err
}
return RecordUpdateFields(rc, rdata, nil)
}

// SetTargetMXString is like SetTargetMX but accepts one big string.
func (rc *RecordConfig) SetTargetMXString(s string) error {
part := strings.Fields(s)
if len(part) != 2 {
return fmt.Errorf("MX value does not contain 2 fields: (%#v)", s)
}
return rc.SetTargetMXStrings(part[0], part[1])
}
23 changes: 17 additions & 6 deletions models/t_srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,46 @@ import (
// SetTargetSRV sets the SRV fields.
func (rc *RecordConfig) SetTargetSRV(priority, weight, port uint16, target string) error {
rc.Type = "SRV"

return RecordUpdateFields(rc, SRV{Priority: priority, Weight: weight, Port: port, Target: target}, nil)
}

// SetTargetSRVStrings is like SetTargetSRV but accepts all parameters as strings.
func (rc *RecordConfig) SetTargetSRVStrings(priority, weight, port, target string) (err error) {
return PopulateFromRawSRV(rc, []string{rc.Name, priority, weight, port, target}, nil, "")
rc.Type = "SRV"

rdata, err := ParseSRV([]string{priority, weight, port, target}, "")
if err != nil {
return err
}
return RecordUpdateFields(rc, rdata, nil)
}

// SetTargetSRVPriorityString is like SetTargetSRV but accepts priority as an
// uint16 and the rest of the values joined in a string that needs to be parsed.
// This is a helper function that comes in handy when a provider re-uses the MX preference
// field as the SRV priority.
func (rc *RecordConfig) SetTargetSRVPriorityString(priority uint16, s string) error {
var rdata SRV
var err error

part := strings.Fields(s)
switch len(part) {
case 3:
return PopulateFromRawSRV(rc, []string{rc.Name, strconv.Itoa(int(priority)), part[0], part[1], part[2]}, nil, "")
rdata, err = ParseSRV([]string{strconv.Itoa(int(priority)), part[0], part[1], part[2]}, "")
case 2:
return PopulateFromRawSRV(rc, []string{rc.Name, strconv.Itoa(int(priority)), part[0], part[1], "."}, nil, "")
rdata, err = ParseSRV([]string{strconv.Itoa(int(priority)), part[0], part[1], "."}, "")
default:
return fmt.Errorf("SRV value does not contain 3 fields: (%#v)", s)
}
if err != nil {
return err
}
return RecordUpdateFields(rc, rdata, nil)
}

// SetTargetSRVString is like SetTargetSRV but accepts one big string to be parsed.
func (rc *RecordConfig) SetTargetSRVString(s string) error {
part := strings.Fields(s)
if len(part) != 4 {
return fmt.Errorf("SRV value does not contain 4 fields: (%#v)", s)
}
return rc.SetTargetSRVStrings(part[0], part[1], part[2], part[3])
}
6 changes: 5 additions & 1 deletion models/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,9 @@ func (rc *RecordConfig) SetTargetIP(ip net.IP) error {

// SetTargetA sets the target to an A record.
func (rc *RecordConfig) SetTargetA(s string) error {
return PopulateFromRawA(rc, []string{rc.Name, s}, nil, "")
rdata, err := ParseA([]string{s}, "")
if err != nil {
return err
}
return RecordUpdateFields(rc, rdata, nil)
}

0 comments on commit 5bd4854

Please sign in to comment.