...

Source file src/github.com/theupdateframework/go-tuf/internal/signer/sort.go

Documentation: github.com/theupdateframework/go-tuf/internal/signer

     1  package signer
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/theupdateframework/go-tuf/pkg/keys"
     7  )
     8  
     9  // ByIDs implements sort.Interface for []keys.Signer based on
    10  // the sorted public IDs() for each Signer. This facilitates
    11  // deterministic order of signatures, which prevents tests
    12  // that use fixtures from being flaky.
    13  type ByIDs []keys.Signer
    14  
    15  func (b ByIDs) Len() int {
    16  	return len(b)
    17  }
    18  
    19  func (b ByIDs) Swap(i, j int) {
    20  	b[i], b[j] = b[j], b[i]
    21  }
    22  
    23  func (b ByIDs) Less(i, j int) bool {
    24  	ids := b[i].PublicData().IDs()
    25  	iIDs := make([]string, len(ids))
    26  	copy(iIDs, ids)
    27  	sort.Strings(iIDs)
    28  
    29  	ids = b[j].PublicData().IDs()
    30  	jIDs := make([]string, len(ids))
    31  	copy(jIDs, ids)
    32  	sort.Strings(jIDs)
    33  
    34  	minLen := len(iIDs)
    35  	if len(jIDs) < minLen {
    36  		minLen = len(jIDs)
    37  	}
    38  
    39  	// Compare iIDs[:minLen] to jIDs[:minLen] element-wise.
    40  	for c := 0; c < minLen; c++ {
    41  		if iIDs[c] == jIDs[c] {
    42  			continue
    43  		}
    44  		return iIDs[c] < jIDs[c]
    45  	}
    46  
    47  	// iIDs[:minLen] is equal to jIDs[:minLen], so sort based on length.
    48  	return len(iIDs) < len(jIDs)
    49  }
    50  

View as plain text