github.com/blueinnovationsgroup/can-go@v0.0.0-20230518195432-d0567cda0028/pkg/dbc/analysis/passes/valuedescriptions/analyzer.go (about)

     1  package valuedescriptions
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/blueinnovationsgroup/can-go/internal/identifiers"
     7  	"github.com/blueinnovationsgroup/can-go/pkg/dbc"
     8  	"github.com/blueinnovationsgroup/can-go/pkg/dbc/analysis"
     9  )
    10  
    11  func Analyzer() *analysis.Analyzer {
    12  	return &analysis.Analyzer{
    13  		Name: "valuedescriptions",
    14  		Doc:  "check that value descriptions are valid CamelCase",
    15  		Run:  run,
    16  	}
    17  }
    18  
    19  func run(pass *analysis.Pass) error {
    20  	for _, def := range pass.File.Defs {
    21  		var valueDescriptions []dbc.ValueDescriptionDef
    22  		switch def := def.(type) {
    23  		case *dbc.ValueTableDef:
    24  			valueDescriptions = def.ValueDescriptions
    25  		case *dbc.ValueDescriptionsDef:
    26  			valueDescriptions = def.ValueDescriptions
    27  		default:
    28  			continue
    29  		}
    30  		for _, vd := range valueDescriptions {
    31  			vd := vd
    32  			if !identifiers.IsCamelCase(vd.Description) {
    33  				// Descriptor has format "<value> <quote><description>"
    34  				//
    35  				// So we increase the column position by the size of value + 2 (space and quotes) so the lint
    36  				// error marker is on the description and not on the value
    37  				vd.Pos.Column += len(fmt.Sprintf("%d", int64(vd.Value))) + 2
    38  				pass.Reportf(vd.Pos, "value description must be CamelCase (numbers ignored)")
    39  			}
    40  		}
    41  	}
    42  	return nil
    43  }