github.com/jshiv/can-go@v0.2.1-0.20210224011015-069e90e90bdf/pkg/dbc/identifier.go (about) 1 package dbc 2 3 import ( 4 "fmt" 5 6 "go.einride.tech/can/internal/identifiers" 7 ) 8 9 // Identifier represents a DBC identifier. 10 type Identifier string 11 12 // maxIdentifierLength is the length of the longest valid DBC identifier. 13 const maxIdentifierLength = 128 14 15 // Validate returns an error for invalid DBC identifiers. 16 func (id Identifier) Validate() (err error) { 17 defer func() { 18 if err != nil { 19 err = fmt.Errorf("invalid identifier '%s': %w", id, err) 20 } 21 }() 22 if len(id) == 0 { 23 return fmt.Errorf("zero-length") 24 } 25 if len(id) > maxIdentifierLength { 26 return fmt.Errorf("length %v exceeds max length %v", len(id), maxIdentifierLength) 27 } 28 for i, r := range id { 29 if i == 0 && r != '_' && !identifiers.IsAlphaChar(r) { // first char 30 return fmt.Errorf("invalid first char: '%v'", r) 31 } else if i > 0 && r != '_' && !identifiers.IsAlphaChar(r) && !identifiers.IsNumChar(r) { 32 return fmt.Errorf("invalid char: '%v'", r) 33 } 34 } 35 return nil 36 }