...

Source file src/go.einride.tech/aip/resourcename/segment.go

Documentation: go.einride.tech/aip/resourcename

     1  package resourcename
     2  
     3  import "strings"
     4  
     5  // RevisionSeparator is the separator character used to separate resource IDs from revision IDs.
     6  const RevisionSeparator = '@'
     7  
     8  // Segment is a segment of a resource name or a resource name pattern.
     9  //
    10  // EBNF
    11  //
    12  //	Segment  = Literal | Variable ;
    13  //	Variable = "{" Literal "}" ;
    14  type Segment string
    15  
    16  // IsVariable reports whether the segment is a variable segment.
    17  func (s Segment) IsVariable() bool {
    18  	return len(s) > 2 && s[0] == '{' && s[len(s)-1] == '}'
    19  }
    20  
    21  // Literal returns the literal value of the segment.
    22  // For variables, the literal value is the name of the variable.
    23  func (s Segment) Literal() Literal {
    24  	switch {
    25  	case s.IsVariable():
    26  		return Literal(s[1 : len(s)-1])
    27  	default:
    28  		return Literal(s)
    29  	}
    30  }
    31  
    32  // IsWildcard reports whether the segment is a wildcard.
    33  func (s Segment) IsWildcard() bool {
    34  	return s == Wildcard
    35  }
    36  
    37  // Literal is the literal part of a resource name segment.
    38  //
    39  // EBNF
    40  //
    41  //	Literal  = RESOURCE_ID | RevisionLiteral ;
    42  //	RevisionLiteral = RESOURCE_ID "@" REVISION_ID ;
    43  type Literal string
    44  
    45  // ResourceID returns the literal's resource ID.
    46  func (l Literal) ResourceID() string {
    47  	if !l.HasRevision() {
    48  		return string(l)
    49  	}
    50  	return string(l[:strings.IndexByte(string(l), RevisionSeparator)])
    51  }
    52  
    53  // RevisionID returns the literal's revision ID.
    54  func (l Literal) RevisionID() string {
    55  	if !l.HasRevision() {
    56  		return ""
    57  	}
    58  	return string(l[strings.IndexByte(string(l), RevisionSeparator)+1:])
    59  }
    60  
    61  // HasRevision returns true if the literal has a valid revision.
    62  func (l Literal) HasRevision() bool {
    63  	revisionSeparatorIndex := strings.IndexByte(string(l), RevisionSeparator)
    64  	if revisionSeparatorIndex < 1 || revisionSeparatorIndex >= len(l)-1 {
    65  		return false // must have content on each side of the revision marker
    66  	}
    67  	if strings.IndexByte(string(l[revisionSeparatorIndex+1:]), RevisionSeparator) != -1 {
    68  		return false // multiple revision markers means no valid revision
    69  	}
    70  	return true
    71  }
    72  

View as plain text