protogolint

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 13 Imported by: 0

README

Protogolint

Welcome to the Protogolint project!

Overview

Protogolint is a linter developed specifically for Go programmers working with nested protobuf types.
It's designed to aid developers in preventing invalid memory address or nil pointer dereference errors arising from direct access of nested protobuf fields.

When working with protobuf, it's quite common to have complex structures where a message field is contained within another message, which itself can be part of another message, and so on. If these fields are accessed directly and some field in the call chain will not be initialized, it can result in application panic.

Protogolint addresses this issue by suggesting use of getter methods for field access.

How does it work?

Protogolint analyzes your Go code and helps detect direct protobuf field accesses that could give rise to panic.
The linter suggests using getters:

m.GetFoo().GetBar().GetBaz()

instead of direct field access:

m.Foo.Bar.Baz

And you will then only need to perform a nil check after the final call:

if m.GetFoo().GetBar().GetBaz() != nil {
    // Do something with m.GetFoo().GetBar().GetBaz()
}

instead of:

if m.Foo != nil {
    if m.Foo.Bar != nil {
        if m.Foo.Bar.Baz != nil {
            // Do something with m.Foo.Bar.Baz
        }
    }
}

or use zero values:

// If one of the methods returns `nil` we will receive 0 instead of panic.
v := m.GetFoo().GetBar().GetBaz().GetInt() 

instead of panic:

// If at least one structure in the chains is not initialized, we will get a panic. 
v := m.Foo.Bar.Baz.Int

which simplifies the code and makes it more reliable.

Installation

go install github.com/ghostiam/protogolint/cmd/protogolint@latest

Usage

To run the linter:

protogolint ./...

Or to apply suggested fixes directly:

protogolint --fix ./...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAnalyzer added in v0.0.2

func NewAnalyzer() *analysis.Analyzer

Types

type Checker

type Checker struct {
	// contains filtered or unexported fields
}

func NewChecker

func NewChecker(pass *analysis.Pass) *Checker

NewChecker creates a new Checker instance.

func (*Checker) Check

func (c *Checker) Check(expr ast.Expr)

func (*Checker) Result

func (c *Checker) Result() (*CheckerResult, error)

func (*Checker) SetError

func (c *Checker) SetError(err error)

type CheckerResult

type CheckerResult struct {
	From string
	To   string
}

CheckerResult contains source code (from) and suggested change (to)

type InlineFix added in v0.0.2

type InlineFix struct {
	StartCol  int // zero-based
	Length    int
	NewString string
}

type Issue added in v0.0.2

type Issue struct {
	Pos       token.Position
	Message   string
	InlineFix InlineFix
}

Issue is used to integrate with golangci-lint's inline auto fix.

func Run added in v0.0.2

func Run(pass *analysis.Pass, mode Mode) []Issue

type Mode added in v0.0.2

type Mode int
const (
	StandaloneMode Mode = iota
	GolangciLintMode
)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL