Skip to content

Commit

Permalink
add support for comments in files (#224)
Browse files Browse the repository at this point in the history
* add support for comments in files

* update lint action

* fix lint errs
  • Loading branch information
dogancanbakir authored Jan 8, 2025
1 parent 6de7e8a commit df786f2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
18 changes: 5 additions & 13 deletions .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ on:

jobs:
lint:
name: Lint Test
name: "Lint"
if: "${{ !endsWith(github.actor, '[bot]') }}"
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21.x
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3.3.0
with:
version: latest
args: --timeout 5m
working-directory: .
- uses: actions/checkout@v4
- uses: projectdiscovery/actions/setup/go@v1
- uses: projectdiscovery/actions/golangci-lint@v1
2 changes: 1 addition & 1 deletion ratelimit_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (rateLimitMap *RateLimitMap) Del(key string) error {

// IsEmpty specifies if the underlying map is empty
func (rateLimitMap *RateLimitMap) IsEmpty() bool {
return rateLimitMap.kv == nil || len(rateLimitMap.kv) == 0
return len(rateLimitMap.kv) == 0
}

// AsMap returns the internal map as reference - changes are allowed
Expand Down
2 changes: 1 addition & 1 deletion runtime_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (runtimeMap *RuntimeMap) Del(key string) error {

// IsEmpty specifies if the underlying map is empty
func (runtimeMap *RuntimeMap) IsEmpty() bool {
return runtimeMap.kv == nil || len(runtimeMap.kv) == 0
return len(runtimeMap.kv) == 0
}

// AsMap returns the internal map as reference - changes are allowed
Expand Down
31 changes: 25 additions & 6 deletions slice_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/pkg/errors"
fileutil "github.com/projectdiscovery/utils/file"
stringsutil "github.com/projectdiscovery/utils/strings"
)

var quotes = []rune{'"', '\'', '`'}
Expand Down Expand Up @@ -64,10 +65,10 @@ func ToStringSlice(value string, options Options) ([]string, error) {
}

addPartToResult := func(part string) {
if options.Normalize != nil {
part = options.Normalize(part)
}
if !options.IsEmpty(part) {
if options.Normalize != nil {
part = options.Normalize(part)
}
result = append(result, part)
}
}
Expand Down Expand Up @@ -120,13 +121,31 @@ func isFromFile(_ string) bool {
}

func normalizeTrailingParts(s string) string {
return strings.TrimSpace(s)
return stringsutil.NormalizeWithOptions(s,
stringsutil.NormalizeOptions{
StripComments: true,
TrimSpaces: true,
},
)
}

func normalize(s string) string {
return strings.TrimSpace(strings.Trim(strings.TrimSpace(s), string(quotes)))
return stringsutil.NormalizeWithOptions(s,
stringsutil.NormalizeOptions{
StripComments: true,
TrimCutset: string(quotes),
TrimSpaces: true,
},
)
}

func normalizeLowercase(s string) string {
return strings.TrimSpace(strings.Trim(strings.TrimSpace(strings.ToLower(s)), string(quotes)))
return stringsutil.NormalizeWithOptions(s,
stringsutil.NormalizeOptions{
StripComments: true,
TrimCutset: string(quotes),
TrimSpaces: true,
Lowercase: true,
},
)
}
2 changes: 1 addition & 1 deletion string_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestFileNormalizedStringSliceOptions(t *testing.T) {

func TestFileStringSliceOptions(t *testing.T) {
filename := "test.txt"
_ = os.WriteFile(filename, []byte("value1,value2\nvalue3"), 0644)
_ = os.WriteFile(filename, []byte("# this is a comment\nvalue1,value2\nvalue3"), 0644)
defer os.RemoveAll(filename)

result, err := ToStringSlice(filename, FileStringSliceOptions)
Expand Down

0 comments on commit df786f2

Please sign in to comment.