aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ghostiam/protogetter/processor.go
diff options
context:
space:
mode:
authordependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>2024-03-04 17:40:11 +0000
committerTaras Madan <tarasmadan@google.com>2024-03-04 18:34:55 +0000
commit5fc5366972c874b919f93165bb4ed4e2bcb7c350 (patch)
tree287c3361a0dee0c72af80d9a1a66714a06e98a62 /vendor/github.com/ghostiam/protogetter/processor.go
parent1be5ce38a9059c356eb193a8c34d60d61c9fc31f (diff)
mod: bump github.com/golangci/golangci-lint from 1.55.2 to 1.56.2
Bumps [github.com/golangci/golangci-lint](https://github.com/golangci/golangci-lint) from 1.55.2 to 1.56.2. - [Release notes](https://github.com/golangci/golangci-lint/releases) - [Changelog](https://github.com/golangci/golangci-lint/blob/master/CHANGELOG.md) - [Commits](https://github.com/golangci/golangci-lint/compare/v1.55.2...v1.56.2) --- updated-dependencies: - dependency-name: github.com/golangci/golangci-lint dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
Diffstat (limited to 'vendor/github.com/ghostiam/protogetter/processor.go')
-rw-r--r--vendor/github.com/ghostiam/protogetter/processor.go131
1 files changed, 125 insertions, 6 deletions
diff --git a/vendor/github.com/ghostiam/protogetter/processor.go b/vendor/github.com/ghostiam/protogetter/processor.go
index 445f136b8..d65199dd2 100644
--- a/vendor/github.com/ghostiam/protogetter/processor.go
+++ b/vendor/github.com/ghostiam/protogetter/processor.go
@@ -12,16 +12,18 @@ import (
type processor struct {
info *types.Info
filter *PosFilter
+ cfg *Config
to strings.Builder
from strings.Builder
err error
}
-func Process(info *types.Info, filter *PosFilter, n ast.Node) (*Result, error) {
+func Process(info *types.Info, filter *PosFilter, n ast.Node, cfg *Config) (*Result, error) {
p := &processor{
info: info,
filter: filter,
+ cfg: cfg,
}
return p.process(n)
@@ -31,8 +33,12 @@ func (c *processor) process(n ast.Node) (*Result, error) {
switch x := n.(type) {
case *ast.AssignStmt:
// Skip any assignment to the field.
- for _, lhs := range x.Lhs {
- c.filter.AddPos(lhs.Pos())
+ for _, s := range x.Lhs {
+ c.filter.AddPos(s.Pos())
+
+ if se, ok := s.(*ast.StarExpr); ok {
+ c.filter.AddPos(se.X.Pos())
+ }
}
case *ast.IncDecStmt:
@@ -47,6 +53,14 @@ func (c *processor) process(n ast.Node) (*Result, error) {
}
case *ast.CallExpr:
+ if !c.cfg.ReplaceFirstArgInAppend && len(x.Args) > 0 {
+ if v, ok := x.Fun.(*ast.Ident); ok && v.Name == "append" {
+ // Skip first argument of append function.
+ c.filter.AddPos(x.Args[0].Pos())
+ break
+ }
+ }
+
f, ok := x.Fun.(*ast.SelectorExpr)
if !ok {
return &Result{}, nil
@@ -66,6 +80,67 @@ func (c *processor) process(n ast.Node) (*Result, error) {
c.processInner(x)
+ case *ast.StarExpr:
+ f, ok := x.X.(*ast.SelectorExpr)
+ if !ok {
+ return &Result{}, nil
+ }
+
+ if !isProtoMessage(c.info, f.X) {
+ return &Result{}, nil
+ }
+
+ // proto2 generates fields as pointers. Hence, the indirection
+ // must be removed when generating the fix for the case.
+ // The `*` is retained in `c.from`, but excluded from the fix
+ // present in the `c.to`.
+ c.writeFrom("*")
+ c.processInner(x.X)
+
+ case *ast.BinaryExpr:
+ // Check if the expression is a comparison.
+ if x.Op != token.EQL && x.Op != token.NEQ {
+ return &Result{}, nil
+ }
+
+ // Check if one of the operands is nil.
+
+ xIdent, xOk := x.X.(*ast.Ident)
+ yIdent, yOk := x.Y.(*ast.Ident)
+
+ xIsNil := xOk && xIdent.Name == "nil"
+ yIsNil := yOk && yIdent.Name == "nil"
+
+ if !xIsNil && !yIsNil {
+ return &Result{}, nil
+ }
+
+ // Extract the non-nil operand for further checks
+
+ var expr ast.Expr
+ if xIsNil {
+ expr = x.Y
+ } else {
+ expr = x.X
+ }
+
+ se, ok := expr.(*ast.SelectorExpr)
+ if !ok {
+ return &Result{}, nil
+ }
+
+ if !isProtoMessage(c.info, se.X) {
+ return &Result{}, nil
+ }
+
+ // Check if the Getter function of the protobuf message returns a pointer.
+ hasPointer, ok := getterResultHasPointer(c.info, se.X, se.Sel.Name)
+ if !ok || hasPointer {
+ return &Result{}, nil
+ }
+
+ c.filter.AddPos(x.X.Pos())
+
default:
return nil, fmt.Errorf("not implemented for type: %s (%s)", reflect.TypeOf(x), formatNode(n))
}
@@ -204,14 +279,14 @@ func isProtoMessage(info *types.Info, expr ast.Expr) bool {
return false
}
-func methodIsExists(info *types.Info, x ast.Expr, name string) bool {
+func typesNamed(info *types.Info, x ast.Expr) (*types.Named, bool) {
if info == nil {
- return false
+ return nil, false
}
t := info.TypeOf(x)
if t == nil {
- return false
+ return nil, false
}
ptr, ok := t.Underlying().(*types.Pointer)
@@ -221,6 +296,15 @@ func methodIsExists(info *types.Info, x ast.Expr, name string) bool {
named, ok := t.(*types.Named)
if !ok {
+ return nil, false
+ }
+
+ return named, true
+}
+
+func methodIsExists(info *types.Info, x ast.Expr, name string) bool {
+ named, ok := typesNamed(info, x)
+ if !ok {
return false
}
@@ -232,3 +316,38 @@ func methodIsExists(info *types.Info, x ast.Expr, name string) bool {
return false
}
+
+func getterResultHasPointer(info *types.Info, x ast.Expr, name string) (hasPointer, ok bool) {
+ named, ok := typesNamed(info, x)
+ if !ok {
+ return false, false
+ }
+
+ for i := 0; i < named.NumMethods(); i++ {
+ method := named.Method(i)
+ if method.Name() != "Get"+name {
+ continue
+ }
+
+ var sig *types.Signature
+ sig, ok = method.Type().(*types.Signature)
+ if !ok {
+ return false, false
+ }
+
+ results := sig.Results()
+ if results.Len() == 0 {
+ return false, false
+ }
+
+ firstType := results.At(0)
+ _, ok = firstType.Type().(*types.Pointer)
+ if !ok {
+ return false, true
+ }
+
+ return true, true
+ }
+
+ return false, false
+}