Skip to content

Commit

Permalink
all: more naming imps
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jan 19, 2022
1 parent d3d417f commit 03c6798
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
18 changes: 9 additions & 9 deletions internal/aghalg/aghalg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ type comparable = interface{}
type UniqChecker map[comparable]int64

// Add adds a value to the validator. v must not be nil.
func (v UniqChecker) Add(elems ...comparable) {
func (uc UniqChecker) Add(elems ...comparable) {
for _, e := range elems {
v[e]++
uc[e]++
}
}

// Merge returns a validator containing data from both v and other.
func (v UniqChecker) Merge(other UniqChecker) (merged UniqChecker) {
merged = make(UniqChecker, len(v)+len(other))
for elem, num := range v {
func (uc UniqChecker) Merge(other UniqChecker) (merged UniqChecker) {
merged = make(UniqChecker, len(uc)+len(other))
for elem, num := range uc {
merged[elem] += num
}

Expand All @@ -41,9 +41,9 @@ func (v UniqChecker) Merge(other UniqChecker) (merged UniqChecker) {
// Validate returns an error enumerating all elements that aren't unique.
// isBefore is an optional sorting function to make the error message
// deterministic.
func (v UniqChecker) Validate(isBefore func(a, b comparable) (less bool)) (err error) {
func (uc UniqChecker) Validate(isBefore func(a, b comparable) (less bool)) (err error) {
var dup []comparable
for elem, num := range v {
for elem, num := range uc {
if num > 1 {
dup = append(dup, elem)
}
Expand All @@ -62,13 +62,13 @@ func (v UniqChecker) Validate(isBefore func(a, b comparable) (less bool)) (err e
return fmt.Errorf("duplicated values: %v", dup)
}

// IntIsBefore is a helper sort function for UniquenessValidator.Validate.
// IntIsBefore is a helper sort function for UniqChecker.Validate.
// a and b must be of type int.
func IntIsBefore(a, b comparable) (less bool) {
return a.(int) < b.(int)
}

// StringIsBefore is a helper sort function for UniquenessValidator.Validate.
// StringIsBefore is a helper sort function for UniqChecker.Validate.
// a and b must be of type string.
func StringIsBefore(a, b comparable) (less bool) {
return a.(string) < b.(string)
Expand Down
8 changes: 4 additions & 4 deletions internal/dnsforward/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ func validateAccessSet(list *accessListJSON) (err error) {
}

// validateStrUniq returns an informative error if clients are not unique.
func validateStrUniq(clients []string) (uv aghalg.UniqChecker, err error) {
uv = make(aghalg.UniqChecker, len(clients))
func validateStrUniq(clients []string) (uc aghalg.UniqChecker, err error) {
uc = make(aghalg.UniqChecker, len(clients))
for _, c := range clients {
uv.Add(c)
uc.Add(c)
}

return uv, uv.Validate(aghalg.StringIsBefore)
return uc, uc.Validate(aghalg.StringIsBefore)
}

func (s *Server) handleAccessSet(w http.ResponseWriter, r *http.Request) {
Expand Down
12 changes: 6 additions & 6 deletions internal/home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,24 @@ func parseConfig() (err error) {
return err
}

uv := aghalg.UniqChecker{}
uc := aghalg.UniqChecker{}
addPorts(
uv,
uc,
config.BindPort,
config.BetaBindPort,
config.DNS.Port,
)

if config.TLS.Enabled {
addPorts(
uv,
uc,
config.TLS.PortHTTPS,
config.TLS.PortDNSOverTLS,
config.TLS.PortDNSOverQUIC,
config.TLS.PortDNSCrypt,
)
}
if err = uv.Validate(aghalg.IntIsBefore); err != nil {
if err = uc.Validate(aghalg.IntIsBefore); err != nil {
return fmt.Errorf("validating ports: %w", err)
}

Expand All @@ -321,10 +321,10 @@ func parseConfig() (err error) {
}

// addPorts is a helper for ports validation. It skips zero ports.
func addPorts(uv aghalg.UniqChecker, ports ...int) {
func addPorts(uc aghalg.UniqChecker, ports ...int) {
for _, p := range ports {
if p != 0 {
uv.Add(p)
uc.Add(p)
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions internal/home/controlinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ type checkConfResp struct {

// validateWeb returns error is the web part if the initial configuration can't
// be set.
func (req *checkConfReq) validateWeb(uv aghalg.UniqChecker) (err error) {
func (req *checkConfReq) validateWeb(uc aghalg.UniqChecker) (err error) {
defer func() { err = errors.Annotate(err, "validating ports: %w") }()

port := req.Web.Port
addPorts(uv, config.BetaBindPort, port)
if err = uv.Validate(aghalg.IntIsBefore); err != nil {
addPorts(uc, config.BetaBindPort, port)
if err = uc.Validate(aghalg.IntIsBefore); err != nil {
// Avoid duplicating the error into the status of DNS.
uv[port] = 1
uc[port] = 1

return err
}
Expand All @@ -130,12 +130,12 @@ func (req *checkConfReq) validateWeb(uv aghalg.UniqChecker) (err error) {
// validateDNS returns error if the DNS part of the initial configuration can't
// be set. autofix is true if the port can be unbound by AdGuard Home
// automatically.
func (req *checkConfReq) validateDNS(uv aghalg.UniqChecker) (canAutofix bool, err error) {
func (req *checkConfReq) validateDNS(uc aghalg.UniqChecker) (canAutofix bool, err error) {
defer func() { err = errors.Annotate(err, "validating ports: %w") }()

port := req.DNS.Port
addPorts(uv, port)
if err = uv.Validate(aghalg.IntIsBefore); err != nil {
addPorts(uc, port)
if err = uc.Validate(aghalg.IntIsBefore); err != nil {
return false, err
}

Expand Down Expand Up @@ -184,13 +184,13 @@ func (web *Web) handleInstallCheckConfig(w http.ResponseWriter, r *http.Request)
}

resp := &checkConfResp{}
uv := aghalg.UniqChecker{}
uc := aghalg.UniqChecker{}

if err = req.validateWeb(uv); err != nil {
if err = req.validateWeb(uc); err != nil {
resp.Web.Status = err.Error()
}

if resp.DNS.CanAutofix, err = req.validateDNS(uv); err != nil {
if resp.DNS.CanAutofix, err = req.validateDNS(uc); err != nil {
resp.DNS.Status = err.Error()
} else if !req.DNS.IP.IsUnspecified() {
resp.StaticIP = handleStaticIP(req.DNS.IP, req.SetStaticIP)
Expand Down
8 changes: 4 additions & 4 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,23 +296,23 @@ func setupConfig(args options) (err error) {
Context.clients.Init(config.Clients, Context.dhcpServer, Context.etcHosts)

if args.bindPort != 0 {
uv := aghalg.UniqChecker{}
uc := aghalg.UniqChecker{}
addPorts(
uv,
uc,
args.bindPort,
config.BetaBindPort,
config.DNS.Port,
)
if config.TLS.Enabled {
addPorts(
uv,
uc,
config.TLS.PortHTTPS,
config.TLS.PortDNSOverTLS,
config.TLS.PortDNSOverQUIC,
config.TLS.PortDNSCrypt,
)
}
if err = uv.Validate(aghalg.IntIsBefore); err != nil {
if err = uc.Validate(aghalg.IntIsBefore); err != nil {
return fmt.Errorf("validating ports: %w", err)
}

Expand Down
12 changes: 6 additions & 6 deletions internal/home/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ func (t *TLSMod) handleTLSValidate(w http.ResponseWriter, r *http.Request) {
}

if setts.Enabled {
uv := aghalg.UniqChecker{}
uc := aghalg.UniqChecker{}
addPorts(
uv,
uc,
config.BindPort,
config.BetaBindPort,
config.DNS.Port,
Expand All @@ -263,7 +263,7 @@ func (t *TLSMod) handleTLSValidate(w http.ResponseWriter, r *http.Request) {
setts.PortDNSCrypt,
)

err = uv.Validate(aghalg.IntIsBefore)
err = uc.Validate(aghalg.IntIsBefore)
if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "validating ports: %s", err)

Expand Down Expand Up @@ -344,9 +344,9 @@ func (t *TLSMod) handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
}

if data.Enabled {
uv := aghalg.UniqChecker{}
uc := aghalg.UniqChecker{}
addPorts(
uv,
uc,
config.BindPort,
config.BetaBindPort,
config.DNS.Port,
Expand All @@ -356,7 +356,7 @@ func (t *TLSMod) handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
data.PortDNSCrypt,
)

err = uv.Validate(aghalg.IntIsBefore)
err = uc.Validate(aghalg.IntIsBefore)
if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "%s", err)

Expand Down

0 comments on commit 03c6798

Please sign in to comment.