github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/appfile/customization.go (about)

     1  package appfile
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // CustomizationSet is a struct that maintains a set of customizations
     8  // from an Appfile and provides helper functions for retrieving and
     9  // filtering them.
    10  //
    11  // Note: While "set" is in the name, this is not a set in the formal
    12  // sense of the word, since customizations can be duplicated.
    13  type CustomizationSet struct {
    14  	// Raw is the raw list of customizations.
    15  	Raw []*Customization
    16  }
    17  
    18  // Filter filters the customizations by the given type and returns only
    19  // the matching list of customizations.
    20  func (s *CustomizationSet) Filter(t string) []*Customization {
    21  	// Allow this method call on nil s
    22  	if s == nil {
    23  		return nil
    24  	}
    25  
    26  	// Lowercase the type
    27  	t = strings.ToLower(t)
    28  
    29  	// Pre-allocate the result slice to the size of the raw list. There
    30  	// usually aren't that many customization (a handful) so it is easier
    31  	// to just over-allocate here.
    32  	result := make([]*Customization, 0, len(s.Raw))
    33  	for _, c := range s.Raw {
    34  		if c.Type == t {
    35  			result = append(result, c)
    36  		}
    37  	}
    38  
    39  	return result
    40  }