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

     1  package requireddefinitions
     2  
     3  import (
     4  	"reflect"
     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: "requireddefinitions",
    13  		Doc:  "check that the file contains exactly one of all required definitions",
    14  		Run:  run,
    15  	}
    16  }
    17  
    18  func requiredDefinitions() []dbc.Def {
    19  	return []dbc.Def{
    20  		&dbc.BitTimingDef{},
    21  		&dbc.NodesDef{},
    22  	}
    23  }
    24  
    25  func run(pass *analysis.Pass) error {
    26  	counts := make(map[reflect.Type]int)
    27  	for _, def := range pass.File.Defs {
    28  		counts[reflect.TypeOf(def)]++
    29  	}
    30  	for _, requiredDef := range requiredDefinitions() {
    31  		if counts[reflect.TypeOf(requiredDef)] == 0 {
    32  			// we have no definition to return, so return the first
    33  			pass.Reportf(pass.File.Defs[0].Position(), "missing required definition(s)")
    34  			break
    35  		}
    36  	}
    37  	return nil
    38  }