github.com/wI2L/jettison@v0.7.5-0.20230106001914-c70014c6417a/tags.go (about)

     1  package jettison
     2  
     3  import "strings"
     4  
     5  // tagOptions represents the arguments following
     6  // a comma in a struct field's tag.
     7  type tagOptions []string
     8  
     9  // parseTag parses the content of a struct field
    10  // tag and return the name and list of options.
    11  func parseTag(tag string) (string, tagOptions) {
    12  	if idx := strings.Index(tag, ","); idx != -1 {
    13  		return tag[:idx], strings.Split(tag[idx+1:], ",")
    14  	}
    15  	return tag, nil
    16  }
    17  
    18  // Contains returns whether a list of options
    19  // contains a particular substring flag.
    20  func (opts tagOptions) Contains(name string) bool {
    21  	if len(opts) == 0 {
    22  		return false
    23  	}
    24  	for _, o := range opts {
    25  		if o == name {
    26  			return true
    27  		}
    28  	}
    29  	return false
    30  }