Skip to content

Commit

Permalink
fix(pkg/blockbuilder): make settings fields nilable
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jun 14, 2023
1 parent 55c6711 commit 0715917
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
6 changes: 3 additions & 3 deletions internal/setup/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func BuildBlockBuilder(userSettings settings.Block,
client *http.Client) (blockBuilder *blockbuilder.Builder) {
settings := blockbuilder.Settings{
Client: client,
BlockMalicious: *userSettings.BlockMalicious,
BlockAds: *userSettings.BlockAds,
BlockSurveillance: *userSettings.BlockSurveillance,
BlockMalicious: userSettings.BlockMalicious,
BlockAds: userSettings.BlockAds,
BlockSurveillance: userSettings.BlockSurveillance,
}

settings.AllowedHosts = make([]string, len(userSettings.AllowedHosts))
Expand Down
6 changes: 3 additions & 3 deletions pkg/blockbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func New(settings Settings) *Builder {

return &Builder{
client: settings.Client,
blockMalicious: settings.BlockMalicious,
blockAds: settings.BlockAds,
blockSurveillance: settings.BlockSurveillance,
blockMalicious: *settings.BlockMalicious,
blockAds: *settings.BlockAds,
blockSurveillance: *settings.BlockSurveillance,
allowedHosts: settings.AllowedHosts,
allowedIPs: settings.AllowedIPs,
allowedIPPrefixes: settings.AllowedIPPrefixes,
Expand Down
15 changes: 9 additions & 6 deletions pkg/blockbuilder/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

type Settings struct {
Client *http.Client
BlockMalicious bool
BlockAds bool
BlockSurveillance bool
BlockMalicious *bool
BlockAds *bool
BlockSurveillance *bool
AllowedHosts []string
AllowedIPs []netip.Addr
AllowedIPPrefixes []netip.Prefix
Expand All @@ -27,6 +27,9 @@ type Settings struct {

func (s *Settings) SetDefaults() {
s.Client = gosettings.DefaultPointerRaw(s.Client, http.DefaultClient)
s.BlockMalicious = gosettings.DefaultPointer(s.BlockMalicious, false)
s.BlockAds = gosettings.DefaultPointer(s.BlockMalicious, false)
s.BlockSurveillance = gosettings.DefaultPointer(s.BlockMalicious, false)
}

var hostRegex = regexp.MustCompile(`^([a-zA-Z0-9]|[a-zA-Z0-9_][a-zA-Z0-9\-_]{0,61}[a-zA-Z0-9_])(\.([a-zA-Z0-9]|[a-zA-Z0-9_][a-zA-Z0-9\-_]{0,61}[a-zA-Z0-9]))*$`) //nolint:lll
Expand Down Expand Up @@ -60,13 +63,13 @@ func (s *Settings) ToLinesNode() (node *gotree.Node) {
node = gotree.New("Filter build settings:")

var blockedCategories []string
if s.BlockMalicious {
if *s.BlockMalicious {
blockedCategories = append(blockedCategories, "malicious")
}
if s.BlockSurveillance {
if *s.BlockSurveillance {
blockedCategories = append(blockedCategories, "surveillance")
}
if s.BlockAds {
if *s.BlockAds {
blockedCategories = append(blockedCategories, "ads")
}

Expand Down

0 comments on commit 0715917

Please sign in to comment.