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

     1  package unitsuffixes
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/blueinnovationsgroup/can-go/pkg/dbc"
     7  	"github.com/blueinnovationsgroup/can-go/pkg/dbc/analysis"
     8  )
     9  
    10  func Analyzer() *analysis.Analyzer {
    11  	return &analysis.Analyzer{
    12  		Name: "unitsuffixes",
    13  		Doc:  "check that signals with units have correct name suffixes",
    14  		Run:  run,
    15  	}
    16  }
    17  
    18  func unitSuffixes() map[string]string {
    19  	return map[string]string{
    20  		"°":    "Degrees",
    21  		"rad":  "Radians",
    22  		"%":    "Percent",
    23  		"km/h": "Kph",
    24  		"m/s":  "Mps",
    25  	}
    26  }
    27  
    28  func run(pass *analysis.Pass) error {
    29  	suffixes := unitSuffixes()
    30  	for _, def := range pass.File.Defs {
    31  		message, ok := def.(*dbc.MessageDef)
    32  		if !ok {
    33  			continue
    34  		}
    35  		for _, signal := range message.Signals {
    36  			if suffix, ok := suffixes[signal.Unit]; ok {
    37  				if !strings.HasSuffix(string(signal.Name), suffix) {
    38  					pass.Reportf(signal.Pos, "signal with unit %s must have suffix %s", signal.Unit, suffix)
    39  				}
    40  			}
    41  		}
    42  	}
    43  	return nil
    44  }