github.com/letsencrypt/boulder@v0.20251208.0/allowlist/main.go (about)

     1  package allowlist
     2  
     3  import (
     4  	"github.com/letsencrypt/boulder/strictyaml"
     5  )
     6  
     7  // List holds a unique collection of items of type T. Membership can be checked
     8  // by calling the Contains method.
     9  type List[T comparable] struct {
    10  	members map[T]struct{}
    11  }
    12  
    13  // NewList returns a *List[T] populated with the provided members of type T. All
    14  // duplicate entries are ignored, ensuring uniqueness.
    15  func NewList[T comparable](members []T) *List[T] {
    16  	l := &List[T]{members: make(map[T]struct{})}
    17  	for _, m := range members {
    18  		l.members[m] = struct{}{}
    19  	}
    20  	return l
    21  }
    22  
    23  // NewFromYAML reads a YAML sequence of values of type T and returns a *List[T]
    24  // containing those values. If data is empty, an empty (deny all) list is
    25  // returned. If data cannot be parsed, an error is returned.
    26  func NewFromYAML[T comparable](data []byte) (*List[T], error) {
    27  	if len(data) == 0 {
    28  		return NewList([]T{}), nil
    29  	}
    30  
    31  	var entries []T
    32  	err := strictyaml.Unmarshal(data, &entries)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return NewList(entries), nil
    37  }
    38  
    39  // Contains reports whether the provided entry is a member of the list.
    40  func (l *List[T]) Contains(entry T) bool {
    41  	_, ok := l.members[entry]
    42  	return ok
    43  }