Skip to content

Commit

Permalink
perf(params): use NewReplacer to replace params (#1148)
Browse files Browse the repository at this point in the history
Compared to `ReplaceAll()`, `NewReplacer()` is probably a better choice,
because you only need to maintain a list of old and new string pairs.
  • Loading branch information
WaterLemons2k authored Jun 7, 2024
1 parent 20af380 commit 7466211
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
19 changes: 9 additions & 10 deletions config/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,15 @@ func getDomainsStatus(domains []*Domain) updateStatusType {
}

// replacePara 替换参数
func replacePara(domains *Domains, orgPara string, ipv4Result updateStatusType, ipv6Result updateStatusType) (newPara string) {
orgPara = strings.ReplaceAll(orgPara, "#{ipv4Addr}", domains.Ipv4Addr)
orgPara = strings.ReplaceAll(orgPara, "#{ipv4Result}", util.LogStr(string(ipv4Result))) // i18n
orgPara = strings.ReplaceAll(orgPara, "#{ipv4Domains}", getDomainsStr(domains.Ipv4Domains))

orgPara = strings.ReplaceAll(orgPara, "#{ipv6Addr}", domains.Ipv6Addr)
orgPara = strings.ReplaceAll(orgPara, "#{ipv6Result}", util.LogStr(string(ipv6Result))) // i18n
orgPara = strings.ReplaceAll(orgPara, "#{ipv6Domains}", getDomainsStr(domains.Ipv6Domains))

return orgPara
func replacePara(domains *Domains, orgPara string, ipv4Result updateStatusType, ipv6Result updateStatusType) string {
return strings.NewReplacer(
"#{ipv4Addr}", domains.Ipv4Addr,
"#{ipv4Result}", util.LogStr(string(ipv4Result)), // i18n
"#{ipv4Domains}", getDomainsStr(domains.Ipv4Domains),
"#{ipv6Addr}", domains.Ipv6Addr,
"#{ipv6Result}", util.LogStr(string(ipv6Result)), // i18n
"#{ipv6Domains}", getDomainsStr(domains.Ipv6Domains),
).Replace(orgPara)
}

// getDomainsStr 用逗号分割域名
Expand Down
27 changes: 20 additions & 7 deletions dns/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dns

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -101,17 +102,29 @@ func (cb *Callback) addUpdateDomainRecords(recordType string) {
}

// replacePara 替换参数
func replacePara(orgPara, ipAddr string, domain *config.Domain, recordType string, ttl string) (newPara string) {
orgPara = strings.ReplaceAll(orgPara, "#{ip}", ipAddr)
orgPara = strings.ReplaceAll(orgPara, "#{domain}", domain.String())
orgPara = strings.ReplaceAll(orgPara, "#{recordType}", recordType)
orgPara = strings.ReplaceAll(orgPara, "#{ttl}", ttl)
func replacePara(orgPara, ipAddr string, domain *config.Domain, recordType string, ttl string) string {
// params 使用 map 以便添加更多参数
params := map[string]string{
"ip": ipAddr,
"domain": domain.String(),
"recordType": recordType,
"ttl": ttl,
}

// 也替换域名的自定义参数
for k, v := range domain.GetCustomParams() {
if len(v) == 1 {
orgPara = strings.ReplaceAll(orgPara, "#{"+k+"}", v[0])
params[k] = v[0]
}
}

return orgPara
// 将 map 转换为 [NewReplacer] 所需的参数
// map 中的每个元素占用 2 个位置(kv),因此需要预留 2 倍的空间
oldnew := make([]string, 0, len(params)*2)
for k, v := range params {
k = fmt.Sprintf("#{%s}", k)
oldnew = append(oldnew, k, v)
}

return strings.NewReplacer(oldnew...).Replace(orgPara)
}
11 changes: 6 additions & 5 deletions dns/namecheap.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ func (nc *NameCheap) modify(domain *config.Domain, ipAddr string) {

// request 统一请求接口
func (nc *NameCheap) request(result *NameCheapResp, ipAddr string, domain *config.Domain) (err error) {
var url string = nameCheapEndpoint
url = strings.ReplaceAll(url, "#{host}", domain.GetSubDomain())
url = strings.ReplaceAll(url, "#{domain}", domain.DomainName)
url = strings.ReplaceAll(url, "#{password}", nc.DNS.Secret)
url = strings.ReplaceAll(url, "#{ip}", ipAddr)
url := strings.NewReplacer(
"#{host}", domain.GetSubDomain(),
"#{domain}", domain.DomainName,
"#{password}", nc.DNS.Secret,
"#{ip}", ipAddr,
).Replace(nameCheapEndpoint)

req, err := http.NewRequest(
http.MethodGet,
Expand Down
33 changes: 19 additions & 14 deletions dns/namesilo.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,30 @@ func (ns *NameSilo) modify(domain *config.Domain, recordID, recordType, ipAddr s
}
}

func (ns *NameSilo) listRecords(domain *config.Domain) (resp NameSiloDNSListRecordResp, err error) {
//lint:ignore SA4006 false positive
func (ns *NameSilo) listRecords(domain *config.Domain) (*NameSiloDNSListRecordResp, error) {
result, err := ns.request("", domain, "", "", nameSiloListRecordEndpoint)
err = xml.Unmarshal([]byte(result), &resp)
return
if err != nil {
return nil, err
}

var resp NameSiloDNSListRecordResp
if err = xml.Unmarshal([]byte(result), &resp); err != nil {
return nil, err
}

return &resp, nil
}

// request 统一请求接口
func (ns *NameSilo) request(ipAddr string, domain *config.Domain, recordID, recordType, url string) (result string, err error) {
if domain.SubDomain == "@" {
url = strings.ReplaceAll(url, "#{host}", "")
} else {
url = strings.ReplaceAll(url, "#{host}", domain.SubDomain)
}
url = strings.ReplaceAll(url, "#{domain}", domain.DomainName)
url = strings.ReplaceAll(url, "#{password}", ns.DNS.Secret)
url = strings.ReplaceAll(url, "#{recordID}", recordID)
url = strings.ReplaceAll(url, "#{recordType}", recordType)
url = strings.ReplaceAll(url, "#{ip}", ipAddr)
url = strings.NewReplacer(
"#{host}", domain.SubDomain,
"#{domain}", domain.DomainName,
"#{password}", ns.DNS.Secret,
"#{recordID}", recordID,
"#{recordType}", recordType,
"#{ip}", ipAddr,
).Replace(url)
req, err := http.NewRequest(
http.MethodGet,
url,
Expand Down

0 comments on commit 7466211

Please sign in to comment.