github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/entity.go (about)

     1  package flow
     2  
     3  type IDEntity interface {
     4  	// ID returns a unique id for this entity using a hash of the immutable
     5  	// fields of the entity.
     6  	ID() Identifier
     7  }
     8  
     9  // Entity defines how flow entities should be defined
    10  // Entities are flat data structures holding multiple data fields.
    11  // Entities don't include nested entities, they only include pointers to
    12  // other entities. For example, they keep a slice of entity commits instead
    13  // of keeping a slice of entity object itself. This simplifies storage, signature and validation
    14  // of entities.
    15  type Entity interface {
    16  	IDEntity
    17  
    18  	// Checksum returns a unique checksum for the entity, including the mutable
    19  	// data such as signatures.
    20  	Checksum() Identifier
    21  }
    22  
    23  func EntitiesToIDs[T Entity](entities []T) []Identifier {
    24  	ids := make([]Identifier, 0, len(entities))
    25  	for _, entity := range entities {
    26  		ids = append(ids, entity.ID())
    27  	}
    28  	return ids
    29  }
    30  
    31  // Deduplicate entities in a slice by the ID method
    32  // The original order of the entities is preserved.
    33  func Deduplicate[T IDEntity](entities []T) []T {
    34  	if entities == nil {
    35  		return nil
    36  	}
    37  
    38  	seen := make(map[Identifier]struct{}, len(entities))
    39  	result := make([]T, 0, len(entities))
    40  
    41  	for _, entity := range entities {
    42  		id := entity.ID()
    43  		if _, ok := seen[id]; ok {
    44  			continue
    45  		}
    46  
    47  		seen[id] = struct{}{}
    48  		result = append(result, entity)
    49  	}
    50  
    51  	return result
    52  }