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

     1  package siunits
     2  
     3  import (
     4  	"github.com/blueinnovationsgroup/can-go/pkg/dbc"
     5  	"github.com/blueinnovationsgroup/can-go/pkg/dbc/analysis"
     6  )
     7  
     8  func Analyzer() *analysis.Analyzer {
     9  	return &analysis.Analyzer{
    10  		Name: "unitsuffixes",
    11  		Doc:  "check that signals with SI units have the correct symbols",
    12  		Run:  run,
    13  	}
    14  }
    15  
    16  const (
    17  	metersPerSecond   = "m/s"
    18  	kilometersPerHour = "km/h"
    19  	meters            = "m"
    20  	degrees           = "°"
    21  	radians           = "rad"
    22  )
    23  
    24  // symbolMap returns a map from non-standard unit symbols to SI unit symbols.
    25  func symbolMap() map[string]string {
    26  	return map[string]string{
    27  		"kph":        kilometersPerHour,
    28  		"mps":        metersPerSecond,
    29  		"meters/sec": metersPerSecond,
    30  		"meters":     meters,
    31  		"deg":        degrees,
    32  		"degrees":    degrees,
    33  		"radians":    radians,
    34  	}
    35  }
    36  
    37  func run(pass *analysis.Pass) error {
    38  	symbols := symbolMap()
    39  	for _, def := range pass.File.Defs {
    40  		message, ok := def.(*dbc.MessageDef)
    41  		if !ok {
    42  			continue
    43  		}
    44  		for _, signal := range message.Signals {
    45  			if symbol, ok := symbols[signal.Unit]; ok {
    46  				pass.Reportf(signal.Pos, "signal with unit %s should have SI unit %s", signal.Unit, symbol)
    47  			}
    48  		}
    49  	}
    50  	return nil
    51  }