github.com/jshiv/can-go@v0.2.1-0.20210224011015-069e90e90bdf/pkg/descriptor/database.go (about)

     1  package descriptor
     2  
     3  import (
     4  	"path"
     5  	"strings"
     6  )
     7  
     8  // Database represents a CAN database.
     9  type Database struct {
    10  	// SourceFile of the database.
    11  	//
    12  	// Example:
    13  	//  github.com/einride/can-databases/dbc/j1939.dbc
    14  	SourceFile string
    15  	// Version of the database.
    16  	Version string
    17  	// Messages in the database.
    18  	Messages []*Message
    19  	// Nodes in the database.
    20  	Nodes []*Node
    21  }
    22  
    23  func (d *Database) Node(nodeName string) (*Node, bool) {
    24  	for _, n := range d.Nodes {
    25  		if n.Name == nodeName {
    26  			return n, true
    27  		}
    28  	}
    29  	return nil, false
    30  }
    31  
    32  func (d *Database) Message(id uint32) (*Message, bool) {
    33  	for _, m := range d.Messages {
    34  		if m.ID == id {
    35  			return m, true
    36  		}
    37  	}
    38  	return nil, false
    39  }
    40  
    41  func (d *Database) Signal(messageID uint32, signalName string) (*Signal, bool) {
    42  	message, ok := d.Message(messageID)
    43  	if !ok {
    44  		return nil, false
    45  	}
    46  	for _, s := range message.Signals {
    47  		if s.Name == signalName {
    48  			return s, true
    49  		}
    50  	}
    51  	return nil, false
    52  }
    53  
    54  // Description returns the name of the Database.
    55  func (d *Database) Name() string {
    56  	return strings.TrimSuffix(path.Base(d.SourceFile), path.Ext(d.SourceFile))
    57  }