-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add type check decision cache As regexps are heavy, it is a good idea to cache check result and preform checks only once per type. * feat: add struct fields cache Also improve code splitting.
- Loading branch information
Showing
4 changed files
with
191 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package analyzer | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
type PatternsList []*regexp.Regexp | ||
|
||
// MatchesAny matches provided string against all regexps in a slice. | ||
func (l PatternsList) MatchesAny(str string) bool { | ||
for _, r := range l { | ||
if r.MatchString(str) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// newPatternsList parses slice of strings to a slice of compiled regular | ||
// expressions. | ||
func newPatternsList(in []string) (PatternsList, error) { | ||
list := PatternsList{} | ||
|
||
for _, str := range in { | ||
re, err := strToRegexp(str) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
list = append(list, re) | ||
} | ||
|
||
return list, nil | ||
} | ||
|
||
type reListVar struct { | ||
values *PatternsList | ||
} | ||
|
||
func (v *reListVar) Set(value string) error { | ||
re, err := strToRegexp(value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*v.values = append(*v.values, re) | ||
|
||
return nil | ||
} | ||
|
||
func (v *reListVar) String() string { | ||
return "" | ||
} | ||
|
||
func strToRegexp(str string) (*regexp.Regexp, error) { | ||
if str == "" { | ||
return nil, ErrEmptyPattern | ||
} | ||
|
||
re, err := regexp.Compile(str) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to compile %s as regular expression: %w", str, err) | ||
} | ||
|
||
return re, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package analyzer | ||
|
||
import ( | ||
"go/types" | ||
) | ||
|
||
type StructFields struct { | ||
Public []string | ||
|
||
All []string | ||
} | ||
|
||
func NewStructFields(strct *types.Struct) *StructFields { | ||
sf := StructFields{} //nolint:exhaustruct | ||
|
||
for i := 0; i < strct.NumFields(); i++ { | ||
f := strct.Field(i) | ||
|
||
sf.All = append(sf.All, f.Name()) | ||
|
||
if f.Exported() { | ||
sf.Public = append(sf.Public, f.Name()) | ||
} | ||
} | ||
|
||
return &sf | ||
} |